$(function(){
	
	
	// Hoofd en submenu
	$('div#menu ul li.main').bind('mouseenter', function(){		
		$(this).find('div.submenu').stop();
		$(this).find('div.submenu').show();
		$(this).find('div.submenu').animate(
				{				
                	height: "252px"
                }
            ,	150
            ,	"linear"
		);
	});
	
	$('div#menu ul li.main').bind('mouseleave', function(){
		
		$(this).find('div.submenu').stop();		
		$(this).find('div.submenu').animate(
				{				
                	height: "0"
                }
            ,	150
            ,	"linear"
            ,	function(){
            		$(this).hide();
            	}
		);
		
	});
	
	
	// Zoek logica
	var sDefaultValue = "Zoeken";	
	$("input.search_input").bind("focus", function(){		
		if(sDefaultValue == $(this).val())
		{
			$(this).val("");
		}
	});
	
	$("input.search_input").bind("blur", function(){
		setTimeout(function()
		{						
			$("div#header_search_results").hide();
			if("" == $(this).val())
			{
				$(this).val(sDefaultValue);
			}
		}, 500);
		
		if("" == $(this).val())
		{
			$(this).val(sDefaultValue);
		}
	});
	
	
	$("input.search_input").bind("keyup", function(){
		var sSearchTerm = $(this).val();
		if($("div#header_search_results").css("display") == "none")
		{
			$("div#header_search_results").show();
			$("div#header_search_results").append("<p class=\"defaulttext\">Loading data...</p>");
		}																							
		
		$.ajax({
				url: "/search/searches/livesearchResults/"+sSearchTerm				
			,	cache: false
			,	async: true
			,	success: function(data){
					$("div#header_search_results").html(data);
				}
		});
	});
	
	$("div#header_search_results div").mouseover(function(){
		$(this).css("background-color", "#f3f3f3");
	});
	$("div#header_search_results div").mouseout(function(){
		$(this).css("background-color", "#FFFFFF");
	});
	
	// Verschuifbaar submenu voor de referenties
	if('referentie-categorie' == sPageSlug)
		submenuPosition = 118;
	else
		submenuPosition = 0;
	
	$(window).scroll(function (){
		var scrollHeight = $(document).scrollTop();			
		if(scrollHeight > 140)
		{
			$('ul#reference-categories').addClass('moveable-menu');				
			$('ul#reference-categories').animate({top:(scrollHeight-140)},{duration:400, queue:false});
		}
		if(scrollHeight < 140)
		{				
			$('ul#reference-categories').animate({top:(submenuPosition)},{duration:400, queue:false}, function(){
				$('ul#reference-categories').removeClass('moveable-menu');
			});
		}
	});
	
	$('ul#reference-categories ul li a').bind('click', function(){
		var sClassname = $(this).parent().attr('class');
		$('div#references-wrapper div.reference-item').hide();
		$('div#references-wrapper div.'+sClassname).show();
		$('ul#reference-categories ul li a').removeClass('active');
		$(this).addClass('active');
	});
	
    // Home diensten
    
    var iAmountItems = 6;
    iRandInt = Math.floor(Math.random()*iAmountItems);
    var sClassname = $("div.diensten ul li").slice(iRandInt).attr('class');
    $("div.diensten ul li."+sClassname+" a").addClass('active');
    $('div.diensten-content div.'+sClassname).addClass('active');
        
    
    $('div.diensten ul li a').bind('click', function(){
    	var sClassName = $(this).parent().attr('class');
    	$('div.diensten ul li a').removeClass('active');
    	$(this).addClass('active');
    	
    	$('div.diensten-content div.alinea').removeClass('active');
    	$('div.diensten-content div.'+sClassName).addClass('active');
    	
    	return false;
    });
    
    
    // Input velden (Leeg maken en vullen)
    
    var sDefaultNewsLetterInputValue = "E-mailadres";
    $('input#register-nieuwsletterEmail').bind("focus", function(){    	
    	var sCurrentValue = $(this).val();
    	if(sCurrentValue == sDefaultNewsLetterInputValue)
    	{
    		$(this).val("");
    		$(this).css('font-style', 'normal').css('color', '#666666');
    	}
    }).bind("blur", function(){
    	var sCurrentValue = $(this).val();
    	if("" == sCurrentValue)
    	{
    		$(this).val(sDefaultNewsLetterInputValue);
    		$(this).css('font-style', 'italic').css('color', '#999999');
    	}
    });
    
    $('form.nieuwsbrief input.search_item').bind("focus", function(){
    	var sCurrentValue = $(this).val();
    	if(sCurrentValue == sDefaultNewsLetterInputValue)
    	{
    		$(this).val("");    		
    	}
    }).bind("blur", function(){
    	var sCurrentValue = $(this).val();
    	if("" == sCurrentValue)
    	{
    		$(this).val(sDefaultNewsLetterInputValue);
    	}
    });
    
});
