/* Copyright (c) 2008 Kean Loong Tan http://www.gimiti.com/kltan
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Copyright notice and license must remain intact for legal use
 * jHelpertip
 * Version: 1.0 (Jun 2, 2008)
 * Requires: jQuery 1.2+
 */
(function($) {
	$.fn.jHelperTip = function(options) {
		// merge users option with default options
		var opts = $.extend({}, $.fn.jHelperTip.defaults, options);
		
		// default actions
		// create a ttC is not found
		if ($(opts.ttC).length == 0)
			$('<div id="'+opts.ttC.slice(1)+'"></div>').appendTo("body");
		
		// create a dC is not found
		if ($(opts.dC).length == 0)
			$('<div id="'+opts.dC.slice(1)+'"></div>').appendTo("body");
		
		if ($(opts.aC).length == 0)
			$('<div id="'+opts.aC.slice(1)+'"></div>').appendTo("body");

		
		// initialize our tooltip and our data container and also the close box
		$(opts.ttC).add(opts.aC).css({
			position: "absolute",
			display: "inline"
		}).hide();
		$(opts.dC + $(this).attr('id')).hide();
		
		// close the tooltip box
		var closeBox = function(){
			if (opts.source == "attribute")
				$(opts.aC).hide().empty();
			else
				$(opts.ttC).hide().empty();
		};
		
		$(".jHelperTipClose").bind("click", closeBox);
		$(opts.ttC).bind("mouseover",function(){
			$(opts.ttC).show();
			return false;
		});

		// the sources of getting data
		var getData = function(obj,e){
			if (opts.source == "ajax") {
				var retour = getPosition(obj,e);
				$(opts.ttC).html('<div><img src="'+opts.loadingImg+'"/> '+opts.loadingText+'</div>').show();
				if(opts.attrName.length>0){
					newData = opts.data + $(obj).attr(opts.attrName);
				}
				else{
					newData = opts.data;
				}
				$.ajax({
					type: opts.type,
					url: opts.url,
					data: newData, 
					success: function(msg){
						$(opts.ttC).html(msg);
						// reInitialize the close controller
						$(".jHelperTipClose").unbind("click", closeBox); 
						$(".jHelperTipClose").bind("click", closeBox);
						var div = document.createElement('div');
						if(retour == 'left') $(div).addClass('arrowLeft');
						else $(div).addClass('arrowRight');
						$(opts.ttC+' h3').before($(div));
					}
				});
			}
			
			else if (opts.source == "container"){
				orig = opts.dC;
				opts.dC += $(obj).attr('id');
				$(opts.ttC).show().empty();
				$(opts.dC).clone(true).show().appendTo(opts.ttC);
				$(opts.dC).hide();
				opts.dC = orig;
			}
			
			if (opts.source == "attribute"){
				$(opts.aC).html($(obj).attr(opts.attrName));
			}
		};
		var getAbsoluteLeft = function(o) {
			// Get an object left position from the upper left viewport corner
			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;
		}

		var getAbsoluteTop = function(o) {
			// Get an object top position from the upper left viewport corner
			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;
		}		
		
		
		// used to position the tooltip
		var getPosition = function (obj,e){
			var top = e.pageY+opts.topOff;
			var left = e.pageX+opts.leftOff;
			var de = document.documentElement;
			var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
			var hasArea = w - getAbsoluteLeft(obj);
			var top = getAbsoluteTop(obj) - 3 + +opts.topOff; //set y position
			if(hasArea>$(opts.ttC).width()+75){
				var arrowOffset = obj.offsetWidth;
				var left = getAbsoluteLeft(obj) + arrowOffset +opts.leftOff; //set x position
				var retour = 'left';
			}
			else{
				var retour = 'right';
				var arrowOffset = obj.offsetWidth;
				var left = getAbsoluteLeft(obj) - $(opts.ttC).width()  + +opts.leftOff; //set x position
			}
			
			if (opts.source == "attribute"){
				$(opts.aC).css({
					top: top,
					left: left,
					opacity: opts.opacity
				}).show();
			}
			else {
				$(opts.ttC).css({
					top: top,
					left: left,
					opacity: opts.opacity
				}).show();
			}
			return retour;
		}

		// just close tool tip when not needed usually trigger by anything outside out tooltip target
		if (opts.trigger == "hover") {
			$(this).bind("mouseover", function(e){
				e.preventDefault();
				getData(this, e);
				getPosition(this,e);
				return false;
			});
			
			$(this).bind("mouseout", function(e){
			    if (opts.source == "attribute")
					$(opts.aC).hide().empty();
				else
					$(opts.ttC).hide().empty();
				return false;
			});
		}
		
		else if (opts.trigger == "click") {
			$(this).bind("click", function(e){
				getData(this, e);
				getPosition(this,e);
				$(document).bind("click", function(e){
					if (opts.autoClose) {
						if (opts.source == "attribute")
							$(opts.aC).hide().empty();
						else
							$(opts.ttC).hide().empty();
					}
				});
				
				return false;
			});

		}
	};
	
	$.fn.jHelperTip.defaults = {
		trigger: "click",
		topOff: 2,
		leftOff: 0,
		source: "container", /* attribute, container, ajax */
		attrName: '',
		ttC: "#jHelperTipContainer", /* tooltip Container*/
		dC: "#jHelperTipDataContainer", /* data Container */
		aC: "#jHelperTipAttrContainer", /* attr Container */
		opacity:  1.0,
		loadingImg: "ajax-loader.gif",
		loadingText: "Loading...",
		type: "GET", /* data can be inline or CSS selector */
		//url: '',
		//data: '',
		autoClose: true
	};
		
	  

})(jQuery);