/**
 * Functie om hele blokken klikbaar te maken, en hover toevoegen 
**/
$.fn.hoverClick = function()
{
	this.each(function()
	{
		if($("a:first", this).length)
		{
			$(this).hover(
				function() { $(this).addClass("hover").css("cursor", "pointer"); },
				function() { $(this).removeClass("hover").css("cursor", "pointer"); }
			);
			
			$(this).attr("title", $("a:first", this).attr("title"));
			
			$(this).click(function(){
				window.location = $("a:first", this).attr("href");
			});
		}
	});
	
	return this;
};

/**
 * inputDefaultText 0.1
 * 
 * file: jquery.inputdefaulttext.js 	
 *
 * Add default text to input elements and automatically delete them on focus
 *
 * @usage		$("#input_element").inputDefaultText({ default_txt:"My Default Text" });
 * @copyright 	A&M ImpacT 2009
 * @author		DS
**/	
(function($){ 
$.fn.inputDefaultText = function( options )
{	
	var defaults = {
		default_txt: ""
	};
	
	var options = $.extend(defaults, options);

	// set default text
	if ( $(this).val() == "" )
		$(this).val( options.default_txt );

	// add behaviour at focus and blur
	$(this).focus( function() 
	{ 
		if ( $(this).val() == "" || $(this).val() == options.default_txt )
			$(this).val(""); 
	});
	
	$(this).blur( function() 
	{ 
		if ( $(this).val() == "" )
			$(this).val( options.default_txt );
	});
};
})(jQuery);

/**
 * Functie om een stuk tekst in te korten, inclusief een lees meer link 
**/
$.fn.truncate = function( max_karakters )
{
	$(this).each(function()
	{
		$(this).data("orginele_tekst", $(this).text());

		if( $(this).text().length > max_karakters )
		{
			$(this).text( $(this).data("orginele_tekst").substring(0, max_karakters) );
			$(this).append('... <a class="leesmeer" href="#" title="'+ lang.truncate +'">&gt;&gt;&gt;</a>');
			
			$("a.leesmeer", this).click(function() { 
				$(this).parent().text( $(this).parent().data("orginele_tekst") );
				return false;
			});
		}
	});
	
	return this;
};

/**
 * Fotoboekje op detailpagina
**/
var huidige_foto = null;
function fotoBoekje()
{
	// Bij klik op foto deze foto in vergroting zetten
	var imgLoader = null;
			
	//$("#fotoboek .fotos a").click(function() { return false; });
	$("#productdetail .fotos-pdfs .fotos li a").hover(function()
	{
		if($(this).parent().hasClass("video"))
			return false;
		
		link = this;
		
		if(huidige_foto != $(link).attr("rel"))
		{
			$('#productdetail .fotos-pdfs .fotos .groot a').fadeOut(200, function()
			{
				imgLoader = new Image();  
				imgLoader.onload = function()	
				{
					$('#productdetail .fotos-pdfs .fotos .groot img').attr({ src: imgLoader.src, alt: $(link).attr("title") });
					$('#productdetail .fotos-pdfs .fotos .groot a').attr({ href: $(link).attr("href"), title: $(link).attr("title") }).fadeIn(200);
				}
				imgLoader.src = $(link).attr("rel");
				huidige_foto = $(link).attr("rel")
			});
		}
	});	
}

$(function()
{
	
	$(".inleiding").truncate(550);
	$("#productdetail .fotos-pdfs .pdfs li").hoverClick();
	$("ol#nieuwsoverzicht li").hoverClick();
	$("#zoekoverzicht li").hoverClick();
	$("#frm-zoeken input[type=text]").inputDefaultText({ default_txt: $("#frm-zoeken legend").text() });
	
	/**
	 * Menu
	**/
	var menu = {};

	menu.laatstGeopend = null;
	menu.timeoutTime = 2000;
	menu.timeout = null;
	
	menu.init = function()
	{		
		
		$("#menu>li").hover(
			function() 
			{ 		
			
				// als laatstegeopend dezelfde is als de huidige, dan de timeout verwijderen
				if($(this).hasClass('hover'))
				{
					clearTimeout(menu.timeout);
				}
				// nieuwe uitschuiven
				else
				{
					$("#menu>li>ul").hide();
					$("#menu>li").removeClass('hover');
					
					$(this).addClass('hover');
					$(">ul", this).slideDown(300);									
				}
			},
			function() 
			{ 
				// timeout zetten om na x aantal sec te sluiten
				menu.laatstGeopend = this;
				menu.timeout = setTimeout(function()
				{
					$(menu.laatstGeopend).removeClass('hover'); 
				}, menu.timeoutTime);
			}
		);
	}

	menu.init();
	
	/**
	 * Producten stappen sub 
	**/
	// Wat styles meegeven
	$("#producten .stappen li div .sluiten").css("visibility", "visible");
	$("#producten .stappen li div").css(
	{
		'display'	: 'none',
		'position'	: 'absolute'
	});
	// Hover van menuitem
	$("#producten .stappen>li>p").hover(
		function() { $(this).addClass("hover"); $(this).attr("title", lang.selecteer_categorie); },
		function() { $(this).removeClass("hover"); });
	
	// Klikken op menuitem
	$("#producten .stappen>li").click( function( e ) 
	{	
		// Als sub al open is, dan sub sluiten 
		if( $("p:first", this).hasClass("actief") )
		{
			$("p.actief", this).removeClass("actief");
			$("div", this).hide(); 
		} 
		// Als sub nog niet open is, dan sub openen
		else
		{
			// Alle subs die open zijn, sluiten
			$("#producten .stappen>li>div").hide();
			$("#producten .stappen>li p.actief").removeClass("actief");
			
			// Sub onder aangeklikte link openen
			$("p:first", this).addClass("actief");
			$("div", this).show();
		}
		
		// Buiten sub klikken moet sub ook sluiten
		e.stopPropagation();
		$("body").one("click", function()	{
			$("#producten .stappen>li>div").hide();
			$("#producten .stappen>li p.actief").removeClass("actief");
		});
	});
	// Klik event van parent niet meenemen naar deze div
	$("#producten .stappen>li>div").click( function(e) { e.stopPropagation(); });
	// Sluiten submenu dmv kruisje
	$("#producten .stappen>li>div>a.sluiten").click( function() 
	{ 
		$(this).parent().parent().children("p").removeClass("actief");
		$(this).parent().hide();
		return false;
	});	
	// Met escape sluiten
	document.onkeydown = function(e){ 	
		// ie
		if (e == null) { keycode = event.keyCode; } 
		// mozilla
		else { keycode = e.which; }
		if(keycode == 27)
		{ 
			$("#producten .stappen>li>div").hide();
			$("#producten .stappen>li p.actief").removeClass("actief");
		} 
	};
	
	/**
	 * Productdetail dropdowns onder productaanvullingen 
	**/
	$("#product-aanvullend .blok .inhoud").not(".open").hide();
	$("#product-aanvullend .blok h4").css("cursor", "pointer").attr("title", lang.toggle);
	/*$("#product-aanvullend .blok h4").hover(
		function() { $(this).addClass("hover");	},
		function() { $(this).removeClass("hover"); });*/
	$("#product-aanvullend .blok h4").click( function()
	{
		if( $(this).hasClass("open") )
		{
			$(this).removeClass("open");
			$(this).parent().children(".inhoud").slideUp(200);
		}
		else
		{
			$(this).addClass("open");
			$(this).parent().children(".inhoud").slideDown(200, function()
			{
				// focus zetten op eerste veld, als dit veld leeg is of 0 is
				if( $('input:first', this.parentNode).attr('value') == 0 || $('input:first', this.parentNode).attr('value') == '' )
				{
					$('input:first', this.parentNode).attr('value', '').focus();
				}
			});
		}
	});

	
	/**
	 * Formulier focus op velden
	**/
	$(":input").not("input[type=button], input[type=submit], input[type=radio], input[type=checkbox]").focus(function() { $(this).addClass("veldfocus"); });
	$(":input").not("input[type=button], input[type=submit], input[type=radio], input[type=checkbox]").blur(function() { $(this).removeClass("veldfocus"); });

});