var wikilinkno;
var xmlHttp;
var currentPos = null;
var dataDiv;
var dataTable;
var dataTableBody;
var curX=0,curY=0;
var gDivMoveFlag = false;//是否需要移动DIV
var gDisplayFlag = false;//是否需要钉住DIV
var gUpdateFlag = true;//是否需要更新DIV中的数据
var gDoctitle = null;//当前条目的标题
var gCurWidth=0,gCurHeight=0;//鼠标和DIV的坐标的差值。
window.onload=initVars;
var linktype=0;

function send_request(url) {//初始化、指定处理函数、发送请求的函数
  xmlHttp = false;
  //开始初始化XMLHttpRequest对象
  createXMLHttpRequest();
  xmlHttp.onreadystatechange = processRequest;
  // 确定发送请求的方式和URL以及是否同步执行下段代码
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
}

function handle_mouseover_IE()
{
  if (!gUpdateFlag)  return;
  showDocSummary_IE(this,this.getAttribute("linktype"));
  gUpdateFlag = false;
}

 
function handle_mouseover()
{
  if (!gUpdateFlag) 
    return;
  showDocSummary(this.getAttribute("linktype"));
  gUpdateFlag = false;
}

// 处理返回信息的函数
  function processlinks() {
      //取得条目内容里的所有内部链接
      var links=document.links;
      var total=links.length;
      for (i=0;i<total;i++){
         var vhref=null;
         try{
           vhref=links[i].href;
           if(IE){cssName=links[i].getAttribute("className");}else{cssName=links[i].getAttribute("class");}
           if(cssName!="innerlink"&&cssName!="outerlink")continue;
         } catch (e){continue;}
         if(vhref!=null){
         	linktype=0;
         	if (vhref.indexOf("doc.php?action=view&title=")!=-1) linktype=1;	
          	if (vhref.indexOf("http://www.hoodong.com/wiki/")!=-1) linktype=2;
          	if(linktype!=0){
            	wikilinkno="wikilink"+i;  
	            if(IE==true){
	            	links[i].onmouseover=handle_mouseover_IE;
	               	links[i].setAttribute("target","_blank");
	               	links[i].setAttribute("linktype",linktype);
	               	links[i].onmouseout=function (){gUpdateFlag = true;}
	            }else{
	               	links[i].setAttribute("target","_blank");
	               	links[i].setAttribute("linktype",linktype);
	               	links[i].setAttribute("id",wikilinkno);
	               	links[i].onmouseover = handle_mouseover;
	               	links[i].onmouseout=function (){gUpdateFlag = true;}
	            }
	        }
	        linktype=0;
         }
      }
  }


        function initVars() {
          if (document)
            dataTableBody = document.getElementById("summaryDataBody");           
            dataTable = document.getElementById("summaryData");
            dataDiv = document.getElementById("popup");
            dataDiv.onmouseleave = onMouseLeave;  
            dataDiv.onmousedown = onMouseDown;
            dataDiv.onmouseup = onMouseUp;
            document.onmousemove = onMouseMove;
            processlinks();
        }
        
        //当鼠标离开时，除钉住或正在移动外，关闭DIV
        function onMouseLeave(){
          if (gDisplayFlag || gDivMoveFlag)
            return ; 
            clearData();
        }
        //当鼠标在DIV中按下时，1.移动DIV标记打开，2.记录鼠标和DIV的坐标的差值，3.更改鼠标的形状，4.禁止鼠标选中对象
        function onMouseDown(){
          gDivMoveFlag = true;
          if(IE==true)
            getCursor(event);
          else {  
            try {
             window.constructor.prototype.__defineGetter__("event", window_prototype_get_event);
            }catch(e) {}
          
            curX = event.pageX;
            curY = event.pageY; 
          }
          gCurWidth = curX - parseInt(dataDiv.style.left);
          gCurHeight = curY - parseInt(dataDiv.style.top);//alert(gCurWidth+"  "+gCurHeight);
          dataDiv.style.cursor="hand";
          document.onselectstart=new Function("event.returnValue=false");
        }
        //当鼠标在DIV中松开时，1.移动DIV标记关闭，2.更改鼠标的形状，3.取消禁止鼠标选中对象
        function onMouseUp(){
          gDivMoveFlag = false;
          dataDiv.style.cursor="default";
          document.onselectstart=new Function("event.returnValue=true");
        } 
        //当鼠标移动并且移动DIV标记打开时，移动DIV
        function onMouseMove(){       
          if (!gDivMoveFlag)
            return ; 
          if(IE==true)
            getCursor(event);
          else {  
            try {
             window.constructor.prototype.__defineGetter__("event", window_prototype_get_event);
            }catch(e) {}
          
            curX = event.pageX;
            curY = event.pageY; 
          }
          dataDiv.style.left = (curX-gCurWidth) + "px";
          dataDiv.style.top = (curY-gCurHeight) + "px";
        }
        function showDocSummary(ltype){
          try {
           window.constructor.prototype.__defineGetter__("event", window_prototype_get_event);
          }catch(e) {}
        
          curX = event.pageX;
          curY = event.pageY;

	    	var tempObj = event.target;
			var docTitle=tempObj.firstChild.data;
			if (0 == docTitle.Trim().length)
				return;
			while(tempObj.nodeName.toUpperCase() != "A"){
				tempObj = tempObj.parentNode;
			}  
		
		  createXMLHttpRequest();
		  offsetEl = tempObj;
		  var vhref=tempObj.href;
		  
		  if(ltype=='1'){
            	if (vhref.substring(vhref.lastIndexOf("=")+1,vhref.length) != docTitle){
					tempObj.href = vhref.substring(0,vhref.lastIndexOf("=")+1) + docTitle; 
				}
          }else if(ltype=='2'){
            	if (vhref.substring(vhref.lastIndexOf("/")+1,vhref.length) != docTitle){
					tempObj.href = vhref.substring(0,vhref.lastIndexOf("/")+1) + docTitle; 
				}	
          }
          
          var str =""; 
			var url = "";
	        
	        if(ltype=='1'){
	        	str = tempObj.href.substring(vhref.indexOf("title=")+6,vhref.length);
	           	url = "doc.php?action=insummary&title="+ str;
	        }
	        
	        if(ltype=='2'){
	        	str = tempObj.href.substring(vhref.lastIndexOf("/")+1,vhref.length);
	        	url = "doc.php?action=outsummary&title="+ str;
	        }
  
	        
			if (docTitle == gDoctitle && dataDiv.style.visibility == "visible")
				return;
			gDoctitle = docTitle;
					
          xmlHttp.open("GET", url, true);
          xmlHttp.onreadystatechange = callback;
          xmlHttp.send(null);
        }
        
        function window_prototype_get_event()
        {
         var func = arguments.callee.caller; 
         var evt; 
         while(func != null){
          evt = func.arguments[0]; 
          if(evt && (evt.constructor == Event || evt.constructor == MouseEvent)) return evt; 
          func = func.caller; 
         } 
         return null; 
        }       
              
        function showDocSummary_IE(element,ltype) {

        	getCursor(event);           
            createXMLHttpRequest();
            offsetEl = element;
            
            var FLAG = "";
            
            if(ltype=='1'){
            	FLAG = "doc.php?action=view&title=";
            }
            
            if(ltype=='2'){
            	FLAG = "http://www.hoodong.com/wiki/";
            }
                       
        	var vhref = element.href;
        	var beginPos = vhref.indexOf(FLAG);
        	if (beginPos < 0)
        		return;
        	var docTitle=element.innerText.Trim();

			if (docTitle == gDoctitle && dataDiv.style.visibility == "visible")
				return;
			gDoctitle = docTitle;  

  			var str =""; 
  			var url = "";
            
            if(ltype=='1'){
            	url = "doc.php?action=insummary&title="+ encodeURI(gDoctitle);
            }
            
            if(ltype=='2'){
            	url = "doc.php?action=outsummary&title="+ encodeURI(gDoctitle);
            }
            
            xmlHttp.open("GET", url, true);
            xmlHttp.onreadystatechange = callback;
            xmlHttp.send(null);
        }
        
        function callback() {
           if (xmlHttp.readyState == 4) {
             if (xmlHttp.status == 200) {
                 setData(xmlHttp.responseXML);
             }
           }
        }
        

        function setData(courseData) {            
            var ind = dataTableBody.childNodes.length;            
            for (var i = ind - 1; i >= 0 ; i--) {
                dataTableBody.removeChild(dataTableBody.childNodes[i]);       
            }
            
            var flag = xmlHttp.responseXML.getElementsByTagName("flag")[0].firstChild.data;
            var docid = xmlHttp.responseXML.getElementsByTagName("docid")[0].firstChild.data;
            var doctitle = xmlHttp.responseXML.getElementsByTagName("doctitle")[0].firstChild.data;
            var summary=xmlHttp.responseXML.getElementsByTagName("summary")[0].firstChild.data;
            var firstimg=xmlHttp.responseXML.getElementsByTagName("firstimg")[0].firstChild.data;
            var xmllinktype=xmlHttp.responseXML.getElementsByTagName("linktype")[0].firstChild.data;

            var outflag="",outdocid="",outsummary="",outfirstimg="";
            
            if(xmllinktype=='1') {
            	outflag = xmlHttp.responseXML.getElementsByTagName("outflag")[0].firstChild.data;
	            outdocid = xmlHttp.responseXML.getElementsByTagName("outdocid")[0].firstChild.data;
	            outsummary=xmlHttp.responseXML.getElementsByTagName("outsummary")[0].firstChild.data;
	            outfirstimg=xmlHttp.responseXML.getElementsByTagName("outfirstimg")[0].firstChild.data;
            }
            
            var doctitleData = "";
            var summaryData = "";
            var firstimgData="";
            
            //Add head information
            closeData = constructHead(xmllinktype);
            dataTableBody.appendChild(createRow(closeData));
            
            //Add content information
            if(flag=="false"){
              dataTable.setAttribute("width", "200");
              setOffsets();
              if(xmllinktype=='1') {
              	linkdata="doc.php?action=createdoc&title="+gDoctitle;
              }
              if(xmllinktype=='2') {
              	linkdata="http://www.hoodong.com/createDocAuth.do?doc_title="+encodeURI(gDoctitle);
              }
              if(xmllinktype=='1' && outflag=='true'){
              	doctitleData="<tr><td width=\"300\" bgcolor=\"#F9F7F7\" align=\"center\"><br><a target='_blank' href=\""+linkdata+"\"><font size='2' color='red' >本站条目不存在,请点击创建!</font></a><br><br><font size='2' color='red' ><b>以下为互动维客百科条目参考结果(<a class='e' href='http://www.hoodong.com/wiki/"+encodeURIComponent(doctitle)+"' target='_blank'>查看</a>)</b></font><br>";
                var tbldata="";
                tbldata=tbldata+"<table border='0'><tr>";
                
                if(outsummary=='null') outsummary='无';
                
                if(outfirstimg!='null'){
                	tbldata=tbldata+"<td align='center'><img src=\""+ outfirstimg +"\" width=\"84\" height=\"64\" vspace=\"10\" />";
                	tbldata=tbldata+"</tr></td><tr align='left'><td>";
                	tbldata=tbldata+"<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>摘要：</b>"+ outsummary +"...";
                }else{
                	tbldata=tbldata+"<td align='left'><br>&nbsp;&nbsp;&nbsp;&nbsp;<b>摘要：</b>"+ outsummary +"...";
                }
                
                tbldata=tbldata+"</tr></td></table>";
                
                doctitleData=doctitleData+tbldata+"</td></tr>";
                
              }else{
              	doctitleData="<tr><td width=\"300\" bgcolor=\"#F9F7F7\" align=\"center\"><br><br><a target='_blank' href=\""+linkdata+"\"><font size='2' color='red' >条目不存在,请点击创建!</font></a><br><br><br></td></tr>";
              }
              dataTableBody.appendChild(createRow(constructContent(doctitleData, "")));
            } 
  
            if(flag=="true"){
              dataTable.setAttribute("width", "300");
              setOffsets();
			  //Add image information
              if(firstimg!="null"){
                firstimgData = constructIamge(firstimg);
              }else{
              	firstimgData="";
              }
              
              //Add content information              
              summaryData = constructSummary(summary);         			
         	  dataTableBody.appendChild(createRow(constructContent(firstimgData, summaryData)));
            }
            dataDiv.style.visibility ="visible";
        }
        //Add head information
        function constructHead(linktype1) { 
            var closeData = "";
            var doctitle = "";
            
            if(linktype1=='1') {
            	doctitle = (decodeURI(gDoctitle).length > 10)? (decodeURI(gDoctitle).substring(0, 10)+"...") : decodeURI(gDoctitle);
              	linkdata1="doc.php?action=view&title="+encodeURI(gDoctitle);
            }
            if(linktype1=='2') {
            	doctitle = (gDoctitle.length > 10)? (gDoctitle.substring(0, 10)+"...") : gDoctitle;
             	linkdata1="http://www.hoodong.com/wiki/"+encodeURI(gDoctitle);
            }
            
			var imgscr = (gDisplayFlag == true)? "templates/default/images/tudinged_normal.gif" :"templates/default/images/tuding_normal.gif";
            if (IE)
            	closeData = "<td><table width=\"315\"  border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td width=\"4\" bgcolor=\"#D1E7FC\"></td><td width=\"19\" bgcolor=\"#D1E7FC\"><div align=\"center\"><img src=\"templates/default/images/momo_3.gif\" width=\"13\" height=\"13\" /></div></td><td width=\"252\" bgcolor=\"#D1E7FC\" class=\"momozth\"><a target='_blank' href=\""+linkdata1+"\"><font color=\"#ff0000\">"+ gDoctitle +"</font></a></td><td width=\"145\" bgcolor=\"#D1E7FC\">&nbsp;</td><td width=\"21\" bgcolor=\"#D1E7FC\"><img src=\""+ imgscr +"\" onmouseover=\"changImgholdStatus_IE(this)\" onmouseout=\"changImgholdStatus_IE(this)\" onclick=\"changImgholdType_IE(this);\" width=\"12\" height=\"10\" /></td><td width=\"23\" bgcolor=\"#D1E7FC\"><img src=\"templates/default/images/guanbi_normal.gif\" onmouseover=\"changImgcloseStatus_IE(this)\" onmouseout=\"changImgcloseStatus_IE(this)\" onclick=\"clearData()\" width=\"14\" height=\"14\" /></td><td width=\"4\" bgcolor=\"#D1E7FC\"></td></tr></table></td>";
            else  
	            closeData = "<td><table width=\"315\"  border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td width=\"4\" bgcolor=\"#D1E7FC\"></td><td width=\"19\" bgcolor=\"#D1E7FC\"><div align=\"center\"><img src=\"templates/default/images/momo_3.gif\" width=\"13\" height=\"13\" /></div></td><td width=\"152\" bgcolor=\"#D1E7FC\" class=\"momozth\"><a target='_blank' href=\""+linkdata1+"\"><font color=\"#ff0000\">"+ gDoctitle +"</font></a></td><td width=\"145\" bgcolor=\"#D1E7FC\">&nbsp;</td><td width=\"21\" bgcolor=\"#D1E7FC\"><img src=\""+ imgscr +"\" onmouseover=\"changImgholdStatus()\" onmouseout=\"changImgholdStatus()\" onclick=\"changImgholdType();\" width=\"12\" height=\"10\" /></td><td width=\"23\" bgcolor=\"#D1E7FC\"><img src=\"templates/default/images/guanbi_normal.gif\" onmouseover=\"changImgcloseStatus()\" onmouseout=\"changImgcloseStatus()\" onclick=\"clearData()\" width=\"14\" height=\"14\" /></td><td width=\"4\" bgcolor=\"#D1E7FC\"></td></tr></table></td>";
            return closeData;
            
        }
        function constructIamge(v_imgscr) { 
          var firstimgData ="";
          if(v_imgscr != "null"){
          	firstimgData ="<tr><td colspan=\"2\" bgcolor=\"#F9F7F7\"  align=\"center\"><img src=\""+ v_imgscr +"\" width=\"84\" height=\"64\" vspace=\"10\" /></td></tr>";
          }
          return firstimgData
        }
        function constructSummary(v_summary) {
          var summaryData=""; 
          if(v_summary == "null") 
             summaryData = "<tr><td width=\"4\" bgcolor=\"#F9F7F7\"></td><td bgcolor=\"#F9F7F7\" class=\"momozt12\"><br>&nbsp;&nbsp;&nbsp;&nbsp;<b>摘要：</b>无</td></tr>";
          else   
             summaryData = "<tr><td width=\"4\" bgcolor=\"#F9F7F7\"></td><td bgcolor=\"#F9F7F7\" class=\"momozt12\"><br>&nbsp;&nbsp;&nbsp;&nbsp;<b>摘要：</b>"+ v_summary +"...</td></tr>";
          return summaryData;    
        } 
        function constructContent(v_img, v_summary) {
        	var head = "<tr><td colspan=\"8\" valign=\"top\" background=\"templates/default/images/momobg.gif\"><table width=\"98%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td height=\"3\"></td></tr><tr><td><table class=\"biankuangmomo\" width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
   				var end = "<tr><td height=\"4\" colspan=\"2\" bgcolor=\"#F9F7F7\"></td></tr></table></td></tr><tr><td height=\"2\"></td></tr></table></td>";
   				return head+v_img+v_summary+end;
        }               
        function createRow(v_data) {            
            var row, cell, txtNode;
            row = document.createElement("tr");
            cell = document.createElement("td");
            cell.innerHTML=v_data;
            row.appendChild(cell);
            return row;  
        }
        
        function setOffsets() {
          if (gDisplayFlag)
            return;
            dataDiv.style.left = (curX-50) + "px";
            dataDiv.style.top = curY +5+ "px";
        }
        
        function clearData() {         
            var ind = dataTableBody.childNodes.length;            
            for (var i = ind - 1; i >= 0 ; i--) {
                dataTableBody.removeChild(dataTableBody.childNodes[i]);       
            }
            dataDiv.style.visibility ="hidden";
            gDisplayFlag =false;
        }        
function changImgcloseStatus_IE(v_scr)
{
	var imgScr = v_scr.src; window.status=imgScr;
	var head = imgScr.substring(0, imgScr.lastIndexOf("/")+1);

	if (imgScr.substring(imgScr.lastIndexOf("/")+1, imgScr.length) == "guanbi_normal.gif")
		v_scr.src = head + "guanbi_over.gif";
	else
		v_scr.src = head + "guanbi_normal.gif";
}
function changImgholdStatus_IE(v_scr)
{
	var imgScr = v_scr.src;
	var head = imgScr.substring(0, imgScr.lastIndexOf("_")+1);

	if (imgScr.substring(imgScr.lastIndexOf("_")+1, imgScr.length) == "normal.gif")
		v_scr.src = head + "over.gif";
	else
		v_scr.src = head + "normal.gif";
}
function changImgholdType_IE(v_scr)
{
	gDisplayFlag = (gDisplayFlag == true)? false :true;
	
	var imgScr = v_scr.src;
	var head = imgScr.substring(0, imgScr.lastIndexOf("/")+1);
  var imgName = imgScr.substring(imgScr.lastIndexOf("/")+1, imgScr.length);
	if (imgName == "tudinged_over.gif")
		v_scr.src = head + "tuding_over.gif";
	else
		v_scr.src = head + "tudinged_over.gif";
}
function changImgcloseStatus()
{
  try {
   window.constructor.prototype.__defineGetter__("event", window_prototype_get_event);
  }catch(e) {} 
  var v_scr = event.target;
   
	var imgScr = v_scr.src;
	var head = imgScr.substring(0, imgScr.lastIndexOf("/")+1);

	if (imgScr.substring(imgScr.lastIndexOf("/")+1, imgScr.length) == "guanbi_normal.gif")
		v_scr.src = head + "guanbi_over.gif";
	else
		v_scr.src = head + "guanbi_normal.gif";
}
function changImgholdStatus()
{
	try {
   window.constructor.prototype.__defineGetter__("event", window_prototype_get_event);
  }catch(e) {} 
  var v_scr = event.target;
  
	var imgScr = v_scr.src;
	var head = imgScr.substring(0, imgScr.lastIndexOf("_")+1);

	if (imgScr.substring(imgScr.lastIndexOf("_")+1, imgScr.length) == "normal.gif")
		v_scr.src = head + "over.gif";
	else
		v_scr.src = head + "normal.gif";
}
function changImgholdType()
{
	gDisplayFlag = (gDisplayFlag == true)? false :true;
	
	try {
   window.constructor.prototype.__defineGetter__("event", window_prototype_get_event);
  }catch(e) {} 
  var v_scr = event.target;
  	
	var imgScr = v_scr.src;
	var head = imgScr.substring(0, imgScr.lastIndexOf("/")+1);
  var imgName = imgScr.substring(imgScr.lastIndexOf("/")+1, imgScr.length);
	if (imgName == "tudinged_over.gif")
		v_scr.src = head + "tuding_over.gif";
	else
		v_scr.src = head + "tudinged_over.gif";
}
