var search_engine_box;

var goole_website = "http://www.google.cn/";
var baidu_website = "http://www.baidu.com/";
var sogou_website = "http://www.sogou.com/";
var youdao_website = "http://www.youdao.com/";
var yisou_website = "http://www.yisou.com/";
var yahoo_website = "http://search.cn.yahoo.com/";
var zhongsou_website = "http://page.zhongsou.com/";
var xhbs_website = "http://www.xhbs.com/search/";
var xinhuabookshop_website = "http://www.xinhuabookshop.com/search/";
var xinhuabookstore_website = "http://www.xinhuabookstore.com/search/";
var xhbs_intranet_1_website = "http://10.1.2.41:7001/search/";
var xhbs_intranet_2_website = "http://10.1.2.40:8001/search/";

function create_http_request(){
	var xml_http;
	if(window.ActiveXObject){
		xml_http = new ActiveXObject("Microsoft.XMLHTTP");
	}else if(window.XMLHttpRequest){
		xml_http = new XMLHttpRequest();
	}
	return xml_http;
}

function getFrom(id) {
	var url = document.referrer;
	//var url = "http://10.1.2.41:7003/search/?keyword=%E9%81%93%E5%BE%B7";
	//var url = "http://10.1.2.41:7003/search/?keyword=%E4%BD%93%E8%82%B2";
	
	
	if(url != undefined && url.length > 0) {
		if(url.indexOf(xhbs_website)!=-1 || url.indexOf(xinhuabookshop_website)!=-1 || url.indexOf(xinhuabookstore_website)!=-1 
			|| url.indexOf(goole_website)!=-1 || url.indexOf(baidu_website)!=-1 || url.indexOf(sogou_website)!=-1 
			|| url.indexOf(youdao_website)!=-1 || url.indexOf(yisou_website)!=-1 || url.indexOf(yahoo_website)!=-1
			|| url.indexOf(zhongsou_website)!=-1 || url.indexOf(xhbs_intranet_1_website)!=-1 || url.indexOf(xhbs_intranet_2_website)!=-1) {
			
			var xmlHttp = create_http_request();
			xmlHttp.open("GET","/searchhistory/searchfrom.htm?url=" + encodeURIComponent(url), true);
			xmlHttp.onreadystatechange = function(){
				if(xmlHttp.readyState == 4){      
					if(xmlHttp.status == 200){
						var res = eval('(' + xmlHttp.responseText + ')');
						if(res!=undefined) {
							var keyword = res.keyword;
							var splitKeyword = res.splitKeyword;
							if(splitKeyword != undefined && splitKeyword.length > 0)
								fH2(document.body, splitKeyword);
							if(keyword != undefined && keyword.length > 0)
								displaySearchEngineBox(keyword, id);
						}
					}
				}
			}
			xmlHttp.send(null);  
		}
	}
}

function resetBoxPosition() {
    var box = document.getElementById("search_engine_box");
    var scrollPos = getScrollPos();
    box.style.left = document.body.scrollWidth-248+'px';
    box.style.top = document.documentElement.clientHeight+scrollPos-box.clientHeight+'px';
}

function displaySearchEngineBox(keyword, id) {
	search_engine_box = document.getElementById("search_engine_box");
	get_other_commodity(keyword, id);
}

function get_other_commodity(code, id){
	var url="/searchhistory/searchcommendcommodities.htm?pagination.pageSize=6&keyword=" + code; 
	url = encodeURI(url);
	var xml_http = create_http_request();
	xml_http.open("GET",url, true);
	xml_http.onreadystatechange = function(){
		if(xml_http.readyState == 4){         
			if(xml_http.status == 200){
				var list = eval('(' + xml_http.responseText + ')'); 
				if(list!=undefined && list.length!=undefined && list.length>0)
					load_other_commoditys(list, code, id);
			}
		}	
	};
	xml_http.send(null);
}

function load_other_commoditys(list, code, idTemp){
	var codetitle = code;
	if(code.length > 10)
		code = code.substring(0, 10) + "...";   
	var htmltxt = "<div style=\"float:left; width:214px; color:#000000; padding:2px;\"><span style=\"float:left; display:block; \">您正在搜索: </span><a style=\"float:right; display:block;color:#000;text-decoration:none;\"  href=\"javascript:;\" onclick=\"hide_box()\">[ X ]</a></div>";
	htmltxt += "<div title=\"" + codetitle +  "\" style=\"float:left; width:214px; font-family:'黑体'; font-size:16px; color:#000000; padding:2px;text-align:center;\">" + code + "</div>";
	htmltxt += "<div style=\"float:left; width:214px; font-size:12px; color:#333366; background-color:#D7D1BF; padding:3px; margin:2px 0 0 0; text-align:left;\">关键字已经在上面高亮显示</div>";
	if(list.length > 1){
		htmltxt += "<div style=\"float:left; width:214px; font-size:12px; color:#666666; padding:3px; margin:2px 0 0 0;  text-align:left;\">您可能对下列商品也感兴趣:</div>";
		htmltxt += "<div style=\"float:left; width:214px; font-size:12px; background-color:#ffffff; padding:3px; margin:2px 0 0 0;\">";
		
		for(var i=0; i<list.length; i++){
			var id = list[i].id;
			var name = list[i].name;
			var nametitle = name;
			if(name.length > 13)
				name = name.substring(0, 13) + "...";   
			if(id == idTemp) {
				continue;
			}
			htmltxt += "<div style=\"float:left; width:200px; padding-bottom:2px; padding-top:2px; padding-left:0px; text-align:left; text-valign:bottom;\">·<a  href=\"/product/" + id + "/\" target=\"_blank\" title=\"" + nametitle + "\" style=\"font-size:12px; color:#CC0000;text-decoration:none;\"><font style=\"font-size:12px; color:#CC0000;\">" + name + "</font></a></div>";
		}
		
		htmltxt += "</div>";
		htmltxt += "<div style=\"float:left; width:214px; font-size:14px; color:#666666; padding-top:6px; padding-right:2px; padding-bottom:2px; margin:2px 0 0 0; text-align:right;\"><a style=\"text-decoration:none;\" href=\"/search/?keyword=" + encodeURIComponent(code) + "\" target=\"_blank\" ><strong><font color=\"#000000\">更多商品>></font></strong></a></div>";
	}
	
	search_engine_box.innerHTML = htmltxt;
	search_engine_box.style.display = "block";
	resetBoxPosition();
}

function hide_box() {
	search_engine_box.style.display = "none";
}
