// Copyright ? 2000 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.
// ********************************
// application-specific functions *
// ********************************

// store variables to control where the popup will appear relative to the cursor position
// positive numbers are below and to the right of the cursor, negative numbers are above and to the left
var xOffset = 20;
var yOffset = 5;

var newXCoordinate = 0;
var newYCoordinate = 0;
var coordinatesSet = false;

function showTooltip(event, content){
	document.getElementById("tooltip").innerHTML = content;
	showPopup("tooltip", event, 10, 10);
}


function showTooltipTable(event, newWidth, newHeight, content){
	var tooltipTableElem = document.getElementById("tooltipTable");
	var tooltipElem = document.getElementById("tooltip");
	if(!tooltipTableElem || !tooltipElem){
		return;
	}
	tooltipElem.style.width = newWidth;
	tooltipTableElem.innerHTML = content;

	showPopup("tooltip", event, 10, 10);

	// center the popup horisontally
	var middlex = (screen) ? ((screen.width)/2) : 0;
	var divWidth = parseInt(tooltipElem.style.width) /2;
	var newX = (middlex > divWidth) ? (middlex - divWidth) : 0;

	// center the popup vertically
	// (find ho many percent of the screen the popup will cover, then calculate
	// how many pixels from the 0 the popup should be moved down)
	var divHeight = newHeight;//parseInt(tooltipElem.style.height);
	var percentEmptyTop = (100 - ((divHeight * 100) / screen.availHeight)) / 2;
	var moveY = (screen.availHeight * percentEmptyTop) / 100;

	// add additional 100 px just for nicer look
	moveY = moveY - 100;

	var scrollX =  (document.body.scrollLeft) ? document.body.scrollLeft : 0;
	var scrollY =  (document.body.scrollTop) ? document.body.scrollTop : 0;

	moveObject("tooltip", newX + scrollX, moveY + scrollY);
}

function hideTooltip(event){
	hideCurrentPopup();
}

function showPopup (targetObjectId, eventObj, xOffset, yOffset) {
	if(!eventObj && !coordinatesSet) {
		return false;
	}

	// if coordinates not yet set, try to get them from the event
	if(!coordinatesSet){
		// stop event from bubbling up any farther
		eventObj.cancelBubble = true;
		// (add scrollTop to account for scrolling for IE)
		newXCoordinate = (eventObj.pageX)?eventObj.pageX + xOffset:eventObj.x + xOffset + ((document.body.scrollLeft)?document.body.scrollLeft:0);
		newYCoordinate = (eventObj.pageY)?eventObj.pageY + yOffset:eventObj.y + yOffset + ((document.body.scrollTop)?document.body.scrollTop:0);
		coordinatesSet = true;
	}

	// hide any currently-visible popups
	hideCurrentPopup();

	// move popup div to current cursor position
	// (add scrollTop to account for scrolling for IE)
	//var newXCoordinate = (eventObj.pageX)?eventObj.pageX + xOffset:eventObj.x + xOffset + ((document.body.scrollLeft)?document.body.scrollLeft:0);
	//var newYCoordinate = (eventObj.pageY)?eventObj.pageY + yOffset:eventObj.y + yOffset + ((document.body.scrollTop)?document.body.scrollTop:0);
	moveObject(targetObjectId, newXCoordinate, newYCoordinate);

	// reset the coordinates
	coordinatesSet = false;

	// and make it visible
	if( changeObjectVisibility(targetObjectId, 'visible') ) {
		// if we successfully showed the popup
		// store its Id on a globally-accessible object
		window.currentlyVisiblePopup = targetObjectId;
		return true;
	} else {
		// we couldn't show the popup
		return false;
	}
}

function setCoordinates(x, y){
	newXCoordinate = x;
	newYCoordinate = y;
	coordinatesSet = true;
}


function hideCurrentPopup(elemClicked) {

	// note: we've stored the currently-visible popup on the global object window.currentlyVisiblePopup
	if(window.currentlyVisiblePopup) {

		// do not hide if mouse is withing the popup frame!
		if(clickOutsidePopupFrame()){
			changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
			window.currentlyVisiblePopup = false;
		}
	}
}

function isPopupOpen(targetObjectId){
	return (window.currentlyVisiblePopup == targetObjectId);
}


function clickOutsidePopupFrame(){
	// TODO: is it possible to implement this?

	return true;
}

// ***********************
// hacks and workarounds *
// ***********************

// initialize hacks whenever the page loads
addOnLoadFunction(window, initializeHacks);

// setup an event handler to hide popups for generic clicks on the document
addOnClickFunction(document, hideCurrentPopup);

function initializeHacks() {
	// this ugly little hack resizes a blank div to make sure you can click
	// anywhere in the window for Mac MSIE 5
	if ((navigator.appVersion.indexOf('MSIE 5') != -1)
						&& (navigator.platform.indexOf('Mac') != -1)
						&& getStyleObject('blankDiv')) {
		window.onresize = explorerMacResizeFix;
	}
	resizeBlankDiv();
	// this next function creates a placeholder object for older browsers
	createFakeEventObj();
}

// createFakeEventObj
function createFakeEventObj() {
	// create a fake event object for older browsers to avoid errors in function call
	// when we need to pass the event object to functions
	if (!window.event) {
		window.event = false;
	}
}

function resizeBlankDiv() {
	// resize blank placeholder div so IE 5 on mac will get all clicks in window
	if ((navigator.appVersion.indexOf('MSIE 5') != -1)
					&& (navigator.platform.indexOf('Mac') != -1)
					&& getStyleObject('blankDiv')) {
		getStyleObject('blankDiv').width = document.body.clientWidth - 20;
		getStyleObject('blankDiv').height = document.body.clientHeight - 20;
	}
}

function explorerMacResizeFix () {
	location.reload(false);
}


 // getStyleObject
function getStyleObject(objectId) {
	// cross-browser function to get an object's style object given its id
	if(document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId).style;
	} else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId).style;
	} else if (document.layers && document.layers[objectId]) {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
	} else {
		return false;
	}
}

// changeObjectVisibility
function changeObjectVisibility(objectId, newVisibility) {
	// get a reference to the cross-browser style object and make sure the object exists
	var styleObject = getStyleObject(objectId);
	if(styleObject) {
		styleObject.visibility = newVisibility;
		return true;
	} else {
		// we couldn't find the object, so we can't change its visibility
		return false;
	}
}

// moveObject
function moveObject(objectId, newXCoordinate, newYCoordinate) {
	// get a reference to the cross-browser style object and make sure the object exists
	var styleObject = getStyleObject(objectId);
	if(styleObject) {
		styleObject.left = newXCoordinate + 'px';
		styleObject.top = newYCoordinate + 'px';
		return true;
	} else {
		// we couldn't find the object, so we can't very well move it
		return false;
	}
}


/**
 * Use this function if you don't want onclick events to close the popup!
 */
function doNotClosePopup(event){
	event.cancelBubble = true;
	event.returnValue = false;
	return false;
}



/**
 * add OnLoad function to the element. Run the new function first, then the original function
 */
function addOnLoadFunction(elem, newFunction){
	var orig = elem.onload;
	elem.onload = function(){
						newFunction(this);
						if(orig){
							orig(this);
						}
					}
}

/**
 * add OnClick function to the element.
 */
function addOnClickFunction(elem, newFunction){
	var orig = elem.onclick;
	elem.onclick = function (){
						newFunction(this);
						if(orig){
							orig(this);
						}
					}
}


function getAbsPos(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];
}