
var OP = (navigator.userAgent.indexOf('Opera') != -1);
var IE = (navigator.userAgent.indexOf('MSIE') != -1 && !OP);
var GK = (navigator.userAgent.indexOf('Gecko') != -1);
var SA = (navigator.userAgent.indexOf('Safari') != -1);
var DOM = document.getElementById;

var tooltip = null;

function TOOLTIP() {
//----------------------------------------------------------------------------------------------------
// Configuration
//----------------------------------------------------------------------------------------------------
  this.width = 200;                     // width (pixels)    
  this.opacity = 100;                    // opacity (0 - 100); doesn't work with all browsers
  this.cursorDistance = 5;              // distance from mouse cursor (pixels)    

  // don't change
  this.text = '';
  this.height = 0;
  this.obj = null;
  this.frame=null;
  this.active = false;

//----------------------------------------------------------------------------------------------------
// Methods
//----------------------------------------------------------------------------------------------------
  this.create = function() {
    if(!this.obj) this.init();

    if(DOM || IE) this.obj.innerHTML = this.template();
    if(DOM) this.height = this.obj.offsetHeight;
    else if(IE) this.height = this.obj.style.pixelHeight;

    this.setOpacity();
    this.move();
    this.show();
  }

  this.init = function() {
    if(DOM) this.obj = document.getElementById('ToolTip');
    else if(IE) this.obj = document.all.ToolTip;
    
    if(DOM) this.frame = document.getElementById('ToolTipIFrame');
    else if(IE) this.obj = document.all.ToolTipIFrame;
  }

  this.move = function() {
    var winX = getWinX() - (((GK && !SA) || OP) ? 17 : 0);
    var winY = getWinY() - (((GK && !SA) || OP) ? 17 : 0);
    var x = mouseX;
    var y = mouseY;

    if(x + this.width + this.cursorDistance > winX + getScrX())
      x -= this.width + this.cursorDistance;
    else x += this.cursorDistance;

    if(y + this.height + this.cursorDistance > winY + getScrY())
      y -= this.height;
    else y += this.cursorDistance;

    this.frame.style.left = x + 'px';
    this.frame.style.top = y + 'px';
    //this.frame.style.height= this.height;
    this.frame.style.width= this.width;

    this.obj.style.left = x + 'px';
    this.obj.style.top = y + 'px';
  }

  this.show = function() {
    this.obj.style.visibility = 'visible';
    this.obj.style.zIndex = 1000;
    //this.frame.style.height = '100'
    this.active = true;
  }

  this.hide = function() {
    this.obj.style.visibility = 'hidden';
    this.obj.style.zIndex = -1;
    this.frame.style.height = '0'
    this.active = false;
  }

  this.setOpacity = function() {
    this.obj.style.filter = 'alpha(opacity=' + this.opacity + ')';
    this.obj.style.mozOpacity = '.1';
    if(this.obj.filters) this.obj.filters.alpha.opacity = this.opacity;
    if(!document.all && this.obj.style.setProperty)
      this.obj.style.setProperty('-moz-opacity', this.opacity / 100, '');
  }
}

//----------------------------------------------------------------------------------------------------
// Global functions
//----------------------------------------------------------------------------------------------------
function getScrX() {
  var offset = 0;
  if(window.pageXOffset)
    offset = window.pageXOffset;
  else if(document.documentElement && document.documentElement.scrollLeft)
    offset = document.documentElement.scrollLeft;
  else if(document.body && document.body.scrollLeft)
    offset = document.body.scrollLeft;
  return offset;
}

function getScrY() {
  var offset = 0;
  if(window.pageYOffset)
    offset = window.pageYOffset;
  else if(document.documentElement && document.documentElement.scrollTop)
    offset = document.documentElement.scrollTop;
  else if(document.body && document.body.scrollTop)
    offset = document.body.scrollTop;
  return offset;
}

function getWinX() {
  var size = 0;
  if(window.innerWidth)
    size = window.innerWidth;
  else if(document.documentElement && document.documentElement.clientWidth)
    size = document.documentElement.clientWidth;
  else if(document.body && document.body.clientWidth)
    size = document.body.clientWidth;
  else size = screen.width;
  return size;
}

function getWinY() {
  var size = 0;
  if(window.innerHeight)
    size = window.innerHeight;
  else if(document.documentElement && document.documentElement.clientHeight)
    size = document.documentElement.clientHeight;
  else if(document.body && document.body.clientHeight)
    size = document.body.clientHeight;
  else size = screen.height;
  return size;
}

function getMouseXY(e) {
  if(e && e.pageX != null) {
    mouseX = e.pageX;
    mouseY = e.pageY;
  }
  else if(event && event.clientX != null) {
    mouseX = event.clientX + getScrX();
    mouseY = event.clientY + getScrY();
  }
  if(mouseX < 0) mouseX = 0;
  if(mouseY < 0) mouseY = 0;
  if(tooltip && tooltip.active) tooltip.move();
}

function toolTip(text, width, opacity) {
  if(text) {
    tooltip = new TOOLTIP();
    tooltip.text = text;
    if(width) tooltip.width = width;
    if(width == 250) tooltip.width = 550;
    if(opacity) tooltip.opacity = opacity;
    tooltip.create();
  }
  else if(tooltip) tooltip.hide();
}

//----------------------------------------------------------------------------------------------------
// Build tooltip box
//----------------------------------------------------------------------------------------------------
document.write('<div id="ToolTip" style="position:absolute; visibility:hidden;"></div><iframe id="ToolTipIFrame" src="" class="frmcls" frameborder="0" style="height:0"></iframe>');

//----------------------------------------------------------------------------------------------------
// Event handlers
//----------------------------------------------------------------------------------------------------
var mouseX = mouseY = 0;
document.onmousemove = getMouseXY;

//----------------------------------------------------------------------------------------------------
//new
//JX-small ajax library
jx={http:false,format:'text',callback:function(data){},error:false,getHTTPObject:function(){var http=false;
if(typeof ActiveXObject !='undefined'){try{http=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){
try{http=new ActiveXObject("Microsoft.XMLHTTP");}catch(E){http=false;}}
}else if(XMLHttpRequest){try{http=new XMLHttpRequest();}catch(e){http=false;}}return http},
load:function(url,callback,format){this.init();if(!this.http||!url)return;
if(this.http.overrideMimeType)this.http.overrideMimeType('text/xml');this.callback=callback;
if(!format)var format="text";this.format=format.toLowerCase();var ths=this;
if(this.http.overrideMimeType)this.http.overrideMimeType('text/xml');this.http.open("GET",url,true);
if(this.format)this.http.setRequestHeader("Content-Type", this.format);this.http.onreadystatechange=function(){
if(!ths)return;var http=ths.http;if(http.readyState==4){if(http.status==200){var result="";
if(http.responseText)result=http.responseText;if(ths.format.charAt(0)=="j"){result=result.replace(/[\n\r]/g,"");
result=eval('('+result+')'); }if(ths.callback)ths.callback(result);}else{if(ths.error)ths.error()}}}
this.http.send(null);},init:function(){this.http=this.getHTTPObject();}}
//extending tooltip
TOOLTIP.prototype.setText=function(text){
	this.text=text;
	if(DOM || IE) this.obj.innerHTML = this.template();
}
TOOLTIP.prototype.template=function(){//this method returns this.text applied with the given template
    return "<table border=0 cellspacing=0 cellpadding=4 width=" + this.width + "><tr>" +
    "<td align='center' class='JSToolTip'>" + this.text +
    "</td></tr></table>";
}
tooltipLoadingTemplate=function(){//this function return the content of the tooltip being loaded
    return '<div style="text-align:center;">Loading...</div>';
}
function loadJS(scriptId,scriptUrl) {//this function loads a js file dynamicly
	var head = document.getElementsByTagName('head').item(0);
	var old  = document.getElementById(scriptId);
	if (old) head.removeChild(old);

	script = document.createElement('script');
	script.src = scriptUrl;
	script.type = 'text/javascript';
	script.defer = true;
	script.id = scriptId;
	void(head.appendChild(script));
}
var tooltipTemplateUrl='tooltip_template.js';
var tooltipTemplateId = 'loading_template';
//loadJS(tooltipTemplateId,tooltipTemplateUrl);
//cache hash map
var lookUpUrlTable={
	'currentUrl':null,
	'addUrl':function(url){
		this.currentUrl=url;
	},
	'addText':function(text){
		if(this.currentUrl){
			this[this.currentUrl]=text;
			this.currentUrl=null;
		}	
	},
	'lookUp':function(url){
		if(this[url]){
			return this[url];
		}
		else{
			return null;
		}
	},
	'reset':function(){
		this.currentUrl=null;
	}
};
//server communication
document.write('<iframe id="ToolTipLoadingIFrame" src="" class="frmcls" frameborder="0" style="height:0"></iframe>');
var toolTipIframe=null;
function loadIframe(url){
	if(toolTipIframe==null){
		toolTipIframe=document.getElementById('ToolTipLoadingIFrame');
	}
	toolTipIframe.src=url;
}
function notifyParent(text){
	if(tooltip&&text&&tooltip.active){
		tooltip.setText(text);
	}
	if(text){
		lookUpUrlTable.addText(text);
	}
}
function loadAjax(url){
	jx.load(url,function(data){notifyParent(data);},'text/plain');
}
//modified tooltip
function loadUrl(url){
	//loadIframe(url);
	loadAjax(url);
	lookUpUrlTable.addUrl(url);
}
function tooltipUrl(url,width){
	if(url){
		var cachedContent=lookUpUrlTable.lookUp(url);
		if(cachedContent){
			toolTip(cachedContent,width);
		}
		else{
			toolTip(tooltipLoadingTemplate(),width);
			loadUrl(url);
		}
	}
	else{
		lookUpUrlTable.reset();
		toolTip();
	}
}
