$(document).ready(function() {

	$("div.utils p.collapse a").click(
		function() {
			par = GetParentBlock($(this));
			id = par.attr('id');
			
			if (par.hasClass("hpCollapsed")) {
				par.find("." + GetDataClass(id)).slideDown("slow");
				par.removeClass("hpCollapsed");
				collapse = 0;
			} else {
				par.find("." + GetDataClass(id)).slideUp("slow");
				par.addClass("hpCollapsed");
				collapse = 1;
			}
			
			return false;
		}
	);
	
	$("div.utils p.remove a").click(
		function() {
			par = GetParentBlock($(this));
			id = par.attr('id');
			par.slideUp("slow");
			
			return false;
		}
	);
	
	$("div.utils p.edit a").click(
		function() {
			par = GetParentBlock($(this));
			par.find("div.hpSet div.options").slideToggle("slow");
			return false;
		}
	);
	
	$("div.options input.cancel").click(
		function() {
			par = $(this).parents("div.options").slideUp("slow");
			return false;
		}
	);
	
	$("div.hpData p.addremove a.add").click(
		function() {
			par = GetParentSubBlock($(this));
			if (par.find("ul li.blocked").length > 0) {
				par.find("ul li.blocked:first").removeClass("blocked");
				SetItemsCount(par);
			}
			
			SetOnOffCount(par);
			return false;
		}
	);	
	
	$("div.hpData p.addremove a.remove").click(
		function() {
			par = GetParentSubBlock($(this));
			if (par.find("ul li:not(.blocked)").length > 1) {
				par.find("ul li:not(.blocked):last").addClass("blocked");
				SetItemsCount(par);
			}
			
			SetOnOffCount(par);
			return false;
		}
	);
	
 	
  	$("div.hpData").each(function() {
  		SetOnOffCount($(this));
  	});
});

function SetOnOffCount(par) {
		if (par.find("ul li.blocked").length == 0) {
			//alert('nomore');
			par.find("a.add").css("background-position", "0px -24px");
		} else {
			//alert('more');
			par.find("a.add").css("background-position", "left top");
		}
		
		if (par.find("ul li:not(.blocked)").length <= 1) {
			par.find("a.remove").css("background-position", "-18px -24px");
		} else {
			par.find("a.remove").css("background-position", "-18px 0px");
		}
}

function SetItemsCount(par) {
	cnt = par.find("ul li:not(.blocked)").length;
	$.get("/system/php/service.php", {f : 'set_show_items', id : par.attr("id"), cnt:cnt});
}


function GetDataClass(id) {
	if (id == "f8938") {
		return 'adSetcat';
	}
	
	return 'hpSet';
}

function GetParentSubBlock(el) {
	return el.parents('div.hpData');
}

function GetParentBlock(el) {
	return el.parents('div.hpMod');
}
