/******************/
/* Menu functions */
/******************/
window.Menu = {
	delay		: 600,
	timer		: null,
	menuitem	: null,
	
	/**
	 * apply
	 * @param	string	selector
	 */
	apply: function( selector ) {
		$(selector).hover(Menu.open, Menu.setTimer);
		$(document).click(Menu.close);
	},
	
	/** 
	 * cancelTimer
	 */
	cancelTimer: function() {
		if(Menu.timer)	{
			clearTimeout(Menu.timer);
     		Menu.timer = null;
		}
	},
	
	/**
	 * setTimer
	 */
	setTimer: function() {
		Menu.timer = window.setTimeout(Menu.close, Menu.delay);
	},
		
	/** 
	 * close
	 * @param	string	current_menu_id
	 */
	close: function( current_menu_id ) {
		if(Menu.menuitem)	{
			if(Menu.menuitem.data("menuID") != current_menu_id)	
			{
				$("div", Menu.menuitem).css({ height: 'auto', zIndex: "" }).stop().hide();
				$(Menu.menuitem).removeClass("hover");
			}
		}
	},
			
	/** 
	 * open
	 */
	open: function() {
		current_menu = $(this);
		
		current_menu.addClass("hover");
		
		// uniek menu id per submenu, dit om bij het sluiten te checken of niet de actieve wordt gesloten
		if(!current_menu.data("menuID"))	{
			current_menu.data("menuID", (Math.random() +''+ Math.random()).replace(/\./g,""))
		}
		
		Menu.cancelTimer();
		Menu.close( current_menu.data("menuID") );
		Menu.menuitem = current_menu;
		
		$("div", Menu.menuitem).css({ height: 'auto', zIndex: 100 }).stop().show();	
	}

};

/*****************************************************************/
/* 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;
};

/*******************************/
/* Animate background position */
/*******************************/
(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);


/**********/
/* Easing */
/**********/
jQuery.extend( jQuery.easing,
{
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	}
});


/***************/
/* Nieuwsbrief */
/***************/
function toonExtra(extra)
{
	if(extra == 'j')
	{
		$('#extra_variabelen').show();
	}
	else
	{
		$('#extra_variabelen').hide();
	}
}

function controleerNieuwsbrief(formulier)
{
	var melding = "";

	if( $("#nb_email").val() == "")
	{
		$("#nb_email").addClass("fout");
		if(melding == "") $("#nb_email").focus();
		melding = "geef melding";
	}
	else
	{
		if (formulier.email.value.search(/^[^@]+@[^@]+.[a-z]{2,}$/i) == -1) 		
		{
			$("#nb_email").addClass("fout");
			if(melding == "") $("#nb_email").focus();
			melding = "geef melding";
		}
		else
		{
			$("#nb_email").removeClass("fout");
		}
	}
	
	if ( $("#nieuwsbriefformulier input#afmelden:checked").length == 0 )
	{		
		if ($("#nb_voornaam").val() == "") 		
		{
			$("#nb_voornaam").addClass("fout");
			if(melding == "") $("#nb_voornaam").focus();
			melding = "geef melding";
		}
		else
		{
			$("#nb_voornaam").removeClass("fout");
		}
		
		if ($("#nb_achternaam").val() == "") 		
		{
			$("#nb_achternaam").addClass("fout");
			
			if(melding == "") $("#nb_achternaam").focus();
			melding = "geef melding";
		}
		else
		{
			$("#nb_achternaam").removeClass("fout");
		}	
	}

	if (melding !== "")
	{
	   $("#nb-melding").slideDown(300);
	   return false;
	}
}


/***********************/
/* Submenu pijl actief */
/***********************/
function zetSubmenuActief()
{
	if( $("#submenu li.actief").length > 0 )
	{
		$("#submenu").append('<div class="actiefpijl"></div>');
		
		var actiefHoogte = $("#submenu li.actief").offset();
		var submenuHoogte = $("#submenu").offset();
		
		$("#submenu .actiefpijl").css("top", (actiefHoogte.top - submenuHoogte.top - 25) + "px");
	}
}

/*********/
/* isint */
/*********/
function isInt(input){
	return !isNaN(input)&&parseInt(input)==input;
}
