$(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;
			}
			
			$.get("/system/php/service.php", {f : 'collapse_folder', id : id, collapse : collapse});
			return false;
		}
	);
	
	$("div.utils p.remove a").click(
		function() {
			par = GetParentBlock($(this));
			id = par.attr('id');
			par.slideUp("slow");
			
			$.get("/system/php/service.php", {f : 'hide_folder', id : id});
			return false;
		}
	);
	
	$("div.utils p.edit a").click(
		function() {
			par = GetParentBlock($(this));
			par.find("div.hpSet div.options").slideDown("slow");
			par.find("div.utils p.remove").hide();
			return false;
		}
	);
	

	$("div.options input.cancel").click(
		function() {
			par = GetParentBlock($(this));
			par.find("div.hpSet div.options").slideUp("slow");
			par.find("div.utils p.remove").show();
			return false;
		}
	);
	
	$("div.options input.cancelall").click(
		function() {
			par = GetParentBlock($(this));
			par.find("div.hpSet div.options").slideUp("slow");
			par.find("div.utils p.remove").show();
			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;
		}
	);
	
  	$("#hpColOne,#hpColTwo,#hpColThree").sortable(
  		{
  			accurateIntersection: false,
  			connectWith: ["#hpColOne,#hpColTwo,#hpColThree"],
  			revert: true,
  			placeholder: 'placeholder',
  			tolerance : 'pointer', 
  			forceHelperSize: true,
  			forcePlaceholderSize : true, 
  			cursor: 'move',
  			
  			handle: 'h2',
  			items: 'div.hpMod:not(.nodrag)',
  			stop: function (ev, ui) {
  				col0 = $("#hpColOne").sortable('toArray').toString();
  				col1 = $("#hpColTwo").sortable('toArray').toString();
  				col2 = $("#hpColThree").sortable('toArray').toString();
  				$.get("/system/php/service.php", {f : 'set_sort',  col0: col0, col1 : col1, col2: col2});
  			},
  			start: function (ev, ui) {
  				height = ui.helper.attr('clientHeight');
  				ui.placeholder.css('height', height);
  			}
  			
  		}
  	);
  	
  	$("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');
}