/// <reference path="jquery.js"/>
/// <reference path="Util.class.js"/>

$(function () {

    if ($.browser.msie) {
        $('ul li:last-child,div.b-free-col:last-child,div.b-more-flyout-item:last-child,div.b-page-section:last-child, div.b-sub-tabs div.b-sub-tab-item::last-child').addClass('last-child');
        $('div.h-section-bodies div.b-section-body:first-child, div.b-about-us-tab-bodies div.b-tab-body:first-child,.b-filter .h-category:first-child').addClass('first-child');
        if (parseInt($.browser.version) == 7) { $('body').addClass("IE7"); }
        if (parseInt($.browser.version) == 8) { $('body').addClass("IE8"); }
    }

    Util.PreloadImages($('img.m-clickable'));

    $('img.m-clickable').hover(
		function () {
		    if (!/-over-btn\.png$/.test($(this).attr('src')) && !/-over-ico\.png$/.test($(this).attr('src'))) {
		        $(this).attr('src', $(this).attr('src').replace(/-btn\.png/, '-over-btn.png').replace(/-ico\.png/, '-over-ico.png'))
		    }
		},
		function () {
		    $(this).attr('src', $(this).attr('src').replace(/-over-btn\.png/, '-btn.png').replace(/-over-ico\.png/, '-ico.png'));
		}
	);

    $('div.b-combo-list div.b-combo-item, div.b-cat-list div.b-cat-item, div.b-search-btn, div.b-choice-1-flyout div.b-choice-1-flyout-item, div.b-my-account div.b-tab, div.b-about-us-tabs div.b-tab').hover(
		function () { $(this).addClass('m-over'); },
		function () { $(this).removeClass('m-over'); }
	);

    $('div.b-more-flyout div.b-more-flyout-item, span.m-pseudo-link,div.b-browse-btn').hover(
		function () { $(this).addClass('m-over'); },
		function () { $(this).removeClass('m-over'); }
	);

    $('body').click(function (e, o) {
        $('div.b-search > div.b-combo').removeClass('m-over').next().hide();
        $('div.b-browse-categories > div.b-browse-btn').removeClass('m-over').next().hide();
        $('div.m-to-left > div.b-choice-1').removeClass('m-over').next().hide();

        $('div.h-options > div.b-options').next().hide();

        if (!$(e.target).closest('div#cat-search-flyout').size()) $('#cat-search-flyout').hide();
    });

    $('div.b-search > div.b-combo').click(function () {

        $('div.m-to-left > div.b-choice-l').removeClass('m-over').next().hide();
        $('div.b-browse-categories > div.b-browse-btn').removeClass('m-over').next().hide();

        $(this).addClass('m-over').next().toggle();
        return false;
    });

    $('div.b-search > div.b-combo-list > div.b-combo-item').click(function () {
        var html = $(this).html();
        $(this).parent().prev().html(html.length > 22 ? html.substring(0, 22) + '...' : html);
        $(this).parent().prev().attr('id', ($(this).attr('id')));
        $(this).parent().hide().prev().removeClass('m-over');
        return false;
    });

    $('div.b-search > input:first').focus(function () {
        var $this = $(this);
        if (!$this.attr('placeholder') || $this.attr('placeholder') == $this.val()) {
            $this.val('');
        }
    });

    // Deactivate all active items
    $(document).click(function () {
        var stop = $(this).parents(".b-cat-list:first").length > 0 || $(this).parents(".m-active:first").length > 0;
        if (!stop) {
            $('.m-active').removeClass('m-active');
        }
    });

    $('div.b-browse-categories > div.b-browse-btn').click(function () {
        $('div.m-to-left > div.b-choice-1').removeClass('m-active').next().hide();
        $('div.b-search > div.b-combo').removeClass('m-active').next().hide();

        $(this).toggleClass('m-active');
        $(this).next().toggle();
        return false;
    });

    // Aliving search button click
    $('div.b-search > div.b-search-btn').click(function () {
        var cat = $(this).prev().prev().prev().html();
        var tag = $(this).prev().prev().prev().attr('id');

        return Util.GoToSearchPage(tag, $('div.b-search > input[name="search"]').val());
    });

    $('div.b-search > input[name="search"]').keyup(function (e) {
        if (e.keyCode == 13) {
            var cat = $(this).prev().prev().html();
            var tag = $(this).prev().prev().attr('id');

            if (tag == undefined) {
                tag = '';
            }
            return Util.GoToSearchPage(tag, $('div.b-search > input[name="search"]').val());
        }
    });



    // replacing all <a href='#'> elements with Not Implemented alert
    $("a[href='#']").click(function () { Util.NotImplemented(); return false; });

    // aliving X button in the lightboxes
    $('div.h-lightbox > div.b-lightbox > div.b-title > div.b-close-btn').live('click', function () {
        $('div.b-overlay').hide();
        $('div.h-lightbox').hide();
        return false;
    });
});





