jQuery(function($) {
    initClickButton();
    
    // Script to add the '/'
    jQuery("#NavigationHorizontal >li").not(':first').each(function() {
        jQuery(this).html('/' + jQuery(this).html());
    });
    
    // Hover for Main Menu
    jQuery("#NavigationHorizontal li")
    .hover(function() {
        jQuery(">ul", jQuery(this))
        .addClass('active')
        .slideDown(function() {
            jQuery("#NavigationHorizontal ul")
            .not('.active')
            .hide();
        });
    }, function() {
        jQuery(">ul", jQuery(this))
        .removeClass('active')
        .hide();
    });

    jQuery("#NavigationHorizontal")
    .hover(function() {
        // Do nothing
    },
    function() {
        jQuery("#NavigationHorizontal ul")
        .hide()
        .removeClass('active');
    });
    
    jQuery("#MainContentMenu div.kundepanel").hover(function() {
        jQuery("#MainContentMenu div.kundepanel.active").removeClass('active');
        jQuery(this).addClass('active');
        jQuery("div.subarrow", jQuery(this)).show();
        jQuery("div.sub", jQuery(this)).slideDown();
    }, function() {
        jQuery(this).removeClass('active');
        jQuery("div.subarrow", jQuery(this)).hide();
        jQuery("div.sub", jQuery(this)).hide();
    });
    
    jQuery("#lowerFooter").height(jQuery("#lowerFooter").innerHeight()+15);
    
    jQuery("#MenuTabs").SimpleTabs({
        onBeforeChange: function(t, d) {
            if (d == '#categorylist-box-content-cat') {
                jQuery("#BgMenuTab").removeClass('left').addClass('right');
            } else if (d == '#categorylist-box-content-brands') {
                jQuery("#BgMenuTab").removeClass('right').addClass('left');
            }
        }
    });
});

var heightForOne = 0;
var initialHeight = 0;
var newHeight = 0;
var origText = 'Se alle <strong>kategorier</strong> <img style="position:relative; top:-2px;" src="/upload_dir/themes/web/Unik/graphics/arrowcat.png" alt="Arrow" />';
var newText = 'Vis kun 6 <strong>kategorier</strong> <img style="position:relative; top:-2px;" src="/upload_dir/themes/web/Unik/graphics/arrow.jpg" alt="Arrow" />';
function initClickButton() {
    jQuery('#ViewAll')
    .click(function(e) {
        var btnEl = jQuery("#ViewAllBtn");
        var el = jQuery("#categories-listing");
        if (el.hasClass('expanded')) {
            // Is expanded
            el.animate({
                height: initialHeight
            }, 500, function() {
                btnEl.html(origText);
                el.removeClass('expanded');
            })
        } else {
            // Is not expanded
            var items = jQuery(">div.categories-listing-wrapper", el);
            initialHeight = el.height();
            heightForOne = jQuery(items[0]).height() + (parseInt(el.css('padding-bottom')));
            newHeight = (Math.ceil(items.length / 3) * heightForOne) + 28;
            el.animate({
                height: newHeight
            }, 500, function() {
                btnEl.html(newText);
                el.addClass('expanded');
            });
        }
    });
}

function scrollToBrands() {
    jQuery("#brandsbottom").show();
    jQuery("#lowerFooter")
        .data('origHeight', jQuery("#lowerFooter").height());
    jQuery("#lowerFooter")
        .height((jQuery(window).height()-jQuery("#Footer").innerHeight()));
    jQuery
        .scrollTo('#Footer', 1000);
}

function scrollToTop() {
    jQuery("#brandsbottom").hide();
    var origHeight = jQuery("#lowerFooter").data('origHeight');
    jQuery
        .scrollTo('#Logo', 1000, function() {
            jQuery("#lowerFooter")
                .height(origHeight);
        });
}
