/**
 *  jQuery Tooltip Plugin
 *	@requires jQuery v1.2.6
 *  http://www.socialembedded.com/labs
 *
 *  Copyright (c)  Hernan Amiune (hernan.amiune.com)
 *  Dual licensed under the MIT and GPL licenses:
 *  http://www.opensource.org/licenses/mit-license.php
 *  http://www.gnu.org/licenses/gpl.html
 *
 *  Version: 1.0
 */

(function($){ $.fn.popupTooltip = function(options){

	if (!this.length) {
		// when no matching elements found do nothing
		return this;
	}

	var defaults = {
		persistent: false,
		position: 'cursor', // Where to open tooltip (at cursor or in window center)
		cssClass: "",     //CSS class or classes to style the tooltip
		delay : 0,        //The number of milliseconds before displaying the tooltip
		duration : 500,   //The number of milliseconds after moving the mouse cusor before removing the tooltip.
		xOffset : 15,     //X offset will allow the tooltip to appear offset by x pixels.
		yOffset : 15,     //Y offset will allow the tooltip to appear offset by y pixels.
		opacity : 0,      //0 is completely opaque and 100 completely transparent
		fadeDuration: 400,//[toxi20090112] added fade duration in millis (default = "normal")
		dontUseAjax : false,
		ajaxUrlAttributeName: 'href',
		loadingHTML: 'Loading ...',
		width: false,
		height: false,
		closeOnClick: true,
		closeOnEsc: true,
		closeButton: false,
		effect: false
	};

	var options = $.extend(defaults, options);

	this.data('options', options);
	var elements = this;

    $(document).ready(
    	function() {
    		var options = elements.data('options');
    		var divId = options.persistent ? 'divPopup' : 'divTooltip';

    		$tooltip = $('#'+divId);
    		if ($tooltip.length == 0) {
				$tooltip = $('<div id="'+divId+'"'+(divId == 'divPopup' ? ' style="z-index: 10;"' : '')+'></div>');
				$('body').append($tooltip);
				if (!options.persistent) $tooltip.data('allowFade', true);
				$tooltip.data('loadingContent', false);
				$tooltip.hide();

				if (!options.persistent) {
		    		$tooltip.mouseover(
						function() {
							// prevent main fade stuff
							$('#divTooltip').data('allowFade', false);
						}
					);

					$tooltip.mouseout(
						function() {
							clearTimeout($('#divTooltip').data("showTimeoutId"));
							$('#divTooltip')
								.data("hideTimeoutId", setTimeout("if ( $('#divTooltip').data('allowFade') ) { $.fadeOutTooltip(); }", options.duration))
								.data('allowFade', true);
						}
					);
				}
    		}
    	}
    );

	return this.each(function(index) {

		var $this = $(this);
		var divId = options.persistent ? 'divPopup' : 'divTooltip';
		$tooltip=$('#'+divId);
		var bindEvent = options.persistent ? 'click' : 'mouseover';

		//displays the tooltip
		$this.bind(bindEvent, function(e){
			//compatibility issue (need to check if it still exists)
			e = e ? e : window.event;
			e.preventDefault();

			var options = $this.data('options');

			var $tooltip = $(options.persistent ? '#divPopup' : '#divTooltip');
			$tooltip.data('options', options);
			$tooltip.data('mouseEvent', e);

			$tooltip.data('currentElement', $this);

			//don't hide the tooltip if the mouse is over the element again
			if (!options.persistent) {
				clearTimeout($tooltip.data("hideTimeoutId"));
			}

			//set the tooltip class
			$tooltip.removeClass($tooltip.attr("class"));
			$tooltip.css("width","");
			$tooltip.css("height","");
			if (options.cssClass) {
				var cssClasses = options.cssClass.split(' ');
				for (var i=0; i<cssClasses.length; i++) {
					$tooltip.addClass(cssClasses[i]);
				}
			}
			$tooltip.css("opacity",1-options.opacity/100);
			$tooltip.css("position","absolute");

			//save the title text and remove it from title to avoid showing the default tooltip
			var titleToSave = $this.attr("title") ? $this.attr("title") : $this.data("title");
			if (!titleToSave) titleToSave = '';
			$tooltip.data("title",titleToSave);
			$this.attr("title","");
			$tooltip.data("alt",$this.attr("alt"));
			$this.attr("alt","");

			var closeButtonHTML = options.closeButton ? '<div style="float:right; padding-right: 3px; padding-top: 3px; cursor: pointer;" onclick="$.closePopup();">'+options.closeButton+'</div>' : '';

			//set the tooltip content
			$tooltip.html(closeButtonHTML+$tooltip.data("title"));
//			updateCorners($tooltip);
			// [toxi20090112] only use ajax if there actually is an href attrib present
			if(!options.dontUseAjax) {
				var href=$this.attr(options.ajaxUrlAttributeName);
				if(href!=undefined && href != "#") {
					$tooltip.data('loadingContent', true);

					if (options.width) $tooltip.width(options.width);
					if (options.height) $tooltip.height(options.height);
					$tooltip.html(options.loadingHTML);
//					updateCorners($tooltip);
					$tooltip
						.positionTooltip(e, options)
						.showTooltip(options);

					$.get(
						href,
						function (data) {
							$tooltip.html(closeButtonHTML+data);
//							updateCorners($tooltip);
							$tooltip
								.positionTooltip($tooltip.data('mouseEvent'), options)
								.showTooltip(options)
								.data('loadingContent', false)
								.data('currentElement')
									.data("title",data)
									.removeAttr(options.ajaxUrlAttributeName);
						}
					);
				}
			}

			//set the tooltip position
			if (!$tooltip.data('loadingContent')) {
				$tooltip
					.positionTooltip(e, options)
					.showTooltip(options);
			}
		});

		if (!options.persistent) {
			$this.mouseout(function(e){
				//restore the title
				$this
					.attr("title",$('#divTooltip').data("title"))
					.attr("alt",$('#divTooltip').data("alt"));
				//don't show the tooltip if the mouse left the element before the delay time
				clearTimeout($('#divTooltip').data("showTimeoutId"));
				//start the timer to hide the tooltip
				//[toxi20090112] modified to make use of fadeDuration option
				$('#divTooltip').data("hideTimeoutId", setTimeout("if ( $('#divTooltip').data('allowFade') ) { $.fadeOutTooltip(); }",options.duration));
			});
		}

		$this.click(function(e){
			if (options.ajaxUrlAttributeName == 'href') {
		    	e.preventDefault();
			}
		});
	});

}})(jQuery);

(function($){ $.closeOnClick = function(e){

	$this = $(this);
    $tooltip = $("#divPopup");
	if ($tooltip.data('loadingContent')) return;
    var options = $tooltip.data('options');
    if (!options.persistent || !options.closeOnClick) return $this;

	var startX = $tooltip.position().left;
	var startY = $tooltip.position().top;
	var endX = startX + $tooltip.width();
	var endY = startY + $tooltip.height();

	if (e.pageX < startX || e.pageX > endX || e.pageY < startY || e.pageY > endY) {
		$.closePopup();
	}

}})(jQuery);

(function($){ $.closeOnEsc = function(e){

	$this = $(this);
    $tooltip = $("#divPopup");
    var options = $tooltip.data('options');
    if (!options.persistent || !options.closeOnEsc) return $this;

    if (e.keyCode == 27) {
		$.closePopup();
	}

}})(jQuery);

(function($){ $.closePopup = function(){

	var availableEffects = {
		fade: 'fadeOut', slide: 'slideUp'
	}

    $tooltip = $("#divPopup");
	$tooltip.triggerHandler('beforeclose');

    var options = $tooltip.data('options');
    options.effect && availableEffects[options.effect] ? $tooltip[availableEffects[options.effect]](options.fadeDuration) : $tooltip.hide();
    $tooltip.data('visible', false);

    $(document).unbind('click', $.closeOnClick).unbind('keyup', $.closeOnEsc);

}})(jQuery);

(function($){ $.fn.positionTooltip = function(e, options){

	if (options.position == 'center') {
		if (options.width) {
			this.width(options.width);
		}

		var $width = options.width ? options.width : this.width();

		if (options.height) {
			this.height(options.height);
		}

		var $height = options.height ? options.height : this.height();

		this.css(
			{
				left: Math.round(($(window).width() - $width) / 2),
				top: $(window).scrollTop() + Math.round(($(window).height() - $height) / 2)
			}
		);

		return this;
	}

	winw = $(window).width();
	w = options.width ? options.width : this.width();
	xOffset = options.xOffset;

	//right priority
	if(w+xOffset+50 < winw-e.clientX)
	  this.css("left", $(document).scrollLeft() + e.clientX+xOffset);
	else if(w+xOffset+50 < e.clientX)
	  this.css("left", $(document).scrollLeft() + e.clientX-(w+xOffset*2));
	else{
	  //there is more space at left, fit the tooltip there
	  if(e.clientX > winw/2){
		this.width(e.clientX-50);
		this.css("left", $(document).scrollLeft() + 25);
	  }
	  //there is more space at right, fit the tooltip there
	  else{
		this.width((winw-e.clientX)-50);
		this.css("left", $(document).scrollLeft() + e.clientX+xOffset);
	  }
	}

	if (options.width) {
		this.width(options.width);
	}

	winh = $(window).height();
	if (options.height) {
		this.height(options.height);
	}
	h = this.height();
	yOffset = options.yOffset;
	//top position priority
	if(h+yOffset + 50 < e.clientY)
	  this.css("top", $(document).scrollTop() + e.clientY-(h+yOffset));
	else if(h+yOffset + 50 < winh-e.clientY)
	  this.css("top", $(document).scrollTop() + e.clientY+yOffset);
	else
	  this.css("top", $(document).scrollTop() + 10);

    return this;

}})(jQuery);

(function($){ $.fn.showTooltip = function(options){

	if (this.data('visible')) return this;

	var availableEffects = {
		fade: 'fadeIn', slide: 'slideDown'
	}

	var $this = $(this);
	
	if (options.persistent) {
		options.effect && availableEffects[options.effect] ? this[availableEffects[options.effect]](options.fadeDuration) : this.show();
		if (options.closeOnClick) {
			setTimeout('$(document).click($.closeOnClick)', 0);
		}
		if (options.closeOnEsc) {
			setTimeout('$(document).keyup($.closeOnEsc)', 0);
		}
		this.data('visible', true);
	}
	else {
		var showMethod = options.effect && availableEffects[options.effect] ? availableEffects[options.effect] : 'show';
		this.data("showTimeoutId", setTimeout("$('#divTooltip')."+showMethod+"("+(showMethod == 'show' ? '' : options.fadeDuration)+").data('visible', true); ",options.delay));
	}

    return this;

}})(jQuery);

(function($){ $.fadeOutTooltip = function(options){

	var $tooltip = $('#divTooltip');

	if (!$tooltip.data('visible')) return this;

	var availableEffects = {
		fade: 'fadeOut', slide: 'slideUp'
	}

	var options = $tooltip.data('options');
	options.effect && availableEffects[options.effect] ? $tooltip[availableEffects[options.effect]](options.fadeDuration) : $tooltip.hide();

//	$tooltip.data('currentElement', false);
	$tooltip.data('visible', false);

    return this;

}})(jQuery);
