var header = {
    curMenu: {},
    defSearchText: {},

    init: function() {

        $(".searchTerm").keypress(function(event) {
            if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {
                if ($(this).attr("value") == "") {
                    event.preventDefault();
                } else {
                    document.getElementById('ctl00_search_button').focus();
                }
            } else return true
        })

        var siteLinks = $(".topNav a");
        var navMenuShadow = $("#navMenuShadow")

        siteLinks.mouseenter(function() {

            if ($(".topNav li").index($(this).parent()) < 12) {
                var indexPos = $(".topNav li").index($(this).parent())  / 2;
                header.curMenu = $(".dropDownMenu").eq(indexPos);
                //Add 120 on to the left hand position to compensate for relative/absolute positioning
                var locleft = $(this).position().left + 120;
                if (header.curMenu.width() <= $(this).parent().width()) {
                    header.curMenu.width($(this).parent().width() + 5);
                    header.curMenu.css("background-color", "#000");
                }
                if (header.curMenu.width() > (965 - locleft)) locleft = (locleft + 7) - (header.curMenu.width() - $(this).width()); //locleft = locleft - (locleft + menuToShow.width() - 965)
                header.curMenu.css("left", locleft).fadeIn(200);
                //$("#navMenuJoint").show().css("left", locleft).height(5).width($(this).parent().width());
                navMenuShadow.show().css("left", locleft + 5).height(header.curMenu.height() - 5).width(header.curMenu.width());
                //$(".navMenuJoint").eq($("#siteNav li").index($(this).parent())).show().height(5).width($(this).parent().width());
            }
        })
        siteLinks.mouseleave(function(event) {

            $(this).prev().css("visibility", "hidden");
            if (!header.determineHide(header.curMenu.get(0), event)) {
                navMenuShadow.hide(0);
                header.curMenu.hide(0);
            }
        })
        navMenuShadow.fadeTo(0, 0.6)
        $(".dropDownMenu").mouseleave(function(event) {
            //if (event.toElement.id != 'navMenuJoint') {
            $(this).hide(0);
            //$("#navMenuJoint").hide(0);
            navMenuShadow.hide(0);
            //}
        })
    },

    determineHide: function(menu, e) {
        var e = window.event || e
        var c = e.relatedTarget || e.toElement
        while (c && c != menu) try { c = c.parentNode } catch (e) { c = menu }
        if (c == null) {
            return false;
        }
        if (c == menu) {
            return true;
        }
        if (e.toElement.id == 'navMenuJoint') {
            return true;
        }
        else {
            return false;
        }
    }
}

$(document).ready(function() {
    header.init();
});


var home = {
    imgID: {},
    timer: {},
    links: {},

    showImages: function(pane) {
        if (pane.data("images") != "true") {
            pane.find("img").each(function() {
                var curImg = $(this);
				if ($(this).attr("rel") != 'undefined') {
                $(new Image()).load(function() {
                    curImg.css("visibility", "visible").attr("src", $(this).attr("src"));
                }).attr("src", "/ecom/rebel/artwork/product_images/small/" + $(this).attr("rel") + "?width=" + $(this).width() + "&height=" + $(this).height() + "&quality=90");
				}
            });
            pane.data("images", "true");
        }
    },

    init: function() {

        // Initiate tabs effect for merch zone
        $("#hpMz").kb_tabs({ 'tabEffect': 'scroll' });

        // Load images in first merch zone
        home.showImages($(".tabpane", "#hpMz").eq(0));

        // When a tabheader is clicked load the product images for that zone
        $(".tabheader a", "#hpMz").mousedown(function() {
            var ind = $(".tabheader a").index($(this));
            home.showImages($(".tabpane", "#hpMz").eq(ind));
        });

        $(".tabheader a").hover(function() {
            $(this).css('cursor', 'pointer');
        },
            function() {
                $(this).css('cursor', 'auto');
            });

    },

    initCarousel: function() {
        home.imgID = 0;
        //$("#promoimage").data("animating", false).queue(function() {
        //    $(this).css("background", "url(" + tlma[0].image + ") top center no-repeat"); $(this).dequeue();
        //}).show().queue(function() { $(this).height($("#pageContainer").height()); $(this).dequeue() });

        $("#navbarnext").click(function() {
            clearTimeout(home.timer);
            home.tlmFireRotate();
        });

        if (tlma[0].desc == '') {
            $("#promodescbg, #promodesc").hide(0);
        } else {
            $("#promodescbg").css("opacity", 0.5).show();
            //$("#promodesc").show(0).children("span").html(tlma[0].title);
            //$("#hpTopBnr").attr("href", tlma[0].link).attr("title", tlma[0].desc);
            //$("#promodesc").find("a").attr("href", tlma[0].link).html(tlma[0].desc);
            $("#azLnk").attr("href", tlma[0].link).attr("title", tlma[0].desc);
            $("#azImg").attr("src", tlma[0].image);
        }
        home.links = $("a", "#navbarlinks");
        home.links.filter(":not(:first)").fadeTo(200, 0.6);
        home.links.live("mouseenter", function() { if (home.links.index($(this)) != home.imgID) { $(this).addClass("promoimageselected").fadeTo(50, 1) } });
        home.links.live("mouseleave", function() { if (home.links.index($(this)) != home.imgID) { $(this).removeClass("promoimageselected").fadeTo(50, 0.6) } });
        home.links.show();
        //$("a.promoimage", "#hpTopBnrs").click(function() { sc_trackLink('HPTopBnr' + $("a.promoimage", "#hpTopBnrs").index(this), 'eVar6'); });
    },

    tlmRotate: function() {
        home.timer = setTimeout(function() { home.tlmFireRotate(); }, 8000);
    },

    tlmFireRotate: function() {

        home.imgID += 1;
        if (home.imgID == tlma.length) { home.imgID = 0 }
        var nextImage = tlma[home.imgID].image;
        $(new Image()).load(function() {
            home.changeImg();
        }).attr('src', nextImage);
    },

    changeImg: function() {


        var nextPromo = tlma[home.imgID];
        home.links.eq(home.imgID - 1).removeClass("promoimageselected").fadeTo(200, 0.6);
        home.links.eq(home.imgID).addClass("promoimageselected").fadeTo(200, 1);
        $("#azImg").attr('src', nextPromo.image).fadeIn(800, function() {
            $(this).data("animating", false);
        });
        $("#azLnk").attr("href", nextPromo.link).attr("title", nextPromo.desc);
        if (nextPromo.desc == '') {
            $("#promodescbg, #promodesc").hide(0);
        } else {
            //$("#promodescbg, #promodesc").show(0);
            //$("#promodesc").children("span").html(nextPromo.title);
            //$("#promodesc").find("a").attr("href", nextPromo.link).html(nextPromo.desc);
        }

        home.tlmRotate();
    }
};

var proddetails = {
    imgID: {},
    timer: {},
    links: {},

    init: function() {

        // Tabify the tabs area
        $("#pdTabs").kb_tabs();

        // Load images in first merch zone
        //home.showImages($(".tabpane", "#hpMz").eq(0));

        // When a tabheader is clicked load the product images for that zone
        $(".tabheader a", "#hpMz").mousedown(function() {
            var ind = $(".tabheader a").index($(this));
            home.showImages($(".tabpane", "#hpMz").eq(ind));
        });

        $(".tabheader a").hover(function() {
            $(this).css('cursor', 'pointer');
        },
            function() {
                $(this).css('cursor', 'auto');
            });

    }


};



var promo = {
    imgID: {},
    timer: {},
    links: {},

    initCarousel: function() {
        promo.imgID = 0;
        //$("#promoimage").data("animating", false).queue(function() {
        //    $(this).css("background", "url(" + tlma[0].image + ") top center no-repeat"); $(this).dequeue();
        //}).show().queue(function() { $(this).height($("#pageContainer").height()); $(this).dequeue() });

        $("#navbarnext").click(function() {
            clearTimeout(promo.timer);
            promo.tlmFireRotate();
        });

        if (tlma[0].desc == '') {
            $("#promodescbg, #promodesc").hide(0);
        } else {
            $("#promodescbg").css("opacity", 0.5).show();
            //$("#promodesc").show(0).children("span").html(tlma[0].title);
            //$("#hpTopBnr").attr("href", tlma[0].link).attr("title", tlma[0].desc);
            //$("#promodesc").find("a").attr("href", tlma[0].link).html(tlma[0].desc);
            $("#azLnk").attr("href", tlma[0].link).attr("title", tlma[0].desc);
            $("#azImg").attr("src", tlma[0].image);
        }
        promo.links = $("a", "#navbarlinks");
        promo.links.filter(":not(:first)").fadeTo(200, 0.6);
        promo.links.live("mouseenter", function() { if (promo.links.index($(this)) != promo.imgID) { $(this).addClass("promoimageselected").fadeTo(50, 1) } });
        promo.links.live("mouseleave", function() { if (promo.links.index($(this)) != promo.imgID) { $(this).removeClass("promoimageselected").fadeTo(50, 0.6) } });
        promo.links.show();
        //$("a.promoimage", "#hpTopBnrs").click(function() { sc_trackLink('HPTopBnr' + $("a.promoimage", "#hpTopBnrs").index(this), 'eVar6'); });
    },

    tlmRotate: function() {
        promo.timer = setTimeout(function() { promo.tlmFireRotate(); }, 8000);
    },

    tlmFireRotate: function() {

        promo.imgID += 1;
        if (promo.imgID == tlma.length) { promo.imgID = 0 }
        var nextImage = tlma[promo.imgID].image;
        $(new Image()).load(function() {
            promo.changeImg();
        }).attr('src', nextImage);
    },

    changeImg: function() {


        var nextPromo = tlma[promo.imgID];
        promo.links.eq(home.imgID - 1).removeClass("promoimageselected").fadeTo(200, 0.6);
        promo.links.eq(promo.imgID).addClass("promoimageselected").fadeTo(200, 1);
        $("#azImg").attr('src', nextPromo.image).fadeIn(800, function() {
            $(this).data("animating", false);
        });
        $("#azLnk").attr("href", nextPromo.link).attr("title", nextPromo.desc);
        if (nextPromo.desc == '') {
            $("#promodescbg, #promodesc").hide(0);
        } else {
            //$("#promodescbg, #promodesc").show(0);
            //$("#promodesc").children("span").html(nextPromo.title);
            //$("#promodesc").find("a").attr("href", nextPromo.link).html(nextPromo.desc);
        }

        promo.tlmRotate();
    }
};
