var SelectedCategories = 0;
var SelectedProductsCategories = 0;
var PSPL = Array();

$(function (){  
	GetSetCategories();
	
	$("#pcategory").change(function(){
		SelectedCategories = $("#pcategory").selectedValues();
		if (SelectedCategories > 0){
			GetSetProductsSections(SelectedCategories);
		} else {
			removePSectionOptions("psection");
		}
		removePSectionOptions("pproducts");
	});
	$("#psection").change(function(){
	   SelectedProductsCategories = $("#psection").selectedValues();
	   if (SelectedProductsCategories > 0){
		   GetSetProducts(SelectedCategories, SelectedProductsCategories);
	   } else {
	   }
	   removePSectionOptions("pproducts");
	});
	$("#pbtn").click(function(){
		var thisid = "osr_" + $("#pcategory option:selected").val() + "-" + $("#psection option:selected").val();
		if ($("#" + thisid).length > 0){
			//alert(thisid);
			$("#" + thisid).remove();
		}
		if ($("#PSProductsList").html().length > 0){
			$("#OldSearchResults").after("<span id=\"" + thisid + "\" style=\"display: block; padding-left: 20px; margin-left: 10px;\"><h3>" + $("#pcategory option:selected").text()  + " / " + $("#psection option:selected").text()  + "</h3>" + $("#PSProductsList").html() + "<div class=\"clear\"></div></span>");			
			removePSectionOptions2();
		}
		checkCNTImages();
	});
});  


function GetSetCategories(){
	$.getJSON("/callme.php",{action: "products.getCategories.json.php", lang: cLang, category: "1,2"}, function(data){ 
		var Obj = eval(data.databack);
		var ObjLen = Obj.length;
		if (ObjLen > 0){
			var mObject = {};
			for (i=0; i < ObjLen; i++){
				mObject[Obj[i].id] = Obj[i].translation;
			}
			$("#pcategory").addOption(mObject, false);
			$("#pcategory").selectOptions("0");
		}
	});
}
function GetSetProductsSections(category){
	$.getJSON("/callme.php",{action: "products.getProductsSections.json.php", lang: cLang, category: category}, function(data){ 
		var Obj = eval(data.databack);
		var ObjLen = Obj.length;
		if (ObjLen > 0){
			removePSectionOptions("psection");
			var mObject = {};
			for (i=0; i < ObjLen; i++){			
				mObject[Obj[i].id] = Obj[i].translation;
			}
			$("#psection").addOption(mObject, false);
			$("#psection").attr("disabled", false);
			//$("#pcategory").selectOptions("0");
		}
	});
}
function GetSetProducts(category, section){
	$.getJSON("/callme.php",{action: "products.getProducts.json.php", lang: cLang, category: category, section: section}, function(data){ 
		var Obj = eval(data.databack);
		var ObjLen = Obj.length;
		if (ObjLen > 0){
			//removePSectionOptions();
			var mObject = {};
			PSPL = Array();
			for (i=0; i < ObjLen; i++){
				PSPL[i] = Obj[i];
				mObject[Obj[i].id] = Obj[i].translation;
			}
			PopulatePList();
			$("#pproducts").addOption(mObject, false);
			$("#pproducts").attr("disabled", false);
			//$("#pcategory").selectOptions("0");
		}
	});
}
function PopulatePList(){
	var _list = "";
	if (PSPL.length > 0){
		for (i=0; i<PSPL.length; i++){
			_list = _list + "<div style=\"width: 150px; padding: 0px;\" id=\"prp_" + PSPL[i].id  + "\" class=\"float-left\"><a href=\"/" + nlang + "/" + PSPL[i].stitle + "/product/" + PSPL[i].id + "/\" title=\"" + PSPL[i].translation + "\">" + PSPL[i].translation + "</a> <span onclick=\"removeThisP('prp_" + PSPL[i].id  + "')\" class=\"clearitembtn\">[x]</span><a class=\"cntimages\" href=\""+PSPL[i].alink+"\" title=\"" + PSPL[i].translation + "\" id=\"imgpack_PSPL" + PSPL[i].id + "\"><img src=\"" + getThumb(PSPL[i].alink) + "\" alt=\"" + PSPL[i].translation + "\" title=\"" + PSPL[i].translation + "\" class=\"float-left\" /></a><div class=\"clear\"></div></div>";
		}
		$("#pbtn").show();
		$("#PSProductsList div").remove();
		$("#PSProductsList").append(_list);
		checkCNTImages();
	}
	//PSProductsList
}
function removePSectionOptions(selid){
	$("#" + selid).removeOption(/[^0]/i); 
	$("#" + selid).attr("disabled", true);
	$("#PSProductsList div").remove();
	$("#pbtn").hide();
}
function removePSectionOptions2(){
	removePSectionOptions("psection");
	$("#pcategory").selectOptions("0"); 
}
function removeThis(id){
	$("#" + id).remove();
}
function removeThisP(id){
	$("#" + id).remove();
}
