

$(document).ready(function() {
    // initialize country locator menu
    initMenu();
});


var CSMenuFadeInTime = 250;
var CSMenuFadeOutTime = 150;

function initMenu() {
    
    
    //browser hacks
    if (isExplorer) {
        $("ul.navMain li ul").css("margin-top", "-6px");
        $("ul.navMain li .menuarrow").css("background-position", "0 3px");
    }
    // end browser hacks
    
    
    var shopImg = '/images/pdds-countrylocator/icon_onlineshop.gif';
    var shopImgHover = '/images/pdds-countrylocator/icon_onlineshop_hover.gif';

    var catImg = '/images/pdds-countrylocator/icon_catalog.gif';
    var catImgHover = '/images/pdds-countrylocator/icon_catalog_hover.gif';

    $("ul.navMain ul").css({ display: "none" }); // Opera Fix

    $(".navWrapper").hover(
        function() {
            // IN
            //**    Reset all <li>'s    **//
            $(this).find("li").fadeIn(CSMenuFadeInTime);
            $(this).find("li[class^='title']").addClass("highlighted");
        },

        function() {
            // OUT
            $(this).find("li[class^='regular']").fadeOut(CSMenuFadeOutTime);
            $(this).find("li")
            .removeClass("highlighted")
            .each(function() {
                var cname = this.className;
                if (cname.indexOf('onlineshop') != -1) {
                    $(this).css('background-image', 'url(' + shopImg + ')');
                }
                else if (cname.indexOf('catalog') != -1) {
                    $(this).css('background-image', 'url(' + catImg + ')');
                }
            }); // each
            $('#notice p').removeClass('hilite');
        });

        /* set mouse cursor on hovering and click action 
        (also on list item, not only hyperlink)*/
        $(".navMain li")
        .hover(function() { $(this).css("cursor", "pointer"); }, function() { $(this).css("cursor", "default"); })
        .click(function() {
            var href = $(this).find("a").attr('href');
            if (href != '#') {
                log('change url to: ' + href);
                window.location.href = href;
            }
        })
        ;

    /** normal list element hover action **/
        $("li.regular").hover(
    function() {
        // highlight self
        $(this).addClass("highlighted");

        // set highlighted background image
        var cname = this.className;
        if (cname.indexOf('onlineshop') != -1) {
            log('onlineshop');
            $(this).css('background-image', 'url(' + shopImgHover + ')');
            $('#notice p.catalog').removeClass('hilite');
            $('#notice p.onlineshop').addClass('hilite');
        }
        else if (cname.indexOf('catalog') != -1) {
            log('catalog');
            $(this).css('background-image', 'url(' + catImgHover + ')');
            $('#notice p.onlineshop').removeClass('hilite');
            $('#notice p.catalog').addClass('hilite');
        }
        $(this).find(".menuarrow").css("background-image", "url(/images/arrow-red.gif)");

        // turn off all others
        // 1. De-highlight
        // 2. Set background images to normal
        $(this).siblings(".regular").removeClass("highlighted");
        $(this).siblings(".onlineshop").css('background-image', 'url(' + shopImg + ')'); ;
        $(this).siblings(".catalog").css('background-image', 'url(' + catImg + ')'); ;
        $(this).siblings().find(".menuarrow").css("background-image", "url(/images/arrow-white.gif)");

        // close any other sub menus
        $(this)
            .siblings().find('ul:first')
            .fadeOut(CSMenuFadeOutTime);
        //.css({ visibility:'hidden'});
        // display own sub menu, if there is one.
        $(this).find('ul:first')
            .css({ visibility: "visible" })
            .fadeIn(CSMenuFadeInTime);
    },
    function() {
    });

    // On title item, reset all other list items.
    $("li.title").mouseover(
    function() {
        //$(this).css("background-image", "url(/images/arrow-red-down.gif)");
        $(this).addClass('highlighted');
        $(this).siblings().removeClass("highlighted");
        $(this).siblings(".onlineshop").css('background-image', 'url(' + shopImg + ')'); ;
        $(this).siblings(".catalog").css('background-image', 'url(' + catImg + ')'); ;
        $(this).siblings().find(".menuarrow").css("background-image", "url(/images/arrow-white.gif)");
        $(this).siblings().find("ul:first")
        .fadeOut(CSMenuFadeOutTime);
    });

}

