// stop BackgroundImageCache voor IE				
try	{ document.execCommand("BackgroundImageCache", false, true); } catch(e) { }

/**
 * variabelen met jQuery met een $ beginnen!!!
 */
$(function()
{
	/* submenu effectje en timeout */
	Menu.apply("#menu > li");
	
	/* Hoverclick */
    $("#content .overzicht li").hoverClick();
	$("#nieuwsoverzicht li").hoverClick();
	$(".zoekoverzicht li").hoverClick();
	
	// default value bij formulieren
	$("input:text[title], textarea[title]").each(function() { 
		$(this).defaultvalue( $(this).attr("title") );
	});
	
	// Formulier focus op velden
	$(":input").not("input[type=button], input[type=submit], input[type=radio], input[type=checkbox]")
		.focus(function() 	{ $(this).addClass("veldfocus"); })
		.blur(function() 	{ $(this).removeClass("veldfocus"); });
		
	// Effect productmenu
	$("#productmenu > li:not(.actief)").hover(
	function()
	{
		$("> a", this).stop().css({width: 174, paddingLeft: 0}).animate({ width: 164, paddingLeft: 10}, 300);
		$("> a > span", this).stop().css({width: 144}).animate({ width: 134}, 300);
	},
	function()
	{
		$("> a", this).animate({ width: 174, paddingLeft: 0}, 300);
		$("> a > span", this).animate({ width: 144}, 300);
	});
	
	// Nieuwsbrief formulier
	$("#topmenu li.nb a").click( toonNbForm );

    // nb layer tonen als er iets te melden is :-)
    if ( document.location.hash == "#nieuwsbrief_status" )
    {
        $("#topmenu li.nb a").click();
    }
    
	// Filter bij subcategorie
	$("#filter li:not(.aantal-per-pagina) strong").click( toonFilter );
	$("#aantal-per-pagina strong").click( toonAantalRecordsKeuze );
    $("#sorteren strong").click( toonSorteerKeuze );

    /**
     * Bij drukken op enter terwijl er een zoekresultaat is geselecteerd
     * naar de pagina van dat product springen
     */
    $("#zoekform form").submit( function(e) {
        var selectedIndex = $("#zoekform form .suggesties li.hover").index();

        if ( selectedIndex > -1 && $("#zoekform form .suggesties li.hover a").attr('href') != null && $("#zoekform form .suggesties li.hover a").attr('href') != '' && $("#zoekform form .suggesties li.hover a").attr('href') != 'null' )
        {
            window.location = $("#zoekform form .suggesties li.hover a").attr('href');

            return false;
        }

        return true;
    });

	/**
	 * Bij invullen zoekfunctie overzicht tonen
	**/
    var timeout = null;
	$("#zoekform input[type=text]").keyup(function(e)
	{
        // pijl navigatie
        if ( e.keyCode == 38 /* up */ || e.keyCode == 40 /* down */ )
        {
            var selectedIndex = $("#zoekform form .suggesties li.hover").index();

            if ( selectedIndex == -1 )
            {
                var newIndex = 1;
            }
            else if ( e.keyCode == 38 /* up */ )
            {
                if ( selectedIndex < 1 ) return false;
                
                var newIndex = selectedIndex - 1;
                
                if ($("#zoekform form .suggesties li:eq("+newIndex+")").hasClass("header"))
                {
                	var newIndex = newIndex - 1;
                }
            }
            else if ( e.keyCode == 40 /* down */ )
            {
                if ( selectedIndex >= ( $("#zoekform form .suggesties li").length - 1 ) ) return false;

                var newIndex = selectedIndex + 1;

                if ($("#zoekform form .suggesties li:eq("+newIndex+")").hasClass("header"))
                {
                	var newIndex = newIndex + 1;
                }
            }

            $("#zoekform form .suggesties li").removeClass("hover");

            $("#zoekform form .suggesties li:eq("+newIndex+")").addClass("hover");

            return;
        }        
               
        // zoeken
        if ( $(this).val().length > 2 )
        {
			if (timeout) { clearTimeout(timeout); }
			timeout = setTimeout(function(){ zoeken(); }, 500);
        }
	});
	
	var zoeken = function() {
		if ($("#zoekform input[type=text]").val().length > 2)
		{
			$.get( submap + '/ajax/zoeken?q=' + $.URLEncode($("#zoekform input[type=text]").val()), function( data ) {
				var alGeopend = ($("#zoekform form .suggesties").length > 0);
		        $("#zoekform form .suggesties").remove();
		        $("#zoekform form").append( data );
		        if (alGeopend == false)
		        {
		        	$("#zoekform form .suggesties").stop().slideDown(200);
		        }
		        else
		        {
		        	$("#zoekform form .suggesties").show();	
		        }
	        });
		}
    }; 
	
});

//Cufon.replace('h1, h2, h3, h4, #header q, #headeractie .overlay .subtitel', { fontFamily: 'Helvetica Neue Light' });
