//Ajax Tooltip script: By JavaScript Kit: http://www.javascriptkit.com
//Last update (July 10th, 08'): Modified tooltip to follow mouse, added Ajax "loading" message.
//2010-04-27 cmeans Sset popup z-index to 6000.
//2010-05-06 cmeans Adjusted positiontip function to accomodate scroll bar position and improved handling.

var ajaxtooltip={
	fadeeffect: [true, 300], //enable Fade? [true/false, duration_milliseconds]
	//useroffset: [10, 10], //additional x and y offset of tooltip from mouse cursor, respectively
	loadingHTML: '<div style="font-style:italic"><img src="images/ajaxload.gif" /></div>',

	positiontip:function($tooltip, e)
	{
		// Client dimensions...assumes scrollbars are present.
		var docwidth = ((window.innerWidth)? window.innerWidth : ajaxtooltip.iebody.clientWidth) -15
		var docheight = ((window.innerHeight)? window.innerHeight - 3 : ajaxtooltip.iebody.clientHeight) - 15;

		// Content dimensions.
		var twidth=$tooltip.get(0).offsetWidth
		var theight=$tooltip.get(0).offsetHeight

		// Get current Scroll position.
		var scroll = this.getPageScroll();

		// Fixed left position. Not a good choice for small windows.
		var tipx = 100;

		// Base optimum position...assuming no bounds issues.
		var tipy = (e.clientY + scroll.y) - (theight / 2);

		// Check for upper bounds issues.
		if (tipy - scroll.y < 0)
		{
			tipy = scroll.y;
		}

		// Check for lower bounds issues.
		if (tipy + theight > (docheight + scroll.y))
		{
			tipy = docheight + scroll.y - theight;
		}

		$tooltip.css({left: tipx, top: tipy})
	},

	showtip:function($tooltip, e){  
		if (this.fadeeffect[0])
			$tooltip.hide().fadeIn(this.fadeeffect[1])
		else
			$tooltip.show()
	},

	hidetip:function($tooltip, e){
		if (this.fadeeffect[0])
			$tooltip.fadeOut(this.fadeeffect[1])
		else
			$tooltip.hide()
	},

	// getPageScroll() by quirksmode.com
	getPageScroll: function () 
	{
	    var xScroll, yScroll;
	    if (self.pageYOffset) 
	    {
	      yScroll = self.pageYOffset;
	      xScroll = self.pageXOffset;
	    }
	    else if (document.documentElement && document.documentElement.scrollTop) 
	    {
	      yScroll = document.documentElement.scrollTop;
	      xScroll = document.documentElement.scrollLeft;
	    }
	    else if (document.body) 
	    {
	      // all other Explorers
	      yScroll = document.body.scrollTop;
	      xScroll = document.body.scrollLeft;
	    }
	    
	    return { x: xScroll, y: yScroll };
	}
}
var $j = jQuery.noConflict();

jQuery(document).ready(function(){
	ajaxtooltip.iebody=(document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
	var tooltips=[] //array to contain references to all tooltip DIVs on the page
	$j('*[@title^="ajax-"]').each(function(index){ //find all links with "title=ajax:" declaration
		this.titleurl=jQuery.trim(this.getAttribute('title').replace('ajax-','')) //get URL of external fil

		this.titleposition=index+' pos' //remember this tooltip DIV's position relative to its peers
		tooltips.push($j('<div class="ajaxtooltip" style="min-height:215px; min-width:800px; z-index:6000"></div>').appendTo('body'))
		var $target=$j(this)
		$target.removeAttr('title')
		$target.hover(
			function(e){ //onMouseover element
				var $tooltip=tooltips[parseInt(this.titleposition)]
				if (!$tooltip.get(0).loadsuccess){ //first time fetching Ajax content for this tooltip?
						$tooltip.html(ajaxtooltip.loadingHTML).show()
						$tooltip.load(this.titleurl, '', function(){
						 
							ajaxtooltip.positiontip($tooltip, e)
							ajaxtooltip.showtip($tooltip, e)
							$tooltip.get(0).loadsuccess=true
						})

					}
				else{
					ajaxtooltip.positiontip($tooltip, e)
					ajaxtooltip.showtip($tooltip, e)
				}
			},
			function(e){ //onMouseout element
				var $tooltip=tooltips[parseInt(this.titleposition)]
				ajaxtooltip.hidetip($tooltip, e)		
			}
		)
		$target.bind("mousemove", function(e){
			var $tooltip=tooltips[parseInt(this.titleposition)]
			ajaxtooltip.positiontip($tooltip, e)
		})
	})
})
