/*
 * 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.
 *
 *		Modified by BOM Media
 *		The text is included in the htmlpage 
 */

//on page load (as soon as its ready) call JT_init
$(document).ready(JT_init);

function JT_init(){
	       $("a.jTip")
		   .hover(
				function(){JT_show(this.href,this.id,this.name,false)},
				function(){$('#JT').remove()})
           .click(
				function(){return false});
			$("a.clickTip")
           .click(
				function(){
					$('#JT').remove();
					JT_show(this.href,this.id,this.name,true);
					$('#CLOSE').click(
						function(){	
								$('#JT').remove();
								return false;
						}
						);
					return false;
				}
			);
}

function JT_show(url,linkId,title,hasCloseLink,headColor){
	if(title == false)title="&nbsp;";
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - 3; //set y position
	
	var textId = url.replace(/^.*\//,'');
	
	try{
		var textElement= document.getElementById(textId);
		var text = textElement.innerHTML;
	}catch(e){
		var text = "Empty string";
	}

	try{
		if( hasCloseLink ){
			title= "<div style='height: 25px;background-color:#085c07;color:white;'><p style='float:left;'>"+title + 
				"</p><p style='float:right;'><a href='' id='CLOSE' style='color:white;'> [X] </a>&nbsp;&nbsp;</p>" +
				"</div>";
		}
	}catch(e){
		//nothing
	}

	if( text.length > 100 ){
		var width = 540;
	}else{
		var width = text.length*5 +40;
	}

	if(hasArea>((width*1)+75)){
		$("body").append("<div id='JT' style='border-color:#085c07;color:white;width:"+width*1+
			"px'><div id='JT_arrow_left' style='margin-top:4px;'></div><div id='JT_close_left' style='background-color:#085c07;color:white;'>"+
			title+
			"</div><div id='JT_copy'><p>"+ text +"</p></div></div>");//right side
		var arrowOffset = getElementWidth(linkId) + 11;
		var clickElementx = getAbsoluteLeft(linkId) + arrowOffset; //set x position
	}else{
		$("body").append("<div id='JT' style='border-color:#085c07;color:white;width:"+width*1+
			"px'><div id='JT_arrow_right' style='margin-top:4px;left:"+((width*1)+1)+
			"px'></div><div id='JT_close_right' style='background-color:#085c07;color:white;'>"+
			title+
			"</div><div id='JT_copy'>"+text+"</div></div>");//left side
		var clickElementx = getAbsoluteLeft(linkId) - ((width*1) + 15); //set x position
	}

	$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#JT').show();
}

function getElementWidth(objectId) {
	x = document.getElementById(objectId);
	return x.offsetWidth;
}

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
}
