/**
 * Steps For Successful Tooltips:
 *	1.) Include "tooltip_v2.js" and "tooltip_v2.css" on the page and make sure "btn_learnMoreTT.png" is ftp'd to server
 *	2.) Identify an object on the page that when moused over will generate the tooltip
 *	3.) Give the object identified above a class of "tooltip_v2"
 *	4.) Give the object from step 2 a title attribute with the text that you would like within the tooltip
 *	5.) If you would like a Learn More Link in tooltip, include a hash symbol (#) and then the url at end of title attribute; ex: title=" ... #http://stayclassy.org"
 **/

var $t = jQuery.noConflict();
var currentTooltip;
var topOverride;
var learnMoreImage;
$t(document).ready(function(){
	//preLoadImage();
	listenersTooltip_v2();
});

function listenersTooltip_v2(){
	preLoadImage();
	
	//Event Listeners
	$t(".tooltip_v2").mouseenter(function(){
		var generated;
		var elementText = $t(this).attr('title');
		currentTooltip = $t(this);
		
		if ($t(this).attr('class').indexOf('#tooltipTop:') != -1){
			topOverride = $t(this).attr('class');
			topOverride = topOverride.split('#tooltipTop:')[1];
			topOverride = topOverride.split(' ')[0];
			topOverride = topOverride.split('px')[0];
			topOverride = parseInt(topOverride);
		}
		
		clearTimeout($t(this).data('timeoutId'));
		if (document.getElementById('tooltip_v2') == null)
			generated = new tooltip_v2($t(this), null, topOverride, elementText);
		
		if (document.getElementById('tooltip_v2') != null){
			$t("#tooltip_v2").fadeIn('fast').css('display','inline-block');
		}
		
	}).mouseleave(function(){
		thisElement = this;
		var timeoutId = setTimeout(function(){ $t("#tooltip_v2").fadeOut('fast',function(){$t(this).remove();});}, 300);
		$t(thisElement).data('timeoutId', timeoutId); //set the timeoutId, allowing us to clear this trigger if the mouse comes back over
	});
	
	$t("#tooltip_v2").live('mouseenter',function(){
		clearTimeout($t('.tooltip_v2').data('timeoutId'));
		//$t("#box2").fadeIn("slow");
	}).live('mouseleave',function(){
		thisElement = this;
		var timeoutId = setTimeout(function(){ $t("#tooltip_v2").fadeOut('fast',function(){$t(this).remove();});}, 300);
		$t('.tooltip_v2').data('timeoutId', timeoutId); //set the timeoutId, allowing us to clear this trigger if the mouse comes back over
	});
}

function tooltip_v2(target, startLeft, startTop, text){
	//Instance Vars
	this.positionLeft = null;
	this.positionTop = null;
	this.verbiage = text;
	
	if (startLeft != null){
		this.positionLeft = startLeft;
	}
	if (startTop != null){
		this.positionTop = startTop;
	}
	
	//Instance Methods
	this.returnTooltip = makeTooltip;
	
	//Default Action
	this.returnTooltip(target, this.positionLeft, this.positionTop, this.verbiage);
}

//Creates the tooltip and specifies location, content, etc.
function makeTooltip(element, left, top, content){
	var container = document.createElement("DIV");
	var innerText = document.createElement("DIV");
	var innerLinkContainer = document.createElement("DIV");
	var innerLink = document.createElement("a");
	var innerLinkImage = document.createElement("IMG");
	
	container.id = 'tooltip_v2';
	innerText.id = 'tooltipText';
	innerLinkContainer.id = 'innerLinkContainer';
	innerLink.id = 'innerLink';
	innerLinkImage.id = 'innerLinkImage';
	
	container.style.display = 'none';
	container.style.zIndex = '99999999';
	container.style.position = 'absolute';
	
	left = $t(element).position().left + $t(element).width()  + 15 + 'px';
	container.style.left = left;
	
	if (top == null){
		top = $t(element).position().top + 'px';
	}
	else{
		top = $t(element).position().top + top + 'px';
	}
	container.style.top = top;
	
	innerText.innerHTML = content.split('#')[0];
	//innerLinkImage.src = 'images/btn_learnMoreTT.png';
	innerLink.href = content.split('#')[1];
	innerLink.target = '_blank';
	
	innerLink.appendChild(learnMoreImage);
	
	innerLinkContainer.appendChild(innerLink);
	
	if (jQuery.trim(content.split('#')[1]) != '')
		innerText.appendChild(innerLinkContainer);	
	
	container.appendChild(innerText);
	
	if (document.getElementsByTagName('body') && document.getElementById('tooltip_v2') == null){
		document.getElementsByTagName('body')[0].insertBefore(container, document.getElementsByTagName('body')[0].firstChild);
	}
	
	//Color options implemented here
	var colors = $t(element).attr('class');
	if (colors.indexOf('#backgroundColor:') != -1 ){
		colors = colors.split('#backgroundColor:')[1];
		colors = colors.split(' ')[0];
		colors = colors.split(':endBackgroundColors')[0];
		$t('#tooltip_v2').css('background-color', colors);
	}
	
	return true;
}

function preLoadImage(){
	learnMoreImage = new Image(91,21);
	learnMoreImage.src = 'images/btn_learnMoreTT.png';
	learnMoreImage.title = 'Learn More';
}


















