﻿/**************************************
AssetSP tooltip (Jquery 1.4.3)
*************************************/
(function($){  
	$.assetspTips = function (settings0) {
		var assetspTipsFrame= '<div class="assetspTipsFrame"></div>';
		$('.assetspTipsFrame').assetspTipsHideAll();
		$('.assetspTipsFrame').remove();
		$('body').append(assetspTipsFrame);
		settings0.hover=0;
		settings0.tail=0;
		$('.assetspTipsFrame').assetspTips(settings0);
	}
	$.assetspTipsShow = function () {
		$('.assetspTipsFrame').trigger("showTtooltip");
		return false;
	}
	$.assetspTipsHide = function () {
		$('.assetspTipsFrame').trigger("hideTtooltip");
		return false;
	}

	$.fn.assetspTipsShow = function () {
        $(this).each(function (d, e) {
            $(e).trigger("showTtooltip");
            return false;
        });
	}
	$.fn.assetspTipsHide = function () {
        $(this).each(function (d, e) {
            $(e).trigger("hideTtooltip");
            return false;
        });
	}
	$.fn.assetspTipsHideAll = function () {
        $(this).each(function (d, e) {
            $(e).trigger("hideTtooltip");
        });
	}
	
	$.fn.assetspTips = function (settings0) {
		settings0 = jQuery.extend({
			theme:		'green',
			html:		'title',
			speed :		0,
			ofstart:0,
			tail:"bottom",
			hover:true,
			mouse:false,
			staticpos:{}
		},settings0);
		if ($(this).data("settings"))settings0 = jQuery.extend($(this).data("settings"),settings0);
		$(this).data("settings",settings0);
		if ($(this).data("initip")==1)return;
		var settings=$(this).data("settings");
		
		var tipName = settings.theme+ 'Tip';
		
		var cGlUI=tipName +getGlUID();
		while ($("#"+cGlUI).length){
			cGlUI =tipName +getGlUID();
		}
		var divTip = '#'+cGlUI ;//'div.'+tipName;

		var tipFrame = '';//'<div id="'+cGlUI +'" class="' + tipName + '"><div class="content"></div><div class="bottom">&nbsp;</div></div>';
		var animvalue,animvalue2;
		var astTip;
		var tText,ee,cee;
		var cee=0;
		var node=$(this);
		$(this).data("initip",1);
		function getGlUID(){
		   return  Math.floor(Math.random()*50000);
		} 
		var _checkViewport= function(nPos,swit,ee,cee) {
			Page.initialize();
			var offset = node.offset();
			var x,y;
			switch (swit){
				case "left":
					x=ee;
					y=nPos.top;
				break;
				case "top":
					x=nPos.left;
					y=ee;
				break;
				case "mouse":
					x=ee;
					y=cee;
				break;
			}//alert(x+"="+y);
			var width = astTip.outerWidth();
			var height = astTip.outerHeight();	
			// Greater than x viewport
			//alert(x+" = "+width+" = "+ Page.dimensions.width+" = "+offset.left);
			if ((x + width) > Page.dimensions.width){
				x = (offset.left - width);
				$(".tiptail").hide();
			}
	
			// Less than x viewport
			else if (x < 0){
				x = 15;
				$(".tiptail").hide();
			}

			// Greater than y viewport
			if ((y + height) > (Page.scroll.top + Page.dimensions.height))
				y = y - ((y + height) - (Page.scroll.top + Page.dimensions.height));
	
			// Node on top of viewport scroll
			else if ((offset.top ) < Page.scroll.top){
				y = offset.top + node.outerHeight()+15;
				$(".tiptail").hide();
			}
			// Less than y viewport scrolled
			else if (y < Page.scroll.top){//alert(y +"="+ Page.scroll.top);
				y = Page.scroll.top + 15;
				y = offset.top + node.outerHeight()+15;
				$(".tiptail").hide();
			}
			// Less than y viewport
			else if (y < 0)
				y = 15;
			//alert(x+"="+y);
			switch (swit){
				case "top":
					animvalue={opacity: 1,top: y,left: x };
					animvalue2={opacity: 0,top: cee};
				break;
				case "left":
					animvalue={opacity: 1,left: x };
					animvalue2={opacity: 0,left: cee};
				break;
				case "mouse":
					animvalue={opacity: 1,top: y,left: x };
					animvalue2={opacity: 0,top: cee};
				break;
			}
			return;
		}
		


		var hover1=function(event) {
			var tipFrame = '<div id="'+cGlUI +'" class="' + tipName + '"><div class="tiptop tiptail"></div><div class="tipleft tiptail"></div><div class="tipright tiptail"></div><div class="content"></div><div class="tipbottom tiptail"></div></div>';
			if (!$("#"+cGlUI).length)$('body').append(tipFrame);
			astTip = $(divTip);
			astTip.hide();
			$(".tiptail").hide();
			if (settings.html=== 'title') {
				var tipCont = $(this).attr('title');
			} else if (settings.html!== 'title') {
				var tipCont = settings.html;//alert(settings.html);
			}
			$(divTip + ' .content').html(tipCont);
			$(divTip + ' .tip'+settings.tail).show();
			tText = $(this).attr('title');
			$(this).attr('title', '');
			
			var pos = $(this).offset();
			var nPos = pos;
			


			var swit="";
			switch(settings.tail){
				case "top":
					var yOffset = $(this).height() + 2;
					var xOffset = (astTip.width() / 2) - ($(this).width() / 2);
					ee = pos.top + $(this).height();
					nPos.left = pos.left - xOffset;
					nPos.top= cee = pos.top + $(this).height() + settings.ofstart;
					animvalue={opacity: 1,top: ee };
					animvalue2={opacity: 0,top: cee};
					swit="top";
				break;			
				case "left":
					var yOffset = $(this).height() + 2;
					var xOffset = -$(this).width()-11;
					ee = pos.left - xOffset;
					nPos.left = cee = pos.left - xOffset+settings.ofstart;
					nPos.top= pos.top - ($(this).height()/2);
					animvalue={opacity: 1,left: ee };
					animvalue2={opacity: 0,left: cee};
					swit="left";
				break;			
				case "right":
					var yOffset = $(this).height() + 2;
					var xOffset = astTip.width()+11;
					ee = pos.left - xOffset;
					nPos.left = cee = pos.left - xOffset-settings.ofstart;
					nPos.top= pos.top - ($(this).height()/2);
					animvalue={opacity: 1,left: ee };
					animvalue2={opacity: 0,left: cee};
					swit="left";
				break;
				case "bottom":
				default:
					var yOffset = astTip.height() + 2;
					var xOffset = (astTip.width() / 2) - ($(this).width() / 2);
					ee = pos.top - yOffset;
					nPos.left = pos.left - xOffset;
					nPos.top = cee = pos.top - yOffset -settings.ofstart;
					animvalue={opacity: 1,top: ee };
					animvalue2={opacity: 0,top: cee};
					swit="top";
				break;			
			}
			if (settings.mouse){
					var yOffset = $(this).height() + 2;
					var xOffset = (astTip.width() / 2) - ($(this).width() / 2);
					
					nPos.left =  ee =event.pageX+15;
					nPos.top= cee = event.pageY+10;
					animvalue={opacity: 1,top: ee };
					animvalue2={opacity: 0,top: cee};
					swit="mouse";
				
			}
			_checkViewport(nPos,swit,ee,cee);




			astTip.css('position', 'absolute').css('z-index', '1000');
			astTip.css('display', 'block').css('opacity', '0');
			if (settings.staticpos.pos){
				astTip.css('position', settings.staticpos.pos);
				nPos.top=settings.staticpos.x ;
				nPos.left=settings.staticpos.y;
				ofx=settings.staticpos.offset?parseInt(settings.staticpos.offset.x):0;
				ofy=settings.staticpos.offset?parseInt(settings.staticpos.offset.y):0;
				if (nPos.top=="100%")astTip.css('margin-top', -astTip.height()-ofx);
				if (nPos.left=="100%")astTip.css('margin-left', -astTip.width()-ofy);
				animvalue={opacity: 1};
				animvalue2={opacity: 0};
			}
			astTip.css(nPos);
			astTip.animate(animvalue, settings.speed, function() {
			  });
			
		};

		var hover2=function(event) {
			$(this).attr('title', tText);
		
			if(astTip)astTip.animate(animvalue2, settings.speed, function() {
			    $(this).remove();
			  });
  			
			
			
		}
		$(this).bind("showTtooltip", hover1);
		$(this).bind("hideTtooltip", hover2);
		if(settings.mouse){
			$(this).bind("mousemove",hover1);
			$(this).bind("mouseout",hover2);
			}
		if(settings.hover)$(this).hover(hover1, hover2);
		
	}

})(jQuery);
