var g_hLoading;

function productSearch()
{	
	var strSearch = $("#productSearchText").val().trim();

	if (strSearch != "")
	{
		document.location.href = '/catalogue/search/term/' + encodeURIComponent(strSearch);
	}
	
}



function filterProducts()
{
	//productListing
	
	$('.productResults').fadeOut(500, function()
	{
		$(".productsLoading").fadeIn(500,function ()
		{
			
			if (g_hLoading)
			{
				g_hLoading.abort();
			}
			
			strPost = "ajax=true";
			
			$(".filterHeader .filter").each(function ()
			{
				if ($(this).is(":checked"))
				{
					strPost += "&" + $(this).attr("name") + "=" + $(this).val();
				}
			});
			
			g_hLoading = $.ajax({
				type: "POST",
				url: $("[name=page]").val(),
				data: strPost,
				success: function(strData)
				{
					$(".productsLoading").fadeOut(500,function ()
					{
						$(".productResults").html(strData);
						
						$('.productResults').fadeIn(500, function()
						{
						});
					});
				   
				}
			    });
		});
	});
	
}

$(document).ready(function () 
{
	$("#productSearch").submit(function ()
	{
		productSearch();
		
		return false;
	});
	
	$(".filterHeader .filter").change(function ()
	{
		filterProducts();	
	});

	$(".filterHeader > .categorySub").click(function ()
	{
		objList = $(this).closest(".filterHeader").find("ul");
		
		if ($(objList).is(":visible"))
		{
			$(objList).slideUp();
			$(this).closest(".filterHeader").removeClass("open");
		}
		else
		{
			$(objList).slideDown();
			$(this).closest(".filterHeader").addClass("open");
		}
		
		return false;
	});
});
