﻿$(document).ready(function() {

    var last_levelcontainer;
    var last_a = null;
    var h = 0;

    if ($('#submenus a.has-children.active').length) {
        $('#submenus a.has-children.active').each(function() {
            var a = $(this);
            var levelcontainer = a.closest('span.levelcontainer');

            levelcontainer.show(); // show self container
            levelcontainer.children('ul').children('li').hide(); // hide all list items by default
            a.parent('li').show(); // redisplay self parent list item
            a.next().css({ 'position': 'absolute', 'top': '0', 'left': a.parent().width(), 'width': '1000px' }).show();
            a.next().children('ul').children('li').show();

        });

        $('#submenus').css({ 'height': 'auto' });
        $('#submenus').find('.levelcontainer:visible').each(function() {
            $(this).css({ 'height': 'auto' });
            if ($(this).height() > h) h = $(this).height();
        });
        if (h > $('#submenus').height()) {
            $('#submenus').height(h);
            $('#submenus').find('.levelcontainer:visible').height(h);
        }
    };

    $('#submenus a.has-children').click(function(event) {
        var a = $(this);
        var levelcontainer = a.closest('span.levelcontainer');
        var he = 0;

        a.addClass('active'); // set active state on self
        levelcontainer.children('ul').children('li').hide(); // hide all list items by default
        a.parent('li').show(); // redisplay self parent list item
        a.next().css({ 'position': 'absolute', 'top': '0', 'left': a.parent().width(), 'width': '1000px' }).show();
        a.next().children('ul').children('li').show();
        a.next().find('.active').removeClass('active');
        a.next().find('.levelcontainer').hide();
        $('#submenus').css({ 'height': 'auto' });
        $('#submenus').find('.levelcontainer:visible').each(function() {
            $(this).css({ 'height': 'auto' });
            if ($(this).height() > he) he = $(this).height();
        });
        if (he > $('#submenus').height()) {
            $('#submenus').height(he);
            $('#submenus').find('.levelcontainer:not(.level-0)').css({ 'height': he });
        }

        event.preventDefault();
    });

    // LOGIN 
    $('#simplelogin #loginheader').click(function(event) { $(this).toggle().siblings('#logincontrols').toggle() });
    $("#simplelogin label").inFieldLabels({ fadeOpacity: 0.1 });

    // /Common/Script/fsbase.js

    // Handle click anywhere on page (close dropdowns etc.)
    $(this).click(function(event) {
        hideDropDowns();
    });

    function hideDropDowns() {
        $('#support ul:visible').hide(); // support dropdown
        $('#countryselect ul:visible').hide(); // country dropdown
        $('#divisionmenu .quickselect .dropdowncontainer:visible').hide(); // quickselect dropdown
        $('.filtermanufacturer .dropdowncontainer:visible').removeClass('active'); // filter manufacturer dropdown
        $('.filtermanufacturer a.select').removeClass('active'); // filter manufacturer dropdown
        $('#top-menu-support-list').hide();
    };

    // Custom dropdowns
    $('.custom.dropdowncontainer').prepend(function() {
        return '<span class="dropdownsub"><span><span class="dropdowninner">' +
            $(this).children('select.dropdown').css({ 'opacity': '0' }).change(function() {
                $(this).prev('.dropdownsub').find('.dropdowninner').html($(this).children('option:selected').text());
            }).children('option:selected').text() + '</span></span></span>';
    });
    
    // PRODUCTLIST
    $("a#selectedbrand").click(function(event) {
        event.preventDefault();
        if ($("div.brandscontainer:visible").length != 0)
            $("div.brandscontainer").hide();
        else
            $("div.brandscontainer").slideDown("fast");
    });

    // NEWSLETTER
    //$("#foot .newslettersignup label").inFieldLabels({ fadeOpacity: 0.1 });

    // COUNTRY SELECT
    $('#countryselect h4').click(function(event) {
        //hideDropDowns();
        $('#support ul:visible').hide(); // support dropdown    
        $(this).siblings('ul').show();
        event.stopPropagation();
    });

    // SUPPORT (dropdown)
    $('#support h4').click(function(event) {
        //hideDropDowns();
        $('#countryselect ul:visible').hide(); // country dropdown
        $(this).siblings('ul').show();
        event.stopPropagation();
    });

    // QUICKSELECT (dropdown)
    $('#divisionmenu .quickselect h4').click(function(event) {
        //hideDropDowns();
        $(this).siblings('.dropdowncontainer').toggle();
        event.stopPropagation();
    });

    // FILTER MANUFACTURER (dropdown)
    $('.filtermanufacturer .select').click(function(event) {

        $(this).siblings('.dropdowncontainer').toggleClass('active');
        $(this).toggleClass('active');

        event.preventDefault();
        event.stopPropagation();
    });

    // SHOW/HIDE CONTAINERS
    $(".showhidecontrol:hidden").show(); // show "Show/hide"-controller
    $(".showhide:visible").hide();
    $(".showhidecontrol a.hide:visible").hide();

    // reopen default show
    $(".showhidecontainer.show .showhide:hidden").show();
    $(".showhidecontainer.show .showhidewrapper a.hide:hidden").show();
    $(".showhidecontainer.show .showhidewrapper a.show:visible").hide();

    $("#orderdataextended span.submit").hide(); // hide "Search" button in "My orders"
    $(".showhidecontrol a").click(function(event) {
        var href = $(this).attr("href");
        if ($(this).attr("class") == "show") {
            $(href).slideDown("fast"); // show hidden container
            $("a.hide[href='" + href + "']").show(); // show hide control
        } else {
            $(href).slideUp("fast"); // hide visible container
            $("a.show[href='" + href + "']").show(); // show show control
        }
        $(this).hide(); // hide yourself
        event.preventDefault();
    });

});
