﻿var menuFlag = true;

function fillHeader() {
    $('.topNavM p').each(function(i, n) { $(this).css('display', 'none'); });

    $('#topR_tab_0').hover(function() { $('#topR_info_0').css({ 'display': 'block' }); $('#topR_info_1').css({ 'display': 'none' }); $('#topR_tab_1').removeClass('tab_on').addClass('tab_off'); $(this).removeClass('tab_off').addClass('tab_on'); }, function() { });
    $('#topR_tab_1').hover(function() { $('#topR_info_0').css({ 'display': 'none' }); $('#topR_info_1').css({ 'display': 'block' }); $('#topR_tab_0').removeClass('tab_on').addClass('tab_off'); $(this).removeClass('tab_off').addClass('tab_on'); }, function() { });

    $('#topTabList li').each(function(i, n) {
        if (i == whichTabIsOn()) {
            $(this).addClass('on');
            $($('.topNavM p')[i]).css({ 'display': 'block', 'margin-left': i * 35 + 'px' });
        }

        $(this).hover(function() {
        	$('#topTabList li').removeClass();
        	$($('#topTabList li')[0]).addClass('noBg');
            menuFlag = true;
            showSubMenu($(this), $($('.topNavM p')[i]), i);
        }, function() {
            menuFlag = false;
            hideSubMenu($(this), $($('.topNavM p')[i]), i);
        });
    });
    

    $.getScript("http://mall.sina.com.cn/common/SearchCategory.js",function(){
    	for(i=0;i<searchCategory.length;i++){
    		$('#search-categories').html($('#search-categories').html() + '<li style="text-align:left;" value=' + searchCategory[i].value + '>' + searchCategory[i].name + '</li>');
    	}
    
    	$('#search-categories li').click(function() {
            var text = $(this).text();
            var value = $(this).attr('value');
            $('#search-option-box').val(text.substr(0, 4));
            $('#search-option-value').val(value);
            $('#search-categories').css({ 'display': 'none' });
            $(document).unbind("click");
        });
    }); 
    
    
    $('#search-option-box').click(function() {
        if ($('#search-categories').css('display').toLowerCase() == 'block') {
            $('#search-categories').css('display', 'none');
        }
        else {
            var pos = $(this).offset();
            $('#search-categories').css({ 'z-index': '9999', 'position': 'absolute', 'top': (pos.top + 20) + 'px', 'left': (pos.left - 1) + 'px', 'display': 'block' });
            $(document).bind('click', function(event) {
                if (event.target.tagName == 'LI' && $(event.target).attr('value') || $(event.target).attr('id') == 'search-option-box') {

                }
                else {
                    $('#search-categories').css({ 'display': 'none' });
                    $(document).unbind("click");
                }

            });
        }
        //添加侦听事件
    });


    $('#do-search').click(function() {
        search();
    });
    $(document).bind('keydown', function(event) {
        if (event.keyCode == 13) search();
    });
}

function search() {
    if ($('#search-word').val() != '') {
        var word = escape($('#search-word').val());
        var category = $('#search-option-value').val();
        var url = 'http://mall.sina.com.cn/Search/ProductSearch.aspx?w=' + word + '&c=' + category;
        window.open(url);
    }
}

function whichTabIsOn() {
    var tab = -1;
    var url = window.location.href.toLowerCase();

	if (url=='http://mall.sina.com.cn/') tab=0;
	if (url.indexOf('/c/cooperate/redbaby') > 0) tab=2;
	if (url.indexOf('/zhuanti/mallztlist') > 0) tab=8;
    if (url.indexOf('digi') > 0) tab = 5;
    if (url.indexOf('elec') > 0) tab = 4;
    if (url.indexOf('man') > 0) tab = 3;
    if (url.indexOf('lady') > 0) tab = 1;
    if (url.indexOf('book') > 0) tab = 6;

    return tab;
}

function showSubMenu(f, o, i) {
    f.addClass('on');
    $('.topNavM p').each(function() { $(this).css({ 'display': 'none' }); });

    o.css({ 'display': 'block', 'margin-left': i * 35 + 'px' });
    o.hover(function() {
        menuFlag = true;
    }, function() {
        menuFlag = false;
        hideSubMenu(f, o, i);
    });
}

function hideSubMenu(f, o, i) {
    setTimeout(function() {
        if (!menuFlag) {
		    if (i != whichTabIsOn())
		    {
		        f.removeClass();
        		$($('#topTabList li')[0]).addClass('noBg');
        	}
            o.css({ 'display': 'none' });
            showSubMenu($($('#topTabList li')[whichTabIsOn()]), $($('.topNavM p')[whichTabIsOn()]), whichTabIsOn());
        } 
    }, 50);
}