/*
* dieu khien image slideshow
* Type:     Function use Jquery
* Name:     vonTooltip 
* Date:     Aug 20, 2008
* Update: 	Sep 10, 2008
* @author   Tri Do <tridn@von-inc.com >
*/
(function($) {
		$.fn.vonTooltip = function() 
		{	
			var flag;
			var intFixLeft=null;
			function _initialize(e) {
				flag=-1;
				var arrwsize=_getPageSize();
				var arrscroll=_getPageScroll();
				var objLeft=$(this).offset().left;
				var objWidth=$(this).width();
				var intToolW=0;
				var intToolConLeft=0;
				var strFixOverlay='<!--[if lte IE 6.5]><iframe></iframe><![endif]-->';
				_addPanel();
				$('#tooltipcontainer').css('top',($(this).offset().top+$(this).height()+$('#tooltipconnector').height()-5));
				$('#tooltipcontainer .toolcont').html(strFixOverlay+$(this).children('.tooltipcontent').html());
				
				$('#tooltipcontainer').width($(this).children('.tooltipcontent').width()+20);
				intToolW=$('#tooltipcontainer').width();
				if(intFixLeft==null){
					if((objLeft+intToolW)>(arrscroll[0]+arrwsize[2])){
						$('#tooltipcontainer').css('left',(arrwsize[2]+arrscroll[0])-intToolW-10);
						intToolConLeft=e.pageX-15;
						$('#tooltipconnector').addClass("con_right");
					}
					else{
						
						$('#tooltipcontainer').css('left',e.pageX-30);
						intToolConLeft=e.pageX - 7 +(objWidth/3);
					}
				}
				else{
					$('#tooltipcontainer').css('left',objLeft+intFixLeft);
					intToolConLeft=objLeft+(objWidth/3);
				}
				$('#tooltipcontainer').fadeIn(300);
				$('#tooltipconnector').css('top',$('.toolcont').offset().top-$('#tooltipconnector').height()+2);
				$('#tooltipconnector').css('left',intToolConLeft);
				$('#tooltipconnector').fadeIn(400);									 
				return false;
			};
			function _addPanel(){
				_finish();
				$('body').append('<div id="tooltipcontainer"><div class="toolcont"></div></div><div id="tooltipconnector"></div>');
				return false;
			};
			function _finish() {
				if(flag!=0){
					$('body #tooltipcontainer').html('');
					$('body #tooltipcontainer').hide();
					$('body #tooltipconnector').hide();
					$('body #tooltipconnector').remove();
					$('body #tooltipcontainer').remove();
				}
			};
			function _getPageSize() {
				var xScroll, yScroll;
				if (window.innerHeight && window.scrollMaxY) {	
					xScroll = window.innerWidth + window.scrollMaxX;
					yScroll = window.innerHeight + window.scrollMaxY;
				} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
					xScroll = document.body.scrollWidth;
					yScroll = document.body.scrollHeight;
				} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
					xScroll = document.body.offsetWidth;
					yScroll = document.body.offsetHeight;
				}
				var windowWidth, windowHeight;
				if (self.innerHeight) {	// all except Explorer
					if(document.documentElement.clientWidth){
						windowWidth = document.documentElement.clientWidth; 
					} else {
						windowWidth = self.innerWidth;
					}
					windowHeight = self.innerHeight;
				} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
					windowWidth = document.documentElement.clientWidth;
					windowHeight = document.documentElement.clientHeight;
				} else if (document.body) { // other Explorers
					windowWidth = document.body.clientWidth;
					windowHeight = document.body.clientHeight;
				}	
				var pageHeight, pageWidth;
				// for small pages with total height less then height of the viewport
				if(yScroll < windowHeight){
					pageHeight = windowHeight;
				} else { 
					pageHeight = yScroll;
				}
				// for small pages with total width less then width of the viewport
				if(xScroll < windowWidth){	
					pageWidth = xScroll;		
				} else {
					pageWidth = windowWidth;
				}
				arrayPageSize=new Array(pageWidth,pageHeight,windowWidth,windowHeight);
				return arrayPageSize;
			};
			//ham lay thong so thanh cuon
			function _getPageScroll() {
				var scrOfX = 0, scrOfY = 0;
				if( typeof( window.pageYOffset ) == 'number' ) {
				//Netscape compliant
					scrOfY = window.pageYOffset;
					scrOfX = window.pageXOffset;
				} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
				//DOM compliant
					scrOfY = document.body.scrollTop;
					scrOfX = document.body.scrollLeft;
				} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
				//IE6 standards compliant mode
					scrOfY = document.documentElement.scrollTop;
					scrOfX = document.documentElement.scrollLeft;
				}
				arrayPageScroll = new Array(scrOfX, scrOfY);
				return arrayPageScroll;
			};
			if(this.attr("fixleft")!="undifined")
				{
					var temp=parseInt(this.attr("fixleft"),0);
					if(!isNaN(temp)) intFixLeft=temp;	
				}
			if(this.attr("static")=="true")
			{
				this.parent().unbind('mouseover','mouseout').mouseover(_initialize);
				$('#tooltipcontainer').bind('mouseover',function(){
					flag=0;
				});
				$('#tooltipcontainer').bind('mouseout',function(){
				 	flag=1;
				});
				$(document).click(_finish);	
			}
			else{
				this.parent().unbind('ouseover','mouseout').mouseover(_initialize).mouseout(_finish);
			}
			return false;
		};
})(jQuery);
