/*
 * JTip
 * By Cody Lindley (http://www.codylindley.com) modified by Dj (dhanjay@oceanic.com.fj) on 30th March 2008
 * Under an Attribution, Share Alike License
 * JTip is built on top of the very light weight jquery library.
 */

function JT_show(elem,title,content,width){
	if(title == false)title="&nbsp;";
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(elem);
	var clickElementy = getAbsoluteTop(elem) - 5; //set y position
	
	if(width === undefined){width = 250};
	
	if(hasArea>((width*1)+75)){
		$("body").append("<div id='JT' style='width:"+width*1+"px'><div id='JT_arrow_left'></div><div id='JT_close_left'>"+title+"</div><div id='JT_copy'>"+content+"</div></div>");//right side
		var arrowOffset = getElementWidth(elem) + 11;
		var clickElementx = getAbsoluteLeft(elem) + arrowOffset; //set x position
	}else{
		$("body").append("<div id='JT' style='width:"+width*1+"px'><div id='JT_arrow_right' style='left:"+((width*1)+1)+"px'></div><div id='JT_close_right'>"+title+"</div><div id='JT_copy'>"+content+"</div></div>");//left side
		var clickElementx = getAbsoluteLeft(elem) - ((width*1) + 15); //set x position
	}
	
	$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#JT').show();

}

function getElementWidth(obj) {
	return obj.offsetWidth;
}

function getAbsoluteLeft(obj) {
	// Get an object left position from the upper left viewport corner
	o = obj
	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(obj) {
	// Get an object top position from the upper left viewport corner
	o = obj
	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
}