//Call function when the dom is ready
$(function() {
	
	var timer;
	var childheight;
	
	//set the active menu item
	 setupMenu();
	
	//Insert nav BG
	$('#MAINnav').append('<div id="MAINnav-bg"></div>');
	
	//Insert submenu html (tmp)
	$('#gn-3').append('<ul><li><a href="/over-ons/contact/">Contact</a></li></ul>');
	$('#gn-2').append('<ul><li><b>Account</b></li><li><a href="/myhalo/login/?height=325&amp;width=500" class="thickbox" title="MyHALO paneel">Inloggen</a></li><li><a href="/myhalo/registreren/" title="">Registreren</a></li><li><a href="/myhalo/lostpw/">Wachtwoord?</a></li></ul>');
	$('#gn-1').append('<ul><li><b>Opslag<br/></b></li><li><a href="/diensten/webhosting/">Webhosting</a></li><li><b>Overig</b></li><li><a href="/diensten/domeinen/">Domeinen</a></li></ul>');
	
	$('#MAINnav li[id^="gn-"]').hover(
		function () {
			
		clearTimeout(timer);
		
		$($('#MAINnav li')).removeAttr('class'); // remove class
		
		$('#MAINnav li ul').hide();
		childheight = $(this).children('ul').height();
			
   		if(childheight > 0){
			$(this).addClass('hover'); // set active state to current item
			$("#MAINnav-bg").animate({"height": (childheight+10)+"px"}, {queue:false, duration:200}).css('border-bottom', '2px solid #33d9f7');
			$(this).children('ul').fadeIn('medium');
		} else{
			$(this).addClass('hover'); 	
			$("#MAINnav-bg").animate({"height": "0px"}, {queue:false, duration:200}).css('border', 'none');
			$('#MAINnav li ul').hide();
		}
		
      }, 
      function () {
		 clearTimeout(timer);
		 if(childheight > 0){
			timer = setTimeout ( "hideMenu()", 800 );
		 } else {
			timer = setTimeout ( "hideMenu()", 200 );
		}
      }
	  
	);

});

function hideMenu() {
	$("#MAINnav-bg").animate({"height": "0px"}, {queue:false, duration:200}).css('border', 'none');	
	$('#MAINnav li ul').hide();
	setupMenu();
}

function setupMenu(){
	
	if ( $('body.home').length > 0 ){ $('#gn-0').addClass('active')} else{ $('#gn-0').removeAttr('class')};
	if ( $('body.programmas').length > 0 ){ $('#gn-1').addClass('active')} else {$('#gn-1').removeAttr('class')};
	if ( $('body.films').length > 0 ){ $('#gn-2').addClass('active')} else {$('#gn-2').removeAttr('class')};
	if ( $('body.muziek').length > 0 || $('body.shop_native').length > 0 ){ $('#gn-3').addClass('active')} else {$('#gn-3').removeAttr('class')};
	if ( $('body.epg').length > 0 ){ $('#gn-4').addClass('active')} else {$('#gn-4').removeAttr('class')};
	if ( $('body.ug').length > 0 ){ $('#gn-5').addClass('active')} else {$('#gn-5').removeAttr('class')};
}

