var $j = jQuery.noConflict();

$j(document).ready(function() {

    if (self != top) {
        if (document.images)
            top.location.replace(window.location.href);
        else
            top.location.href = window.location.href;
    }

    var translateStatus = 0;
    var uiStatus = 0;

    $j("#alert_close").click(function() {
        $j("#ctl00_alert").hide();
        //IE6 display refresh hack
        $j(".interface_all").hide();
        $j(".interface_all").show();
        $j("#nav").hide();
        $j("#nav").show();
        $j("#interface_user").hide();
        $j("#interface_user").show();
        createCookie("IE6_alert", "false", 90);
    });

    function createCookie(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    }

    function loadPopup() {
        if (translateStatus == 0) {
            $j("#bg_overlay").css({
                "opacity": "0.7"
            });
            $j("#bg_overlay").fadeIn("slow");
            $j("#interface_translate").fadeIn("slow");
            centerPopup();
            translateStatus = 1;
        }
    }

    function disablePopup() {
        if (translateStatus == 1) {
            $j("#bg_overlay").fadeOut("slow");
            $j("#interface_translate").fadeOut("slow");
            translateStatus = 0;
        }
    }

    function centerPopup() {
        var windowWidth = document.documentElement.clientWidth;
        var windowHeight = document.documentElement.clientHeight;
        var popupHeight = $j("#interface_translate").height();
        var popupWidth = $j("#interface_translate").width();

        $j("#interface_translate").css({
            "position": "absolute",
            "top": windowHeight / 2 - popupHeight / 2,
            "left": windowWidth / 2 - popupWidth / 2
        });

        //IE6 force height
        $j("#bg_overlay").css({
            "height": windowHeight
        });

    }

    $j("#popupContactClose").click(function() {
        disablePopup();
    });

    $j("#bg_overlay").click(function() {
        disablePopup();
    });

    $j(document).keypress(function(e) {

        if (e.keyCode == 27 && translateStatus == 1) {
            disablePopup();
        }

        if (e.keyCode == 27 && uiStatus == 1) {
            disableLogin();
        }

        if (e.keyCode == 27 && uiStatus == 2) {
            disableUser();
        }

    });

    $j(".all_translate").click(function() {

        loadPopup();

    });

    $j("#interface_translate_close").click(function() {

        disablePopup();

    });

    $j("#interface_translate_button").click(function() {

        disablePopup();

    });

    $j(".nav_inactive").hover(function() {

        $j(".main_nav a").removeClass("nav_active");
        $j(this).addClass("nav_active");

        $j(this).parent().find("ul.subnav").css("display", "block");
        $j(this).parent().hover(function() {
        }, function() {
            $j("ul.subnav").css("display", "none");
            $j(".main_nav a").removeClass("nav_active");
        });

        if ($j("#nav_home").hasClass("nav_active")) {
            $j("#nav_left").css("background", "url('/Shared/images/bg_nav_active_left.jpg') repeat-x top left");
        }

        $j(this).mouseleave(function() {

            if ($j("#nav_home").hasClass("nav_active")) {
                $j("#nav_left").css("background", "url('/Shared/images/bg_nav_left.jpg') repeat-x top left");
            }

        });

    });

    //IE6 Nav Menu Rollovers
    $j(".subnav li").hover(function() {
        $j(this).css("background", "#0a82df");

        $j(this).mouseleave(function() {

            $j(this).css("background", "#045594");

        });

    });

    $j("#splash_buttons a").hover(function() {

        $j(this).find('img').remove();
        $j(this).prepend("<img src='/Shared/images/icon_bullet_button_blue.jpg' src='Bullet' />");

        $j(this).mouseleave(function() {

            $j(this).find('img').remove();
            $j(this).prepend("<img src='/Shared/images/icon_bullet_button.jpg' src='Bullet' />");

        });

    });

    $j("#featured_content_tabs_shop").click(function() {

        if ($j(this).hasClass("featured_content_tabs_inactive") == true) {
            $j("#featured_content_tabs div").removeClass("featured_content_tabs_inactive");
            $j("#featured_content_tabs div").removeClass("featured_content_tabs_active");
            $j("#featured_content_tabs div").addClass("featured_content_tabs_inactive");
            $j(this).removeClass("featured_content_tabs_inactive");
            $j(this).addClass("featured_content_tabs_active");
            $j("#featured_content_shop").show();
            $j("#featured_content_ship").hide();
            $j("#featured_content_save").hide();

        }

    });

    $j("#featured_content_tabs_ship").click(function() {

        if ($j(this).hasClass("featured_content_tabs_inactive") == true) {
            $j("#featured_content_tabs div").removeClass("featured_content_tabs_inactive");
            $j("#featured_content_tabs div").removeClass("featured_content_tabs_active");
            $j("#featured_content_tabs div").addClass("featured_content_tabs_inactive");
            $j(this).removeClass("featured_content_tabs_inactive");
            $j(this).addClass("featured_content_tabs_active");
            $j("#featured_content_shop").hide();
            $j("#featured_content_ship").show();
            $j("#featured_content_save").hide();

        }

    });

    $j("#featured_content_tabs_save").click(function() {

        if ($j(this).hasClass("featured_content_tabs_inactive") == true) {
            $j("#featured_content_tabs div").removeClass("featured_content_tabs_inactive");
            $j("#featured_content_tabs div").removeClass("featured_content_tabs_active");
            $j("#featured_content_tabs div").addClass("featured_content_tabs_inactive");
            $j(this).removeClass("featured_content_tabs_inactive");
            $j(this).addClass("featured_content_tabs_active");
            $j("#featured_content_shop").hide();
            $j("#featured_content_ship").hide();
            $j("#featured_content_save").show();

        }

    });

    $j("#featured_social_tabs_twitter").click(function() {

        if ($j(this).hasClass("featured_social_tabs_inactive") == true) {
            $j("#featured_social_tabs div").removeClass("featured_social_tabs_inactive");
            $j("#featured_social_tabs div").removeClass("featured_social_tabs_active");
            $j("#featured_social_tabs div").addClass("featured_social_tabs_inactive");
            $j(this).removeClass("featured_social_tabs_inactive");
            $j(this).addClass("featured_social_tabs_active");
            $j("#featured_social_twitter").show();
            $j("#featured_social_blog").hide();

            $j("#featured_social_tabs_twitter_icon").css("background-position", "0px 0px");
            $j("#featured_social_tabs_blog_icon").css("background-position", "0px 28px");

        }

    });

    $j("#featured_social_tabs_blog").click(function() {

        if ($j(this).hasClass("featured_social_tabs_inactive") == true) {
            $j("#featured_social_tabs div").removeClass("featured_social_tabs_inactive");
            $j("#featured_social_tabs div").removeClass("featured_social_tabs_active");
            $j("#featured_social_tabs div").addClass("featured_social_tabs_inactive");
            $j(this).removeClass("featured_social_tabs_inactive");
            $j(this).addClass("featured_social_tabs_active");
            $j("#featured_social_twitter").hide();
            $j("#featured_social_blog").show();

            $j("#featured_social_tabs_twitter_icon").css("background-position", "0px 19px");
            $j("#featured_social_tabs_blog_icon").css("background-position", "0px 0px");

        }

    });

    if ($j(".end_nav").is(":visible")) {
        $j("#nav_right").css("background", "url('/Shared/images/bg_nav_active_right.jpg') no-repeat top left");
        if ($j("#live_help").is(":visible")) {
            $j(".end_nav .nav_inactive").css("padding", "15px 20px 0 20px");
            $j(".end_nav .subnav").css("left", "-92px");
        } else {
            $j(".end_nav .nav_inactive").css("padding", "15px 11px 0 20px");
            $j(".end_nav .subnav").css("left", "-97px");
        }
    } else {
        $j("#nav_right").css("background", "url('/Shared/images/bg_nav_right.jpg') no-repeat top left");
    }

    if ($j("#inbox #ctl00_MainContent_btnSubmitMailOnlyRequest").length == 0 && $j("#inbox #ctl00_MainContent_btnSubmitRequest").length > 0) {
        $j("#inbox #ctl00_MainContent_imgHideablePackage2").css("margin-left", "440px");
        $j("#inbox #ctl00_MainContent_btnSubmitRequest").css("margin-left", "350px");
    }

    $j("#featured_content_save a").attr('target', '_blank');

    $j("#newegg td").attr("style", "");
    $j("#newegg img:first").wrap('<div id="newegg_logo"></div>');
    $j("#newegg_logo").insertBefore("#newegg");
    $j("center").remove();

    // --------- Registration ---------

    var registrationStep = 1;

    $j("#steps .step_inactive").hover(function() {

        $j(this).addClass("step_active");
        $j(this).removeClass("step_inactive");

        $j(this).mouseleave(function() {
            if ($j(this).hasClass("step_selected")) {
                $j(this).removeClass("step_active");
            } else {
                $j(this).addClass("step_inactive");
                $j(this).removeClass("step_active");
            }
        });

        $j(this).click(function() {
            $j("#steps .step_selected").each(
            function() {
                $j(this).addClass("step_inactive");
                $j(this).removeClass("step_selected");
            });

            $j(this).addClass("step_selected");
            $j(this).removeClass("step_inactive");
            $j(this).removeClass("step_active");
        });

    });
    
    $j("#ctl00_MainContent_CouponPromotionalMessage1_divBanner h5:first").addClass("bold");

    function ClearValidators() {
        var isValidEntry = true;
        if (typeof (Page_Validators) != 'undefined') {
            for (var i = 0; i < Page_Validators.length; i++) {
                Page_Validators[i].style.visibility = 'hidden';
            }
        }
        return isValidEntry
    }

    // --------- Memberships ---------

//    var memToggle = 1;
//    $j("#memberships_show").click(function() {
//        if (memToggle == 1) {
//            memToggle = 2;
//            $j("#memberships_additional").animate({
//                opacity: 'toggle',
//                height: 'toggle'
//            }, 500, function() {
//                $j("#memberships_show").html('<a id="memberships_show"><img src="/Shared/images/hide.gif" alt="Hide" />Hide Additional Features</a>');
//            });
//        } else if (memToggle == 2) {
//            memToggle = 1;
//            $j("#memberships_additional").animate({
//                opacity: 'toggle',
//                height: 'toggle'
//            }, 500, function() {
//                $j("#memberships_show").html('<a id="memberships_show"><img src="/Shared/images/show.gif" alt="Show" />Show Additional Features</a>');
//            });
//        }
//    });

//    $j("#memberships_cr_button").click(function() {
//        $j("#memberships_compare").animate({
//            opacity: 'show',
//            height: 'show'
//        }, 500);
//        target = $j("#memberships_compare");
//        if (target.length) {
//            var top = target.offset().top;
//            $j("html,body").animate({ scrollTop: top }, 500);
//            return false;
//        }
//    });

    // --------- Rates ---------

    $j("#ctl00_MainContent_btnCalcRates").click(function() {
        $j("#ctl00_MainContent_tboxAddWeight").focus()
    });

    $j("#ctl00_MainContent_tboxAddWeight").focus()

});
