/*
 * JTip
 * By Cody Lindley (http://www.codylindley.com)
 * Under an Attribution, Share Alike License
 * JTip is built on top of the very light weight jquery library.
 */

//on page load (as soon as its ready) call JT_init
jQuery(document).ready(JT_init);

var docHeight, clickElementx, clickElementy, safariOffset;
var de = document.documentElement;
var safariOffset = 0;
var currentTip;
if (navigator.vendor == "Apple Computer, Inc.") {
	safariOffset = 11;//don't know why, but Safari's off by a few pixels
}


function JT_init(){
	jQuery("a.jTip")
//	.hover(function(){JT_show(this.href,this.id,this.name)},function(){jQuery('#JT').remove()})
	.hover(function(){JT_show(this.href,this.id,this.name)},function(){});//tooltip doesn't disappear when square's rolled off
	docHeight = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
}

function JT_show(url,linkId,title){
	jQuery('#JT').remove();//get rid of old one to display new
	if(title == false)title="&nbsp;";
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = docHeight;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - safariOffset; //set y position
	var defaultWidth = 210;
	
	var queryString = url.replace(/^[^\?]+\??/,'');
	var params = parseQuery( queryString );
	if(params['width'] === undefined){params['width'] = defaultWidth};
	if(this.name !== undefined){
		jQuery('#' + linkId).bind('click',function(){if (this.name) {window.top.location = this.name} else {return false;}});
		jQuery('#' + linkId).css('cursor','pointer');
	}
	if(hasArea>((params['width']*1)+75)){//right
//		clickElementx = getAbsoluteLeft(linkId) + getElementWidth(linkId); //set x position (but IE PC can't calculate div width properly, so we hardcode it)
		clickElementx = getAbsoluteLeft(linkId) + 106; //set x position
	}else{//left
		clickElementx = getAbsoluteLeft(linkId) - ((params['width']*1) + 3); //set x position
	}
	jQuery("body").append("<div id='JT' style='width:"+params['width']*1+"px'><div id='JT_copy'></div></div>");
	
	jQuery('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	jQuery('#JT').hover(function(){},function(){jQuery('#JT').remove()});//removes the tooltip when rolled off
	jQuery('#JT_copy').load(url,'',Jpos);

}

function Jpos(which) {
	if (jQuery('.tooltipcopy').html()) {
	var offset = jQuery('#JT').offset();
	if (typeof(offset) != 'undefined') {
		if (offset.top + jQuery('#JT').height() > docHeight - 12) {
			var newtop = docHeight - jQuery('#JT').height() - 20;
			jQuery('#JT').css({left: clickElementx+"px", top: newtop+"px"});
		}
	}
	jQuery('#JT').show();
	}
}

function getElementWidth(objectId) {
	x = document.getElementById(objectId);
	return x.offsetWidth;
}

function getElementHeight(objectId) {
	x = document.getElementById(objectId);
	return x.offsetHeight;
}

function getAbsoluteLeft(objectId) {
	// Get an object left position from the upper left viewport corner
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	return oLeft
}

function getAbsoluteTop(objectId) {
	// Get an object top position from the upper left viewport corner
	o = document.getElementById(objectId)
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	return oTop
}

function parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

function blockEvents(evt) {
              if(evt.target){
              evt.preventDefault();
              }else{
              evt.returnValue = false;
              }
}
