/*******************************************************************************

Ajax Popup Tooltip by LowTechGeezer.com

Based on Ajax Tooltip (c) 2006  DTHMLGoodies.com, Alf Magne Kalleland

This software has been specifically designed to work with Virtual Mechanics
products (SiteSpinner and WebEngine). It contains code that intercat directly
with the HTML generated by those products.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.

*******************************************************************************/

/* Custom variables */

popPages = [];   // Contains the pages that data can be extrcted from
var scriptURL;   // URL of the script to extract <div> objects
var popObjName;  // The SiteSpinner object name used to hold the extracted <div>

var popX, popY;  // The mouse position when the script is executed.

var x_offset = 75; // X & Y offsets added to the output position; can be used
var y_offset = 0;  // to ensure the ajax object does not cover the onCLick object

var enableCache = true;

/* Don't change anything below here */
var jsCache = new Array();
var popup_ajaxObjs = new Array();
var pop_tooltipObj = false;

/*******************************************************************************
This function initializes script parameters:
  urlscript - the URL path to the script used to provide data
  popObj    - the name of th eobject to hold the returned data
  pages     - an array of HTML pages names used to retrieve data
*******************************************************************************/
function initPopTip(urlscript, popObj, pages) {
	scriptURL = urlscript;
  for (i=0; i<pages.length; i++) {
  	popPages[i] = pages[i];
  }
  popObjName = popObj;

  // Set up the default style for the ajax container object and set an onClick
  // function to hide it.
	pop_tooltipObj = document.getElementById(popObjName);
	pop_tooltipObj.style.visibility = "hidden";
	pop_tooltipObj.style.position = "static";
  pop_tooltipObj.onclick = function() {
  	  pop_tooltipObj.innerHTML = "";
  		pop_tooltipObj.style.visibility = "hidden";
			pop_tooltipObj.style.zIndex = 0;
			}
}

/*******************************************************************************
This function is called from a mouse event (like an onClick) to retrieve the
desired data from the specified HTML page:
    evt    - the global event object
    popObj - object name of the <div> block to be retrieved
    page   - a number indexing into the popPages array indictaing which page
             should be referenced to get popObj.
*******************************************************************************/
function showPopTip(evt, popObj, page) {

	// Get and save the mouse position when the function is called.
  if (evt.pageX || evt.pageY) {
    popX = evt.pageX;
    popY = evt.pageY;
  }
  else {
    popX = evt.clientX + document.body.scrollLeft;
    popY = evt.clientY + document.body.scrollTop;
  }

	// Build a URl string using the passed arguments
  externalFile = scriptURL + "?pg=" + popPages[page] + "&div=O" + popObj;

	// Load the desired object and show the container
	ajax_loadContent(popObjName, externalFile);
  pop_tooltipObj.style.visibility = 'visible';
}

/*******************************************************************************
This function displays the retrieved data in the container.
		divId     - the id name of the container object
		ajaxIndex - a pointer into the ajax request array
		url       - a pointer into the ajax cache (if enabled)
*******************************************************************************/
function ajax_showContent(divId, ajaxIndex, url) {

	// Load the ajax response into the container
	ajaxDiv = document.getElementById(divId);
	ajaxDiv.innerHTML = popup_ajaxObjs[ajaxIndex].response;

	// If caching is nabled, save the response index by its url string
	if (enableCache) {
		jsCache[url] = 	popup_ajaxObjs[ajaxIndex].response;
	}

	popup_ajaxObjs[ajaxIndex] = false;  // Remove this request

	// We need to position the container near the mouse position than
	// triggered the ajax request. Thw response always has an id name
	// of 'content'.
	contentDiv = document.getElementById('content');
	if (contentDiv) {
		c = document.getElementById("centered");
		if (c) {
			center_offset = document.body.clientWidth - c.offsetWidth;
			contentDiv.style.left = center_offset - popX + x_offset;
		}
		else {
	  	center_offset = 0;
			contentDiv.style.left = popX;
		}
		contentDiv.style.top = popY;
		contentDiv.style.position = 'absolute';
		contentW = parseInt(contentDiv.style.width);

		// If the container would exceed the body width, shift it so it
		// remains in view.
		if (popX+contentW > document.body.clientWidth) {
			if (c)
			 contentDiv.style.left = popX - center_offset - contentW + x_offset;

			else
			 contentDiv.style.left = popX - (x_offset/2) - contentW;
		}

		// Scroll the browser window, if necessary, to keep the container
		// in view.
		w = document.body.clientHeight + parseInt(contentDiv.style.height);
	  window.scrollTo(popX, w);
	}
}

/*******************************************************************************
This function dispatches the ajax request.
		divId - the id name of the container object
		url   - the server script to be executed
*******************************************************************************/
function ajax_loadContent(divId, url) {

	// If cachin is enabled and the reqponse has already been obtained, then just
	// use the saved one instead of fetching it again.
	if (enableCache && jsCache[url]) {
		document.getElementById(divId).innerHTML = jsCache[url];
		return;
	}

  // Set up the ajax request
	var ajaxIndex = popup_ajaxObjs.length;
	document.getElementById(divId).innerHTML = 'Loading content - please wait';
	document.getElementById(divId).style.visibility = 'hidden';
	popup_ajaxObjs[ajaxIndex] = new sack();

	if (url.indexOf('?') >= 0) {
		popup_ajaxObjs[ajaxIndex].method = 'GET';
		var string = url.substring(url.indexOf('?'));
		url = url.replace(string, '');
		string = string.replace('?', '');
		var items = string.split(/&/g);
		for(var no=0; no<items.length; no++){
			var tokens = items[no].split('=');
			if (tokens.length==2) {
				popup_ajaxObjs[ajaxIndex].setVar(tokens[0],tokens[1]);
			}
		}
		url = url.replace(string, '');
	}

	popup_ajaxObjs[ajaxIndex].requestFile = url;	// Specifying which file to get
	// Specify function that will be executed after file has been retrieved
	popup_ajaxObjs[ajaxIndex].onCompletion = function() {
		ajax_showContent(divId, ajaxIndex, url);
		};
	popup_ajaxObjs[ajaxIndex].runAJAX();		// Execute AJAX function
}
