var uberZindex = 10000000;

$(document).ready(function()			{
 		 
		 
		 // show catalog
		 
		 $(".download_catalogue").click(function() {			
			$("#pdf-catalog").fadeIn();
			$("#pdf-catalog-bg").fadeIn();
			$("#pdf-catalog").css("z-index", uberZindex);
			uberZindex++;
			
			$("#pdf-catalog iframe").attr("src", "/brochure/index.html");
		 });
		 
		 $(".pdf-catalog-close, #pdf-catalog-bg").click(function() {	
			$("#pdf-catalog").hide();
			$("#pdf-catalog-bg").hide();
			$("#pdf-catalog iframe").attr("src", "");
		 });
	  
			
	     //newspanel home
	     $(".news_imgs").cycle({
			before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
			   goto_slide(nextSlideElement);
			},
			fx:     'scrollVert',
			speed: '700',
			timeout: 6000
		 });
		 
		 $(".btns a:not(.more)").hover(function() {
			$(".news_imgs").cycle('pause');
			
			var nr = $(this).parent().prevAll().length;
			$(".news_imgs").cycle(nr);
			goto_slide($(this).parent());
		 }, function() {
			$(".news_imgs").cycle('resume');
		 });
		 
		$(".btns .item").hover(function() {
			$(this).find(".arrow").stop().animate({ left: "217px"  }, 300, "easeOutQuint" );
		 }, function() {
			
			$(this).find(".arrow").stop().animate({ left: "300px"  }, 500 );
		});	

		/* search left action buttons */
		$(".submit-img").hover(function() {
			$(this).css({ width: "20px", marginLeft: "-2px", marginTop: "1px"  });
		 }, function() {
			$(this).css({ width: "22px", marginLeft: "0px", marginTop: "0px"  });
		});			
		
		
	     //morepanel product page
	     $(".news_imgs2").cycle({
			before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
			   goto_slide(nextSlideElement);
			},
			fx:     'scrollVert',
			speed: '700',
			timeout: 6000
		 });
		 
		 $(".btns a:not(.more)").click(function() {
			$(".news_imgs2").cycle('pause');
			
			var nr = $(this).parent().prevAll().length;
			$(".news_imgs2").cycle(nr);
			goto_slide($(this).parent());
		 }, function() {
			$(".news_imgs2").cycle('resume');
		 });
		 
		 
		 // home fin motion extra
		 
		$(".finmotion").hover(function() {
		   $(this).find(".finart").stop().animate({ width: "400px", marginLeft: "-20px", opacity: "1" }, 500, "easeOutQuart");
		}, function() {
		   $(this).find(".finart").stop().animate({ width: "380px", marginLeft: "0px", opacity: "0.5" }, 500, "easeOutQuart");		 

		});
		 // home 12v extra
		 
		$(".12v").hover(function() {
		   $(this).find(".arm").stop().animate({ top: "40px" }, 500, "easeOutQuart");
		   $(this).find(".12v_info").stop().animate({ opacity: "1" }, 700, "easeOutQuart");
		}, function() {
		   $(this).find(".arm").stop().animate({ top: "50px" }, 500, "easeOutQuart");	 
		   $(this).find(".12v_info").stop().animate({ opacity: "0" }, 700, "easeOutQuart");
		});
		
		 // bestelknop
		 
		$("#shoppingcart  .btn").hover(function() {
		   $(this).stop().animate({ opacity: "0.8" }, 700, "easeOutQuart");
		}, function() {
		   $(this).stop().animate({ opacity: "1" }, 700, "easeOutQuart");
		});		
		
		// cycle sfeer
		
	     $(".sfeerpanel div").cycle({
			fx:     'fade',
			speed: '700',
			timeout: 2000
		 });

		// meer uit serie cycle + mouseovers
		
	     $(".seriepanel-content").cycle({
			fx:     'scrollHorz',
			speed: 'fast',
			timeout: 0,
			next: '.zap-right',
			prev: '.zap-left'
		 });
		 
		$(".seriepanel-content a").hover(function() {
		   $(this).css({ opacity: "0.6" });
		}, function() {
		   $(this).css({ opacity: "1" });		 

		});		 
		 
		// show dealer login
		
		$(".dealer-login").click(function() {
			$(".dealer-login-panel").toggle();
			$(".dealer-login-panel").css("z-index", uberZindex);
			uberZindex++;
		 });		
		$(".dealer-login-panel").hover(function() {
			$(".dealer-login-panel").css("z-index", uberZindex);
			uberZindex++;
		 });	

		// show dealer settings
		
		$(".dealer-settings, .dealer-settings-panel").hover(function() {
			$(".dealer-settings-panel").toggle();
			$(".dealer-settings-panel").css("z-index", uberZindex);
			uberZindex++;
		 });		
		$(".dealer-settings-panel").hover(function() {
			$(".dealer-login-panel").css("z-index", uberZindex);
			uberZindex++;
		 });		 
		
		// header fx.
		 
		swooosh();
		
		// product pag > delen en vraag
		
		$(".relproducts .item span").click(function() {
			$(".pop").hide();
			$(this).parent().find(".pop").toggle();
			$(".pop").css("z-index", uberZindex);
			uberZindex++;
		 });	
		 
		$(".closepop").click(function() {
			$(".pop").hide();
		 });		 
		 
		// melding pop up sluiten
		
		$(".form_melding_overlay .sluiten").click(function() {
			$(".form_melding_overlay").hide();
		 });	
		 

		
	$('#items').masonry({
		itemSelector : '.product',
		columnWidth : 200,
		isAnimated : true	
	  });
		

		activate_producten();		   
		 
		
	$("#uitvoering, #stijl, #serie").change(function () {
		filter_producten();	 
				
	});
});



		

$(window).load(function () {
	
	// Werk load
    
	$("#items .product img:eq(0)").animate({ opacity: "1" }, function () {
		$(this).parent().next().find("img").animate({ opacity: "1" }, 100, arguments.callee);
	});
	
 
});

function activate_producten() {
	$(".product").hover(function() {
		$(this).css("z-index", uberZindex);
		uberZindex++;
        var imgWidth = 	$(this).find("img").width();
        var paddingX = Math.floor((150-imgWidth)/2)+10;	
		$(this).find("img").css({ border: "1px solid #D2D2D2", marginTop: "-35px", marginLeft: "-10px", paddingLeft: paddingX, paddingRight: paddingX});
        $(this).find(".product-descr, .product-bestel a, .product-info").css({ display: "block" });
	 }, function() {
		$(this).find("img").css({ border: "1px solid #fff", margin: "0px auto", padding: "0px" });
		$(this).find(".product-descr, .product-bestel a, .product-info").css({ display: "none" });
	 });

	$(".product img, .product-descr, .product-info").click(function() {
		$(".product-view").css("z-index", uberZindex);
		$(".product").css({ zIndex: "1" });
		uberZindex++;	
		$(".product-view-fade").toggle();
		$(".product-view").toggle();
	});
	
	$(".product-view-fade").click(function() {
		$(".product-view-fade").toggle();
		$(".product-view").toggle();		
	});


	// cycle for product pag
	$('.product-view-imgs .product-view-imgs-images').before('<ul id="nav">').cycle({ 
		fx:     'fade', 
		speed:  '500', 
		timeout: 0, 
		pager:  '#nav', 
		after: onAfter, 
		// callback fn that creates a thumbnail to use as pager anchor 
		pagerAnchorBuilder: function(idx, slide) { 
			return '<li><a href="#" title="Bekijk foto"><img src="/images/thumb-product.png" width="50" height="50" /></a></li>'; 
		} 
	});	 
	
	function onAfter(curr, next, opts, fwd) {
	  var $ht = $(this).height();

	  //set the container's height to that of the current slide
	  $(this).parent().animate({height: ($ht)+ 20});
	}	
	
}

function filter_producten() {


	
}

function Xfilter_producten() {

	$(window).scrollTop(0);
	
	var uitvoering = $("#uitvoering option:gt(0):selected").val();
	var stijl = $("#stijl option:gt(0):selected").val();
	var serie = $("#serie option:gt(0):selected").val();
	
	if (typeof uitvoering == "undefined") {
		uitvoering = "";
	}
	if (typeof stijl == "undefined") {
		stijl = "";
	}
	if (typeof serie == "undefined") {
		serie = "";
	}
	
	$("#items").html("<div class='laden'>producten worden geladen</div>");
	$.post("/ajax/request.php", {
		action: "filter_producten",
		uitvoering: uitvoering,
		stijl: stijl,
		serie: serie
	}, function (data) {
		$("#items").html("");
		
		//rubrieknaam aanpassen
		$("#cat_title").html("producten " + serie);
		
		
		for (var i = 0; i < data.products.length; i++) {			
			var product = $("<div class='product'></div>").appendTo($("#items"));
			$(product).css({
				height: (data.products[i].foto_h) + "px", // + 60
				width: (data.products[i].foto_b + 60) + "px"
			});
			var img = $("<img src='/thumbs/product/150x0/" + data.products[i].foto + "' alt='' />").appendTo(product);
			var desc = $("<div class='product-descr'></div>").appendTo(product);
			$("<h2>" + data.products[i].serie + " <span>" + data.products[i].artikelnr + "</span></h2>").appendTo(desc);
			$("<span class='product-descr-price'>&euro; " + data.products[i].prijs + "</span>").appendTo(desc);
			$("<div class=\"product-bestel\"><a href=\"/product/\" title=\"Bekijk " + data.products[i].serie + "\">Bekijk</a></div><a href=\"/product/" + data.products[i].url_titel + "/\" title=\"Bekijk " + data.products[i].serie + "\" class=\"alllink\"><img src=\"http://www.franssenverlichting.nl/images/blank.gif\" alt=\"\" /></a>").appendTo(product);
		}
		
		activate_producten();
		$("#items").masonry('reload');
	}, "json");
}

function outlet_producten() {
	$(window).scrollTop(0);
	
	$("#items").html("<div class='laden'>producten worden geladen</div>");
	$.post("/ajax/request.php", {
		action: "outlet"}, function (data) {
		$("#items").html("");
		
		for (var i = 0; i < data.products.length; i++) {
			var product = $("<div class='product'></div>").appendTo($("#items"));
			$(product).css({
				height: (data.products[i].foto_h) + "px", // + 60
				width: (data.products[i].foto_b + 60) + "px"
			});
			var img = $("<img src='/thumbs/product/150x0/" + data.products[i].foto + "' alt='' />").appendTo(product);
			var desc = $("<div class='product-descr'></div>").appendTo(product);
             data.products[i].serie = data.products[i].titel;
			$("<h2><span>" + data.products[i].artikelnr + "</span></h2>").appendTo(desc);
			$("<span class='product-descr-price'>Van: &euro; <s>" + data.products[i].prijs + "</s></span><br/><span class='product-descr-price'>Voor: &euro; " + data.products[i].outlet_prijs + "</span>").appendTo(desc);
			$("<div class=\"product-bestel\"><a href=\"/product/\" title=\"Bekijk " + data.products[i].serie + "\">Bekijk</a></div><a href=\"/product/" + data.products[i].url_titel + "/\" title=\"Bekijk " + data.products[i].serie + "\" class=\"alllink\"><img src=\"http://www.franssenverlichting.nl/images/blank.gif\" alt=\"\" /></a>").appendTo(product);
		}
		
		activate_producten();
		$("#items").masonry('reload');
	}, "json");
}

// header fx lights.
function swooosh() {
	$(".swoosh").delay(4000).animate({ left: "110px"  }, 500, "easeOutQuint", function () {
		$(".swoosh").css({ display: "none" });
		$("#logo a").css({ backgroundPosition: "0px -142px" });
		swooosh2();
	});
}
function swooosh2() {
	$(".swoosh").animate({ left: "155px"  }, 100, "easeOutQuint", function () {
		$(".swoosh").css({ display: "block" });
		$("#logo a").css({ backgroundPosition: "0px 0px" });
		swooosh3();
	});
}
function swooosh3() {
	$(".swoosh").animate({ left: "940px"  }, 2000, "easeOutQuint", function () {
		$(".swoosh").css({ display: "block" });
		swoooshReset();
	});
}
function swoooshReset() {
	$(".swoosh").css({ left: "-600px" });
	swooosh();
}
//newspanel home
function goto_slide(nextSlideElement) {
 var nr = $(nextSlideElement).prevAll().length;
 $(".btns a").removeClass("on");
 $(".btns a:eq(" + nr + ")").addClass("on");
 $(".snippet div").css("display", "none");
 $(".snippet div:eq(" + nr + ")").css("display", "block");
}

/* Collectie selector */
function setCollectie(col) {
    var currentUrl = $(location).attr('href');
    $("form#collectieselectform input[name=collectie]").val(col);
    $("form#collectieselectform").attr('action',currentUrl);
    $("form#collectieselectform").submit();
    return false;    
}


//Jquery validate toevoeging
jQuery.validator.addMethod("postcodeNL", function(value, element, params) {
        	return this.optional(element) || /^\d{4} ?[a-z]{2}$/i.test(value);
        }, "Vul een geldige postcode in. (1234AB)");

        
jQuery.validator.addClassRules({
	postcodeNL: 		{ postcodeNL: true }
});

