//********************************************************************************************
//
// Mouseover popup
//
// Neil Burgess, 2007 - Rocks!
//
//********************************************************************************************


var posx;
var posy;
var overpopup;

document.onmousemove = getMouse;

function createPopup(item) {
	
	document.getElementById('popupcontainer').innerHTML = document.getElementById(item).innerHTML;
	document.getElementById('popupcontainer').style.width = parseInt(getStyle(document.getElementById("tooltip"), 'width'))+"px";
	if (getStyle(document.getElementById("tooltip"), 'height') != 'auto') {
		document.getElementById('popupcontainer').style.height = parseInt(getStyle(document.getElementById("tooltip"), 'height'))+"px";
	}
	document.getElementById('popupcontainer').style.display = "block";
}

function destroyPopup() {
	if (overpopup == false) {
		document.getElementById('popupcontainer').style.display = "none";
	}
}

function getMouse(e){
	posx=0;posy=0;ie=false;
	
	var ev=(!e)?window.event:e; //IE:Moz
	
	var scroll_offset_y = 0; scroll_offset_x = 0;
	// Find out scroll position
	if( typeof( window.pageYOffset ) == 'number' ) {
		scroll_offset_y = window.pageYOffset;
		scroll_offset_x = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		scroll_offset_y = document.body.scrollTop;
		scroll_offset_x = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		scroll_offset_y = document.documentElement.scrollTop;
		scroll_offset_x = document.documentElement.scrollLeft;
	}

	if ( ev.pageX ){ //Moz
		mouse_x = ev.pageX;
		mouse_y = ev.pageY;
	
		ie=false;
	} else if( ev.clientX ){ //IE
		mouse_x = ev.clientX;
		mouse_y = ev.clientY;
		ie=true;
	} else{ return false } //old browsers

	if (ie == true) {
		posx = mouse_x + scroll_offset_x;
		posy = mouse_y + scroll_offset_y;
	} else {
		posx = mouse_x;
		posy = mouse_y;
//document.getElementById("popupcontainer").innerHTML = "scroll_offset_y="+scroll_offset_y+" scroll_offset_x="+scroll_offset_x;
	}

	if (document.layers) {
		// IE6?
		objWidth = document.layers["popupcontainer"].document.width;
		objHeight = document.layers["popupcontainer"].document.height;
	} else if (document.all) {
		// IE7
		objWidth = document.all["popupcontainer"].clientWidth;
		objHeight = document.all["popupcontainer"].clientHeight;
	} else {
		// Moz
		docObj = document.getElementById("popupcontainer");
		objHeight = document.defaultView.getComputedStyle(docObj, "").getPropertyValue("height");
		objWidth = document.defaultView.getComputedStyle(docObj, "").getPropertyValue("width");
	}

	if (window.innerWidth) {
		screenWidth = window.innerWidth;
		screenHeight = window.innerHeight;
	} else {
		screenWidth = document.body.clientWidth;
		screenHeight = document.body.clientHeight;
	}

	// If they exist, strip px from the values
	if (typeof objWidth == "string") {
		objWidth = parseInt(objWidth.replace(/px/,""));
		objHeight = parseInt(objHeight.replace(/px/,""));
	}

	// Space box away from pointer
	posx = posx + 10;
	posy = posy + 10;

	posx = posx - 200;
	posy = posy - 0;

	hittingright = false;
	hittingbottom = false;
	
//	if ((posx+objWidth) >= screenWidth) {
//		posx = (screenWidth) - objWidth;
//		hittingright = true;
//	}
//	if ((posy+objHeight) >= screenHeight) {
//		posy = (screenHeight) - objHeight;
//		hittingbottom=true;
//	}

	if ( (hittingright == true) && (hittingbottom == true) ) {
		overpopup = true;
	} else {
		overpopup = false;
	}

	document.getElementById('popupcontainer').style.left = posx+"px";
	document.getElementById('popupcontainer').style.top  = posy+"px";
}

function getStyle(el, style) {
	
	if(!document.getElementById) return;
		var value = el.style[toCamelCase(style)];
	if(!value)
		if(document.defaultView)
			value = document.defaultView.
			getComputedStyle(el, "").getPropertyValue(style);
		else if(el.currentStyle)
			value = el.currentStyle[toCamelCase(style)];
	return value;
}

function setStyle(objId, style, value) {
	document.getElementById(objId).style[style] = value;
}

function toCamelCase( sInput ) {
	var oStringList = sInput.split('-');
	if(oStringList.length == 1) return oStringList[0];
		var ret = sInput.indexOf("-") == 0 ? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) : oStringList[0];
		for(var i = 1, len = oStringList.length; i < len; i++){
		var s = oStringList[i];
		ret += s.charAt(0).toUpperCase() + s.substring(1)
	}
	return ret;
}