/* Uitvoeren wanneer pagina is geladen */
$(function(){
	// Search
	$("#tbSearch").keypress(function(e){
		if (e.which == 13)
		{
			$("#btnSearch").trigger("click");
		}
	});
	
	// Zoekscherm tonen
	$("#btnSearch").click(function(){
		if(typeof(_gaq) != "undefined")
		{
			_gaq.push(['_trackEvent', 'Zoeken', 'clicked']);
		}
		
		var searchVal = $.trim($("#tbSearch").val());
		
		var customSearchControl = new google.search.CustomSearchControl('005740458775357238173:mx_ywrysvf8');
		
		createDialog({
			url: "",
			width: 800,
			height: 600,
			type: "GoogleCSE",
			title: "U wilt zoeken op: " + searchVal
		});
		
		var drawOptions = new google.search.DrawOptions();
		
		customSearchControl.draw("GoogleCSEResults", drawOptions);
		customSearchControl.execute(searchVal);
	});
	
	// Tweetable Twitter plugin
	if($("#Tweets").length > 0)
	{
		$("#Tweets").tweetable();
	}
	
	// Faq items
	$("input.FaqButton", $("div.FaqItem")).click(function(){
		var objElm = $("#" + $(this).attr("data-faqid") + "FaqItem");
		var objVisibleElm = $("div.FaqDescription:visible", $("div.FaqItem"));
		
		$(this).css({"visibility":"hidden"});
		
		if(objVisibleElm.length > 0)
		{
			objVisibleElm.slideToggle(500);
			objVisibleElm.parent().children("input.FaqButton").css({"visibility":"visible"});
			objVisibleElm.parent().children("h3.FaqHeader").animate({"font-size":"12px"}, 150);
		}
			
		objElm.parent().children("h3.FaqHeader").animate({"font-size":"16px"}, 150);
		objElm.slideToggle(500);
		
	}).first().click();
	
	// Nieuws
	$.each($("div.NewsMessage h3"),function(i){
		if($(this).height() <= 20)
		{
			$(this).parent().children("div.Description").css({"height":"144px"});
		}
	});
	
	// Broken Images
	$("img").error(function(){
		$(this).attr("src","images/noimage.png");
	});
	
	// Scrollbar op artikelgroepen slider
	if($("#ArticleGroupSliderWrapper").length > 0)
	{
		$("#ArticleGroupSliderWrapper").jScrollPane();
	}
	
	// Artikel bestellen
	$("a[rel=OrderArticle]").click(function(e){
		e.preventDefault();
		
		createDialog({
			url: "article/order.php?ArticleId=" + $(this).attr("href"),
			width: 700,
			height: 600,
			type: "iframe",
			title: "U wilt graag een " + $(this).attr("data-name") + " bestellen"
		});
	});
	
	// Form submitten
	$("a[rel=SubmitForm]").click(function(e){
		e.preventDefault();
		
		var frm = $(this).closest("form");
		
		$(frm).submit();
	});
	
	// Newsletter tonen
	$("#NewsLetter, a[rel=NewsLetter]").click(function(e){
		e.preventDefault();
		
		createDialog({
			url: "",
			width: 350,
			height: 206,
			type: "newsletter",
			title: "Aanmelden voor nieuwsbrief"
		});
	});
	
	// Select disabelen
	$("input:radio[@name='Delivery']").click(function(){
		if($(this).val() == "Collect")
		{
			$("#cbCollectLocation").removeAttr("disabled");
		}
		else
		{
			$("#cbCollectLocation").attr("disabled","disabled");
		}
	});
	
	// Cancel button Dialog
	$("a[rel='CloseDialog']").click(function(e){
		e.preventDefault();
		
		parent.closeDialog();
	});
	
	$(document.frm).submit(function(){
		var result = true;
		var validElm = $("input[type=text], input[type=password], input[type=checkbox], input[type=radio] select, textarea", $(this));
		
		validElm.removeClass("InvalidInput");
		
		if($("#SkipValidation").val() === "true")
		{
			return result;
		}
		
		$.each(validElm, function(i, elm){
			var value = $.trim($(elm).val());
			
			if($(elm).attr("data-validation-kind") == "integer" && value != "")
			{
				var numberResult = Number(value);
				
				if (isNaN(numberResult))
				{
					$(elm).addClass("InvalidInput").parent().append('<span rel="InvalidInputDescription">Invoer is geen geldig getal</span>');
					
					if(result === true)
					{
						result = false;
					}
				}
			}
		
			if($(elm).attr("data-required") == "true" && value == "")
			{				
				$(elm).addClass("InvalidInput");
				
				if(result === true)
				{
					result = false;
				}
			}			
		});		
		
		if(result === false)
		{
			showMessage("Niet alle velden zijn juist ingevuld!", "Error");
		}
		
		return result;
	});
});

var showMessage = function(message, type)
{
	$('<div id="MessageBox" class="MessageBox ' + type + '">' + message + '</div>').appendTo(document.body).slideDown(400);
	
	setTimeout(function(){
		$("#MessageBox").slideUp(400, function(){
			$(this).remove();
		});
	}, 5000);
}

// Dialog maken
var createDialog = function(options)
{
	var html;
	var width = options.width;
	var height = options.height;
	var callback = options.callback;
	var init = options.init;
	var dialogWasteHeight = 32;
	
	html += '<div id="Dialog">';
	html += 	'<div class="DialogHeader"><h2>' + options.title + '</h2><a rel="CloseDialog">X</a></div>';
	
	switch(options.type)
	{
		case "iframe":
			html += '<iframe id="DialogIframe" src="' + options.url + '" frameborder="0"></iframe>';
			break;
		
		case "newsletter":
			html += '<div class="NewsLetterSubscriptionWrapper DialogBackgroundWrapper">';
			html += 	'<div class="NewsLetterSubscription">';
			html += 		'<form method="post" action="http://ymlp.com/subscribe.php?YMLPID=guquuuhgmgb" target="_blank">';
			html += 			'<p>Vul hier uw gegevens in om onze nieuwsbrief te ontvangen.</p>';
			html += 			'<label>Naam:<input type="text" name="YMP1" class="text" style="width: 175px; float: right;"></label>';
			html += 			'<label>E-mail:<input type="text" name="YMP0" class="text" style="width: 175px; float: right;"></label>';
			html += 			'<a id="SubmitNewsLetter" class="Button Red">Verzenden&nbsp;&gt;&gt;</a>';
			html += 			'<a rel="CloseDialog" class="Button Red">Annuleren</a>';
			html += 		'</form>';
			html += 	'</div>';
			html += '</div>';
			break;
			
		case "GoogleCSE":
			html += '<div id="GoogleCSEResults" class="DialogBackgroundWrapper"></div>';
			break;
	}
	
	html += '</div>';
	html += '<div id="DialogBackground"></div>';
	
	$(document.body).append($(html));
	
	$("#Dialog").css({
		width: width + "px",
		height: height + "px",
		marginTop: ((height/2) *-1) + "px",
		marginLeft: ((width/2) *-1) + "px"
	});
	
	$("#DialogIframe, #GoogleCSEResults").css({
		height: (height - dialogWasteHeight) + "px"
	});
	
	if(typeof(init) == "function")
	{
		init();
	}
	
	$("#DialogBackground").fadeIn(100, function(){
		$("#Dialog").show();
	});
	
	$("#DialogBackground, a[rel='CloseDialog']").click(function(e){
		e.preventDefault();
		
		closeDialog(callback);
	});
	
	$("#SubmitNewsLetter").click(function(e){
		e.preventDefault();
		
		var frm = $(this).closest("form");
		$(frm).submit();
		
		closeDialog(callback);
	});
}

// Dialog verwijderen
var closeDialog = function(callback)
{
	if(typeof(callback) == "function")
	{
		callback();
	}

	$("#Dialog, #DialogBackground").fadeOut(100, function(){
		$("#Dialog, #DialogBackground").remove();
	});
}

