// global variable representing the root of my site
var baseURL = "http://www.intelligence.tuc.gr/~spyros/elab/";

/****************************************************************************** 
* create and return the asynchronous requests objects 
*******************************************************************************/ 
function getXMLHttpRequest() {
  try {
      xmlHttp = new XMLHttpRequest();
      
   } catch (trymicrosoft) {
      try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");      
      } catch (othermicrosoft) {
        try {
           xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");           
        } catch (failed) {
          xmlHttp = false;          
        }
    }
   }

   if (!xmlHttp) {
      alert("Error initializing XMLHttpRequest!");  
      xmlHttp = null;
   }
   
   return xmlHttp;
}

/****************************************************************************** 
* show/update the abstract div
* 
* id -  the id of the span tag than contains the publication title. It is actually
*	the id of the publication in the database
*
* offset - an offset for the X possition of the div that will show the abstract
* 	   body, i.e. the pop-up window (it is a div actually) that will be appeared 
*	   when clicking on an abstract link.
*
* action - the url describing the servlet (or the php page) that will retrieve 
*	   the abstract body from the database and will generate the html code
*	   that will present it. It generates actually the html code that will be
*	   displayed in the pop-window (it is a div actually)
* example:
* <span id='abstract-16' onclick="showAbstract('16', '-100', 'showAbstract.php?id=16'');">title</span 
*******************************************************************************/ 
function showAbstract(id, offset, action ) {
  // get mouse xPos and mouse yPos when mouse click occurs
  var abstractId = 'abstract-' + id;
  
  var obj = d.getElementById(abstractId);
	
  var coors = findPos(obj);  
  xPos = coors[0] - ((screen.width/1280)*offset) - 25; 
  yPos = coors[1] + 17;
  
  xmlHttp_Abstract = getXMLHttpRequest();	
  // Build the URL to connect to
  var url = baseURL + action;  
  var currentDate = new Date();
  
  //add current date, time in millisecods to avoid the caching problem
  //url += "&ms=" + currentDate.getTime(); 
  
  // Open a connection to the server
  xmlHttp_Abstract.open("GET", url, true); 
  // Setup a function for the server to run when it's done
  xmlHttp_Abstract.onreadystatechange = updateAbstract;

  // Send the request
  xmlHttp_Abstract.send(null);    
}

/****************************************************************************** 
* the callback function that will be called automatically when the request made
* by the previous function call has been completed
*******************************************************************************/ 
function updateAbstract() {
  if (xmlHttp_Abstract.readyState == 4) {
    if (xmlHttp_Abstract.status == 200) {      
      var response = xmlHttp_Abstract.responseText;       

      // abstractPresentation is a hidden div tag. It is actually the 
      // pop-up window into which the text of the abstract will be presented
      var abstractDiv = d.getElementById("abstractPresentation");
            
      abstractDiv.innerHTML = response;        

      abstractDiv.style.left = xPos + 'px';
      abstractDiv.style.top = yPos + 'px';
      abstractDiv.style.visibility = 'visible';                         
    }	
    else if (xmlHttp_Abstract.status == 404) {
      alert("Request URL does not exist");
    }
    else {
      alert("Error: status code is " + xmlHttp_Abstract.status);
    }
  }
}
/******************************************************************************/

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 exitAbstract() {
	var abstractDiv = d.getElementById("abstractPresentation");

	abstractDiv.style.visibility = 'hidden';
}
