var rpc;
var loadTimer;
var loadMsg = "Loading...";

function getInt(val)
{
    var ret = parseInt(val);
	if(isNaN(ret)) {return 0;};
	return ret;
}

function getLocalOffsetX(what)
{
	var ret = what.offsetLeft;
	var p;
	for( p = what.offsetParent; p && p.style.position!='relative'; p = p.offsetParent )
		ret += p.offsetLeft;
	return ret;
}
function getLocalOffsetY(what)
{
	var ret = what.offsetTop;
	var p;
	for( p = what.offsetParent; p && p.style.position!='relative'; p = p.offsetParent )
		ret += p.offsetTop;
	return ret;
}
function getPageOffsetX(what)
{
	var ret = what.offsetLeft;
	var p;
	for( p = what.offsetParent; p; p = p.offsetParent )
		ret += p.offsetLeft;
	return ret;
}
function getPageOffsetY(what)
{
	var ret = what.offsetTop;
	var p;
	for( p = what.offsetParent; p; p = p.offsetParent )
		ret += p.offsetTop;
	return ret;
}

function getWidth( what )
{
	var w = what.offsetWidth;
	if( w==0 && what.parentNode )
		w = what.parentNode.offsetWidth - 20;
	return w;
}
function ShowGameTooltip( _this, _id, _class, _url, _x, _y )
{	// Check if we have already got one (cached tooltips)
	if( navigator.userAgent )
	{
		if( (navigator.userAgent).indexOf("MSIE 7") >= 0 )
			return;
	}
	
	var newdiv = document.getElementById(_id)
	if(!newdiv)
	{
		newdiv = document.createElement('div');
		newdiv.setAttribute('id', _id);
		newdiv.setAttribute('class', _class);
		newdiv.setAttribute('className', _class);
		newdiv.innerHTML = loadMsg;
		_this.appendChild(newdiv);
	}
	newdiv.style.left =  _x + 'px';
	newdiv.style.top = _y + 'px';
	forceOnScreen(_id);	
	
	newdiv.style.visibility = 'visible';
	// if contents are same as original (i.e. 'Loading...') then wait 1 sec before sending an RPC request
	if( newdiv.innerHTML == loadMsg )
	{
		loadTimer = setTimeout("GetTooltipContents('"+_id+"','"+_url+"')",1000);
	}
}
function GetRPC()
{
	var ret = null;
    try { ret = new ActiveXObject('Msxml2.XMLHTTP');}
    catch (e) {
        try { ret = new ActiveXObject('Microsoft.XMLHTTP');}
        catch (e) {
			try { ret = new XMLHttpRequest();}
			catch (e) {
				return null;
			}
        }
    }
	return ret;
}
function GetTooltipContents( _id, _url )
{
	rpc = GetRPC();
	if(rpc != null)
	{
		rpc.onreadystatechange = function() {
			rpcCallback(_id);
			forceOnScreen( _id );
			loadTimer = setTimeout("forceOnScreen('"+_id+"')",1500);
		};
		rpc.open("GET",_url,true);
		rpc.send(null);
	}
}
function HideGameTooltip( _id )
{
	var newdiv = document.getElementById(_id);
	if(newdiv)
	{
		newdiv.style.visibility = 'hidden';
		// Stop any 'pending' RPC calls
		if (typeof loadTimer!="undefined")
			clearTimeout(loadTimer)
	}
}


function forceOnScreen( _id ) {
	// Check '_id' is visible & shift it if not...
	var newdiv = document.getElementById(_id);
	if(!newdiv){ return;}

	var target = newdiv;
	
	var px = getPageOffsetX(target);
	var lx = getLocalOffsetX(target);
	
	var py = getPageOffsetY(target);
	var ly = getLocalOffsetY(target);
	
  var vw = 0, vh = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    vw = window.innerWidth;
    vh = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    vw = document.documentElement.clientWidth;
    vh = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    vw = document.body.clientWidth;
    vh = document.body.clientHeight;
  }
  var vx = 0, vy = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    vy = window.pageYOffset;
    vx = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    vy = document.body.scrollTop;
    vx = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    vy = document.documentElement.scrollTop;
    vx = document.documentElement.scrollLeft;
  }
  
    var pw = target.offsetWidth;
    var ph = target.offsetHeight;
	var qx = px - lx;
	var qy = py - ly;
	if( px <  vx )
	{	// off the left of the view area, so bump right...
		target.style.left = (vx - qx) + 'px';
	}
	else if( px+pw >  vx+vw )
	{	// off the right...
		target.style.left = (vx+vw - pw - qx - 16) + 'px';
	}
	
	if( py <  vy )
	{	// off the left of the view area, so bump right...
		target.style.top = (vy - qy) + 'px';
	}
	else if( py+ph >  vy+vh )
	{	// off the right...
		target.style.top = (vy+vh - ph-qy - 16) + 'px';
	}
	
}
function rpcCallback(id) {
	var newdiv = document.getElementById(id);
	if(!newdiv){ return;}
	if(rpc.readyState == 4) {
		if(rpc.status == 200) {
			newdiv.innerHTML = rpc.responseText;
		} else {
			newdiv.innerHTML = "Error: Page not found!";
		}
	}
	forceOnScreen(id);
}
function ChangeScreen(_id,_new) {
	var newdiv = document.getElementById(_id)
	if(newdiv)
		newdiv.innerHTML = '<img src="'+_new+'" width="400" height="300" border="0">';
}

function ShowSS(_this,_img,_class,_x,_y) {
	var _id = "SSpop";
	var newdiv = document.getElementById(_id)
	if(!newdiv)
	{
		newdiv = document.createElement('div');
		newdiv.setAttribute('id', _id);
		newdiv.setAttribute('class', _class);
		newdiv.setAttribute('className', _class);
		newdiv.onclick = function() {hideSS();};
		newdiv.innerHTML = loadMsg;
		_this.parentNode.appendChild(newdiv);
	}
	newdiv.style.left =  _x + 'px';
	newdiv.style.top = _y + 'px';
	
	newdiv.style.visibility = 'visible';
	newdiv.innerHTML = '<img src="'+_img+'" width="400" height="300" border="0" ><br />Click&nbsp;to&nbsp;close';
//	forceOnScreen(_id);
}
function hideSS()
{
	var _id = "SSpop";
	var newdiv = document.getElementById(_id);
	if(newdiv)
		newdiv.style.visibility = 'hidden';
}
