function populateTitleInfoPopup(div2Populate)
{
  //var url = "/modules/popups/ajax_servers/get_ttitle_data.php";
	var url = "/modules/popups/ajax_servers/get_title_data.php";
  var params = 'tefno=' + div2Populate;

  //alert(params);
  var ajaxUpdater = new Ajax.Updater
  (
    div2Populate,
    url,
    {
      method: 'post',
      postBody: params,
			onFailure: reportError
    }
		);
    //Return empty string to get rid of undefined string in calling div
    return "";
}
 

function populateThumbnailPopup(div2Populate, image_id)
{
  //var url = "/modules/popups/ajax_servers/get_ttitle_data.php";
	var url = "/modules/popups/ajax_servers/get_thumbnail_data.php";
  var params = 'image_id=' + image_id;

  //alert(params);
  var ajaxUpdater = new Ajax.Updater
  (
    div2Populate,
    url,
    {
      method: 'post',
      postBody: params,
			onFailure: reportError
    }
		);
    //Return empty string to get rid of undefined string in calling div
    return "";
}

	var myGlobalHandlers = 
  {
		onCreate: function()
    {
 			Element.show(systemWorking);
		},

		onComplete: function()
    {
			if(Ajax.activeRequestCount == 0)
      {
				Effect.BlindDown(div2Populate);
        Element.hide(systemWorking);
			}
		}
	};
  
  Ajax.Responders.register(myGlobalHandlers);
	
	function reportError(request)
	{
			Element.hide(systemWorking);
			div2Populate.innerHTML = "<H1>Problem connecting to the server</H1>Sorry it is not possible to get a connection to the server for the moment, please try hovering the mouse pointer over the book cover again.";
			Effect.BlindDown(div2Populate);
	}	
	
var last_started_timeout;
function create_ajax_popupDelayed(popuptype, divid, popupName, on, delay, popup_width, popup_height, popup_xoffset, popup_yoffset)
{
	clearTimeout(last_started_timeout);
	last_started_timeout = setTimeout('create_ajax_popup(\'' + popuptype + '\',\'' + divid + '\',\'' + popupName + '\',\'' + on + '\',' + popup_width + ',' + popup_height + ',' + popup_yoffset + ',' + popup_xoffset + ')', delay);
}
//divid: The div where to show the popup
function create_ajax_popup(popuptype, divid, popupName, on, popup_width, popup_height, popup_xoffset, popup_yoffset)
{
	var myDiv = $(divid);
  var popupType = popuptype;
	var popupDivName = popupName;
  var popupDiv = $(popupDivName);
  var workingDiv = null;
  
  //var cords = findPos(myDiv);
  var cords = myDiv.cumulativeOffset(myDiv);
  var x_pos = cords[0];
  var y_pos = cords[1];
  
  //alert("x_pos:" + x_pos + " | y_pos: " + y_pos);
	
	if (typeof popup_width == 'undefined' )
	{
		popup_width = "0";
	}
	if (typeof popup_height == 'undefined' )
	{
		popup_height = "0";
	}
	if (typeof popup_xoffset == 'undefined' )
	{
		popup_xoffset = 0;
	}
	if (typeof popup_yoffset == 'undefined' )
	{
		popup_yoffset = 0;
	}
  //Remove the popup
	if(on == false)
	{
		clearTimeout(last_started_timeout);
    if(popupDiv != null)
    {
      //Effect.Fade(popupDivName, { afterFinish: function(element) {  if(popupDiv != null)myDiv.removeChild(popupDiv)} });
      myDiv.removeChild(popupDiv);
			
    }
  }
  //Create and set the popup visible
	else
	{
		var workingDiv = document.createElement("div");
    if(popupDiv != null)
    {
      myDiv.removeChild(popupDiv);
		}
    //console.log("message"); 
    popupDiv = document.createElement("div");
		popupDiv.className = "title_popup_box";
		popupDiv.id = popupDivName;

    //appendChild(popupDiv);
    //new Effect.Appear(popupDiv);

    popupDiv.style.left = x_pos + "px";
    popupDiv.style.top = y_pos + "px";
    
    //Solve problem with offset in IE
    var browser=navigator.appName;
    if(browser=="Microsoft Internet Explorer")
    {
      popupDiv.style.top = (y_pos - 30) + "px";
    }
    
		if(popup_width != 0)
		{
			popupDiv.style.width = popup_width + 'px';
		}
		if(popup_height != 0)
		{
			popupDiv.style.height = popup_height + 'px';
		}
		if(popup_xoffset != 0)
		{
			popupDiv.style.left = x_pos + popup_xoffset + 'px';
		}
		if(popup_yoffset != 0)
		{
			popupDiv.style.top = y_pos + popup_yoffset + 'px';
		}
    //myDiv.appendChild(popupDiv);
    document.body.appendChild(popupDiv);
    
    //Event.observe(popupDiv, 'click', function(e){ popupDiv.remove();} );
    
    if(popupType == 'thumbnail')
    {
      popupDiv.innerHTML = populateThumbnailPopup(popupDivName, popupName);
    }
    else if(popupType == 'title_info')
    {
      popupDiv.innerHTML = populateTitleInfoPopup(popupDivName);
    }
		
    workingDiv.className = "popup_working";
		workingDiv.id = "systemWorking";
    workingDiv.innerHTML = "<img src='/images/progress_indicators/ajax_indicator_small_spinner.gif'>"
    popupDiv.appendChild(workingDiv);
    
    Event.observe(popupDiv, 'mouseout', function(e) { Effect.Fade(popupDiv, { afterFinish: function(e){ popupDiv.remove();}})} );
    
    //popupDiv.onmouseout = console.log(popupDivName);

	}
	//return false to identify that the browser has active javascript.
	return false;
}


