﻿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 * 10 + '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("/css/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 whichTabIsOn() {
    var tab = 0;
    var url = window.location.href.toLowerCase();

	if (url.indexOf('index') > 0) tab=0;
	if (url.indexOf('index_gzsj') > 0) tab=1;
	if (url.indexOf('index_czsj') > 0) tab=2;
    if (url.indexOf('index_xxsj') > 0) tab = 3;
    if (url.indexOf('index_gzjx') > 0) tab = 4;
    if (url.indexOf('index_czjx') > 0) tab = 5;
    if (url.indexOf('index_xxjx') > 0) tab = 6;
    if (url.indexOf('index_ztzz') > 0) tab = 7;
	if (url.indexOf('index_yejy') > 0) tab = 8;
	if (url.indexOf('index_jtjy') > 0) tab = 9;

    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 * 10 + '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();
				var on=whichTabIsOn();
        		$($('#topTabList li')[on]).addClass('noBg');
        	}
            o.css({ 'display': 'none' });
            showSubMenu($($('#topTabList li')[whichTabIsOn()]), $($('.topNavM p')[whichTabIsOn()]), whichTabIsOn());
        } 
    }, 50);
}
