Shadowbox.init();

$(function(){
	$("a.tooltip").easyTooltip();

	$('div.image_slider').cycle({
		fx:			'scrollRight',
		speed:		2000,
		timeout:	6000
	});

	$('.tipsyHover').tipsy();

	$("div.faq_item a").click(function(){
		$(this).parent().parent().find("p").slideToggle();
		return false;
	});

	/*$("div#news_slider nav div.item").click(function(){
		$("div#news_slider nav div.item").css("z-index", 0);
		$(this).css("z-index", 100);
		if($(this).hasClass("active")){
			$(this).removeClass("active");
		} else {
			$("div#news_slider nav div.item").removeClass("active");
			$(this).addClass("active");
		}
	});*/

	$("nav#mainmenu ul li").click(function(){
		window.location = $(this).find("a").attr("href");
	});

	$("div.overview div.item:first").addClass('first');
	$('div.overview div.item').filter(function(index){
		return (index%4 == 0);
	}).addClass('first');

	$("nav.filters ul li ul li").click(function(){
		if($(this).hasClass("checked")){
			$(this).removeClass("checked");
		} else {
			$(this).addClass("checked");
		}
	});

	$("nav.filters ul li").click(function(){
		if($(this).hasClass("active")){
			$(this).removeClass("active");
		} else {
			$("nav.filters ul li ul, nav.filters ul li div.slider_holder").hide();
			$("nav.filters ul li").removeClass("active");
			$(this).addClass("active");
		}
		$(this).children("ul, div.slider_holder").toggle();
		return false;
	});

	$("a.button").each(function(){
		$(this).append("<span></span>").after('<div class="clear"></div>');
	});

	$("ul#tabs span.image").each(function(){
		$("<li>").append($(this)).appendTo($("ul#output"));
	});

	$("ul#output li img").each(function(){
		$(this).wrap("<a>").parent("a").attr("href", $(this).attr("data-link"));
	});

	$.featureList(
		$("#tabs li a"),
		$("#output li"), {
			start_item: 0
		}
	);

	$(".breadcrumbs a:last").wrap("<span>").each(function() {
		$(this).closest("span").html($(this).text());
	});

	// Random logos
	for (var i = 0; i < 4; i++) {
		$("#logos div.size_2").find("figure:hidden:random").each(function() {
			$(this).appendTo($(this).parent()).show();
		});
	}
	for (var i = 0; i < 2; i++) {
		$("#logos div.size_1").find("figure:hidden:random").each(function() {
			$(this).appendTo($(this).parent()).show();
		});
	}

});

$(function() {
	if ($("#slider").length > 0) {
		$("#slider").slider({
				range: true,
				min: 0,
				max: 2500,
				step: 50,
				values: [ 0, 2500 ],
				stop: function(event, ui) {
					updateCatalogFilters();
				},
				slide: function( event, ui ) {
					$( "div.slider_holder span.value" ).html( "&euro;" + ui.values[ 0 ] + " - &euro;" + ui.values[ 1 ] );
				}});
		$( "div.slider_holder span.value" ).html( "&euro;" + $( "#slider" ).slider( "values", 0 ) +
			" - &euro;" + $( "#slider" ).slider( "values", 1 ) );
	}

});

$(window).load(function(){
	$("figure").each(function(){
		$(this).find("figcaption").css("max-width", $(this).find("img").width()).show();
	});
	$("div.input_block").each(function(){
		$(this).find("div.label").height($(this).height());
	});
});

/* Catalogus filters */

var $catalog;
var $overview;
var $slider;

$(function() {

	$catalog = $("#catalogContent");
	$overview = $catalog.find("div.overview");
	$slider = $("#slider");

	$("#filterMerk li, #filterSoort li").click(function() {
		updateCatalogFilters();
	});

	$.history.init(function(hash){
		if(hash == "") {
			// initialize your app
		} else {

			//console.log(hash);
			
			arrHash = hash.split("/");
			$.each(arrHash, function(index, value) {

				arrItem = value.split("=");
				if (arrItem.length == 2) {
					switch (arrItem[0]) {
						case "filter":
							arrFilter = arrItem[1].split("%2C%20");
							$.each(arrFilter, function(index, value) {
								//console.log(value);
								$("#filterMerk, #filterSoort").find("li[data-value='" + (value.replace(/%20/gi, " ")) + "']").addClass("checked");
							});
							break;
						
						case "minprice":
							$slider.slider("values", 0, arrItem[1]);						
							break;

						case "maxprice":
							$slider.slider("values", 1, arrItem[1]);
							break;
					}
				}

			});

			
			$( "div.slider_holder span.value" ).html( "&euro;" + $slider.slider("option", "values")[0] + " - &euro;" + $slider.slider("option", "values")[1] );
			
			updateCatalogFilters();
		}
	},
	{ unescape: ",/" });
});

function updateCatalogFilters() {

	$overview.addClass("loading");
	
	var cRequest = "/pageid=835";
	var cFilter = "";
	$("#filterMerk li.checked, #filterSoort li.checked").each(function() {
		cFilter += (cFilter == "" ? "" : ", ") + $(this).attr("data-value");
	});

	var min = $slider.slider("option", "values")[0];
	var max = $slider.slider("option", "values")[1];
	if (min > $slider.slider("option", "min")) {
		cFilter += "/minprice=" + min;
	}
	if (max < $slider.slider("option", "max")) {
		cFilter += "/maxprice=" + max;
	}
	
	cFilter = filterURLChar(cFilter);

	$.history.load("filter=" + cFilter);
	cRequest = (cFilter == "" ? cRequest : cRequest + "/filter=" + cFilter);

	cRequest += "/Filter.html #catalogContent";

	$catalog.load(cRequest, function() {
		$overview = $catalog.find("div.overview");
		$("span.paging a").click(function() {
			pagingClick($(this));
			return false;
		});
	});
}

function pagingClick($obj) {
	
	$overview.addClass("loading");

	cRequest = filterURLChar($obj.attr("href").replace("pageid=835", "pageid=825")) + " #catalogContent";
	
	$catalog.load(cRequest, function() {
		$overview = $catalog.find("div.overview");
		$("span.paging a").click(function() {
			pagingClick($(this));
			return false;
		});
	});
}

function filterURLChar(cURL) {
	cURL = cURL.replace(/ /gi, "%20");
	cURL = cURL.replace(/,/gi, "%2C");
	return cURL;
}

jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
    random: function(a, i, m, r) {
        if (i == 0) {
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
        };
        return i == jQuery.jQueryRandom;
    }
});

function equalHeight(obj){
	topHeight = 0;
	obj.each(function(){
		   topHeight = ($(this).height() > topHeight ? $(this).height() : topHeight)
	});
	obj.height(topHeight);
}
