var searchterm_str = "uw zoekterm";
var emailsubscription_str = "uw e-mailadres";

var startlist = new Object({booklist_top: 0, booklist_bottom: 0});
var nrElements = new Object({booklist_top:4, booklist_bottom: 3});
var maxMove = new Object({booklist_top:0, booklist_bottom: 0});
var total_list_top = 0;
var total_list_bottom = 0;
var booklist_width = 720;
var top_element_width = 180;
var bottom_element_width = 240;

/* Newsletter form variables */
var input_email = new Object();
var input_voornaam = new Object();
var input_familienaam = new Object();

var slide_interval = 10000;
var timer_top;
var timer_bottom;
 

google.setOnLoadCallback(function(){
					
	$.ajaxSetup({
		type:		"post",
		dataType:	"json",
		error:		"postError"
	});
					
	/**
	 * Set width and height for slider blocks
	 */	
	if($("#booklist_top_holder").length > 0){
		var top_w = (Number($("#booklist_top_holder dl").length) * top_element_width)+30;
		$("#booklist_top_holder").width(top_w);
		
		var top_h = $("#booklist_top_holder").height();
		$("#booklist_top_wrapper").height(top_h);
	}
	
	if($("#booklist_bottom_holder").length > 0){
		var bottom_w = (Number($("#booklist_bottom_holder div.booklist_holder").length) * bottom_element_width)+30;
		$("#booklist_bottom_holder").width(bottom_w);
		
		var bottom_h = $("#booklist_bottom_holder").height();
		$("#booklist_bottom_wrapper").height(bottom_h);
	}
	
	
	/**
	 * Show login screen
	 */
	$("#show_login_btn").click(function(){
		var pos = ($("#holder_login").css("top") == "-150px") ? "0px" : "-150px";
		$("#holder_login").stop().animate(
			{top:pos}, 
			{duration:400}
		)					   
	});
	
	$("#wrapper_top").bind("mouseleave", function(){
		//$("#holder_login input").blur();
		$("#holder_login").stop().animate(	
			{top:"-150px"}, 
			300,
			"linear",
			function(){
				$("fieldset#login_regular").removeClass("hide");
				$("fieldset#login_forgotpass").addClass("hide");
				$("#link_forgotpassword").removeClass("selected");
			}
		)
	})
	
	
	/**
	 * Switch Login - Forgot Password screen
	 */
	$("#link_forgotpassword").click(function(){
		showLoginForm();
		$("fieldset#login_regular").toggleClass("hide");
		$("fieldset#login_forgotpass").toggleClass("hide");
		$(this).toggleClass("selected");
	});
	
	/**
	 * Switch Login - Forgot Password screen on Order-form
	 */
	$("#order_link_forgotpassword").click(function(){
		//showLoginForm();
		$("fieldset#order_login_forgotpass").toggleClass("hide");
		$(this).toggleClass("selected");
	});
		
	/**
	 * Login form button actions
	 */
	$("#login_form").submit(function(){
		// Submit input fields to
		$.ajax({
			url:$("form#login_form").attr("action"),
			data:$("form#login_form").serialize(),
			success:loginSuccess
		});
		
		return false;
	});
	
	
	/**
	 * Clear formfields on focus
	 */
	$("input#searchterm").focus(function(){
		if($(this).val() == searchterm_str){
			$(this).val("");
		}
	}).blur(function(){
		if($(this).val() == ""){
			$(this).val(searchterm_str);
		}
	});
	
	
	$("#subscription_email").focus(function(){
		if($(this).val() == emailsubscription_str){
			$(this).val("");
		}
	}).blur(function(){
		if($(this).val() == ""){
			$(this).val(emailsubscription_str);
		}
	});
	
	
	/**
	 * Open Share Form in Overlay
	 */
	$(".link_share").click(function(){
		$("div#share_overlay_wrapping").load("/includes/delen.php", null, function(){
			var ww = $(window).width();
			var wh = $(window).height();
			
			var bw = $("#share_overlay_popup").width();
			var bh = $("#share_overlay_popup").height();
			
			var posX = String(ww/2 - bw/2) + "px";
			var posY = String(wh/2 - bh/2) + "px";
			
			$("#share_overlay_popup").css({'top' : posY, 'left' : posX});
			
			$("#share_overlay_wrapping").show();
		});								
	});
	
	
	/**
	 * Set overline width
	 */
	if($("ul.overline").length > 0){
		$("ul.overline").each(function(i, val){
			var w = String($("ul.overline li").width()) + "px";
			$(this).css("width", w);
		});	
	}
	
	
	/**
	 * Slide Navigation
	 */
	/*
	$(".slide_navigation li a").click(function(){							
		if($(this).hasClass("selected") == false){
			var id= $(this).attr("id").replace("btn_slide_", "");
			var part = $(this).parent("li").parent("ul").parent("div.title_wrap").attr("id").replace("_slider", "");
			var holder = $("#booklist_" + part + "_wrapper").children(".slide_holder");
			var currPos = startlist[holder.attr("id").replace("_holder", "")];
			var dir = (currPos < id) ? -1 : 1;	
			var totalWidth = ((id-1) * booklist_width) * dir;
			$(this).parent("li").parent("ul").find(".selected").removeClass("selected");
			$(this).addClass("selected");
			currPos = id;
			
			holder.stop().animate(
				{left:String(totalWidth)+"px"}, 
				{duration:500})
		}
	});
	*/
	if($("ul.slide_navigation").length > 0){
		$("ul.slide_navigation").each(function(i, val){
			var part = $(this).parent("div.title_wrap").attr("id").replace("_slider", "");
			var holder = $("#booklist_" + part + "_wrapper").children(".slide_holder");
			if(part == "top"){
				maxMove["booklist_top"] = Math.ceil(holder.children('dl').size() / nrElements["booklist_top"]);
				timer_top = setInterval(switch_slider_top, slide_interval);
			}else{
				maxMove["booklist_bottom"] = Math.ceil(holder.children('.booklist_holder').size() / nrElements["booklist_bottom"]);
				timer_bottom = setInterval(switch_slider_bottom, slide_interval);
			}
			setSlider(part);
		});
	}
	
	$(".slide_navigation li a").click(function(){							
		if($(this).hasClass("selected") == false){
			//var id= $(this).attr("id").replace("btn_slide_", "");
			var dirName = $(this).attr("id").replace("btn_slide_", "");
			var part = $(this).parent("li").parent("ul").parent("div.title_wrap").attr("id").replace("_slider", "");
			
			if(part == "top"){
				clearInterval(timer_top);
				timer_top = setInterval(switch_slider_top, slide_interval);
			}else{
				clearInterval(timer_bottom);
				timer_bottom = setInterval(switch_slider_bottom, slide_interval);
			}
			
			moveSlider(part, dirName);
		}
	});
	
	
	/**
	 * Show functional navigation
	 */
	/*
	$(".functional_navigation_btn").mouseover(function(){
		var block = $(this).parent("div.functional_navigation_wrapper");
		block.children(".functional_navigation").slideDown("fast");
	});
	
	$(".functional_navigation").bind("mouseleave", function(){
		var block = $(this).parent("div.functional_navigation_wrapper");	
		$(this).slideUp("fast");							  
	});
	*/
	
	/**
	 * Registration - show / hide addressblocks
	 */
	$(".title_row input").click(function(){
		var id = $(this).attr("id");
		if($(this).is(":checked")){
			$("div#"+id+"_block").slideDown("fast");
		}else{
			$("div#"+id+"_block").slideUp("fast");
		}
	})
	
	
	/**
	 * Show comment Block
	 */
	$(".btn_react").click(function(){
		switchCommentForm("open");					   
	});
	
	
	/**
	 * Show full comment
	 */
	$("dl.close dd.part a").click(function(){
		$(this).parent("dd.part").parent("dl.close").removeClass("close").addClass("open");								   
	});
	
	/**
	 * Show excerpt
	 */
	$("#product_excerpts dt a.product_excerpt_detail").click(function(){
		var block = $(this).parent().next();
		if(block.hasClass("open")){
			block.removeClass("open");
			block.slideUp("fast");
		}else{
			block.addClass("open");	
			block.slideDown("fast");
		}
	});
	
	/**
	 * Accordeon
	 */
	$("#order_overview dl dt").click(function(){
		var parentBlock = $(this).parent("dl");
		if(parentBlock.hasClass("open")){
			$(this).next("dd").slideUp("fast", function(){
				parentBlock.removeClass("open").addClass("close");
				$(this).find("dl").each(function(i){
					if($(this).hasClass("open")){
						$(this).removeClass("open").addClass("close");
					}					 
				});
			});	
		}else{
			var contentBlock = $(this).next("dd");
			contentBlock.find("dl").each(function(i){
				$(this).children("dd").css("display", "none");
				if($(this).hasClass("open")){
					$(this).removeClass("open").addClass("close");
				}						 
			});
			contentBlock.slideDown("fast", function(){
				parentBlock.removeClass("close").addClass("open"); 
			});	
		}								  
	});

	
	
	/**
	 * Form Tooltip
	 */
	$("div.info_btn").mouseover(function(){
		var block = $(this).parent(".form_info").children(".info_content");
		block.fadeIn();
	}).mouseout(function(){
		var block = $(this).parent(".form_info").children(".info_content");
		block.fadeOut();
	})
	
	$("span.info_btn").mouseover(function(){
		var block = $(this).parent("h1").parent("div").children(".info_content");
		$(this).parent("h1").append(block);
		block.fadeIn();
	}).mouseout(function(){
		var block = $(this).parent("h1").children(".info_content");
		block.hide();
		$(this).parent("h1").parent("div").append(block);
	});
	
	
	/**
	 * Font resizing
	 */
	var originalFontSize = $('body').css('font-size');
	
    $("#resize_med").click(function(){
    	$('body #content').css('font-size', originalFontSize);
  	});
	
	// Increase Font Size
	$("#resize_large").click(function(){
		var currentFontSize = $('body #content').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = String(currentFontSizeNum*1.2) + "px";
		$('body #content').css('font-size', newFontSize);
		return false;
	});
	// Decrease Font Size
	$("#resize_small").click(function(){
		var currentFontSize = $('body #content').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = String(currentFontSizeNum*0.8) + "px";
		$('body #content').css('font-size', newFontSize);
		return false;
	});	
	
	/**
	 * Initialize shadowbox
	 */
	//Shadowbox.init({language: "nl", players:["iframe", "img"]});
	/*
	if ($("a[rel='shadowbox']").length > 0) {
		Shadowbox.init();
	}
	*/
	
	
	/**
	 * Minisite functionality
	 */
	$('#header_content_arrow').click(function(){
		if($(this).hasClass("down")){
			$("#header_content").stop().animate(
				{height:"245px"}, 
				300,
				"linear",
				function(){$('#header_content_arrow').removeClass("down").addClass("up");}
			)	
		}else{
			var h = $("#header_content dt").height();
			var totalH = 40 + Number(h);
			$("#header_content").stop().animate(
				{height:String(totalH) + "px"}, 
				300,
				"linear",
				function(){$('#header_content_arrow').removeClass("up").addClass("down");}
			)
		}
	});
	
	
	/**
	 * Newsletter form functionality
	 */
	if($("#newsletter-form fieldset input").length > 0){
		$.each($("#newsletter-form fieldset input"), function(){
			window['input_' + $(this).attr('name')].val = $(this).attr('value');
			
			$(this).focus(function(){
				if($(this).attr('value') == window['input_' + $(this).attr('name')].val){
					$(this).attr('value', '');
				}
			});
			
			$(this).blur(function(){
				if($(this).attr('value') == ''){
					$(this).attr('value', window['input_' + $(this).attr('name')].val);
				}
			});
		});
	}
	
	$("#newsletter-form form").submit(function(){
		var errors = 0;
		$.each($("#newsletter-form fieldset input"), function(){
			if($(this).attr('value') == '' || $(this).attr('value') == window['input_' + $(this).attr('name')].val){
				$(this).next('label').show();
				$(this).focus();
				errors += 1;
				return false;
			}else if($(this).attr('name') == 'email' && isValidEmail($(this).attr('value')) == false){
				$(this).next('label').show();
				$(this).focus();
				errors += 1;
				return false;
			}else{
				$(this).next('label').hide();
			}
		});
		
		if(errors == 0){
			return true;
		}else{
			return false;
		}
	});
	
});

/**
 * Timer Functions
 */
function switch_slider_top(){moveSlider("top", "next");}
function switch_slider_bottom(){moveSlider("bottom", "next");}

/**
 * Check login
 */
function loginSuccess(data){
	if(data.result == 1){
		showLoginMessage("success");
		
		if(data.type == "login"){
			$("#login_false").addClass("hide");
			$("#login_true").removeClass("hide");
			
			if(data.url != ""){
				window.location = data.url;
			}
		}
	}else{
		showLoginMessage("fault");
	}
	$("#login_result").removeClass("hide");
	$("#login_result").html(data.msg);
}


/**
 * Check share
 */
function shareSuccess(data){
	if(data.result == 1){
		$("#share_form_wrapper").hide();
		$("#share_success").show();
	}else{
		$.each(data.error_fields, function(i, val){
			$("#share_form input#"+val).addClass("error");						   
		});
		
		$("#share_error").html(data.msg);
	}
}

/**
 * Move Slider
 */
function moveSlider(part, dirName){
	var holder = $("#booklist_" + part + "_wrapper").children(".slide_holder");

	var currPos = startlist[holder.attr("id").replace("_holder", "")];

	if(dirName ==  "next"){
		//alert(String(currPos) + " -- " + String(Number(maxMove["booklist_" + part]) - 1));
		if(currPos == (Number(maxMove["booklist_" + part]) - 1)){
			currPos = 0;
		}else{
			currPos++;
		}
	}else{
		currPos--;
	}
	
	var newPos = currPos;	
	var totalWidth = (newPos * booklist_width) * -1;

	startlist[holder.attr("id").replace("_holder", "")] = newPos;
	
	setSlider(part);
	
	holder.stop().animate(
		{left:String(totalWidth)+"px"}, 
		{duration:500})
	
}

/**
 * Set Slider
 */
function setSlider(part){
	var nav = $("#" + part + "_slider").children(".slide_navigation");
	//var holder = $("#booklist_" + part + "_wrapper").children(".slide_holder");
	
	/*if(part == "top"){
		maxMove["booklist_top"] = Math.ceil(holder.children('dl').size() / nrElements["booklist_top"]);
	}else{
		maxMove["booklist_bottom"] = Math.ceil(holder.children('.booklist_holder').size() / nrElements["booklist_bottom"]);
	}
	*/
	
	if(startlist['booklist_' + part] <= 0){
		nav.children("li").children('a#btn_slide_prev').addClass('selected');
	}else{
		nav.children("li").children('a#btn_slide_prev').removeClass('selected');
	}
		
	if(startlist['booklist_' + part] >= maxMove['booklist_' + part] - 1){
		nav.children("li").children('a#btn_slide_next').addClass('selected');
	}else{
		nav.children("li").children('a#btn_slide_next').removeClass('selected');
	}
}		


/**
 * Comment form show / hide
 */
function switchCommentForm(state){
	if(state == "open"){
		$("form#comment_form").slideDown();
	}else{
		$("form#comment_form").slideUp();	
	}
}

/**
 * Login screens
 */
function showLoginMessage(type){
	$("#login_form").addClass("hide");
	$("#login_result").removeClass("hide");
	
	if(type == "success"){
		$("#link_forgotpassword").hide();
	}
}

function showLoginForm(){
	$("#login_result").addClass("hide");	
	
	$("#login_form input").val("");
	$("#login_form").removeClass("hide");
}

function closeShareOverlay(){
	$("#share_overlay_wrapping").html("");
	$("#share_overlay_wrapping").hide();
}


/**
 * Validate Email address
 */
function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  //strEmail = document.forms[0].email.value;

   // search email text for regular exp matches
   if (strEmail.search(validRegExp) == -1) 
   {
      //alert('A valid e-mail address is required.\nPlease amend and retry');
      return false;
    } 
    return true; 
}


/**
 * Set width inline image block with caption
 */
$(window).load(function(){
	if($("div.inline img").length > 0){
		$("div.inline img").each(function(i, val){							  
			var w = String($(this).width()) + "px";
			var block = $(this).parent();
			block.css("width", w);
		});	
	}
});