/*
    Document   : vanrooijen javascript
    Author     : PureDutch | Martijn van der Hulst
*/
$(document).ready(function(){

	// COLORBOX - LARGE PICTURES
	$(".largePic").colorbox({
		photo:true
	});


	/**
	 * splash page. To load or not to load..
	 */
	$(function() {
		var COOKIE_NAME = 'splash-page-cookie';
		var $go = $.cookie(COOKIE_NAME);
		var path = window.location.pathname;
		
		if (path != "/"){
			$.cookie(COOKIE_NAME, 'test', {path: '/', expires: 1});
			$("#splashWrapper").hide();
			$("#wrapper").show();
		} else if ($go == null) {
			$.cookie(COOKIE_NAME, 'test', {path: '/', expires: 1});
			$("#splashWrapper").show();
			$("#wrapper").hide();
		} else {
			$("#splashWrapper").hide();
			$("#wrapper").show();
		}
	});

	// *************
	// ** OFFERTE **
	// *************

	// OPEN FUNCTION
	function openOfferte(){
		// CHECK FOR CURRENT STATUS
		if ($('#offerteOut').css("display") == "none"){
			//show close button
			$('#offerteClose').show(0);
			// FOLD OUT
			$('#offerteOut').slideDown(500);
			// UNBIND CURRENT FUNCTIONS
			$('#offerteIn').unbind();
			// BIND SUBMIT TO EXISTING BUTTON
			$('#offerteIn').bind('click', function() {
			  $('#offerteForm').submit();
			});
		}
	}
	$('#offerteIn').click(function() {
		openOfferte();
	});

	if ($('#OPENOFFERTE').length > 0){
		openOfferte();
	}
	// CLOSE FUNCTION
	$('#offerteClose').click(function(){
		if ($('#offerteOut').css("display") == "block"){
			//show close button
			$('#offerteClose').hide(0);
			// FOLD IN
			$('#offerteOut').slideUp(500);
			// UNBIND CURRENT FUNCTIONS
			$('#offerteIn').unbind();
			// BIND OPEN FUNCTION AGAIN
			$('#offerteIn').bind('click', function() {
				openOfferte();
			});
		}
	});

	// ************
	// ** BELMIJ **
	// ************
	
	// BIND SUBMIT TO EXISTING BUTTON
	$('#belmijIn').bind('click', function() {
	  $('#belmijForm').submit();
	});

	// OPEN FUNCTION
	function openBelmij(){
		// CHECK FOR CURRENT STATUS
		if ($('#belmijOut').css("display") == "none"){
			//show close button
			$('#belmijClose').show(0);
			// FOLD OUT
			$('#belmijOut').slideDown(500);
			// UNBIND CURRENT FUNCTIONS
			$('#belmijIn').unbind();
			// BIND SUBMIT TO EXISTING BUTTON
			$('#belmijIn').bind('click', function() {
			  $('#belmijForm').submit();
			});
		}
	}
	$('#belmijIn').click(function() {
		openBelmij();
	});

	// CLOSE FUNCTION
	$('#belmijClose').click(function(){
		if ($('#belmijOut').css("display") == "block"){
			//show close button
			$('#belmijClose').hide(0);
			// FOLD IN
			$('#belmijOut').slideUp(500);
			// UNBIND CURRENT FUNCTIONS
			$('#belmijIn').unbind();
			// BIND OPEN FUNCTION AGAIN
			$('#belmijIn').bind('click', function() {
				openBelmij();
			});
		}
	});	

	$("#offerteForm").validate({
		rules: {
			offerte_naam: {required: true},
			offerte_adres: {required: true},
			offerte_postcode: {required: true},
			offerte_plaats: {required: true},
			offerte_telnr: {required: true},
			offerte_email: {required: true, email: true},
			offerte_vraagopmerking: {required: true}
		},
		errorClass: "invalid",
		messages: {
			offerte_naam: "",
			offerte_adres: "",
			offerte_postcode: "",
			offerte_plaats: "",
			offerte_telnr: "",
			offerte_email: {
				required: "",
				email: ""
			},
			offerte_vraagopmerking: ""
		}
	});

	$("#belmijForm").validate({
		rules: {
			belmij_naam: {required: true},
			belmij_telnr: {required: true}
		},
		errorClass: "invalid",
		messages: {
			belmij_naam: "",
			belmij_telnr: ""
		}
	});

	// settings for offerte
	var COOKIE_NAME_PRODUCT1 = 'offerteProduct1';
	var COOKIE_NAME_PRODUCT2 = 'offerteProduct2';
	var COOKIE_NAME_INPUTS = 'offerteInputs';
	var cookiePath = "/";

	// ADD PRODUCT TO OFFERTE!!!
	$(".addOfferte").click(function(){
		var selectedProduct = $(this).parent().parent().children("img").attr("src");
		var selectedProductID = $(this).attr("id").substr(8);		
		
		// check first cookie
		var $cookie1 = $.cookie(COOKIE_NAME_PRODUCT1);
		if ($cookie1 == null) {
			// set cookie product1
			$.cookie(COOKIE_NAME_PRODUCT1, selectedProduct+"|"+selectedProductID, {path: cookiePath, expires: 6});
			// open offerte scherm
			openOfferte();
			// add product to offerte
			$("#offerteOptie1 .optieImage img").attr("src", selectedProduct);
			$("#_productID1").attr("value", selectedProductID);
			$("#offerteOptie1").show(500);
		} else {
			// check second cookie
			var $cookie2 = $.cookie(COOKIE_NAME_PRODUCT2);
			if ($cookie2 == null) {
				// set cookie product2
				$.cookie(COOKIE_NAME_PRODUCT2, selectedProduct+"|"+selectedProductID, {path: cookiePath, expires: 6});
				// open offerte scherm
				openOfferte();
				// add product to offerte
				$("#offerteOptie2 .optieImage img").attr("src", selectedProduct);
				$("#_productID2").attr("value", selectedProductID);
				$("#offerteOptie2").show(500);
			} else {
				alert("Maximaal 2 producten toegestaan per offerte!")
			}
		}
	});

	// remove offerte 1
	$("#optieDelete1").click(function(){
		// check if offerteOptie2 is filled, if so, move optie2 to optie1!		
		if ($.cookie(COOKIE_NAME_PRODUCT2) != null) {
			// move content cookie2 to cookie1
			$.cookie(COOKIE_NAME_PRODUCT1, $.cookie(COOKIE_NAME_PRODUCT2) ,{path: cookiePath});
			// change img values
			$("#offerteOptie1 .optieImage img").attr("src", $.cookie(COOKIE_NAME_PRODUCT1));
			// remove cookie2
			$.cookie(COOKIE_NAME_PRODUCT2, null, {path: cookiePath});
			// reset offerteOptie2
			$("#offerteOptie2 .optieImage img").attr("src", "");
			$("#offerteOptie2").css({"display":"none"});
		} else {
			// delete cookie
			$.cookie(COOKIE_NAME_PRODUCT1, null, {path: cookiePath});
			// reset offerteOptie1
			$("#offerteOptie1 .optieImage img").attr("src", "");
			$("#offerteOptie1").css({"display":"none"});
		}		
	});

	// remove offerte 2
	$("#optieDelete2").click(function(){
		//delete cookie
		$.cookie(COOKIE_NAME_PRODUCT2, null, {path: cookiePath});
		// reset offerteOptie2
		$("#offerteOptie2 .optieImage img").attr("src", "");
		$("#offerteOptie2").css({"display":"none"});
	});

	// store input fields from offerte form in cookie!!!
	$(".saveCookie").blur(function(){
		// collect and merge all inputs from form
		var inputArray = new Array();
		var i = 0;
		$("#offerteForm :input[type=text]").each(function(){
			inputArray[i] = $(this).attr("value");
			i++;
		});
		// add textarea!
		inputArray[i] = $("#offerteForm :input[type=textarea]").val();

		$.cookie(COOKIE_NAME_INPUTS, inputArray.join("|"), {path: cookiePath, expires: 6})		
	});

	// check if offerte screen should be opened!!
	if (($("#offerteOptie1").css("display") == "block") || ($("#offerteOptie2").css("display") == "block") || ($.cookie(COOKIE_NAME_INPUTS) != null)){
		openOfferte();
	}

	// ****************************************************************
	// ******** SLIDER STUFF ******************************************
	// ****************************************************************

	var autoLoop = true;
	var timeoutID;

	// eerste thumb selected maken en eerste description active zetten!
	$("#sliderBullets ul li:first").children("a").addClass("active");
	$("#description_0").addClass("selected");

	// klik op plaatje move!
	$(".thumbSlide").click(function(){
		// disable auto
		autoLoop = false;
		clearTimeout(timeoutID);

		// GET SELECTED ID
		var selectedImage = $(this).attr("id").substr(6);
		// do move!!
		moveImage(selectedImage);
	});

	function resetSelected()
	{
		// REMOVE ALL SELECTED
		$(".thumbSlide").removeClass("active");
	}

	function moveImage(selectedImage)
	{
		//reset current selected
		resetSelected();

		var imagePos = parseInt(selectedImage);
		var imagePos = imagePos + "";
		var sliderPos = $("#slider").css("left");

		// SET SELECTED IMAGE SELECTED
		$("#thumb_"+selectedImage).addClass("active");

		var pos = (parseInt(selectedImage)-1)*373;
		$("#slider img").fadeOut(500, function(){
			$("#slider").animate({
			left: "-"+pos+"px"
		  }, 150, function(){
			$("#slider img").fadeIn(500);
		  });
		});
	}

	function timer(){
		if (autoLoop == true){
			timeoutID = setTimeout(function() {
				var selectedImage = "";
				$(".thumbSlide").each(function(){
					if ($(this).hasClass("active") == true){
						selectedImage = $(this).attr("id").substr(6);
					}

				})
				var imagePos = parseInt(selectedImage)+1;
				var imagePos = imagePos + "";
				if (imagePos > $(".imageSlide").length){
					imagePos = 1;
				}
				moveImage(imagePos);
				timer();
			}, 5000);
		}
	}

	timer();

});

