function hide_tabs() {
	$('div#tabs > ul > li > a').removeClass('active').attr('current', 'off');
	$('div#tabs > div.tab-content').each(function() {
	    $(this).hide();
	});
}

function do_tabs() {
	hide_tabs();
	$('div#tabs > ul > li > a').bind('click', function(e) {
	    hide_tabs();
	    $('div#' + $(this).attr('href').replace('#', '')).show();
	    $(this).addClass('active');
	    $(this).attr('current', 'on');
	    e.preventDefault();
	    return false;
	});
	$('div#tabs > ul > li > a:first').click();
}

function do_productTabs() {
    hide_ProductTabs();
    $('div.category-list > .category-list-holder > ul > li > a').bind('click', function(e) {
        hide_ProductTabs();
        $('div#' + $(this).attr('href').replace('#', '')).show();
        $(this).addClass('currentProductTab');
        //$(this).attr('current', 'on');
        e.preventDefault();
        return false;
    });
    $('div.category-list > .category-list-holder > ul > li > a:first').click();
}

function hide_ProductTabs() {
    $('div.category-list > .category-list-holder > ul > li > a').removeClass('currentProductTab');
    $('div.category-list > .category-list-holder > div.tab-content').each(function() {
        $(this).hide();
    });
    //add-box
    $('div.category-list > div.add-box').each(function() {
        $(this).hide();
    });
}

function disable_productTabs() {
    $('div.category-list > .category-list-holder > ul > li > a').removeClass('currentProductTab');
}

function do_productTabsSwitch() {
    disable_productTabs();
    if($('div.category-list > div.category-list-holder > ul > li').length > 1)
    {
        $('div.category-list > div.category-list-holder > ul > li > a').bind('click', function(e) {
            disable_productTabs();
            $(this).addClass('currentProductTab');
            PutElementFirst($('div#' + $(this).attr('href').replace('#', '')).parent());
            e.preventDefault();
            return false;
        });
    }
    $('div.category-list > .category-list-holder > ul > li > a:first').click();
}

function PutElementFirst(e) {
    var clone = e.clone();
    clone.insertBefore(e.parent().children().eq(1));
    e.remove();
}

function SwitchView() {
    if ($.cookie('TigerViewStyle') == 'grid')
        SwitchToGrid();
    else
        SwitchToList();        
    return false;
}

function SwitchToList() {
    var itemslist = document.getElementById('itemslist');
    itemslist.className = "listItemCntnr list";
    $(".product-block").height('auto');
    $.cookie('TigerViewStyle', 'list', { expires: 7 });

    if ($('.listItemCntnr.list').length > 0) {
        $('.product-block').unbind('mouseenter');
        $('.product-block').unbind('mouseleave');
        /*$('.product-block').mouseenter(function() {
            $(this).children(".grid-layer").hide();
        })
        .mouseleave(function() {
            $(this).children(".grid-layer").hide();
        });*/
    }
}

function SwitchToGrid() {
    var itemslist = document.getElementById('itemslist');
    itemslist.className = "listItemCntnr grid";
    equalHeight($(".product-block"));
    $.cookie('TigerViewStyle', 'grid', { expires: 7 });

    //product grid view
    if ($('.listItemCntnr.grid').length > 0) {
        $('.product-block').mouseenter(function() {
            $(this).children(".grid-layer").show();
        })
        .mouseleave(function() {
            $(this).children(".grid-layer").hide();
        });

        var i = 1;

        var lastIndex = 4;
        /*if (window.location.href.indexOf("search.aspx") != -1) {
            lastIndex = 5;
        }*/
        
        $('.product-block').each(function() {
            if (i % lastIndex == 0) {
                $(this).attr('class', 'product-block last')
            }
            i++;
        });
    }
}

function equalHeight(group) {
    tallest = 0;
    group.each(function() {
        thisHeight = $(this).height();
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}

function getElementsByClass(className) {
    div = document.getElementsByTagName("div");
    /*while (element = div[i++]) {
    element.className == className;
    }*/
    var j = 0, i = 0;
    var res = new Array();
    for (i = 0; i < div.length; i++) {
        try {
            if (div[i].className == className) {
                res[j] = div[i];
                j++;
            }
        }
        catch (err) {
        }
    }
    return res;
}

$(document).ready(function() {
    if ($('div.menu ul.menu').length > 0) {
        $('div.menu ul.menu').superfish({
            delay: 200,
            animation: { height: 'toggle' },
            animationClose: { height: 'toggle' },
            speed: 100,
            speedOpen: 100,
            speedClose: 100,
            autoArrows: false,
            dropShadows: false
        });
    }

    $("#lnkShopByBrands").mouseenter(function() {
        $(".BrandsMenuWrapper").show();
    })
        .mouseleave(function() {
            $(".BrandsMenuWrapper").hide();
        });
    //panel
    $(".BrandsMenuWrapper").mouseenter(function() {
        $(".BrandsMenuWrapper").show();
        $("#lnkShopByBrands").attr('class', 'lnkShopByBrandsHover');
    })
        .mouseleave(function() {
            $(".BrandsMenuWrapper").hide();
            $("#lnkShopByBrands").attr('class', '');
        });

    if ($('div#featured-content').length > 0) {
        $('div#featured-content').lofJSidernews({
            direction: 'opacity',
            wapperSelector: 'div.featured-content-slideshow ul',
            mainWidth: 636,
            navOuterSelector: 'div.featured-content-navigator',
            navItemsSelector: 'div.featured-content-navigator ul li',
            navigatorWidth: 292,
            navigatorHeight: 93,
            maxItemDisplay: 4,
            interval: 5000,
            easing: 'easeInQuad',
            duration: 1200,
            auto: true
        });
    }

    if ($('div#slideshow').length > 0) {
        /*$('div#slideshow').nivoSlider({
            effect: 'fade',
            animSpeed: 500,
            pauseTime: 10000,
            startSlide: 0,
            directionNav: true,
            directionNavHide: false,
            controlNav: false,
            controlNavThumbs: false,
            controlNavThumbsFromRel: false,
            keyboardNav: true,
            pauseOnHover: true,
            manualAdvance: false,
            captionOpacity: 0.8,
            prevText: 'Previous',
            nextText: 'Next'
        });*/
        $('div#slideshow').slideshow({
			pauseSeconds: 10,
			caption: false });
    }

    if ($('div.products-ticker div#related-products').length > 0) {
        $('div.products-ticker div#related-products').smoothDivScroll({
            scrollingSpeed: 1,
            scrollingHotSpotLeft: 'div.scrollingHotSpotLeft',
            scrollingHotSpotRight: 'div.scrollingHotSpotRight',
            scrollWrapper: 'div.scrollingWrapper',
            scrollableArea: 'div.scrollableArea',
            autoScrollSpeed: 1,
            autoScrollStep: 1,
            autoScrollInterval: 25,
            autoScroll: 'onstart',
            autoScrollDirection: 'backandforth',
            pauseAutoScroll: 'mouseover',
            visibleHotSpots: 'always'
        });
        $('div.products-ticker div#related-products div.product').bind('mouseover', function() {
            $('div.products-ticker div#related-products').smoothDivScroll('stopAutoScroll');
        });
        $('div.products-ticker div#related-products div.product').bind('mouseout', function() {
            $('div.products-ticker div#related-products').smoothDivScroll('startAutoScroll');
        });
        $('div.products-ticker div#related-products div.scrollingHotSpotLeft').bind('mouseout', function() {
            $('div.products-ticker div#related-products').smoothDivScroll('startAutoScroll');
        });
        $('div.products-ticker div#related-products div.scrollingHotSpotRight').bind('mouseout', function() {
            $('div.products-ticker div#related-products').smoothDivScroll('startAutoScroll');
        });
    }

    if ($('div#tabs').length > 0) {
        do_tabs();
        //do_productTabs();
        do_productTabsSwitch();
    }

    //applying narrow down expand collapse
    if ($('dt.odd').length > 0) {
        $('dt.odd').click(function() {
            var e = $(this).next();
            while (!e.is("dd")) {
                e = e.next();
            }
            var arrow = $(this).children('span').eq(0);
            var ol = $(e).children().eq(0);
            if ($(e).css('display') == 'none') {
                $(e).show();
                arrow.css("background", "url('/images/bullet-side-list.gif') no-repeat scroll 0 4px transparent");
                if (ol.attr('class') == "available-brands") {
                    $('.NarrowDownSearchInput').show();
                }
            }
            else {
                $(e).hide();
                arrow.css("background", "url('/images/bullet-side-list-close.gif') no-repeat scroll 0 4px transparent");
                if (ol.attr('class') == "available-brands") {
                    $('.NarrowDownSearchInput').hide();
                }
            }
        });
    }

    //Narrow down search
    if ($(".listItemCntnr").length > 0) {
        SwitchView();
    }

    //
    initAccordion();

    // Narrow down search brands filter
    if ($('.NarrowDownSearchInput').length > 0) {
        $('.NarrowDownSearchInput').keyup(function(event) {
            $('.available-brands').children().each(function() {
                var search = $('.NarrowDownSearchInput').val().toLowerCase();
                var brand = $(this).children('a').html();
                if (brand.toLowerCase().indexOf(search) == -1) {
                    $(this).hide();
                }
                else {
                    $(this).show();
                }
            });
        });
        $('.NarrowDownSearchInput').focus(function() {
            if (this.value == 'Search') this.value = '';
        });
        $('.NarrowDownSearchInput').blur(function() {
            if (this.value == '') { this.value = 'Search'; }
        });
    }

    //Compare
    if ($(".check").length > 0) {
        $(".check").click(function() {
            if ($(".check:checked").length > 4) {
                alert("Sorry you can only compare up to 4 items");
                $(this).attr('checked', false);
            }
        });
    }

});

function initAccordion() {
    if (!$('ul.accordion').length) return false;
    $('ul.accordion').accordion({
        active: ".selected",
        autoHeight: false,
        header: ".opener",
        collapsible: true,
        event: "click"
    });
}
