/*
 * JTip - By Cody Lindley (http://www.codylindley.com)
 * Under an Attribution, Share Alike License
 * Modified by Tyler Silcox for internal content
 */

// jQuery(document).ready(
// 	function(){
// 		jTip_ready();
// 	}
// );

// console.log('jTip loaded');

// TODO: title should be pulled from source div, if not found on target

jTip_ready = function(){
	// console.log('jTip_ready called');
	
	jQuery('.jTipSource').hide(); // would be nice to just hide the value from the ttSource attrs
	jQuery('.jTip')
	   .hover(
			function(){
				jTip_show(this.id, jQuery(this).attr('ttTitle'), jQuery(this).attr('ttWidth'), jQuery(this).attr('ttSource'));
			}, function(){
				jQuery('#jTip').remove()
			}
		)	
}

jTip_show = function(ttId, title, width, source){
	// console.log('jTip_show called');
	// console.log(arguments);

	if(title == undefined) title = 'More info...';
	if(width == undefined) width = 250;
	width = width * 1;
	if(source == undefined) source = ttId + '_source';
	
	var target = jQuery('#' + ttId);
	var offsetLeft = target.offset().left;
	var bodyWidth = jQuery('body').width();
	var posX = 0;
	var posY = target.offset().top - 5;
	
	if(width < (bodyWidth - offsetLeft)){
		// right side
		jQuery('body').append('<div id="jTip" style="width:' + width + 'px"><div id="jTip_arrow_left"></div><div id="jTip_title">' + title + '</div><div id="jTip_copy"></div></div>');
		posX = offsetLeft + (target.width() + 11); // compensates for arrow img
	}else{
		// left side
		jQuery('body').append('<div id="jTip" style="width:' + width + 'px"><div id="jTip_arrow_right" style="left:' + (width + 1) + 'px"></div><div id="jTip_title">'+title+'</div><div id="jTip_copy"></div></div>');
		posX = offsetLeft - (width + 15); 
	}
	
	// if(title.length == 0) $('#jTip_title').remove();
	
	jQuery('#jTip').css({left: posX + 'px', top: posY + 'px'});
	jQuery('#jTip_copy').html(jQuery('#' + source).html());
	jQuery('#jTip').show();
}