// JavaScript Document

$(document).ready(function(){
	
	if ($(".searchrow").length){
		
		$(".colorbox").click(function(){
			if ($("#ProductsSearch").val()==TTT.search){
				$("#ProductsSearch").val("");
			}
			if (this.id == "prdColor_0") {
				$(".colorbox-selected").removeClass("colorbox-selected");
			} else {
			
				if ($(this).hasClass("colorbox-selected")){
					$(this).removeClass("colorbox-selected");
				} else {
					$(this).addClass("colorbox-selected");
				}
			}
			sendProductsSearchRequest();		
		});
		$("#prdOnline").click(function(){
			var ths = $("#prdOnline");
			if (ths.hasClass("ponline")){
				ths.removeClass("ponline");
				ths.addClass("ponline2");
				ths.attr("title", TTTP.words.showonlyproductall);
			} else if (ths.hasClass("ponline2")){
				ths.attr("title", TTTP.words.showonlyproductonline);
				ths.removeClass("ponline2");
				ths.addClass("ponline");
			}
			CheckSearchString();  
			sendProductsSearchRequest();
		});
		$("#ProductsSearch").keyup(function(){
			CheckSearchString();	
			sendProductsSearchRequest();
		});
		$("#ProductsSearch").click(function(){
			CheckSearchString();	
		});
		$("#ProductSearchBtn").click(function(){
			CheckSearchString();	
			sendProductsSearchRequest();
		});

	}
});
function CheckSearchString(){
	if ($("#ProductsSearch").val()==TTT.search){
		$("#ProductsSearch").val("");
	}
}
function sendProductsSearchRequest(){
	var clrs = getColorsIds(getSelectedColorBoxes());
	$.getJSON("/callme.php",{action: "products.json.php", colors:clrs, search_string: ($("#ProductsSearch").val()), category: $("#ProductCategory").val(), lang: cLang, displayonlineproducts: ($("#prdOnline").hasClass("ponline2")?true:false)}, doit);
}
function doit(data){
	if (data.ids != 0){
		setProductsSearchResults(data.ids);
	} else {
		displayNoResults();
	}
}
function setProductsSearchResults(res){
	$("#noSearchResults").hide()
	$(".ProductContainer").hide();
	for (i=0; i<res.length; i++){
		$("#product" + res[i]).show();
	}
}
function displayNoResults(){
	$(".ProductContainer").hide();
	$("#noSearchResults").show();
}
function getSelectedColorBoxes(){
	var r = new Array();
	$(".colorbox-selected").each(function(i){
		r[i] = this.id;									  
	});
	return r;
}
function getColorsIds(colorRefs){
	var r =0;
	if (colorRefs){
		for (i=0; i<colorRefs.length; i++){
			r = r + "-" + parseInt(colorRefs[i].replace("prdColor_",""));	
		}

	}
	return r;
}
