/* 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(jQuery) {

	jQuery.fn.jHelperTip = function(options) {
		// merge users option with default options
		var opts = jQuery.extend({}, jQuery.fn.jHelperTip.defaults, options);

		// default actions
		// create a ttC is not found
		if (jQuery(opts.ttC).length == 0)
			jQuery('<div id="'+opts.ttC.slice(1)+'"></div>').appendTo("body");

		// create a dC is not found
		if (jQuery(opts.dC).length == 0)
			jQuery('<div id="'+opts.dC.slice(1)+'"></div>').appendTo("body");

		if (jQuery(opts.aC).length == 0)
			jQuery('<div id="'+opts.aC.slice(1)+'"></div>').appendTo("body");


		// initialize our tooltip and our data container and also the close box
		jQuery(opts.ttC).add(opts.aC).css({
			position: "absolute",
			display: "inline"
		}).hide();

		jQuery(opts.dC).hide();

		// close the tooltip box
		var closeBox = function(){
			if (opts.source == "attribute")
				jQuery(opts.aC).hide().empty();
			else
				jQuery(opts.ttC).hide().empty();
		};

		jQuery(".jHelperTipClose").bind("click", closeBox);
		jQuery(opts.ttC).bind("mouseover",function(){
			jQuery(opts.ttC).show();
			return false;
		});

		// the sources of getting data
		var getData = function(obj,e){
			if (opts.source == "ajax") {
				getPosition(e);
				jQuery(opts.ttC).html('<div><img src="'+opts.loadingImg+'"/> '+opts.loadingText+'</div>').show();

				jQuery.ajax({
					type: opts.type,
					url: opts.url,
					data: opts.data,
					success: function(msg){
						jQuery(opts.ttC).html(msg);
						// reInitialize the close controller
						jQuery(".jHelperTipClose").unbind("click", closeBox);
						jQuery(".jHelperTipClose").bind("click", closeBox);
					}
				});
			}

			else if (opts.source == "container"){
				jQuery(opts.ttC).empty().show();
				jQuery(opts.dC).clone(true).appendTo(opts.ttC).show();
			}

			if (opts.source == "attribute"){
				jQuery(opts.aC).html(jQuery(obj).attr(opts.attrName));
			}
		};

		// used to position the tooltip
		var getPosition = function (e){
			var top = e.pageY+opts.topOff;
			var left = e.pageX+opts.leftOff;

			if (opts.source == "attribute"){
				jQuery(opts.aC).css({
					top: top,
					left: left,
					opacity: opts.opacity
				}).show();
			}
			else {
				jQuery(opts.ttC).css({
					top: top,
					left: left,
					opacity: opts.opacity
				}).show();
			}
		};

		// just close tool tip when not needed usually trigger by anything outside out tooltip target
		if (opts.trigger == "hover") {
			jQuery(this).bind("mouseover", function(e){
				e.preventDefault();
				getData(this, e);
				return false;
			});
			jQuery(this).bind("mousemove", function(e){
				getPosition(e);
				return false;
			});

			jQuery(this).bind("mouseout", function(e){
				if (opts.source == "attribute")
					jQuery(opts.aC).hide().empty();
				else
					jQuery(opts.ttC).hide().empty();
				return false;
			});
		}

		else if (opts.trigger == "click") {
			jQuery(this).bind("click", function(e){
				getData(this, e);
				getPosition(e);
				jQuery(document).bind("click", function(e){
					if (opts.autoClose) {
						if (opts.source == "attribute")
							jQuery(opts.aC).hide().empty();
						else
							jQuery(opts.ttC).hide().empty();
					}
				});

				return false;
			});

		}
	};

	jQuery.fn.jHelperTip.defaults = {
		trigger: "click",
		topOff: 3,
		leftOff: 10,
		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);