/* 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

*/



function mousePageXY(e)

{

  var x = 0, y = 0;



  if (!e) e = window.event;



  if (e.pageX || e.pageY)

  {

    x = e.pageX;

    y = e.pageY;

  }

  else if (e.clientX || e.clientY)

  {

    x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;

    y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;

  }



  return {"x":x, "y":y};

}



var tout;



var stickytooltip={

	tooltipoffsets: [-5, 10], //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, parent){

		

		var offset = $(parent).offset();

		//var mouse = mousePageXY(e);

		/*var x=e.pageX+this.tooltipoffsets[0], y=e.pageY+this.tooltipoffsets[1]

		var tipw=$tooltip.outerWidth(), tiph=$tooltip.outerHeight(), 

		x=(x+tipw>$(document).scrollLeft()+$(window).width())? x-tipw-(stickytooltip.tooltipoffsets[0]*2) : x

		y=(y+tiph>$(document).scrollTop()+$(window).height())? $(document).scrollTop()+$(window).height()-tiph-10 : y*/

		//$tooltip.css({left:mouse['x']-$tooltip.outerWidth()+this.tooltipoffsets[0], top:mouse['y']-$tooltip.outerHeight()+this.tooltipoffsets[1]})

		$tooltip.css({left:offset.left-$tooltip.outerWidth()+this.tooltipoffsets[0], top:offset.top-$tooltip.outerHeight()+this.tooltipoffsets[1]})

		//$tooltip.css({left:0, top:0})

	},

	

	showbox:function($, $tooltip, e, parent){

		//$tooltip.fadeIn(this.fadeinspeed)

		//alert($(parent).html());

		$tooltip.show()

		this.positiontooltip($, $tooltip, e, parent)

	},



	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){

		jQuery(document).ready(function($){

			var $targets=$(targetselector)

			var $tooltip=$('#'+tipid).appendTo(document.body)

			

			if ($targets.length==0)

				return

			var $alltips=$tooltip.find('div[id^=sticky]')

			if (!stickytooltip.rightclickstick)

				stickytooltip.stickynotice1[1]=''

			stickytooltip.stickynotice1=stickytooltip.stickynotice1.join(' ')

			stickytooltip.hidebox($, $tooltip)

			

			$targets.bind('mouseenter', function(e){

				$tooltip.stopTime("hidetooltip");

				

				$alltips.hide().filter('#'+$(this).attr('data-tooltip')).show()

				stickytooltip.showbox($, $tooltip, e, this)

			})

			$targets.bind('mouseleave', function(e){

				$tooltip.oneTime("1s", "hidetooltip", function() {stickytooltip.hidebox($, $tooltip);});

				//stickytooltip.hidebox($, $tooltip)

			})

			/*$targets.bind('mousemove', function(e){

				if (!stickytooltip.isdocked){

					stickytooltip.positiontooltip($, $tooltip, e)

				}

			})*/

			$tooltip.bind("mouseenter", function(){

				$tooltip.stopTime("hidetooltip");

				//stickytooltip.hidebox($, $tooltip)

			})

			

			$tooltip.bind("mouseleave", function(){

				$tooltip.oneTime("1s", "hidetooltip", function() {stickytooltip.hidebox($, $tooltip);});

				//stickytooltip.hidebox($, $tooltip)

			})

			

			$tooltip.bind("click", function(e){

				e.stopPropagation()

			})

			$(this).bind("click", function(e){

				if (e.button==0){

					stickytooltip.isdocked=false

					stickytooltip.hidebox($, $tooltip)

				}

			})

			

		}) //end dom ready

	}

}



stickytooltip.init("*[data-tooltip]", "tooltip")
