var http_request   = false;
var w2l_mouseOver  = false;
var w2lh_time      = 750;
var w2l_timerId    = false;
var w2l_activeId   = false;
var w2l_activeEl   = false;
var w2l_activePop  = false;
var w2l_activeUrl  = false;
function addEvent(objRef, eventType, functionToCall, captureSwitch) {
	if (objRef.addEventListener) {
		objRef.addEventListener(eventType, functionToCall, captureSwitch);
		return true;
	} else if (objRef.attachEvent) {
		return objRef.attachEvent("on"+eventType, functionToCall);
	} else {
		return w2l_close_on_error();
		return false;
	}
}
function removeEvent(objRef, eventType, functionToCall, captureSwitch) {
	if (objRef.removeEventListener) {
		objRef.removeEventListener(eventType, functionToCall, captureSwitch);
		return true;
	} else if (objRef.detachEvent) {
		return objRef.detachEvent("on"+eventType, functionToCall);
	} else {
		return false;
	}
}


function findPos(obj){
   var curleft = curtop = 0;
   if(obj.offsetParent){
      curleft = obj.offsetLeft
      curtop = obj.offsetTop
      while(obj = obj.offsetParent){
         curleft += obj.offsetLeft;
         curtop  += obj.offsetTop;
      }
   }
   return [curleft,curtop];
}


function calc_popup_position(mcoords){
   var xoff = yoff = xwin = ywin = xpos = ypos = 0;

   if(window.pageYOffset){
      yoff = window.pageYOffset;
   }else if(document.documentElement && document.documentElement.scrollTop){
      yoff = document.documentElement.scrollTop;
   }else if(document.body && document.body.scrollTop){
      yoff = document.body.scrollTop;
   }

   if(window.pageXOffset){
      xoff = window.pageXOffset;
   }else if(document.documentElement && document.documentElement.scrollLeft){
      xoff = document.documentElement.scrollLeft;
   }else if(document.body && document.body.scrollLeft){
      xoff = document.body.scrollLeft;
   }

   if(self.innerHeight){
      xwin = self.innerWidth;
      ywin = self.innerHeight;
   }else if (document.documentElement && document.documentElement.clientHeight){
      xwin = document.documentElement.clientWidth;
      ywin = document.documentElement.clientHeight;
   }else if(document.body){
      xwin = document.body.clientWidth;
      ywin = document.body.clientHeight;
   }

   if((mcoords[1] - yoff - w2l_info_win_height - 25) < 0){
     ypos = mcoords[1] + 25;
   } else {
     ypos = (mcoords[1] - (5 + w2l_info_win_height));
   }

   if((mcoords[0] - xoff + (w2l_info_win_width / 2)) > xwin){
     xpos = (xwin - 20 - w2l_info_win_width + xoff);	
   } else if((mcoords[0] - xoff - (w2l_info_win_width / 2)) < 0){
     xpos =  20 + xoff;
   } else{
     xpos = mcoords[0] - (w2l_info_win_width / 2) + 35;
   }

   return [xpos,ypos];
}


function init_w2l(){
   var link;
   var links = document.getElementsByTagName("a");
   for(var i=0;i<links.length;i++){
      link = links[i];
      if(link.className == "w2l"){
        link.className="w2l_active";        
        addEvent(links[i], "focus", w2l);
        addEvent(links[i], "blur", w2lh);
        addEvent(links[i], "mouseover", w2l);
        addEvent(links[i], "mouseout", w2lh);
     }
   }
}

function w2l(e){ 
   var targ;
   if(!e){
      var e = window.event;
   }
   if(e.target){
      targ = e.target;
   }else if(e.srcElement){
      targ = e.srcElement;
   }
   if(targ.nodeType == 3){
      targ  = targ.parentNode;
   }

   if(w2l_activeEl == targ){
       window.clearTimeout(w2l_timerId);
       return true;
   }else{
       w2lh_now();
   }

   w2l_activeUrl = targ.href; 
   w2l_activeId  = targ.id;
   w2l_activeEl  = targ;

   var thisDiv  = document.createElement("div");
   document.getElementsByTagName("body")[0].appendChild(thisDiv);
   if((BrowserDetect.browser == 'Explorer') && (BrowserDetect.version < 7)){
      thisDiv.className="w2lpopup_IE6";      
   }else{
      thisDiv.className="w2lpopup";
   }
   var thisHeadline = document.createElement("h2");
   var thisHlText   = document.createTextNode(w2l_HeadLine);
   thisHeadline.appendChild(thisHlText);     
   thisDiv.appendChild(thisHeadline);     

   var thisP1     = document.createElement("p");
   var thisP1Text = document.createTextNode(w2l_StartText);
   thisP1.appendChild(thisP1Text);     
   thisDiv.appendChild(thisP1);  

   var coords = findPos(targ);   
   var pcoords = calc_popup_position(coords);

   var pos_x = pcoords[0]  + "px";
   var pos_y = pcoords[1]  + "px";


   if(BrowserDetect.browser == 'Explorer'){	   
     thisDiv.style.position = "absolute";
     thisDiv.style.width    =  w2l_info_win_width + "px";
     thisDiv.style.height   =  w2l_info_win_height + "px";
     thisDiv.style.top      = pos_y;
     thisDiv.style.left     = pos_x;
     thisDiv.style.display  = "inline";
   }else{
     thisDiv.setAttribute("style","display: inline; position: absolute; width: " + w2l_info_win_width + "px; height: " + w2l_info_win_height + "px; top: " + pos_y + "; left: " + pos_x + "; padding: 0; margin: 0;");
   }

   w2l_activePop = thisDiv;

   http_request  = false;
   if(window.XMLHttpRequest){
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
         http_request.overrideMimeType('text/xml');
      }
   } else if(window.ActiveXObject){
      try {
	 http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
	  try {
	     http_request = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (e) {}
      }
   }

   if(!http_request){
      // error handling
      return false;
   }
   http_request.onreadystatechange = function(){
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
	    var xmldoc = http_request.responseXML;
	    if(!xmldoc){
	       return w2l_close_on_error();
	    }
	    if(xmldoc.documentElement){
	       var root_node = xmldoc.documentElement;
	    }else{
	       return w2l_close_on_error();
            }
	    if(!root_node){
	       return w2l_close_on_error();
	    }
	    var res_founds = root_node.getElementsByTagName('found');
	    if(!res_founds[0]){
	       return w2l_close_on_error();
	    }
	    
	    var res_found       = root_node.getElementsByTagName('found')[0].firstChild.nodeValue;
	    var res_url         = root_node.getElementsByTagName('url')[0].firstChild.nodeValue;
	    var res_target      = root_node.getElementsByTagName('target')[0].firstChild.nodeValue;
	    var res_title       = root_node.getElementsByTagName('title')[0].firstChild.nodeValue;
	    var res_description = root_node.getElementsByTagName('description')[0].firstChild.nodeValue;
	    
	    if(!res_found){
	       return w2l_close_on_error();
	    }

	    thisDiv.removeChild(thisP1);
  
	    if(BrowserDetect.browser == "Explorer"){
	       var thisA   = document.createElement("a");
	       thisA.href  = res_target;
	       thisA.className = "wl2_actPopLink";	   
	       var thisContentDiv  = document.createElement("div");
	       var thisAdvHeadline = document.createElement("h3");
	       var thisAdvHlText   = document.createTextNode(res_title);
	       thisAdvHeadline.appendChild(thisAdvHlText);     
	       thisContentDiv.appendChild(thisAdvHeadline);	    	  	    
	       var thisAdvP    = document.createElement("p");	    
	       var thisAdvText = document.createTextNode(res_description);
	       thisAdvP.appendChild(thisAdvText);
	       thisContentDiv.appendChild(thisAdvP);
	       var thisAdvFooter = document.createElement("p");
	       thisAdvFooter.className = "w2lurl"    	    
	       var thisAdvUrl    = document.createTextNode(res_url);	    
	       thisAdvFooter.appendChild(thisAdvUrl);
	       thisContentDiv.appendChild(thisAdvFooter);
	       thisA.appendChild(thisContentDiv);
	       thisDiv.appendChild(thisA);
	    }else{
	       var thisContentDiv  = document.createElement("div");
	       var thisAdvHeadline = document.createElement("h3");
	       var thisAdvHlText   = document.createTextNode(res_title);
	       thisAdvHeadline.appendChild(thisAdvHlText);     
	       thisContentDiv.appendChild(thisAdvHeadline);	    	  	    
	       var thisAdvP    = document.createElement("p");	    
	       var thisAdvText = document.createTextNode(res_description);
	       thisAdvP.appendChild(thisAdvText);
	       thisContentDiv.appendChild(thisAdvP);
	       var thisAdvFooter = document.createElement("p");
	       thisAdvFooter.className = "w2lurl"    	    
	       var thisAdvUrl    = document.createTextNode(res_url);	    
	       thisAdvFooter.appendChild(thisAdvUrl);
	       thisContentDiv.appendChild(thisAdvFooter);
	       thisDiv.appendChild(thisContentDiv);
	       addEvent(thisContentDiv, "mouseout", w2l_cursor_to_default);
	       addEvent(thisContentDiv, "mouseover", w2l_cursor_to_pointer);
	       addEvent(thisContentDiv, "click", w2l_jump_to);
	    }


	    addEvent(thisDiv, "mouseover", set_w2l_mouseOverOn);
	    addEvent(thisDiv, "mouseout", w2lh_active); 
	 }else{
	    return w2l_close_on_error();
	 }
      }
   };
   var thisDate = new Date();
   var epoch    = thisDate.getTime();
   http_request.open('GET', ajax_url + "?cmd=" + ajax_cmd + "&act=word_to_link&id=" +  targ.id + "&timestamp=" + epoch, true);
   http_request.send(null);

   return true;
}

function set_w2l_mouseOverOn(){
   if(w2l_timerId){
     window.clearTimeout(w2l_timerId);
   }
   w2l_mouseOver = "on";   
   return true;
}

function set_w2l_mouseOverOff(){
   w2l_mouseOver = "off";
   return true;
}

function w2lh_active(){
   set_w2l_mouseOverOff();
   w2l_timerId  = setTimeout("w2lh_now()", w2lh_time);
   return true;
}
function w2lh(){
   if(!w2l_activePop){
      return false;
   }
   if(w2l_mouseOver == "on"){
      return true;
   }
   w2l_timerId  = setTimeout("w2lh_now()", w2lh_time);
   return true;
}

function w2lh_now(){
   if(!w2l_activePop){
      return false;
   }
   if(w2l_timerId){
     window.clearTimeout(w2l_timerId);
     w2l_timerId = "";
   }
   if(w2l_activePop){
     var pNode  = document.getElementsByTagName("body")[0];
     pNode.removeChild(w2l_activePop);
     w2l_activeId  = "";
     w2l_activeEl  = "";
     w2l_activePop = "";
   }
   return true;

}
function w2l_close_on_error(){
   w2lh_now_all();
   var link;
   var links = document.getElementsByTagName("a");
   for(var i=0;i<links.length;i++){
      link = links[i];
      if(link.className == "w2l_active"){
        link.className="w2l";
        removeEvent(links[i], "focus", w2l);
        removeEvent(links[i], "blur", w2lh);
        removeEvent(links[i], "mouseover", w2l);
        removeEvent(links[i], "mouseout", w2lh);
     }
   }
   return false;
}

function w2lh_now_all(){
   var pNode  = document.getElementsByTagName("body")[0];
   var divs   = document.getElementsByTagName("div");
   var this_div = false;
   for(var i=0;i<divs.length;i++){
      this_div = divs[i];
      if(this_div.className == "w2lpopup"){
	 pNode.removeChild(this_div);
      }
   }
   w2l_activeId = ""
   w2l_activeEl = "";
   w2l_activePop = "";
   set_w2l_mouseOverOff();
   return true;
}

function w2l_cursor_to_default(e){
    if(!e){
      var e = window.event;
   }
   if(e.target){
      targ = e.target;
   }else if(e.srcElement){
      targ = e.srcElement;
   }
   if(targ.nodeType == 3){
      targ  = targ.parentNode;
   }
   return true;
}
function w2l_cursor_to_pointer(e){
    if(!e){
      var e = window.event;
   }
   if(e.target){
      targ = e.target;
   }else if(e.srcElement){
      targ = e.srcElement;
   }
   if(targ.nodeType == 3){
      targ  = targ.parentNode;
   }
   if(BrowserDetect.browser == 'Explorer'){	   
     targ.style.cursor = "pointer";
   }else{
     targ.setAttribute("style","cursor: pointer;");
   }
   return true;
}

function w2l_jump_to(){
   window.location.href = w2l_activeUrl;
   return false;
}

if(window.addEventListener){
   addEventListener("load",init_w2l,false);
}else{
   attachEvent("onload",init_w2l);
}


