/* Default Text for Search Box */

var hideTitle = function(event) {
    if (this.value == $(this).attr('title')) $(this).attr('value', '').removeClass('default');
}

var restoreTitle = function(event) {
    if (this.value.match(/^s*$/)) $(this).addClass('default').attr('value', $(this).attr('title')).attr('autocomplete', 'off');
}

$(document).ready(function() {
    SetupForm();

    $('div.buckets a:nth-child(1)').css('margin-right', '6px');
    $('div.buckets a:nth-child(2)').css('margin-right', '6px');
    $('.recent li:odd').addClass('odd')
    
    // Hide empty ad rows
    if($.trim($('.landing-ads').html()) == '') {
    	$('.landing-ads').hide();
    }

		if($.trim($('.landing-previews .listing').html()) == '') {
			$('.landing-previews').hide();
		}

});

function SetupForm()
 {
    // setup default text
    $('.default_text').focus(hideTitle).blur(restoreTitle).blur();

    if ($.browser.msie) {
        $('ul.dropdown li').mouseover(function(event) {
            $(this).find('ul').css('display', 'block');
        }).mouseout(function(event) {
            $(this).find('ul').css('display', 'none');
        });
    }

    // product listings
    $('.listing .product, .search ul li').click(function(event) {
        document.location.href = $(this).find("a").attr('href');
    });

    InitializeLightBox();

    if ($.browser.msie) {
        $('.listing .product, button.cart').mouseover(function(event) {
            $(this).addClass('hover');
        });
        $('.listing .product, button.cart').mouseout(function(event) {
            $(this).removeClass('hover');
        });
    }

    SetupTabs();

    // hide searh button
    var search_wrapper = $('form.search_bar span.search_wrapper');
    var btn = search_wrapper.next('input');
    btn.css('display', 'none');
    if (search_wrapper.next('a.search_submit').length == 0) {
        search_wrapper.after('<a href="#button" class="pad_right right_arrow icon search_submit">' + btn.val() + '</a>');
    }
    if ($.browser.safari && parseFloat($.browser.version) <= 312.8) {
        search_wrapper.addClass('safari');
        search_wrapper.children('input').attr({
            'type': 'search',
            'autosave': '',
            'results': '10'
        });
    }

    // Submit site search
    $('a.search_submit').click(function() {
        $(this).parents('form').submit();
        return false;
    });

    NicerButtons();

    if (typeof(InitializeThickBox) == "function")
    InitializeThickBox();
}

function NicerButtons()
 {
    var button = $('input.enhance').each(
    function()
    {
        var klass = $(this).hasClass('quiet') ? ' quiet_button': '';
        if ($(this).next('a.enhanced_button').length == 0)
        {
            $(this).css({
                display: 'none'
            }).
            after('<a href="#" class="enhanced_button' + klass + '"><span>' + $(this).attr('value') + '</span></a>').
            next('a.enhanced_button').unbind("click").click(
            function()
            {
                var btn = $(this).prev();
                if (btn[0].onclick != null) {
                    btn.click();
                    return false;
                }
                else {
                    $(btn).after('<input style="visibility:hidden;display:none" type="hidden" name="' + btn[0].name + '" value="' + btn[0].name + '"/>')
                    $(this).parents('form').submit();
                    return false;
                }
            })
        }
    }).mouseover
    (
    function()
    {
        $(this).addClass('hover');
    }
    ).mouseout(
    function()
    {
        $(this).removeClass('hover');
    }
    );
}

function InitializeLightBox() {

    $('a.lightbox').click(function(event) {
        $('div.lightbox').children('img.main').attr('src', this.href);
        var t = this.title || this.name || null;
        var a = "#TB_inline?height=570&width=530&inlineId=lightboxContent&modal=true";
        var g = this.rel || false;
        tb_show(t, a, g);
        return false;
    });

    // closes lightbox
    $('div.lightbox a.close').click(function(event) {
        tb_remove();
        $('span.DD_belatedPNG_sizeFinder').hide();
        return false;
    });
}

function Action(form, action)
 {
    $('#' + form).attr('action', action);
    return true;
}

function SetupTabs()
 {
    $('.fs dl dt').click(function() {
        $('.tabs .selected').removeClass('selected');
        $(this).addClass('selected').next().addClass('selected');
    });
}

jQuery(function($) {

    // Fix for FF2 rendering issue (language dropdown)
    if ($.browser.mozilla && parseInt($.browser.version) <= 2) {
        $('ul.dropdown.language').css({
            clear: 'none',
            float: 'none'
        });
    }

    // Fix for Safari rendering issue (select not observing defined height)
    if ($.browser.safari) {
        $('.country_selection input').css({
            'vertical-align': 'middle'
        });
    }

    setupProductGrid();
});

function setupProductGrid() {

    $('input.submit').hide();

    $('div.selectors').change(function() {
        $('#filterForm').submit();
    });

    $('#SelectedSort').change(function() {
        $('#filterForm').submit();
    });

    $('#SelectedSort').selectmenu({
        style: 'dropdown',
        width: 54,
        menuWidth: 79
    });

    $('span.selectedReplace').click(function() {
        $(this).parent().find('input').remove();
        $('#filterForm').submit();
    });

    // MOUSEOVER ADD CLASS
    jQuery('.listing .product').mouseover(function() {
        jQuery(this).addClass('over');
    }).mouseleave(function() {
        jQuery(this).removeClass('over');
    });

    $('select#feature_WORK').selectmenu({
        style: 'dropdown',
        menuWidth: 149,
        width: 102,
        transferClasses: true
    });
    $('select#dimension_SS').selectmenu({
        style: 'dropdown',
        menuWidth: 62,
        width: 37
    });
    $('select#dimension_SW').selectmenu({
        style: 'dropdown',
        menuWidth: 63,
        width: 38
    });
    $('select#genericcolor').selectmenu({
        style: 'dropdown',
        menuWidth: 61,
        width: 36
    });
    $('select#feature_TOE').selectmenu({
        style: 'dropdown',
        menuWidth: 57,
        width: 32
    });
    $('select#feature_PTYP').selectmenu({
        style: 'dropdown',
        menuWidth: 65,
        width: 35
    });
    $('select#feature_STYL').selectmenu({
        style: 'dropdown',
        menuWidth: 65,
        width: 40
    });
    $('select#feature_COMF').selectmenu({
        style: 'dropdown',
        menuWidth: 166,
        width: 118
    });
    $('select#feature_ACTV').selectmenu({
        style: 'dropdown',
        menuWidth: 87,
        width: 67
    });
    $('select#DealerLocator_Radius').selectmenu({
        style: 'dropdown',
        menuWidth: 110
        // width: 85
    });
    $('select#DealerLocator_State').selectmenu({
        style: 'dropdown',
        menuWidth: 85
        // width: 60
    });
    $('select#unutilizedStyleVariationID').selectmenu({
        style: 'dropdown',
        menuWidth: 205,
        width: 180
    });

	 $('html>body>ul.ui-selectmenu-menu>li:first-child').hide();
}

// Show a "what's this?" overlay at the cursor position
function showOverlay(overlayDiv, side) {
    var popups = getElementsByClass('positionedOverlay', document, 'div');
    for (i = 0; i < popups.length; i++) {
        popups[i].style.visibility = "hidden";
    }
    var theDiv = document.getElementById(overlayDiv);
    AssignPosition(theDiv, side);
}
// Hide a "what's this" overlay
function hideOverlay(overlayDiv) {
    document.getElementById(overlayDiv).style.visibility = "hidden";
}
function getElementsByClass(searchClass, node, tag) {
    var classElements = new Array();
    if (node == null)
    node = document;
    if (tag == null)
    tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    for (i = 0, j = 0; i < elsLen; i++) {
        if (els[i].className.indexOf(searchClass) > -1) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}

// Utility code to determine where the overlay is to be displayed
var cX = 0;
var cY = 0;
var rX = 0;
var rY = 0;
var divY = 0;
if (document.all) {
    document.onmousemove = UpdateCursorPositionDocAll;
}
 else {
    document.onmousemove = UpdateCursorPosition;
}
function UpdateCursorPosition(e) {
    cX = e.pageX;
    cY = e.pageY;
}
function UpdateCursorPositionDocAll(e) {
    cX = event.clientX;
    cY = event.clientY;
}
function AssignPosition(d, side) {
    var pageX = findPos(document.getElementById("site"))[0];

    if (self.pageYOffset) {
        rX = self.pageXOffset;
        rY = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) {
        rX = document.documentElement.scrollLeft;
        rY = document.documentElement.scrollTop;
    }
    else if (document.body) {
        rX = document.body.scrollLeft;
        rY = document.body.scrollTop;
    }
    if (document.all) {
        cX += rX;
        cY += rY;
    }
    d.style.marginLeft = "-5000px";
    d.style.visibility = "visible";

    divY = cY - (d.clientHeight);
    if (divY < 0)
    divY = 0;
    d.style.marginLeft = "0";
    d.style.top = (divY) + "px";

    if (side == 'left') {
        d.style.right = (cX) + "px";
    } else {
        d.style.left = (cX - pageX + 10) + "px";
    }
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
        while (obj = obj.offsetParent);
    }
    return [curleft, curtop];
}
function getElementsByClass(searchClass, node, tag) {
    var classElements = new Array();
    if (node == null)
    node = document;
    if (tag == null)
    tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    for (i = 0, j = 0; i < elsLen; i++) {
        if (els[i].className.indexOf(searchClass) > -1) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}

function checkSelection(itemSelector, msg) {
    var selectedItems = $(itemSelector);
    if (selectedItems.length == 1) {
        if (selectedItems.val().length <= 0)
        return confirm(msg);
    }
    return true;
}

function IsIE6() {
    return $.browser.msie && $.browser.version == '6.0';
}

jQuery.fn.setupBase = function(base, options) {
    for (extra in base) {
        var self = this;
        if (base[extra] instanceof Array) {
            for (var i = 0; i < base[extra].length; i++)
            base[extra][i].call(self, options);
        } else {
            base[extra].call(self, options);
        }
    }
};
var openFilters = [];
jQuery(function($) {

    var $$ = function(param) {
        var node = $(param)[0];
        var id = $.data(node);
        $.cache[id] = $.cache[id] || {};
        $.cache[id].node = node;
        return $.cache[id];
    };

    $.fn.menu = function(options) {
        options = options || {};
        this.setupBase(options.base || $.fn.menu.base, options);

        // Initialize
        this.each(function() {
            var menu = $(this);
            $$(menu).menus = $();

            $(this).children('li').mouseenter(function(event) {
                var li = $(this).closest('li');
                menu.trigger('menu:enter', [li]);
            });

            $(this).children('li').mouseleave(function(event) {
                var li = $(this).closest('li');
                menu.trigger('menu:leave', [li]);
            });

            $(this).children('li').each(function() {
                var ul = $(this).children('ul');
                $$(menu).menus = $$(menu).menus.add(ul);
            });

            menu.trigger('menu:initialize');

        });

        return this;
    };

    $.fn.menu.base = {
        initialize: [function(options) {
            this.bind('menu:initialize',
            function() {
                $(this).children('li').each(function() {
                    var ul = $(this).children('ul');
                    ul.hide('fast');
                });
            });
        }],
        enter: [function(options) {
            this.bind('menu:enter',
            function(e, li) {
                li.children('a,span').addClass('open');
                li.addClass('hover');
                var ul = li.children('ul');
                ul.show('fast');
            });
        }],
        leave: [function(options) {
            this.bind('menu:leave',
            function(e, li) {
                li.removeClass('hover');
                li.children('a,span').removeClass('open');
                li.children('ul').stop(true, true).hide('fast');
            });
        }]
    };

    $.delayedMenuBase = $.extend({},
    $.fn.menu.base);

    $.delayedMenuBase['leave'] = [function(options) {
        this.bind('menu:leave',
        function(e, li) {
            li.removeClass('hover');
            window.setTimeout(function() {
                if (!li.hasClass('hover')) {
                    li.children('a,span').removeClass('open');
                    li.children('ul').stop(true, true).hide('fast');
                }
            },
            300);
        });
    }];
});

$(document).ready(function() {
      $('div.thumbnails a').click(function(event) {
        $('#mainProductImage').attr('src', this.href);
        return false;
    });
    
    $('.sectionNavigation').menu();
    $('.sectionNavigation > li:last > a, .sectionNavigation > li:last > span').css({
        border: 'none'
    });
    $("#tabs_1").tabs();
    $("#tabs_2").tabs();
    $("#faux_tabs").tabs();
    var standardThumbUrl;
    var buildFullSizeImage = function(url) {
        var fullWidth,
        fullHeight;
        fullWidth = Math.floor($(window).width() * 0.5);
        fullHeight = Math.floor($(window).height() * 0.5);
        return url.replace(/wid=\d+/, 'wid=' + fullWidth.toString())
        .replace(/hei=\d+/, 'hei=' + fullHeight.toString())
        .replace('bgc=FBFBFA', 'bgc=FFFFFF');
    };
    $('.fullScreenAlternates li a').click(function(event) {
        var thumbSrcFull,
        imageFull,
        imageSrcFull,
        fadeWrapperFull,
        i,
        largerUrl;
        try {
            thumbSrcFull = $(this).attr('href');
            imageFull = $('#TB_ajaxContent .fullscreen-container .imagecontainer img');
            fadeWrapperFull = $('.fullscreen-container .fadeWrapper');
            largerUrl = buildFullSizeImage(thumbSrcFull);
            // preload the image while we fade out
            i = new Image();
            i.src = largerUrl;
            var outside = $('.fullscreen-container .imagecontainer');
            outside.css('height', outside.height());
            fadeWrapperFull.fadeOut('normal',
            function() {
                imageFull.attr('src', largerUrl);
                imageFull.load(function() {
                    fadeWrapperFull.fadeIn('fast');
                    outside.css('height', 'auto');
                });
            });
            //$('#fullscreen-image').attr('src', this.href);
            return false;
        } catch(e) {
            /*             console.debug(e); */
            return false;
        }
    });
    $('.fullscreen-trigger-close').click(function() {
        tb_remove();
    });
    $('.fullscreen-trigger').click(function() {
        var boxWidth, boxHeight, largerUrl;
        
        standardThumbUrl = $('.leftColumn .image .fadeWrapper img').attr('src');
        largerUrl = buildFullSizeImage(standardThumbUrl);
        $('#fullscreen-image').attr('src', largerUrl);
        boxWidth = Math.floor($(window).width() * 0.7);
        boxHeight = Math.floor($(window).height() * 0.7);
        tb_show(null, ThickboxInlineUrl('fullscreen-image-container', true, boxHeight, boxWidth, false, 'tb_White'));
        return false;
    });
    $('.fullscreen-trigger-sole').click(function() {
        var boxWidth,
        boxHeight,
        largerUrl;
        standardThumbUrl = $('.productShow .image .fadeWrapper-sole img').attr('src');
        largerUrl = buildFullSizeImage(standardThumbUrl);
        $('#fullscreen-image').attr('src', largerUrl);
        boxWidth = Math.floor($(window).width() * 0.7);
        boxHeight = Math.floor($(window).height() * 0.7);
        return tb_show(null, ThickboxInlineUrl('fullscreen-image-container', true, boxHeight, boxWidth, false, 'tb_White'));
    });
    $('.productShow .column .alternates a.thumbnail').click(function(event) {
        return false;
    });
    $('.productShow .column .alternates a.thumbnail').hover(function(event) {
        var thumbSrc,
        thumbPath,
        image,
        imageSrc,
        imageQueryString,
        newSrc,
        fadeWrapper,
        i;
        try {
            thumbSrc = $(this).attr('href');
            image = $('.productShow .image .fadeWrapper img');
            image.attr('src', thumbSrc);
            /*
            fadeWrapper = $('.productShow .column .image .fadeWrapper');
            newSrc = thumbSrc;
            // preload the image while we fade out
            i = new Image();
            i.src = newSrc;
            fadeWrapper.fadeOut('normal', function() {
            image.attr('src', newSrc);
            setTimeout(function() {
            fadeWrapper.fadeIn('fast');
            }, 200);
            });
            */
            return false;
        } catch(e) {
            return false;
        }
    });
});

$(function() {
    $('.color-thumb').mouseover(function() {
        $('#color-sku').html($(this).attr('title') + ' (' + $(this).attr('data-sku') + ')');
    }).mouseout(function() {
        var color = $('.color-thumb.selected');
        $('#color-sku').html(color.attr('title') + ' (' + color.attr('data-sku') + ')');
    });
});
