/*
param = params.split(","); //the params from the php 	
var paramJSON = {};

for(var i in param) //we store the parameters of the current page into array
{
	temp = param[i].split("=");
	paramJSON[temp[0]] = temp[1];
}


paramJSON.paramCMS = paramCMS;
*/
var firstRun = true;

paramJSON.ajax = 'on';
paramJSON.type = 'filter'
paramJSON.active = 1;
function calculatePriceMax(pricemax)
{
	var interval = Math.floor(pricemax/4);
	paramJSON['priceRange1']=interval;
	paramJSON['priceRange2']=interval*2;
	paramJSON['priceRange3']=interval*3;
}

function filter()
{
	var type= $(this).attr("id");
	
	if(typeof(type) == "undefined" || type=="zoekImage")
		type = "filterInputField"; 
	
	if($(this).hasClass('star-rating'))
		type = "stars";
	else if($(this).hasClass('rating-cancel'))
		type = "removeStars";
	
	switch(type)
	{
		case 'priceAsc':
			paramJSON['sortPrice'] ='asc';
			paramJSON['sortName']='';
			paramJSON['sortStar']='';
			paramJSON['sortLeveranciers']='';

			$("span.selected").removeClass('selected');
			$(this).children("input").attr("checked","checked");
		break;

		case 'priceDesc':
			paramJSON['sortPrice']='desc';
			paramJSON['sortName']='';
			paramJSON['sortStar']='';
			paramJSON['sortLeveranciers']='';

			$("span.selected").removeClass('selected');
			$(this).children("input").attr("checked","checked");
		break;
		 
		case 'nameAsc':
			paramJSON['sortName']='asc';
			paramJSON['sortPrice']='';
			paramJSON['sortStar']='';
			paramJSON['sortLeveranciers']='';

			$("span.selected").removeClass('selected');
			$(this).children("input").attr("checked","checked");
		break;
		
		case 'nameDesc':
			paramJSON['sortName']='desc';
			paramJSON['sortPrice']='';
			paramJSON['sortStar']='';
			paramJSON['sortLeveranciers']='';

			$("span.selected").removeClass('selected');
			$(this).children("input").attr("checked","checked");
		break;
	
		case 'starAsc':
			paramJSON['sortStar']='asc';
			paramJSON['sortPrice']='';
			paramJSON['sortName']='';
			paramJSON['sortLeveranciers']='';

		  	$("span.selected").removeClass('selected');
		break;
		
		case 'starDesc':
			paramJSON['sortStar']='desc';
			paramJSON['sortPrice']='';
			paramJSON['sortName']='';
			paramJSON['sortLeveranciers']='';

		  	$("span.selected").removeClass('selected');
		break;
		
		case 'leveranciersAsc':
			paramJSON['sortLeveranciers']='asc';
			paramJSON['sortPrice']='';
			paramJSON['sortName']='';
			paramJSON['sortStar']='';
		  	$("span.selected").removeClass('selected');
		break;
		
		case 'leveranciersDesc':
		  	paramJSON['sortLeveranciers']='desc';
		  	paramJSON['sortPrice']='';
			paramJSON['sortName']='';
			paramJSON['sortStar']='';
		  	$("span.selected").removeClass('selected');
		break;
		
		case 'priceRange1':
			paramJSON['priceMin'] = 0;
			paramJSON['priceMax'] = paramJSON['priceRange1'];
			$("#column2 .selected").removeClass('selected');
		break;
		
		case 'priceRange2':
			paramJSON['priceMin'] = paramJSON['priceRange1'];
			paramJSON['priceMax'] = paramJSON['priceRange2'];
			$("#column2 .selected").removeClass('selected');
		break;
		
		case 'priceRange3':
			paramJSON['priceMin'] = paramJSON['priceRange2'];
			paramJSON['priceMax'] = paramJSON['priceRange3'];
			$("#column2 .selected").removeClass('selected');
		break;
		
		case 'priceRange4':
			paramJSON['priceMin'] = paramJSON['priceRange3'];
			paramJSON['priceMax'] = 9999999;
			$("#column2 .selected").removeClass('selected');
		break;
		
		case 'leveranciersFilter':
			paramJSON['leveranciers'] = $("#leveranciersFilter").val();
		break;
		
		case 'filterInputField':
			paramJSON['nameString'] = $("#filterInputField").val();
		break;
		
		case 'stars':
			paramJSON['stars'] = $(".star-rating-on").size();
		break;
		
		case 'removeStars':
			paramJSON['stars'] = 0;
		break;
		
		case 'clearFilter':
			paramJSON.sortName = 'asc';
			paramJSON.sortPrice = '';
			paramJSON.sortStar = '';
			paramJSON.sortLeveranciers = '';
			paramJSON.stars = '';
			paramJSON.nameString = '';
			paramJSON.priceMin = 0;
			paramJSON.priceMax = 9999999;
			paramJSON.leveranciers = '';
			$("#filterInputField").val("");
			$("#leveranciersFilter").val("").change();
			$("#advanceFilter .selected").removeClass('selected');
			$(".star-rating").removeClass("star-rating-on");
			$("input:radio").attr("checked","");
		break;
		
		default:
		 	// nothing to be executed if there is no match
	}
	
	$(this).addClass('selected');
	paramJSON['active']= 1; //set the active page to page 1 after sorting
	
	$.ajax({
   		type: "POST",
   		url: "ajax.php?url=http://www.uniekewebshops.nl/php/ajax/ajax_handler.php",
   		dataType: "json",
		data: paramJSON,
   		cache: false,
   		beforeSend: function(){$("#overzichtContent").stop(true,true).html("<div style=\"background: url('../images/wait.gif') no-repeat;z-index:3;position:relative;top:100px;left:300px;height:100px;width:100px;\">&nbsp;</div>");} ,
   		success: function(json)
			{ 
				$("#overzichtContent").hide().html(json.html).stop(true,true).fadeIn();
				$("#paginering .link_actief").removeClass("link_actief").addClass("link");
				$("#paginering > a[title=1]").removeClass("link").addClass("link_actief");
				initiateButtonPaginering();
				
				if(productWeergave=='lijst')
					$("#lijst").click();
				
				if (firstRun) {
				/* set the price range of the filters*/
					calculatePriceMax(json.pricemax);
					$(".pr1").text( paramJSON['priceRange1']); 
					$(".pr2").text( paramJSON['priceRange2']);
					$(".pr3").text( paramJSON['priceRange3']);
					firstRun = false;
				}
			}
		});
}

function movePage()
{	
	($(this).attr("title")=='') ? ($(this).parents().attr("id")=='vorige') ? paramJSON['active']-- : paramJSON['active']++ : paramJSON['active'] = $(this).attr("title");
	
	//alert(paramJSON['active']);
	$.ajax({
   		type: "POST",
   		url: "ajax.php?url=http://www.uniekewebshops.nl/php/ajax/ajax_handler.php",
   		dataType: "json",
		data: paramJSON,
   		cache: false,
   		beforeSend: function(){$("#overzichtContent").stop(true,true).html("<div style=\"background: url('../images/wait.gif') no-repeat;z-index:3;position:relative;top:100px;left:300px;height:100px;width:100px;\">&nbsp;</div>")} ,
   		success: function(json)
			{ 
				$("#overzichtContent").hide().html(json.html).stop(true,true).fadeIn();
				initiateButtonPaginering();
				
				if(productWeergave=='lijst')
					$("#lijst").click();
				
			}
		});
}


function initiateButtonPaginering()
{
	$("#paginering .link").unbind("click").click(movePage);
   	$("#volgende a").unbind("click").click(movePage);
	$("#vorige a").unbind("click").click(movePage);
	
	var lastPage = $("#paginering a:last-child").text();
	
	if(paramJSON['active'] == lastPage)
		$("#volgende a").unbind("click");
	if(paramJSON['active'] == 1)
		$("#vorige a").unbind("click");
	
	$(".product_links_midden:eq(2)").css("border-right","solid 1px white");
 	$(".product_links_midden:eq(5)").css("border-right","solid 1px white");
 	$(".product_links_midden:eq(8)").css("border-right","solid 1px white");
 	$(".product_links_midden:eq(11)").css("border-right","solid 1px white");
}

$(document).ready(function() {
	/* initiate the functions in paginering*/
    initiateButtonPaginering();
    
    /* set the behavior for filter buttons*/
   	$("#advanceFilterButton").toggle(function(){
   		$("#advanceFilter").stop(true,true).slideUp();
   		$("#advanceFilterButton").css("background","url('../images/plus.png') no-repeat");
   		$("#advanceFilterButton").text("Verfijn je keuze");
   		},
   	function(){
   		$("#advanceFilter").stop(true,true).slideDown();
   		$("#advanceFilterButton").css("background","url('../images/minus.png') no-repeat");
   		$("#advanceFilterButton").text("Filter opties inklappen");
   		});
	
	/* standaard bij openen pagina */
	$("#advanceFilter").stop(true,true).slideDown();
	$("#advanceFilterButton").css("background","url('../images/minus.png') no-repeat");
	$("#advanceFilterButton").text("Filter opties inklappen");
	/* einde standaard bij openen pagina */
	
	$(".filterButtons").click(filter);
	$("#leveranciersFilter").change(filter);
	$("#filterInputField").keydown(function(event) 
	{
		if (event.keyCode == '13') filter();
	});
	$('.star-rating-live').click(filter);
	$('.rating-cancel').click(filter);
	$("#clearFilter").click(filter);
 	$("#zoekImage").click(filter);
 	
 	filter();
	//alert(paramJSON.pricemax);
});