﻿$(function() {
    $("div#mainnav > ul > li").mouseenter(function() {
        var divId = this.getAttribute("id") + "-subnav-wrapper";
        if (document.getElementById(divId)) {
            var offset = $(this).siblings().removeClass("active").end().offset();
            $("#" + divId).css({ left: offset.left - 10 + "px", top: offset.top + 19 + "px" });
            subnavRender(this, divId);
        }
    });
    var btnClose = "<span id=\"search-panel-close\" title=\"\u5173\u95ed\u641c\u7d22\u9762\u677f\"></span>";
    var btnOpen = "<span id=\"search-panel-open\" title=\"\u5f39\u51fa\u641c\u7d22\u9762\u677f\"></span>";
    $(btnClose).appendTo("#searchbox-wrapper").click(function() {
        $(this).parent().fadeOut("slow");
        $("#search-panel-open").fadeIn("slow");
        $.cookie("searchbox", "0", { expires: 7, path: '/' });
    });
    $(btnOpen).appendTo("#mainnav").click(function() {
        $("#searchbox-wrapper").fadeIn("slow");
        //this.style.display = 'none';
        $(this).fadeOut("slow");
        $.cookie("searchbox", "1", { expires: 7, path: '/' });
    });

    var isShowSearchbox = $.cookie('searchbox');
    if (isShowSearchbox && isShowSearchbox == '1') {
        $('#searchbox-wrapper').show();
        $('#search-panel-open').hide();
    } else {
        $('#searchbox-wrapper').hide();
    }
    var showedhint = false;
    $("#q").attr("autocomplete", "off").focus(function() {
        $(this).prev().addClass("hidden");
    }).blur(function() {
        if (this.value === "" && !showedhint) {
            $(this).prev().removeClass("hidden");
        }
    }).prev().addClass($("#q").val() !== "" ? "hidden" : "");
    $("#searchtabs > li > a").click(function() {
        $(this).parent().siblings().removeClass("selected").end().addClass("selected");
        var searchtype = this.getAttribute("rel");
        resetSearch(searchtype);
        return false;
    }).mousedown(function() {
        showedhint = true;
        setTimeout(function() { showedhint = false; }, 100);
    });

    var channelDir = getChannelName();
    if (channelDir) {
        $("#" + channelDir).addClass("current");
    }
    if (channelDir == '' || channelDir == 'index') {
        $("#home").addClass("current");
    }

    //Ad. banner Flash
    //$("#header").after("<div id='bigbanner'></div>");
    //swfobject.embedSWF("/uploads/banner/banner970x150.swf", "flashbanner", "970", "150", "9.0.0", "/scripts/expressInstall.swf");
});

function resetSearch(searchType) {
    var searchJson = {
        "products": { "action": "/products/search.aspx", "hint": "\u8f93\u5165\u60a8\u60f3\u641c\u7d22\u7684\u4ea7\u54c1\u5173\u952e\u5b57", "hotkeyword": "\u8336\u5177,\u4e2a\u4eba\u676f" },
        "yellowpage": { "action": "/yellowpage/search.aspx", "hint": "\u8f93\u5165\u60a8\u60f3\u641c\u7d22\u7684\u516c\u53f8\u540d\u79f0\u5173\u952e\u5b57", "hotkeyword": "\u4f73\u7f8e\u96c6\u56e2,\u9676\u74f7\u516c\u53f8" },
        "news": { "action": "/news/search.aspx", "hint": "\u8f93\u5165\u60a8\u60f3\u641c\u7d22\u7684\u8d44\u8baf\u5173\u952e\u5b57", "hotkeyword": "\u5fb7\u5316,\u9676\u74f7" },
        "pictures": { "action": "/pictures/search.aspx", "hint": "\u8f93\u5165\u60a8\u60f3\u641c\u7d22\u7684\u56fe\u7247\u5173\u952e\u5b57", "hotkeyword": "\u4f55\u671d\u5b97,\u89c2\u97f3" },
        "exhibition": { "action": "/news/search.aspx", "hint": "\u8f93\u5165\u60a8\u60f3\u641c\u7d22\u7684\u5c55\u4f1a\u5173\u952e\u5b57", "hotkeyword": "\u827a\u672f\u54c1,\u62cd\u5356\u4f1a" }
    };
    $("#search").attr("action", searchJson[searchType].action);
    $("#q").focus().prev().html(searchJson[searchType].hint || "\u8f93\u5165\u60a8\u60f3\u641c\u7d22\u7684\u5173\u952e\u5b57");
}

//pull-down menu render
function subnavRender(baseID, divID) {
    //baseID = $(baseID);
    divID = document.getElementById(divID);

    if (subnavRender.timer) clearTimeout(subnavRender.timer);
    hideCur();
    divID.style.display = 'block';
    //$(divID).fadeIn("normal");
    $(baseID).addClass("active");
    subnavRender.cur = divID;

    if (!divID.isCreate) {
        divID.isCreate = true;
        divID.onmouseover = function() {
            if (subnavRender.timer) clearTimeout(subnavRender.timer);
            hideCur();
            divID.style.display = 'block';
        };

        function hide() {
            subnavRender.timer = setTimeout(function() {
                divID.style.display = 'none';
                $(baseID).removeClass("active");
            }, 800);
        }

        divID.onmouseout = hide;
        baseID.onmouseout = hide;
    }
    function hideCur() {
        subnavRender.cur && (subnavRender.cur.style.display = 'none');
    }
}


function getChannelName() {
    var pageURL = document.URL.toLowerCase();
    if (pageURL.indexOf("http://") != -1) {
        pageURL = pageURL.replace('http://', '');
    }
    channelDirs = pageURL.split("/");
    if (channelDirs.length < 2) {
        return null;
    }
    var pos = channelDirs[1].indexOf(".");//eg:site.html
    return pos == -1 ? channelDirs[1] : channelDirs[1].substring(0, pos);
}