var t;
$(document).ready(function() {
    $("#secMenu").mouseover(function() {
        clearTimeout(t);
        $(this).css({
            'background-position': '0 -697px'
        });
        $("#secMenu .sf-menu").show();
        $("#dealMenu .sf-menu").hide();
    }).mouseout(function() {
        clearTimeout(t);
        t = setTimeout(function() {
            $("#secMenu .sf-menu").hide();
        }, 0);
        $(this).css({
            'background-position': '0 -757px'
        });
    }).click(function() {
        window.location.href = "/home/siteMap";
    });

    $("#dealMenu").mouseover(function() {
        clearTimeout(t);
        $(this).css({
            'background-position': '0 -577px'
        });
        $("#dealMenu .sf-menu").show();
        $("#secMenu .sf-menu").hide();
    }).mouseout(function() {
        clearTimeout(t);
        t = setTimeout(function() {
            $("#dealMenu .sf-menu").hide();
        }, 0);

        $(this).css({
            'background-position': '0 -637px'
        });
    }).click(function() {
        window.location.href = "/category/u/supermediastore-hot-deal-promotion";
    });

    $("ul.sf-menu").superfish({
        animation: {opacity: 'show'},  // slide-down effect without fade-in
        delay:      0,
        dropShadows:   false
    });

    $('.freeGift').cluetip({
        width:'200px',
        showTitle:false,
        positionBy:'bottomTop',
        hoverIntent: {
            sensitivity:  3,
            interval:     50,
            timeout:      0
        },
        leftOffset:5,
        topOffset:15,
        dropShadow: false,
        cluetipClass: 'jtip'
    });

    $(function() {
        $("#search").autocomplete({
            source: "/home/search",
            options: {
                appendTo: "body",
                delay: 300,
                minLength: 1,
                position: {
                    my: "left top",
                    at: "left bottom",
                    collision: "none"
                }
            }
        }).bind(($.browser.opera ? "keypress" : "keydown") + ".autocomplete", function(event) {
            switch (event.keyCode) {
                case 13:
                    $("#searchForm").submit();
                    break;
                default:
                    break;
            }
        });
    });

    var imgTrack = "/web/track?r=" + escape(document.referrer) + "&a="+clientIP+"" + "&p=" + escape(document.location);
    $.get(imgTrack);

    $.getJSON("/home/getMenuAsJson/campaign", function(json) {
        if (!json || json.length == 0) return;
        var secMenu = $('div#dealMenu ul.sf-menu');
        var menuHtml = '';
        for (var i in json) {
            var c = json[i];
            if (!c.url || !c.name) continue;
            menuHtml += "<li><a href=\"/category/u/"+c.url+"\" onClick=\"_gaq.push(['_trackEvent','"+pageType+"', 'Navigation Deals', '"+c.name+"']);\">"+c.name+"</a></li>";
        }
        secMenu.html(menuHtml);
    });

    if (isHomePage == 'true') {
        return;
    }

    $.getJSON("/home/getMenuAsJson/home", function(json) {
        if (!json || json.length == 0) {
            return;
        }
        var secMenu = $('div#secMenu ul.sf-menu');
        var menuHtml = '';
        for (var i in json) {
            var c = json[i];
            if (!c.url || !c.name) {
                continue;
            }

            var subHtml = "<li><a href=\"/category/u/"+c.url+"\" onClick=\"_gaq.push(['_trackEvent','"+pageType+"', 'Navigation Categories', '"+c.name+"']);\">"+c.name+"</a>";
            //var subHtml = "<li><a href=\"/category/u/"+c.url+"\">"+c.name+"</a>";
            var hasSubmenu = c.subCategories && c.subCategories.length > 0;
            if (!hasSubmenu) {
                continue;
            }

            subHtml += "<ul class=\"menuLevel2\">";
            for (var j in c.subCategories) {
                var subC = c.subCategories[j];
                if (!subC.url || !subC.name) {
                    continue;
                }

                if (j == 0) {
                    subHtml += "<li class=\"firstSub\">";
                } else {
                    subHtml += "<li>";
                }
                subHtml += "<a href=\"/category/u/"+subC.url+"\" onClick=\"_gaq.push(['_trackEvent','"+pageType+"', 'Navigation Categories', '"+subC.name+"']);\">"+subC.name+"</a>";
                subHtml += "</li>";
            }
            subHtml += "</ul></li>";
            menuHtml += subHtml;
        }
        secMenu.html(menuHtml);

        $("ul.sf-menu").superfish({
            animation: {opacity: 'show'},  // slide-down effect without fade-in
            delay:      0,
            dropShadows:   false
        });
    });
});


