$(function(){
    $('#headerPages').find('li:first').css('border-left', 'none');
	$('#headerPages').find('li:first').find('a').css('padding-left', 0);
    
    // Zoeken
    $('#headSearchBtn').click(function() {
        var value = $('input#searchInput').val();
        if (value !== '') {
	        value = value.replace(/\\/g, '');
            value = value.replace(/\&/g, '%26');
            document.location = '/search/' + value + '/';
        }
    });
	$('input#searchInput').keydown(function(e) {
	    if (e.which === 13) {
	        e.preventDefault();
	        $('#headSearchBtn').trigger('click');
	    }
	});
	
	setInterval(ombouw.header.changeTelephoneNumber, 3000);
	// Navigation
	ombouw.navigation.init();
	
	// Go to product detail
	$(".product").click(function() {
	    if ($(this).find("a").length!=0) {
	        if($(this).find("a").attr("class").indexOf('toonInPopup') == -1) {
	            document.location = $(this).find("a").first().attr("href");
	        }
        }
    });
    
    //Pas voor toonInPopup de href aan om in empty ombouw te laden
    $(".toonInPopup").each(function () {
        $(this).attr("href",$(this).attr("href") + "?cm=true");
    });
    
    //Trigger de fancybox
    $(".toonInPopup").fancybox({
        'autoDimensions': true,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'width': 800,
        'height': 600
    });
    
    // Dropdowns
    $(".dropHolder").parent().hover(function(){
        clearTimeout($("body").data("droptimer"));
        $(".tmpdrop").remove();
        var drop = $(this).find(".drop").clone().appendTo("body").addClass("tmpdrop");
        var offset = $(this).offset();
        drop.css({
            top: offset.top + $(this).height(),
            left: offset.left
        });
    },
    function(){
        $("body").data("droptimer", setTimeout(function(){
            $(".tmpdrop").remove();
        }, 500));
        
        $(".tmpdrop").unbind("mouseenter").mouseenter(function(){
            clearTimeout($("body").data("droptimer"));
        })
        .mouseleave(function(){
            $("body").data("droptimer", setTimeout(function(){
                $(".tmpdrop").remove();
            }, 500));
        });
    });
});

var ombouw = {
    navigation: {
        init: function() {
            //$("#mainMenu > ul > li").mouseenter(ombouw.navigation.mouseenter).mouseleave(ombouw.navigation.mouseleave);
            $("#mainMenu > ul > li").bind({
                mouseenter: ombouw.navigation.mouseenter,
                mouseleave: ombouw.navigation.mouseleave
            });
            
            $(".menuDrop").each(function(){
                $(this).find(".menuDropInner").children("ul").eq(0).addClass("dropMerk").end().eq(1).addClass("dropType");
            });
            
            $(".menuDrop .dropType a").each(function(){
                if($(this).attr("data-typeid") != "")
                {
                    $(this).prepend("<span><img src='" + $(this).attr("data-imgurl") + "' alt='' title='' /></span>");
                }
                else
                {
                    $(this).prepend("<span><img src='/dbimages/noimage_navigatietype.jpg' alt='' title='' /></span>");
                    
                }
            });
            
            $(".catName").each(function() {
                if ($(this).text() == "merken") {
                    $(this).closest("ul").addClass("menuDropLeft");
                }
            });
        },
        mouseenter: function() {
            $(".menuDrop").slideUp();
            if ($(this).find(".menuDrop:animated").length == 0) {
                //$(this).find(".menuDrop").stop(false, true).slideDown();
                $(this).find(".menuDrop").show();
                
                var maxHeight = 0;
                $(this).find(".menuDropInner").children("ul").each(function(){
                    if( $(this).height() > maxHeight )
                    {
                        maxHeight = $(this).height();
                    }
                }).height(maxHeight);
                
               // ombouw.navigation.positionNavigation.call($(this));
            }
        },
        mouseleave: function() {
            $(this).find(".menuDrop").hide();
        }
        /*,
        positionNavigation : function(){
            $(this).find(".menuDrop").each(function() {
                if($(this).hasClass("menuDropRightAlign") || $(this).hasClass("menuDropLeftAlign"))
                    return false;
                    
                var maxRight = parseInt($("#wrapper").offset().left) + parseInt($("#wrapper").width());

                var right = parseInt($(this).offset().left) + parseInt($(this).width());
                
                if (right > maxRight) {
                    $(this).addClass("menuDropRightAlign");
                }
                else {
                    $(this).addClass("menuDropLeftAlign");
                }
            })
        }*/
    },
    header : {
        changeTelephoneNumber : function()
        {
            $el = $("#headTel")
            if(!$el.data("original"))
            {
                $el.data("original", $el.text())
            }
            if($el.text() == $el.data("original"))
            {
                $el.find("span").html($el.data("real"))
            }
            else
            {
                $el.find("span").html($el.data("original"))
            }
        }
    }
};

function submitForm(url) {
    url = typeof url === 'string' ? url : document.location;
    $('form#aspnetForm').attr('action', url).submit();
}

function updateShoppingCart() {
    $.ajax({
        url: '/winkelmandje.aspx/GetShoppingCartInfo',
        type: 'POST',
        data: '{}',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function(response) {
            var data = (typeof response.d) === 'string' ? eval('(' + response.d + ')') : response.d;

            $('span#headerWMAmount').html(data.amount);
            $('span#headerWMPrice').html(data.totalPrice);
        }
    });
}

