var AntiXssLibrary = {};
AntiXssLibrary.UrlEncode = function(c) {
    var a, b = "";
    if (c == null) return null;
    if (c == "") return "";
    for (var d = 0; d < c.length; d++) {
        a = c.charCodeAt(d);
        if (a > 96 && a < 123 || a > 64 && a < 91 || a > 47 && a < 58 || a == 46 || a == 45 || a == 95) b = b + String.fromCharCode(a);
        else if (a > 127) b = b + "%u" + this.OutputEncoder_TwoByteHex(a);
        else b = b + "%" + this.OutputEncoder_SingleByteHex(a)
    }
    return b
};
AntiXssLibrary.OutputEncoder_TwoByteHex = function(b) {
    if (b == null) return "";
    var a = b.toString(16);
    for (var c = a.length; c < 4; c++) a = "0" + a;
    return a
};
AntiXssLibrary.OutputEncoder_SingleByteHex = function(b) {
    if (b == null) return "";
    var a = b.toString(16);
    for (var c = a.length; c < 2; c++) a = "0" + a;
    return a
};
var RandomKeyword=["金砖四国","奥斯卡","阮经天丑闻","三八妇女节","犀利哥回家"];
var RandomKeyword114=RandomKeyword[Math.floor(Math.random()*RandomKeyword.length)];
var BESLibrary = {
    GlobalConfig: {
        ServerDNS: "bing.118114.cn",
        Form:"CTCB01",
        ResourcePath: "http://box.118114.cn/msbox/"
    },
    ScopeConfig: {
        Web: {
            Format: "http://{0}/search?q={1}&setlang=zh-cn&form={2}&SourceSiteID={3}&NID={4}",
            InSiteSearch: true,
            Text: "网页"
        },
        Image: {
            Format: "http://{0}/images/search?q={1}&setlang=zh-cn&form={2}&SourceSiteID={3}&NID={4}",
            InSiteSearch: true,
            Text: "图片"
        },
        Video: {
            Format: "http://{0}/videos/search?q={1}&setlang=zh-cn&form={2}&SourceSiteID={3}&NID={4}",
            InSiteSearch: true,
            Text: "视频"
        },
        News:{
            Format: "http://{0}/news/search?q={1}&setlang=zh-cn&form={2}&SourceSiteID={3}&NID={4}",
            InSiteSearch: true,
            Text: "资讯"
        },
	Ditu: {
            Format: "http://{0}/ditu/?q={1}&setlang=zh-cn&form={2}&SourceSiteID={3}&NID={4}",
            InSiteSearch: false,
            Text: "地图"
        },
    Life: {
	    Text: "生活",
	    Format:"http://www.118114.cn"
        },
	Dao:{
		Text:"114导航",
		Format:"http://dao.118114.cn"
	}
    },
    CheckSiteInfo: function(a, b) {
        if (b == "name" && a == "") return "您的站点名称";
        else if (b == "url" && a == "") return "www.yoursite.com";
        else return a
    },
    Browser: {
        isIE: typeof ActiveXObject !== "undefined",
        isOpera: typeof window.opera !== "undefined",
        isInIFrame: function() {
            try {
                return window.frameElement && window.frameElement.tagName === "IFRAME"
            } catch(a) {
                return true
            }
        } ()
    },
    SetStyle: function(b, a) {
        if (this.Browser.isIE) b.className = a;
        else b.setAttribute("class", a)
    },
    Container: null,
    Config: {},
    CurrentTabIndex: 0,
    CurrentSearchType: "",
    LoadStyle: function(b) {
        var c = document.getElementsByTagName("head")[0],
        a = document.createElement("link");
        a.type = "text/css";
        a.rel = "stylesheet";
        a.rev = "stylesheet";
        a.id = "BingSearchBoxStyle";
        a.href = this.GlobalConfig.ResourcePath + "BingSearchBox.css";
        c.appendChild(a);
        if (this.Browser.isIE) a.onreadystatechange = function() {
            if (a && ("loaded" == a.readyState || "complete" == a.readyState)) {
                a.onreadystatechange = null;
                b.apply(BESLibrary)
            }
        };
        else b.apply(BESLibrary)
    },
    FormatString: function() {
        var b = arguments[0];
        for (var a = 0; a < arguments.length - 1; a++) b = b.replace("{" + a + "}", arguments[a + 1]);
        return b
    },
    Init: function(a) {
        this.Container = document.getElementById("BESContainer");
        if (this.Config["Setting"] == null) {
            this.Config = a;
            this.LoadStyle(this.Render)
        } else this.Render()
    },
    Render: function() {
        this.Container.innerHTML = "";
        this.SetStyle(this.Container, "BingEmbeddedSearchBox");
        //统计PV开始
        this.ShowMsbox = document.createElement("iframe");
        this.ShowMsbox.src="http://box.118114.cn/showmsbox.html?id="+this.Config.Setting.SourceSiteID+"&nid="+AntiXssLibrary.UrlEncode(this.Config.Setting.NID);
        this.ShowMsbox.style.display='none';
        this.Container.appendChild(this.ShowMsbox);
        //统计PV结束
        this.TabContainer = document.createElement("div");
        this.SetStyle(this.TabContainer, "BingTabContainer");
        this.Container.appendChild(this.TabContainer);
        if (this.Config.Scopes.length > 1) {
            this.CurrentTabIndex = 0;
            for (var b = 0; b < this.Config.Scopes.length; b++) {
                if (b > 0) {
                    var c = document.createElement("div");
                    this.SetStyle(c, "BingTabSplit");
                    this.TabContainer.appendChild(c)
                }
                var a = document.createElement("a");
                if (b == this.CurrentTabIndex) this.SetStyle(a, "BingTabHighlight");
                else this.SetStyle(a, "BingTabNormal");
                a.innerHTML = this.ScopeConfig[this.Config.Scopes[b]].Text;
                a.href = "#";
                a.setAttribute("Index", b);
                a.onclick = function() {
                    var a = this.getAttribute("Index");
                    BESLibrary.TabClicked(a);
                    return false
                };
                this.TabContainer.appendChild(a)
            }
        } else this.TabContainer.style.display = "none";
        
        this.Logo114 = document.createElement("div");
        this.SetStyle (this.Logo114,"Logo114");
        this.Container.appendChild(this.Logo114);
        var boxwidth = this.Config.Setting.Width-89;
        this.SearchBoxContainer = document.createElement("div");
        this.SetStyle(this.SearchBoxContainer, "BingSearchBoxContainer");
        this.Container.appendChild(this.SearchBoxContainer);
        this.SearchBoxContainer.style.width = boxwidth + "px";
        this.SearchBarLeft = document.createElement("div");
        this.SetStyle(this.SearchBarLeft, "BingSearchBoxContainerLeft");
        this.SearchBoxContainer.appendChild(this.SearchBarLeft);
        this.SearchBarMiddle = document.createElement("div");
        this.SetStyle(this.SearchBarMiddle, "BingSearchBoxContainerMiddle");
        this.SearchBarMiddle.style.width = boxwidth - 81 + "px";
        this.SearchBoxContainer.appendChild(this.SearchBarMiddle);
        this.SearchBarRight = document.createElement("div");
        this.SetStyle(this.SearchBarRight, "BingSearchBoxContainerRight");
        this.SearchBoxContainer.appendChild(this.SearchBarRight);
        this.SearchBoxBorder = document.createElement("div");
        this.SetStyle(this.SearchBoxBorder, "BingSearchBoxBorder");
        this.SearchBoxBorder.style.width = boxwidth - 84 + "px";
        this.SearchBarMiddle.appendChild(this.SearchBoxBorder);
        this.SearchBox = document.createElement("input");
        this.SearchBox.type = "text";
        this.SetStyle(this.SearchBox, "BingSearchBox");
        this.SearchBox.style.width = boxwidth - 130 + "px";
        this.SearchBox.onkeyup = function(a) {
            var a = window.event ? window.event: a;
            if (a.keyCode == 13) {
                BESLibrary.Search();
                return false
            }
            return false
        };
		this.SearchBox.value=RandomKeyword114;
		this.SearchBox.style.color='gray';
		this.SearchBox.onfocus = function(){
			if (this.value==RandomKeyword114){
				this.value="";
				this.style.color='#000';
			}
		};
		this.SearchBox.onblur = function(){
			if (this.value==""||this.value==RandomKeyword114){
				this.value=RandomKeyword114;
				this.style.color='gray';
			}
		};
		
        this.SearchBoxBorder.appendChild(this.SearchBox);
        this.SearchButton = document.createElement("input");
        this.SearchButton.type = "button";
        this.SearchButton.value = "";
        this.SetStyle(this.SearchButton, "BingSearchButton");
       
        this.SearchButton.onclick = function() {
            BESLibrary.Search();
            return false
        };
        this.SearchBoxBorder.appendChild(this.SearchButton);
        this.SearchTypeContainer = document.createElement("div");
        this.SetStyle(this.SearchTypeContainer, "BingSearchTypeContainer");
        this.Container.appendChild(this.SearchTypeContainer);
        if (this.Config.Setting.SearchSite) {
            this.SearchInSite = document.createElement("input");
            this.SearchInSite.name = "BingBembeddedSearchType";
            this.SearchInSite.type = "radio";
            this.SearchInSite.value = "site";
            this.SearchInSite.id = "BingBembeddedSearchType_Site";
            this.SearchInSiteText = document.createElement("label");
            this.SearchInSiteText.innerHTML = this.CheckSiteInfo(this.Config.Setting.SiteName, "site");
            this.SearchInSiteText.setAttribute("for", "BingBembeddedSearchType_Site");
            this.SearchInSite.onclick = function() {
                document.getElementById("BingBembeddedSearchType_Site").checked=true;
                document.getElementById("BingBembeddedSearchType_Web").checked=false;
            };
            
            this.SearchWeb = document.createElement("input");
            this.SearchWeb.name = "BingBembeddedSearchType";
            this.SearchWeb.type = "radio";
            this.SearchWeb.value = "www";
            this.SearchWeb.id = "BingBembeddedSearchType_Web";
            this.SearchWebText = document.createElement("label");
            this.SearchWebText.innerHTML = "互联网";
            this.SearchWebText.setAttribute("for", "BingBembeddedSearchType_Web");
            this.SearchWeb.onclick = function() {
                document.getElementById("BingBembeddedSearchType_Site").checked=false;
                document.getElementById("BingBembeddedSearchType_Web").checked=true;
            };
            
            if (this.Config.Setting.SiteAsDefault) {
                this.CurrentSearchType = "Site";
                this.SearchTypeContainer.appendChild(this.SearchInSite);
                this.SearchTypeContainer.appendChild(this.SearchInSiteText);
                this.SearchTypeContainer.appendChild(this.SearchWeb);
                this.SearchTypeContainer.appendChild(this.SearchWebText);
                this.SearchInSite.checked = true
            } else {
                this.CurrentSearchType = "Web";
                this.SearchTypeContainer.appendChild(this.SearchWeb);
                this.SearchTypeContainer.appendChild(this.SearchWebText);
                this.SearchTypeContainer.appendChild(this.SearchInSite);
                this.SearchTypeContainer.appendChild(this.SearchInSiteText);
                this.SearchWeb.checked = true
            }
            
            if (!this.ScopeConfig[this.Config.Scopes[0]].InSiteSearch) this.SearchTypeContainer.style.display = "none";
        } else {this.CurrentSearchType = "Web";}
    },
    TabClicked: function(a) {
		var jump=this.Config.Scopes[a];
		if (jump=='Life' || jump=='Dao'){
			window.open(this.ScopeConfig[jump].Format);
			return;	
		}
        if (a == this.CurrentTabIndex) return;
        this.SetStyle(this.TabContainer.childNodes[this.CurrentTabIndex * 2], "BingTabNormal");
        this.CurrentTabIndex = a;
        this.SetStyle(this.TabContainer.childNodes[this.CurrentTabIndex * 2], "BingTabHighlight");
        var c = this.Config.Scopes[this.CurrentTabIndex],
        b = this.Config.Setting.SearchSite && this.ScopeConfig[c].InSiteSearch;
        this.SearchTypeContainer.style.visibility = b ? "visible": "hidden";
        this.SearchBox.focus()
    },
    Search: function() {
        var b = this.Config.Scopes[this.CurrentTabIndex],
        e = this.ScopeConfig[b].Format,
        c = this.ScopeConfig[b].InSiteSearch,
        d = this.GlobalConfig.ServerDNS,
        h = AntiXssLibrary.UrlEncode(this.Config.Setting.SourceSiteID),
	g=this.GlobalConfig.Form;
        a = this.SearchBox.value;
        if (this.SearchInSite && this.SearchInSite.checked==true)a = "site:" + this.Config.Setting.SiteURL + " " + a;
       // if (this.CurrentSearchType == "Site" && c) a = "site:" + this.Config.Setting.SiteURL + " " + a;
        //暂时无需转码
		//a = AntiXssLibrary.UrlEncode(a);
	//都进行UTF-8编码
	a=encodeURIComponent(a);
	i=AntiXssLibrary.UrlEncode(this.Config.Setting.NID);
        var i = this.FormatString(e, d, a, g, h , i);
        window.open(i)
    }
}