var tooltipFlag=null;
var ajaxtooltip={
	fadeeffect: [true, 300], //enable Fade? [true/false, duration_milliseconds]
	useroffset: [350, 140], //additional x and y offset of tooltip from mouse cursor, respectively
	loadingHTML: '<div style="font-style:italic"><img width="25" src="/img/loading.gif" /> Loading Preview...</div>',
	positiontip:function($tooltip, e){
		var currentimageheight = 200;
		var docwidth=(window.innerWidth)? window.innerWidth-15 : ajaxtooltip.iebody.clientWidth-15
		var docheight=(window.innerHeight)? window.innerHeight-18 : ajaxtooltip.iebody.clientHeight-15
		var twidth=$tooltip.get(0).offsetWidth
		var theight=$tooltip.get(0).offsetHeight
		var xcoord=this.useroffset[0]
		var ycoord=this.useroffset[1]
		var tipx=e.pageX+this.useroffset[0]
		var tipy=e.pageY+this.useroffset[1]
		//tipx=(e.clientX+twidth>docwidth)? tipx-twidth-(2*this.useroffset[0]) : tipx //account for right edge
		//tipy=(e.clientY+theight>docheight)? tipy-theight-(2*this.useroffset[0]) : tipy //account for bottom edge
		tipx= e.pageX -  docwidth +this.useroffset[0];
		tipy= e.pageY - docheight +this.useroffset[1];




		if (typeof e != "undefined"){
			if (docwidth - e.pageX < 380){
				xcoord = e.pageX - xcoord - 400; // Move to the left side of the cursor
			} else {
				xcoord += e.pageX;
			}
			if (docheight - e.pageY < (currentimageheight)){
				ycoord += e.pageY - 300;
			} else {
				ycoord += e.pageY;
			}
	
		} else if (typeof window.event != "undefined"){
			if (docwidth - event.clientX < 380){
				xcoord = event.clientX + truebody().scrollLeft - xcoord - 400; // Move to the left side of the cursor
			} else {
				xcoord += truebody().scrollLeft+event.clientX
			}
			if (docheight - event.clientY < (currentimageheight + 110)){
				ycoord += event.clientY + truebody().scrollTop - Math.max(0,(110 + currentimageheight + event.clientY - docheight));
			} else {
				ycoord += truebody().scrollTop + event.clientY;
			}
		}
		ycoord = ycoord - 330;
		xcoord = xcoord+40;
		if(ycoord < 0) { ycoord = ycoord*-1; }
		$("#preview").css({left: xcoord, top: ycoord})
		//$("#preview").css({left: tipx, top: tipy})
	},
	showtip:function($tooltip, e){

		if (this.fadeeffect[0])
			$("#preview").hide().fadeIn(this.fadeeffect[1])
		else
			$("#preview").show();
	},

	hidetip:function($tooltip, e){
		
		$("#preview").css("display","none");


		if (this.fadeeffect[0])
			$("#preview").fadeOut(this.fadeeffect[1])
		else
			$("#preview").hide()
	}

}


function pushToolTips(){

	ajaxtooltip.iebody=(document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
	var tooltips=[] //array to contain references to all tooltip DIVs on the page
	$("*[@title^='ajax:']").each(function(index){ //find all links with "title=ajax:" declaration
		this.titleurl=jQuery.trim(this.getAttribute('title').split(':')[1]) //get URL of external file
		var tmp=this.titleurl;
		this.titleposition=index+' pos' //remember this tooltip DIV's position relative to its peers
		//tooltips.push($('<div class="ajaxtooltip"></div>').appendTo('body'))
		var $target=$(this)
		$target.removeAttr('title')
		$target.hover(
			function(e){ //onMouseover element
			var $tooltip=$("#preview");
			var tooltipObj = $("#preview");
			if(tooltipFlag==null){
				clearTimeout(this.timeout);
				if($("#preview").get(0).style.display == 'none'){
					this.timeout = setTimeout(function(){
					$("#preview").html(ajaxtooltip.loadingHTML).show();
					var newHTML;
					$.get(tmp, function(data){
						$("#preview").html(data);	
						ajaxtooltip.positiontip(tooltipObj, e);
						ajaxtooltip.showtip(tooltipObj, e);
						tooltipObj.get(0).loadsuccess=true
					});
				   }, 500);
				} else {
					$("#preview").html("&nbsp;");
					$("#preview").get(0).style.display = 'none';	
					
				}
			//tooltipFlag=this.id;
			}

				
			},
			function(e){ //onMouseout element

							clearTimeout(this.timeout);
							tooltipFlag=null;
							var $tooltip=$("#preview");
							$("#preview").html("&nbsp;");
							ajaxtooltip.hidetip($tooltip, e);
							$("#preview").get(0).style.display = 'none';
			}
		)
		$target.bind("mousemove", function(e){
			var $tooltip=$("#preview");
			ajaxtooltip.positiontip($tooltip, e)
		})
	})

}


	function truebody(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
	}
