/**
 * 
 * this is a tooltip help system
 * 
 * 
*/

/* used to store mouse position at all times */
var globalMousePosition;


//change the opacity for different browsers
function changeOpacity(opacity, id) {
	var object = $(id); 
	
	object.style.opacity = (opacity / 100);
	object.style.MozOpacity = (opacity / 100);
	object.style.KhtmlOpacity = (opacity / 100);

	if(object){
		if(object.style.filter){
			object.style.filter = 'alpha(opacity=' + opacity + ')';
			object.style.opacity = (opacity/100);
		}else{
			object.style.filter = "alpha(opacity=" + (opacity / 100) + ")";
			object.style.opacity = (opacity/100);
		}
	}
	
	if(object.style.visibility == 'hidden'){
		object.style.visibility = 'visible';	
	}
	
}


/**
 * a factory class that creates new tooltips
 */
ToolMan._tooltipfactory = {

	tooltipContainer : null,

	containerWidth : 200,
	
	containerHeight : 200,
	
	tooltipTriggers : new Array(),
	
	tooltips : new Array(),

	init : function(tooltipContainerId) {
		

		if($(tooltipContainerId)){
			this.tooltipContainer = $(tooltipContainerId);
			this.tooltipContainer.style.position = 'absolute';
			
//			alert(this.tooltipContainer.id)
		}
		
		
		if(typeof this.tooltips != "array"){
			this.tooltips = new Array();
		}

		
		this.tooltipTriggers = getElementsByClass('tooltip_for_([a-zA-Z0-9]+)');

		for(var i = 0; i < this.tooltipTriggers.length ; i++){
			this.tooltips[i] = this.create(this.tooltipTriggers[i], this.tooltipTriggers[i].className.replace('tooltip_for_',''));

			this.tooltips[i].trigger.style.cursor = 'help';

		}
	},

	applyOpacity : function (id, opacStart, opacEnd, millisec) {
		//speed for each frame
		var speed = Math.round(millisec / 100);
		var timer = 0;
	
		opacityFinalValue = opacEnd;
	
		//determine the direction for the blending, if start and end are the same nothing happens
		if(opacStart > opacEnd) {
			for(i = opacStart; i >= opacEnd ; i--) {
				setTimeout("changeOpacity(" + i + ",'" + id + "')",(timer * speed));
				timer++;
			}
			
		} else if(opacStart < opacEnd) {
			for(i = opacStart; i <= opacEnd ; i++)
				{
				setTimeout("changeOpacity(" + i + ",'" + id + "')",(timer * speed));
				timer++;
			}
		}
	},


	positionTooltip : function(x,y,w,h){
		var scrollOffset 	= ToolMan.coordinates().scrollOffset();
		var containerSize 	= ToolMan.coordinates()._size(this.tooltipContainer);
		//alert(globalMousePosition.x)
		var computedLeft 	= globalMousePosition.x - 10;
		var computedTop 	= globalMousePosition.y + scrollOffset.y + 15;
		var windowSize 		= ToolMan.coordinates().clientSize();

//		alert(computedLeft + ' ' + containerSize.x)
//		alert(computedLeft + containerSize.x)
		
		if((computedLeft + containerSize.x) >= (windowSize.x - 25)){
			computedLeft -= ((computedLeft + containerSize.x) - windowSize.x) + 35;
			
		}
		
		this.tooltipContainer.style.left = computedLeft + 'px' ;
		this.tooltipContainer.style.top  = computedTop + 'px' ;		
		
		this.tooltipContainer.style.display = 'block';
		this.tooltipContainer.style.visibility = 'hidden';

		

		this.repositionTooltip(computedLeft,computedTop,w,h);
	},

	repositionTooltip : function(x,y,w,h) {
		var adjustedTop 	= 0;
		var scrollOffset 	= ToolMan.coordinates().scrollOffset();
		var containerSize 	= ToolMan.coordinates()._size(this.tooltipContainer);
		var windowSize 		= ToolMan.coordinates().clientSize();

//		var computedTop		= globalMousePosition.y + scrollOffset.y + 10;				
		var computedTop		= y;				
		var computedLeft 	= x;
		var bottomOffset 	= computedTop + containerSize.y;
		
		if(bottomOffset > (windowSize.y + scrollOffset.y - 25) ){
			adjustedTop = bottomOffset - (windowSize.y + scrollOffset.y) + h;
			computedLeft += 30;
		}
		
//		if(computedTop < scrollOffset.y){
//			computedTop = scrollOffset.y + 20;
//		}	
		

		if(computedLeft < 0){
			computedLeft = globalMousePosition.x + scrollOffset.x + 20;
		}else if(computedLeft + containerSize.x > windowSize.x){
			computedLeft -= ((globalMousePosition.x + containerSize.x) - windowSize.x) + 25;
		}else{
			computedLeft += scrollOffset.x;
		}

		this.tooltipContainer.style.left = computedLeft + 'px';
//		this.tooltipContainer.style.top  = globalMousePosition.y + scrollOffset.y + 10;		
		this.tooltipContainer.style.top  = parseInt((computedTop - adjustedTop)) + "px";						
		this.applyOpacity(this.tooltipContainer.id, 0, 100, 500);
		
		
	},
	
	create : function(tooltipTrigger, tooltipFor){
		return new 	_ToolManTooltip(this, tooltipTrigger, tooltipFor);
	},
	
	setContainerWidth : function(width){
		this.containerWidth = width;
	},
	
	setContainerHeight : function(height){
		this.containerHeight = height;
	},
	
	showContainer : function(x,y,w,h){
		var scrollOffset = ToolMan.coordinates().scrollOffset();

		// if mouse is on target .. then show the tooltip
		if(globalMousePosition.x + scrollOffset.x >= x
			&& globalMousePosition.y + scrollOffset.y >= y 
			&& globalMousePosition.x + scrollOffset.x  <= x+w
			&& globalMousePosition.y + scrollOffset.y <= y+h){
				this.positionTooltip(x,y,w,h);
		}
	},
	
	hideContainer : function(x,y,w,h){
		var scrollOffset = ToolMan.coordinates().scrollOffset();
		
		if(globalMousePosition.x + scrollOffset.x >= x
			&& globalMousePosition.y + scrollOffset.y >= y 
			&& globalMousePosition.x + scrollOffset.x <= x+w
			&& globalMousePosition.y + scrollOffset.y <= y+h){
			
			var parentObj = this;
				addEvent(this.tooltipContainer, 'mouseout', function(evt,x,y,w,h){
																				parentObj.hideContainer(x,y,w,h);
																			}
										);
		}else{
//			removeEvent(document, 'mousemove', lastMousePositionCallbackFunc);
			document.onmousemove = null;
//			ToolMan.events().unregister(this.tooltipContainer, 'mouseout', this.hideContainer);
			this.tooltipContainer.style.display = 'none';
		}
	}
	
	
}


/**
 * tooltip class
 */

function _ToolManTooltip(factory, trigger, tipId){
	this.factory 	= factory;
	this.tipId		= tipId;
	this.trigger	= trigger;
	this.content	= '';
	
	var parentObj = this;
	
	addEvent(this.trigger, 'mouseover', function(evt){
									parentObj.show(evt);
							});
	addEvent(this.trigger, 'mouseout', function (evt){
									parentObj.hide(evt);
							});	
	
//	alert(this.toString())
//	this.getTip();
}

_ToolManTooltip.prototype = {
	factory : null,
	tipId : '',
	trigger : null,
	content : '',
	toString : function() {
		return "trigger type: " + this.trigger.tagName +
				"\n trigger id: " + this.trigger.id + 
				"\n content: " + this.content + 
				"\n tipID: " + this.tipId ;
				
	},
	
	getTip : function() {
		if($(this.tipId)){
			this.content = $(this.tipId).innerHTML;
		}
	},
	
	setTip : function() {
		this.factory.tooltipContainer.innerHTML = this.content;
	},
	
	getMousePosition : function(evt) {
		try{
			globalMousePosition = ToolMan.coordinates().mousePosition(evt);
		}catch(e){
			
		}
	},
	
	show : function(evt) {

		if(!evt){
			evt = window.event;
		}
	
		var target = evt.target? evt.target : evt.srcElement;
		
//		addEvent(document, 'mousemove', this.getMousePosition);
		document.onmousemove = this.getMousePosition;
	
		if(this.content == ''){	
			this.getTip();	
		}
		
		this.setTip();
		
		var triggerTopLeftOffset = ToolMan.coordinates().topLeftOffset(target);
		var triggerSize			 = ToolMan.coordinates()._size(target);

		setTimeout('ToolMan._tooltipfactory.showContainer('+triggerTopLeftOffset.x
														+','+triggerTopLeftOffset.y
														+','+triggerSize.x
														+','+triggerSize.y
														+')',600)
	},
	
hide : function(evt) {

		if(!evt){
			evt = window.event;
		}

	
		var target = evt.target? evt.target : evt.srcElement;
		
		var tooltipContainerTopLeftPosition = ToolMan.coordinates().topLeftPosition(this.factory.tooltipContainer);
		var tooltipContainerSize			= ToolMan.coordinates()._size(this.factory.tooltipContainer);
		
		setTimeout('ToolMan._tooltipfactory.hideContainer(' +tooltipContainerTopLeftPosition.x +
															',' + tooltipContainerTopLeftPosition.y +
															',' + tooltipContainerSize.x +
															',' + tooltipContainerSize.y +
															')',300);
		//ToolMan.events().register(this.factory.tooltipContainer, 'mouseout', this.hide);
		//alert("hide >> " + this.toString())
	}
	
}
