/* Sticky Tooltip script (v1.0)
* Created: Nov 25th, 2009. This notice must stay intact for usage 
* Author: Dynamic Drive at http://www.dynamicdrive.com/
* Visit http://www.dynamicdrive.com/ for full source code
*/

/** 
	Modified Sep 2010 to Object Oriented Programming.
	The original code was transformed to OOP.
	More than 1 tooltip is allowed.
*/

function stickytooltip(target_tipid, tipid)
{
	this.target_tipid = target_tipid;
	this.tipid = tipid;
	
	this.tooltipoffsets = [20, -30]; //additional x and y offset from mouse cursor for tooltips
	this.fadeinspeed= 200; //duration of fade effect in milliseconds
	this.rightclickstick= true; //sticky tooltip when user right clicks over the triggering element (apart from pressing "s" key) ?
	this.stickybordercolors= ["black", "darkred"]; //border color of tooltip depending on sticky state
	this.stickynotice1= "Press \"s\" or right click to sticky box"; //customize tooltip status message
	this.stickynotice2= "Click outside this box to hide it"; //customize tooltip status message
	
	this.alltips=null;
	this.tooltip=null;

	//***** NO NEED TO EDIT BEYOND HERE
	this.isdocked= false;

	this.positiontooltip=function($, e){
		var x=e.pageX+this.tooltipoffsets[0], y=e.pageY+this.tooltipoffsets[1]
		var tipw=this.tooltip.outerWidth(), tiph=this.tooltip.outerHeight(), 
		x=(x+tipw>$(document).scrollLeft()+$(window).width())? x-tipw-(this.tooltipoffsets[0]*2) : x
		y=(y+tiph>$(document).scrollTop()+$(window).height())? $(document).scrollTop()+$(window).height()-tiph-10 : y
		this.tooltip.css({left:x, top:y})
	}
	
	this.showbox=function($, e){
		this.tooltip.fadeIn(this.fadeinspeed);
		this.positiontooltip($, e);
	}

	this.hidebox=function($){
		if (!this.isdocked){
			this.tooltip.stop(false, true).hide();
			this.tooltip.css({borderColor:'black'});
			
			var $status = $("#"+this.tipid+"_status");
			$status.css("backgroundColor", this.stickybordercolors[0]);
			$status.html(this.stickynotice1);
		}
	}

	this.docktooltip=function($, e){
		this.isdocked=true;		
		this.tooltip.css({borderColor:'darkred'});
		
		var $status = $("#"+this.tipid+"_status");
		$status.css("backgroundColor", this.stickybordercolors[1]);
		$status.html(this.stickynotice2);
	},


	this.init=function(){
				var $this = this;
				
				var $targets=$("#"+this.target_tipid).find("*[data-tooltip]");
				this.tooltip=$('#'+this.tipid).appendTo(document.body);
				if ($targets.length==0)
					return;
				this.alltips = this.tooltip.children('div.atip');				
				if (!this.rightclickstick)
					this.stickynotice1[1]=this.stickynotice1[1].replace("or right click ", "");
				this.hidebox($);
				
				$targets.bind('mouseenter', 
							  	function(e){
									if (stickytooltip.current_stickytooltip != null && stickytooltip.current_stickytooltip != $this)
									{
										stickytooltip.current_stickytooltip.isdocked=false;
										stickytooltip.current_stickytooltip.hidebox($);
									}
										
									stickytooltip.current_stickytooltip = $this;
									$this.alltips.hide().filter('#'+$(this).attr('data-tooltip')).show();
									$this.showbox($, e);
								});
				$targets.bind('mouseleave', 
							  	function(e){
									if (!$this.isdocked) stickytooltip.current_stickytooltip = null;
									$this.hidebox($);
								});
				$targets.bind('mousemove', 
							  	function(e){
									if (!$this.isdocked){
										$this.positiontooltip($, e);
									}
								});
		
				this.tooltip.bind('mouseenter', 
							  	function(){
									$this.hidebox($)
								});
				this.tooltip.bind('click', 
							  	function(e){
									e.stopPropagation();
								});
				
				$(document).bind("click", 
					   function(e){
							if (e.button==0 && $this == stickytooltip.current_stickytooltip){
								$this.isdocked=false;
								$this.hidebox($);
								stickytooltip.current_stickytooltip = null;
						}
					});
				
				$(document).bind("contextmenu", 
					   function(e){
							if ($this.rightclickstick && $(e.target).parents().andSelf().filter("*[data-tooltip]").length==1 && $this == stickytooltip.current_stickytooltip){ //if oncontextmenu over a target element
								$this.docktooltip($, e);
								return false;
							}
					});
				
				$(document).bind('keypress', 
					   function(e){
							var keyunicode=e.charCode || e.keyCode;
							if (keyunicode==115 && $this == stickytooltip.current_stickytooltip){ //if "s" key was pressed
								$this.docktooltip($, e);
							}
					});
	}
}

var stickytooltip_global = {
	current_stickytooltip:null
}


