//document ready function
$(document).ready( function() {

	// show sort form - we want it hidden by default in case user does not have javascript
	$("#sorter_container").css("display","block");
	$("#submit_sort").css("display","block");
	
	// parse querystring to determine if certain checkboxes should be preselected
	var temptype = swfobject.getQueryParamValue("type");
	var currenturl=window.location.toString();
	if (currenturl.indexOf("products-liquid.jspx") > -1) {
		temptype = "liquid";
	}
	else if (currenturl.indexOf("products-sheets.jspx") > -1) {
		temptype = "sheets";
	}
	var templine = swfobject.getQueryParamValue("line");
	var tempscent = swfobject.getQueryParamValue("scent");
	if (temptype != "") {
		$("#type li").removeClass("checked");
		$("#type li#"+temptype).addClass("checked");
	}
	if (templine != "") {
		$("#line li").removeClass("checked");
		$("#line li#"+templine).addClass("checked");
	}
	if (tempscent != "") {
		$("#scent li").removeClass("checked");
		$("#scent li#"+tempscent).addClass("checked");
	}
	sortproducts();
	
	//bind click to change checkboxes and update sorting values
	$(".sorter li").click(function() {
		if (($(this).hasClass("checked")) && ($(this).parent().parent().attr("id")) != "type") {
			$(this).removeClass("checked");
		}
		else {
			$(this).parent().children().removeClass("checked");
			$(this).addClass("checked");
		}
		sortproducts();
	});
	$("#submit_sort").click(function() {
		$(".sorter li").removeClass("checked");
		$(".sorter li#all").addClass("checked");
		sortproducts();
	});
});
//end document ready function

function sortproducts() {
	var typechosen;
	var linechosen;
	var scentchosen;
	if ($("#type .checked").attr("id") != "all") {
		typechosen = $("#type .checked").attr("id");
	}
	else {
		typechosen = " ";
	}
	if ($("#line .checked").attr("id") != undefined) {
		linechosen = $("#line .checked").attr("id");
	}
	else {
		linechosen = " ";
	}
	if ($("#scent .checked").attr("id") != undefined) {
		scentchosen = $("#scent .checked").attr("id");
	}
	else {
		scentchosen = " ";
	}
	$("#allprod tr").css("display","none");
	var noresults = "yes";
	$("#allprod tr:has(td)").each(function () {
		if (($(this).attr("class").indexOf(typechosen) > -1) && ($(this).attr("class").indexOf(linechosen) > -1) && ($(this).attr("class").indexOf(scentchosen) > -1)) {
			$(this).css("display","block");
			noresults = "no";
			if($(this).prevAll(".headerrow:first").css("display") == "none") {
				$(this).prevAll(".headerrow:first").css("display","block");
			}
		}
	});
	if (noresults == "yes") {
		$(".noresults").css("display","block");
	}
}
