/******* GLOBALS [BEGIN] *******/
var is_opera = /opera\/9/i.test(navigator.userAgent);
var is_gecko = /gecko/i.test(navigator.userAgent);
var is_ie    = /MSIE/.test(navigator.userAgent);
/******* GLOBALS [END] *******/
function Cursor () {
	
	var xCoordinate;
	var yCoordinate;
	
	var truebody = function (){
		if(!window.opera && document.compatMode && document.compatMode!="BackCompat"){
			return document.documentElement;
		}
		else{
			return document.body;
		}
	}
	
	this.updateCursorPosition = function (DOMEvent) {
		if (is_ie) {
			xCoordinate = event.clientX + truebody().scrollLeft;
			yCoordinate = event.clientY + truebody().scrollTop;
		} else {
			xCoordinate = DOMEvent.pageX > 0 ? DOMEvent.pageX : 0;
			yCoordinate = DOMEvent.pageY > 0 ? DOMEvent.pageY : 0;
		}
	}
	
	this.getX = function () {
	   return xCoordinate;
	}
	
	this.getY = function () {
	   return yCoordinate;
	}
}

function Image (path){
	this._path = path;
	this._enabledLoading;
	this._preloadImage;
}

function Tooltip (tooltipObject){
	
	var obj = tooltipObject;
	var eventTimeout;
	var offsetx = 20;
	var offsety = 20;
	var wasOver = false;
	
   var cursorObj = new Cursor();
	if(!is_ie){
		document.captureEvents(Event.MOUSEMOVE);
	}
	document.onmousemove = cursorObj.updateCursorPosition;
	
	this.image = new Image();
	
	obj.style.display = "none";
	obj.style.zIndex = "999";
	
	this.getObject = function () {
		return obj;
	}
	
	this.getObjectStyle = function () {
		return obj.style;
	}
	
	this.getCursorObject = function () {
		return cursorObj;
	}
	
	this.setOffsetFromCursor = function (xOffset, yOffset) {
		offsetx = xOffset;
		offsety = yOffset;
	}
	
	this.setDimension = function (objWidth, objHeight) {
		obj.style.height = objHeight;
		obj.style.width = objWidth;
	}
	
	this.setContent = function (htmlSource) {
		obj.innerHTML = htmlSource;
	}
	
	this.setInnerObjectContent = function (id, htmlSource) {
		
	}
	
	this.dropTimeout = function () {
		clearTimeout(eventTimeout);
	}
	
	var enableMove = function () {
		wasOver=true;
	}
	
	var disableMove = function () {
		wasOver=false;
	}
	
	var truebody = function () {
		if(!window.opera && document.compatMode && document.compatMode!="BackCompat"){
			return document.documentElement;
		}
		else{
			return document.body;
		}
	}
	
	var toPosition = function (xPos, yPos) {
		var ymin = document.all ? truebody().scrollTop : window.scrollY;
		var ymax = document.all ? truebody().scrollTop + truebody().clientHeight : window.scrollY + window.innerHeight - 30;
		var xmin = document.all ? truebody().scrollLeft : window.scrollX;
		var xmax = document.all ? truebody().scrollLeft + truebody().clientWidth : window.scrollX + window.innerWidth - 30;
		
		if(xPos + offsetx + parseInt(obj.style.width) > xmax){
			leftPosition = xmax - parseInt(obj.style.width);
		}
		else{
			leftPosition = xPos + offsetx;
		}
		
		if(yPos + offsety + parseInt(obj.style.height) > ymax){
			topPosition = ymax - parseInt(obj.style.height);
		}
		else{
			topPosition = yPos + offsety;
		}
		
		if(xPos + offsetx < leftPosition + parseInt(obj.style.width) && xPos + offsetx > leftPosition){
			if(yPos + offsety < topPosition + parseInt(popupObj.style.height) && yPos + offsety > topPosition){
				leftPosition = xPos - 2*offsetx - parseInt(obj.style.width);
				topPosition = yPos - 2*offsety - parseInt(obj.style.height);
			}
		}
		
		obj.style.top = topPosition + "px";
		obj.style.left = leftPosition + "px";
		
		//obj.style.top = yPos + offsety + "px";
		//obj.style.left = xPos + offsetx + "px";
	}
	
	this.show = function (htmlSource) {
		if(wasOver == true){
			toPosition(cursorObj.getX(), cursorObj.getY());
			//obj.style.top = cursorObj.getY() + offsety + "px";
			//obj.style.left = cursorObj.getX() + offsetx + "px";
			obj.style.display = "block";
			
			if(arguments.length == 1 && typeof(arguments[0]) == typeof("string")){
				if(obj.innerHTML != htmlSource){
					obj.innerHTML = htmlSource;
				}
			}
		}
	}
	
	this.showWithTimeout = function (timeout, htmlSource) {
		if(eventTimeout){
			clearTimeout(eventTimeout);
			eventTimeout = 0;
		}
		
		tmpfunc = this.show;
		enbl = enableMove;
		if(arguments.length == 1 && typeof(arguments[0]) == typeof("string")){
			enbl();
			tmpfunc(arguments[0]);
		}
		else if(arguments.length == 2){
			eventTimeout = setTimeout("enbl();tmpfunc('" + htmlSource + "');", timeout);
		}
		else{
			eventTimeout = setTimeout("enbl();tmpfunc();", timeout);
		}
	}
	
	this.hide = function () {
		obj.style.display = "none";
		wasOver = false;
		clearTimeout(eventTimeout);
		obj.style.top = "250px";
		obj.style.left = "-500px";
		//this.dropTimeout();
		//this.toPosition(-500, 250);
	}
	
	this.hideWithTimeout = function (timeout) {
		if(eventTimeout){
			clearTimeout(eventTimeout);
			eventTimeout = 0;
		}
		tmpfunc = this.hide;
	   eventTimeout = setTimeout("tmpfunc();", timeout);
	}
}

//popupObj = new Popup(document.getElementById('preview_div'));
//alert(document.getElementById('preview_div').id);
