/*****************************************************************
	Ajax Tooltip generator
	Copyright (C) 2007  Dennis Hilmar
*****************************************************************/

/* Creating the HTML associative array */
var xhtml = new Object;
var key;
var tdpil;
var IE = navigator.userAgent.toLowerCase().indexOf('msie')=='-1'?false:true;

function Popup() {
	var oThis = this;

	this.init();
	this.mouseover = false;
	this.showing = false;

//	if (document.captureEvents)
//		document.captureEvents(Event.MOUSEUP);

	window.onmouseup = function (e) {
		if (!e) e = window.event;
		if (oThis.showing==false) return;

		if (!oThis.mouseover)
			oThis.hide_popup();
	}

/*	filter:alpha(opacity=70);
	-khtml-opacity: 0.7; */
	var cssStyle = '#tooltip{background-color:#EEE; border:1px solid #000; position:absolute; display:none; z-index:99; padding:2px; font-size:0.9em; font-family: "Trebuchet MS", "Lucida Sans Unicode", Arial, sans-serif; border-radius:6px; -moz-border-radius:6px;}';
	cssStyle += '#tooltipShadow{ position:absolute; background-color:#555; display:none; z-index:97; opacity:0.7; border-radius:6px; -moz-border-radius:6px;}';

	cssStyle += '.color {	background-color: #ffd074; }';

	if (typeof PRO_addStyle != 'undefined')
		PRO_addStyle(cssStyle, document);
	else if (typeof GM_addStyle != 'undefined')
		GM_addStyle(cssStyle);
	else
	{
		try {
			var style = document.createElement('STYLE');
			style.type = 'text/css';
			style.innerHTML = cssString;
			document.getElementsByTagName('HEAD')[0].appendChild(style);
		} catch(e) {
			//alert("Your running IE7... This browser does not support appending CSS styles.");
			window.status = "Your running IE7... This browser does not support appending CSS styles.";
		}
	}

}

Popup.prototype = 
{
	init : function()
	{
		var oThis = this;

		this.tooltip = document.createElement('div');
		this.tooltip.id = 'tooltip';
		this.tooltipShadow = document.createElement('div');
		this.tooltipShadow.id = 'tooltipShadow';

		this.tooltip.onmouseover = function (e) { oThis.mouseover = true; }
		this.tooltip.onmouseout =  function (e) { oThis.mouseover = false; }
			
		document.body.appendChild(this.tooltip);
		document.body.appendChild(this.tooltipShadow);	
	
		if(IE){
			this.tooltipIFrame = document.createElement('iframe');
			this.tooltipIFrame.frameborder='5';
			this.tooltipIFrame.style.backgroundColor='#FFF';
			this.tooltipIFrame.src = '#'; 	
			this.tooltipIFrame.style.zIndex = 100;
			this.tooltipIFrame.style.position = 'absolute';
			this.tooltipIFrame.style.display = 'none';
			document.body.appendChild(this.tooltipIFrame);
		}

	}
	,
	show_popup : function(e, file, extra)
	{
		if(document.all) e = event;
	
		var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
		if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0; 
		this.leftPos = e.clientX + 20;
		if(this.leftPos<0) this.leftPos = 0;
		this.topPos = e.clientY;

		var key;
		if (extra!=undefined)
			key = file + extra;
		else
			key = file;

		if (file=='message')
			xhtml[key] = extra;

		if (xhtml[key]==undefined)
			xhtml[key] = this.httpReq('popup.php?form='+file+'\&extra='+extra);

		//this.tooltip.innerHTML = '<b>' + UpperCaseConvert(file) + '</b><hr>';
		//this.tooltip.innerHTML =  xhtml[key];
		this.set_content(xhtml[key]);

		this.tooltipShadow.style.display = 'block';
		this.showing = true;
	}
	,
	httpReq : function(file)
	{
		var http = (typeof XMLHttpRequest != 'undefined' ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'));

		http.open('GET', file, false);
		http.send(null);

		if (http.status != 200 || http.readyState!=4) 
				throw('Error ' + http.status + ' (' + http.statusText + ') trying to apply new page content');

		return http.responseText;
	}
	,
	set_content : function(content)
	{
		this.tooltip.innerHTML =  content;
	
		/* Re-considering the left position... */
		this.tooltip.style.left = '-1000px';
		this.tooltip.style.display = 'block';

		var popupWidth = parseInt(getStyle(this.tooltip, "width"));
		var pageWidth = Math.min(document.body.offsetWidth, window.innerWidth);
		var scrolled = (document.all)?document.body.scrollTop:window.pageYOffset;

		if (popupWidth+this.leftPos > pageWidth) {
			this.leftPos -= popupWidth+10;
		}

		this.tooltip.style.left = this.leftPos + 'px';
		this.tooltip.style.top = this.topPos + scrolled + 'px';

		/* Displaying Shadow */
		this.tooltipShadow.style.width = parseInt(getStyle(this.tooltip, "width")) +'px';
		this.tooltipShadow.style.height = parseInt(getStyle(this.tooltip, "height")) +'px';
		this.tooltipShadow.style.left = parseInt(this.tooltip.style.left)+8+'px';
		this.tooltipShadow.style.top = parseInt(this.tooltip.style.top)+8+'px';
	}
	,
	hide_popup : function()
	{
		this.tooltip.style.display = 'none';
		this.tooltipShadow.style.display = 'none';
		this.showing = false;
	}
}


/* converts a string: "hello_world" -> "Hello World" */
function UpperCaseConvert(str) {
		var result = '';
    var Upper = str.split('_');
    for(i=0; i<=Upper.length-1; i++) {
        result  += Upper[i].substring(0,1).toUpperCase() + Upper[i].substring(1) + ' ';
    }

		return result;
}


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;
}
