var indicator;
var display;
var codes;
var idTemp;

function Event(e){
    var oEvent = document.all ? window.event : e;
    if (document.all) {
        if(oEvent.type == "mouseout") {
            oEvent.relatedTarget = oEvent.toElement;
        } else if(oEvent.type == "mouseover") {
            oEvent.relatedTarget = oEvent.fromElement;
        }
        oEvent.stopPropagation = function() { this.cancelBubble = true; }
    }
    return oEvent;
}

function contains(o1, o2){
	return (o1.contains ? o1.contains(o2)||o1==o2 : o1.compareDocumentPosition(o2)&16);
}

function init(id){
	indicator = document.getElementById("indicator");
	display = document.getElementById("display");
	
	display.onmouseout = function(e){
		 if(!contains(this, Event(e).relatedTarget)){
			  indicator.style.display = "none";
			  display.style.display = "none";
	     }
	}
	idTemp = id;
	getKeywords();
}

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 getKeywords(){
	var url = "/searchhistory/getkeywords.htm"; 
	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 keywords = eval('(' + xml_http.responseText + ')'); 
				if(keywords!=undefined && keywords.length!=undefined && keywords.length>0)
					loadAndReplacekeywords(keywords);
			}
		}	
	};
	xml_http.send(null);
}

function loadAndReplacekeywords(keywords){
	codes = keywords.split(",");
	var contentDiv = document.getElementById("content");
	if(contentDiv != undefined) {
		var content = contentDiv.innerHTML;
		for (var j = 0; j < codes.length; j++) {
			var code = codes[j];
			content = content.replace(code, "<b><a style=\"color:#000;font-size:10pt;text-decoration:none;\" href='/search/?keyword=" + encodeURIComponent(code) + "' onmouseover=\"displayDiv('"+code+"', event)\" onmouseout=\"disableDiv( event)\" target='_blank'>" + code + "</a></b>");
		}
		contentDiv.innerHTML = content;
	}
}

function disableDiv(event) {
	var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;	
	var y = event.clientY + scrollTop + 'px';
	var x = event.clientX + 'px';
	if(x<display.style.left || y<display.style.top) {
		indicator.style.display = "none";
		display.style.display = "none";
	}
}

function displayDiv(code, event){
	var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;	
	
	display.style.top = event.clientY + scrollTop + 'px';
	display.style.left = event.clientX + 'px';
	
	code = encodeURIComponent(code);
	get_commodity(code);
}

function disabledisplay(event){
	indicator.style.display = "none";
	display.style.display = "none";
}

function get_commodity(code){
	indicator.style.width = "200";
	indicator.style.height = "100";
	indicator.style.top = 50;
	indicator.style.left = 50; 
	indicator.style.backgroundColor="#FFFFFF";
	indicator.style.display = "block"; 	

	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){
				indicator.style.display="none"; 
				var list = eval('(' + xml_http.responseText + ')'); 
				if(list!=undefined&&list.length!=undefined&&list.length>0)
					load_commoditys(list, code);
			}
		}	
	};
	xml_http.send(null);
}

function load_commoditys(list, code){
	display.style.position="absolute";
	display.style.display="block";
	var htmltxt = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"color:#990000\"><tr><td align=\"left\" style=\"border-bottom:1px dashed #cccccc;\"><font style=\"font-size:16px; color:#111111; \">相关书籍:</font></td></tr>";
	for(var i=0; i<list.length; i++){
		var id = list[i].id;
		var name = list[i].name;
		var nametitle = name;
		if(name.length > 9)
			name = name.substring(0, 8) + "...";
		if(idTemp!=undefined) {
			if(id == idTemp) {
				continue;
			}
		}
		htmltxt = htmltxt + "<tr><td align=\"left\" style=\"border-bottom:1px dashed #cccccc;\"><font style=\"font-size:20px; color:#CC0000; \">· </font><a style=\"text-decoration:none;\" href=/product/" + id + "/ target='_blank' title=" + nametitle + "><font style=\"font-size:12px; color:#CC0000; \">" + name + "</a></font></td></tr>";
	}
	htmltxt = htmltxt + "<td align=\"right\" style=\" dashed #cccccc; vertical-align:bottom; height:20px \" ><a style=\"text-decoration:none;\" href=/search/?name=" + code +" target='_blank'><font style=\"font-size:12; color:#000000; \">更多书籍...</a></font></td></tr>";
	htmltxt = htmltxt+"</table>";
	document.getElementById("display").innerHTML = htmltxt;
}