/*
 * jQuery Tooltip Plugin
 * Author (c) Miroslav Peterka - wiki@seznam.cz
 * 2008 (c) Xacti Group
 * @version 0.0.3
 */


;(function($) {

	$.fn.tooltip = function ( option ){
		
		var options = {
			className:'tooltip',
			showDelay:100,
			animateLength:300,
			border:'1px solid #000000',
			fontSize:'0.9em',
			backgroundColor:'#fefee2',
			shadowColor:'black',
			bubbleClassName:'tooltipBubble',
			textColor:'#000000',
			padding:'5px 10px 5px 10px',
			maxWidth:300,
			alwaysTop:true,
			dartInCenter:false,
      openOnClick:false,
      textGetter:'title'
		};
    var last=null;

		this.each(function(i,el){
			var tooltip = new tooltipClass( $.extend(options, option) );
     
      if(typeof(tooltip.options.textGetter)=='string')
        tooltip.tooltipText=$(el).attr(tooltip.options.textGetter);
      else{
        tooltip.options.textGetter=$.extend({
          attr:'href',
          url:'',
          loading:'Loading...',
          error:'Service not available. Please try it later...'              
        },tooltip.options.textGetter);

        var attr_value=$(el).attr(tooltip.options.textGetter.attr);
        if(attr_value)
          var id=attr_value.substr(attr_value.lastIndexOf('#')+1)

        switch(tooltip.options.textGetter.type){
          case "id":
            tooltip.tooltipText=$('#'+id).html();
            break;
          case "ajax":
            tooltip.ajaxId=id;
            tooltip.tooltipText=tooltip.options.textGetter.loading;
            break;
        }
      }

			tooltip.el=el;
			$(el).attr('title','');

      if(tooltip.options.openOnClick){
        tooltip.options.showDelay=0;
        $(el).click(function(event){
          if(last) last.hide();
          if(!tooltip.lastTooltip){
            last=tooltip;
            tooltip.delayShow(event);
            $(document).click(function(){
              tooltip.hide()
              last=null;
            })
          }
        return false; 
        });
      }
      else{
        $(el).bind('mouseover, mousemove',function(event){tooltip.delayShow(event)}).mouseout(function(){tooltip.hide()});
      }
			$(el).data('tooltip',tooltip);
		});
		
		return this;

	};

	$.fn.tooltipText = function ( text , showAgain ){
		if(showAgain==null)showAgain=true;

		this.each(function(i,el){
			var tooltip = $(el).data('tooltip');
			if (tooltip){
				tooltip.tooltipText=text;
				if(tooltip.lastTooltip){
					tooltip.hide(showAgain);
					if(showAgain)
						tooltip.show();
				}
			}
		});
		return this;
	};


	function tooltipClass(options){
		this.options = options;
		this.timer=null;
		this.amimateTimer=null;
		this.lastTooltip=null;
		this.el=null;
		this.tooltipText='';
		this.mousePosition=null;
		this.reposition=false;
	};
	
	tooltipClass.prototype.delayShow=function(e){
		if(this.lastTooltip!=null)return true;

		this.mousePosition=gM(e);
		this.clearTimer();
		var that=this;
		var fce=function(){
			that.show();
      if(that.options.textGetter.type == 'ajax' && that.tooltipText==that.options.textGetter.loading){

          $.ajax({
            url:that.options.textGetter.url+that.ajaxId,
            success:function(text){
              $(that.el).tooltipText(text);
            },
            error:function(){
              $(that.el).tooltipText(that.options.textGetter.error);
            }
          });
      }
		}
		this.timer=window.setTimeout(fce,this.options.showDelay);
	};


	tooltipClass.prototype.clearTimer=function(){
		if(this.timer!=null){
			window.clearTimeout(this.timer);
			this.timer=null;
		}
	};
	
	tooltipClass.prototype.show=function(){
		var a=$('<div></div>');
		var b=$('<div>'+this.tooltipText+'</div>');
		this.lastTooltip=a;
		a.css({
			minHeight:'1px',
			position:'absolute',
			opacity:0,
			zIndex:2000,
			left:0,
			top:0,
			padding:'6px 2px 6px 0'
		});

		b.addClass(this.options.bubbleClassName);
		b.css({
			minHeight:'1px',
			border:this.options.border,
			backgroundColor:this.options.backgroundColor,
			color:this.options.textColor,
			padding:this.options.padding,
			fontSize:this.options.fontSize,
			lineHeight:"1.2em",
			zIndex:'2'
		});

		a.append(b).appendTo('body');

		var width=(b.get(0).offsetWidth >this.options.maxWidth )?this.options.maxWidth:a.get(0).offsetWidth;

 		if($.browser.msie){
      var ie_version=navigator.userAgent.match( /MSIE ([\d.]+)/ )[1];
      if (ie_version<7){
        a.css('height','1px');
        b.css('height','1px');
      }
		}

		var	repositionDart=false;
		var left=this.options.dartInCenter ? this.mousePosition.x-width/2 : this.mousePosition.x-20;

		var wind=gW();
		var scroll=gSr();
		
		if(left<scroll.x){
			left=scroll.x;
			repositionDart=true;
		}

		a.css({
			left:left+'px',
			top:this.mousePosition.y+24+"px",
			width:width+'px'
		});


		var size={x:a.get(0).offsetWidth,y:a.get(0).offsetHeight};
		var position={x:a.get(0).offsetLeft,y:a.get(0).offsetTop};
		if(this.options.alwaysTop == true)
			this.reposition=(position.y-size.y<scroll.y+24)
		else
			this.reposition=(position.y+size.y-scroll.y<wind.y);



		var dart=$('<div></div>');
		dart.css({
			position:'absolute',
			'left':this.options.dartInCenter ? width/2-8+'px' : '15px',
			'z-index':3,
			'opacity':1
		});

		for(var i=6,j=0;i>-1;i--,j++){
			$('<div>').css({
				positon:'absolute',
				overflow:'hidden',
				height:'1px',
				width:((this.reposition?j:i)*2)+'px',
				'margin-left':(this.reposition?i:j)+'px',
				top:'6px',
				'border-left':this.options.border,
				'border-right':this.options.border,
				background:this.options.backgroundColor
			}).appendTo(dart);
		}
		a.append(dart);
		
		if(size.x+position.x-scroll.x>wind.x){
			var div=(size.x+position.x-scroll.x)-wind.x;
			a.css('left',position.x-div+"px");
			if(this.options.dartInCenter)
				dart.css('left',((size.x/2+div > size.x-22) ? size.x-22 : size.x/2-8+div )+"px");
			else
				dart.css('left',((30+div<size.x)?div+10:size.x-30)+"px");
		}
		
		if(repositionDart){
			var div=(this.mousePosition.x-scroll.x);
			dart.css('left',div+"px");
		}

		if(this.reposition){
			dart.css('top',"0px");
			a.css('top',a.get(0).offsetTop+20).animate({opacity:1,top:'-=20px'},this.options.animateLength,function(){if(this.style.removeAttribute)this.style.removeAttribute('filter')});

		}
		else{
			a.css('top',this.mousePosition.y-a.get(0).offsetHeight-10+'px');
			dart.css('top',a.get(0).offsetHeight-7+'px');
	
			a.css('top',a.get(0).offsetTop-20).animate({opacity:1,top:'+=20px'},this.options.animateLength,function(){if(this.style.removeAttribute)this.style.removeAttribute('filter')});

		}
		
		//a.fadeTo(this.options.animateLength,'1');
		


		a.append(this.shadow(1,b.get(0).offsetHeight-1,8,b.get(0).offsetWidth,0.3));
		a.append(this.shadow(b.get(0).offsetWidth-1,1,b.get(0).offsetHeight+6,1,0.3));
		a.append(this.shadow(1,b.get(0).offsetHeight-2,9,b.get(0).offsetWidth+1,0.1));
		a.append(this.shadow(b.get(0).offsetWidth-1,1,b.get(0).offsetHeight+7,2,0.1));
	};


tooltipClass.prototype.shadow=function(width,height,top,left,opacity){
	var a=$('<div></div>');
	a.css({
		position:'absolute',
		overflow:'hidden',
		top:top+'px',
		left:left+'px',
		'z-index':1,
		width:width+'px',
		height:height+'px',
		opacity:opacity,
		background:this.options.shadowColor
	});
	return a;
};

tooltipClass.prototype.hide=function(showAgain){

	if(showAgain==null)showAgain=true;

	this.clearTimer();
	if(this.lastTooltip!=null){
		this.lastTooltip.remove();

		if(showAgain)
			this.lastTooltip=null;
	}
};


/*helper functions */
function gM(e){var e=e||window.event;var d=(document.documentElement&&document.documentElement.scrollTop!=null)?document.documentElement:document.body;return {x:(e.pageX||e.clientX+d.scrollLeft),y:(e.pageY||e.clientY+d.scrollTop)}}
function gW() {if (window.innerWidth) return {'x':window.innerWidth-20,'y':window.innerHeight-15};else if (document.documentElement.clientHeight) {return {'x':document.documentElement.clientWidth,'y':document.documentElement.clientHeight};}else if (document.body.clientWidth) {return {'x':document.body.clientWidth,'y':document.body.clientHeight}}}
function gSr(){var t=(document.documentElement.scrollTop)?document.documentElement.scrollTop:document.body.scrollTop;var l=(document.documentElement.scrollLeft)?document.documentElement.scrollLeft:document.body.scrollLeft;return {'x':l,'y':t}}



})(jQuery);