var itemTimer;

jQuery(function($) {
    // Your code using failsafe $ alias here...

    $('.itemRow input[type=checkbox], .tabnav, .tabdivBottom input[type=checkbox]:not([disabled=disabled])').click(function() {
        blockFilters();
    });

    $('.selectWrapper select').change(function() {
        blockFilters();
    });

    $("input[type=checkbox]").checkbox({
        empty: '/images/spacer.gif'
    });

    $("input[type=radio]").checkbox({
        cls: 'jquery-radio',
        empty: '/images/spacer.gif'
    });

    $("a.itemLink", ".mainFilters").live("click", function() {
        var checkbox = $(this).parents(".itemWrapper").find("input[type=checkbox]");
        checkbox.attr('checked', checkbox.attr('checked') ? false : true);
        return false;
    });

    $(".enterZip").live("click", function() {
        $(this).parent().siblings(".shipData").toggle();
        return false;
    });

    $("ul.sf-menu").superfish({
        animation: { height: 'show' },
        speed: 300,
        autoArrows: false,
        dropShadows: false
    }).find("ul").bgIframe();

    $(".itemRow").equalHeights(true);
    $(".modCarousel .items").equalHeights(true);
    $(".columnRow").equalHeights(true);

    $(".itemWrapper ul.swatchList li a").live("mouseover", function() {
        $(this).parents(".swatchList").find("a").removeClass("active");
        $(this).addClass("active");
        var newImg = $(this).find(".swatchAlt").html();
        $(this).parents(".itemWrapper").find(".itemImage").attr({ src: newImg });
    });

    $('.altViews li a').live("click", function() {
        //the new image
        var newimg = $(this).attr("href");

        //update active status
        var altViews = $(".altViews li")
        altViews.each(function(i) {
            var children = altViews.eq(i).children("a");
            children.each(function(j) {
                if (newimg.replace('/large/', '').replace('/regular/', '') == children.eq(j).attr("href").replace('/large/', '').replace('/regular/', '')) {
                    children.eq(j).addClass("active");
                } else {
                    children.eq(j).removeClass("active");
                }
            });
        });

        //find the regular image
        $(".productImage img.mainImage").attr({ src: newimg.replace('/large/', '/regular/') });

        //find the large image
        $(".imageViewer .mainImage img").attr({ src: newimg.replace('/regular/', '/large/') });

        return false;
    });

    //Banner slide show
    $("#homeSSW").cycle({
        fx: 'fade',
        speed: 1000,
        timeout: 7000,
        pager: '.nav'
    });


    

    /* END QUICK BUY */
    $(".homeColumns .roundedBtm").matchHeights();

    $(".itemTitle a", ".itemRow").matchHeights();

    $(".prodFinder").each(function() {
        var dropdown = $(".dropDown", this);
        var finderCont = $(this);

        dropdown.hide().css("top", "28px");

        $(".finderBtn", this).live("click", function() {
            dropdown.show().bgiframe();
            $(this).css("background-position", "0 -24px");

            finderCont.mouseleave(function() {
                dropdown.hide()
                $(".finderBtn", this).css("background-position", "0 0");
            });

            $("select, dl, dt, dd", finderCont).mouseleave(function(event) { event.stopPropagation(); });

            $(".dropDownWrapper, .shadowRight", finderCont).matchHeights();
            $(".shadowBtm, .shadowRight", finderCont).css({ "opacity": 0.25 });

            return false;
        });
    });

    $(".dd-trigger", ".dd-wrapper").live("click", function() {

        var dropwrapper = $(this).parents(".dd-wrapper");
        var dropdown = dropwrapper.find(".dd-content");

        dropdown.css("left", "0");

        dropwrapper.mouseleave(function() {
            dropdown.css("left", "-9999em");
        });

        return false;
    });

    $(".mediaLink").live("click", function() {
        $.blockUI({
            message: $(".mediaWindow"),
            bindEvents: false,
            css: {
                width: '985px',
                border: '6px solid #000',
                top: '15%',
                left: '0',
                cursor: 'default'
            },
            overlayCSS: {
                backgroundColor: '#fff',
                opacity: 0.6
            }
        });
        $(".blockOverlay, .closeOverlay").attr("title", "Click to unblock").click($.unblockUI).css("cursor", "pointer");

        $(document).keydown(function(e) {
            if (e.which == 27) {  // escape, close box
                $.unblockUI();
            }
        });

        return false;
    }).attr('title', 'Click to Enlarge').css("cursor", "pointer");
});

function showItemSlider() {
    clearTimeout(itemTimer);
    $("#item-dialog").slideDown(300).bgiframe();
    itemTimer = setTimeout(function() { hideItemSlider(); }, 5000);
};
function hideItemSlider() {
    $("#item-dialog").slideUp(150);
};
function tabSwitch(tabid) {
    $('.tabModule .tabsRow').tabs('select', '#' + tabid);
};
function blockFilters() {
    $('.cbBrewSolutionFilter').addClass('jquery-checkbox-disabled');
    $('.tabdivBottom').block({
        message: '<div><img src="/images/ajaxloading1.gif" /></div>',
        css: { border: 0, background: 'none' },
        overlayCSS: { opacity: 0.12 }
    });
    $('.selectWrapper').block({
        message: null,
        css: { border: 0, background: 'none' },
        overlayCSS: { opacity: 0.12 }
    });
    //Unblock after 30 seconds in case the page doesn't redirect properly.
    setTimeout(unblockFilters, 30000);
};
function unblockFilters() {
    $('.cbBrewSolutionFilter').removeClass('jquery-checkbox-disabled');
    $('.tabdivBottom, .selectWrapper').unblock()
};


