/* 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
*/


var stickytooltipDescr={
	tooltipoffsets: [-20, -20], //additional x and y offset from mouse cursor for tooltips
	fadeinspeed: 200, //duration of fade effect in milliseconds
	rightclickstick: true, //sticky tooltip when user right clicks over the triggering element (apart from pressing "s" key) ?
	stickybordercolors: ["black", "darkred"], //border color of tooltip depending on sticky state
	stickynotice1: ["Press \"s\"", "or right click", "to sticky box"], //customize tooltip status message
	stickynotice2: "Click outside this box to hide it", //customize tooltip status message

	//***** NO NEED TO EDIT BEYOND HERE

	isdocked: false,

	positiontooltip:function($, $tooltip, e){
		var x=e.pageX+this.tooltipoffsets[0], y=e.pageY+this.tooltipoffsets[1] - $tooltip.outerHeight();
		var tipw=$tooltip.outerWidth(), tiph=$tooltip.outerHeight();
		x=(x+tipw>$(document).scrollLeft()+$(window).width())? x-tipw-(stickytooltipDescr.tooltipoffsets[0]*2) : x
		y=(y+tiph>$(document).scrollTop()+$(window).height())? $(document).scrollTop()+$(window).height()-tiph-10 : y
		$tooltip.css({left:x, top:y})
	},
	
	showbox:function($, $tooltip, e){
		$tooltip.fadeIn(this.fadeinspeed)
		this.positiontooltip($, $tooltip, e)
	},

	hidebox:function($, $tooltip){
		if (!this.isdocked){
			$tooltip.stop(false, true).hide()
			$tooltip.css({borderColor:'black'}).find('.stickystatus:eq(0)').css({background:this.stickybordercolors[0]}).html(this.stickynotice1)
		}
	},

	docktooltip:function($, $tooltip, e){
		this.isdocked=true
		$tooltip.css({borderColor:'darkred'}).find('.stickystatus:eq(0)').css({background:this.stickybordercolors[1]}).html(this.stickynotice2)
	},


	init:function(targetselector, tipid, a){
		if(!a)
		{
		jQuery(document).ready(function($){
			var $targets=$(targetselector)
			var $tooltip=$('#'+tipid).appendTo(document.body)
			if ($targets.length==0)
				return
			var $alltips=$tooltip.find('div.atip')
			if (!stickytooltipDescr.rightclickstick)
				stickytooltipDescr.stickynotice1[1]=''
			stickytooltipDescr.stickynotice1=stickytooltipDescr.stickynotice1.join(' ')
			stickytooltipDescr.hidebox($, $tooltip)
			$targets.bind('mouseenter', function(e){
				$alltips.hide().filter('#'+$(this).attr('data-descr')).first('#'+$(this).attr('data-descr')).show()
				stickytooltipDescr.showbox($, $tooltip, e)
			})
			$targets.bind('mouseleave', function(e){
				stickytooltipDescr.hidebox($, $tooltip)
			})
			$targets.bind('mousemove', function(e){
				if (!stickytooltipDescr.isdocked){
					stickytooltipDescr.positiontooltip($, $tooltip, e)
				}
			})
			$tooltip.bind("mouseenter", function(){
				stickytooltipDescr.hidebox($, $tooltip)
			})
			$tooltip.bind("click", function(e){
				e.stopPropagation()
			})
			$(this).bind("click", function(e){
				if (e.button==0){
					stickytooltipDescr.isdocked=false
					stickytooltipDescr.hidebox($, $tooltip)
				}
			})
			$(this).bind("contextmenu", function(e){
				if (stickytooltipDescr.rightclickstick && $(e.target).parents().andSelf().filter(targetselector).length==1){ //if oncontextmenu over a target element
					stickytooltipDescr.docktooltip($, $tooltip, e)
					return false
				}
			})
			$(this).bind('keypress', function(e){
				var keyunicode=e.charCode || e.keyCode
				if (keyunicode==115){ //if "s" key was pressed
					stickytooltipDescr.docktooltip($, $tooltip, e)
				}
			})
		}) //end dom ready
		}
		else {
			var $targets=$(targetselector)
			/*tipid=mystickytooltipDescr*/
			var $tooltip=$('#'+tipid).appendTo(document.body)
			if ($targets.length==0)
				return
			var $alltips=$tooltip.find('div.atip')
			if (!stickytooltipDescr.rightclickstick)
				stickytooltipDescr.stickynotice1[1]=''
			
			stickytooltipDescr.hidebox($, $tooltip);
			$targets.bind('mouseenter', function(e){
				$alltips.hide().filter('#'+$(this).attr('data-descr')).first('#'+$(this).attr('data-descr')).show()
				stickytooltipDescr.showbox($, $tooltip, e)
			})
			$targets.bind('mouseleave', function(e){
				stickytooltipDescr.hidebox($, $tooltip)
			})
			$targets.bind('mousemove', function(e){
				if (!stickytooltipDescr.isdocked){
					stickytooltipDescr.positiontooltip($, $tooltip, e)
				}
			})
			$tooltip.bind("mouseenter", function(){
				stickytooltipDescr.hidebox($, $tooltip)
			})
			$tooltip.bind("click", function(e){
				e.stopPropagation()
			})
			$(this).bind("click", function(e){
				if (e.button==0){
					stickytooltipDescr.isdocked=false
					stickytooltipDescr.hidebox($, $tooltip)
				}
			})
			$(this).bind("contextmenu", function(e){
				if (stickytooltipDescr.rightclickstick && $(e.target).parents().andSelf().filter(targetselector).length==1){ //if oncontextmenu over a target element
					stickytooltipDescr.docktooltip($, $tooltip, e)
					return false
				}
			})
			$(this).bind('keypress', function(e){
				var keyunicode=e.charCode || e.keyCode
				if (keyunicode==115){ //if "s" key was pressed
					stickytooltipDescr.docktooltip($, $tooltip, e)
				}
			})
		}
	}
}

//stickytooltipDescr.init("targetElementSelector", "tooltipcontainer")
