//    Licensed Materials - Property of IBM
//
//    IBM Cognos Products: cogadmin
//
//    (C) Copyright IBM Corp. 2005, 2010
//
//    US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//
//
// Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
// Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).

//----------------------------------------------------------

com.cognos.admin.ObjectFactory("com.cognos.admin.widget");
com.cognos.admin.widget.Tooltip = function (tag,e,param) {
	this.tag = tag;
	this.tools = com.cognos.admin.util.Tools;
	this.event = new xEvent(e);
	this.tooltip;
	this.timeoutId;
	this.param = param;
	this.interval = this.param.interval || 30;
	this.fadePace = this.param.fadePace || 20;
	this.timeout = this.param.timeout || 200;
};

com.cognos.admin.widget.Tooltip.VERSION = "0.1.0";
com.cognos.admin.widget.Tooltip.prototype = {
	/*
	 *
	 */
	create : function (){
		//id for the tooltip container
		var tooltipId = this.tag.id + "_tooltip";
		//id for the basic content container
		var basicDivId = tooltipId + "_basic";
		var self = this;

		if ($(tooltipId))
			xParent($(tooltipId),true).removeChild($(tooltipId));

		var div = document.createElement("div");
		div.id = tooltipId;
		div.className = "popup-tooltip";
		document.body.appendChild(div);

	    this.tooltip = div;
		xAddEventListener(div,"mouseover",_F_Document.associate(div,this,"_evt_mouseOver"),false);
		xAddEventListener(div,"mouseout",_F_Document.associate(div,this,"_evt_mouseOut"),false);

		var basicDiv = document.createElement("div");
		basicDiv.id = basicDivId;
		this.tooltip.appendChild(basicDiv);

		this.setContent(this.param);
		this.tooltip.style.display = "none";
	},

	setContent : function (param){
		this.tools.parseContent(param,this.tooltip.id+"_basic");

		if (param.extension){
			this.setExtendContent (param.extension);
		} else {
			this.setOffset();
		}
	},

	setExtendContent : function (param){
		//id of the extend content container
		var extensionDivId = this.tooltip.id+"_extension";
		var promptDivId = this.tooltip.id+"_prompt";
		var extendHTML = "<span id=\""+promptDivId+"\" style=\"align:right;\">" +
							"<a href=\"#\" onclick=\"Javascript:$('"+extensionDivId+"').style.display='';$('"+promptDivId+"').style.display='none';\">" +param.prompt + ":</a>" +
						 "</span>" +
						 "<span id=\""+extensionDivId+"\" style=\"display:'';\"></span>";
		var extensionDiv = document.createElement("div");
		extensionDiv.innerHTML = extendHTML;
		this.tooltip.appendChild(extensionDiv);

		this.tools.parseContent(param,this.tooltip.id+"_extension");
		this.setOffset();
		$(extensionDivId).style.display = "none";
	},

	_evt_mouseOver : function (e,tag){
	    clearTimeout(this.timeoutId);
	    this.tooltip.style.display = "block";
	    _F_log("D","mouseOver:"+tag.id);
    },

    _evt_mouseOut : function (e,tag) {
		this.hide(e,tag);
		_F_log("D","mouseOut:"+tag.id);
    },

	show : function (){
		var self = this;
		clearTimeout(this.timeoutId);

		if (!this.tooltip)
			this.create();

		this.timeoutId = setTimeout(function (){
			//self.tooltip.style.display = "block";
			self.fade(true);
		},this.timeout);

	},

	fade : function (isFadeIn){
		var objId = this.tooltip.id;

		for (var i = 0; i <= 100; i += this.fadePace){
			var fadeTime = (isFadeIn) ? ((this.interval*i)/this.fadePace) : ((this.interval*(100-i))/this.fadePace);
			setTimeout("com.cognos.admin.util.Tools.dspOpacity('"+objId+"',"+i+","+isFadeIn+")",fadeTime);
		}
	},

	hide : function (){
		var self = this;
		clearTimeout(this.timeoutId);

		this.timeoutId = setTimeout(function (){
			//self.tooltip.style.display = "none";
			self.fade(false);
		},this.timeout);

		//setTimeout(self.destroy,0);
		//this.destroy();
	},

	destroy : function (){
		var tooltip = this.tooltip;
		xRemoveEventListener (tooltip,"mouseover",_F_Document.associate(tooltip,this,"_evt_mouseOver"),false);
		xRemoveEventListener (tooltip,"mouseout",_F_Document.associate(tooltip,this,"_evt_mouseOut"),false);

		if (this.tooltip)
			document.body.removeChild(this.tooltip);

		this.tooltip = null;
	},

	setOffset : function (){
		xDisplay(this.tooltip,"block");
		xVisibility(this.tooltip,"hidden");

		var widthTooltip = xWidth(this.tooltip);
		var heightTooltip = xHeight(this.tooltip);
		var x = (this.event.pageX != undefined) ? this.event.pageX : (this.tools.getAbsolutePosition(this.tag).left + this.tag.clientWidth);
		var y = (this.event.pageY != undefined) ? this.event.pageY : (this.tools.getAbsolutePosition(this.tag).top + this.tag.clientHeight);

		if (document.body.dir == "rtl")
		{
			var windowLeft =0;
			if (typeof window.pageXOffset != "undefined"){
				windowLeft = window.pageXOffset;
			 }else if (typeof document.documentElement.scrollLeft != "undefined" ){
				 windowLeft = document.documentElement.scrollLeft;

			 }else if (typeof document.body.scrollLeft != "undefined"){
				 windowLeft = document.body.scrollLeft;

			 }

			if((x - widthTooltip)> windowLeft){
				x = x - widthTooltip;
			}
			// test for the existence of left scroll
			if (xWidth(document.body) > document.body.clientWidth)
				x = x- (xWidth(document.body) - document.body.clientWidth);
		}

		if ((x + widthTooltip) > xWidth(document.body)) {
			x = xClientWidth() - widthTooltip;
		}

		if ((y + heightTooltip) > xHeight(document.body)) {
			y = xHeight(document.body) - heightTooltip - xScrollTop();
		} else {
			y += 10;
		}

		xLeft(this.tooltip,x);
		xTop(this.tooltip,y);
		xDisplay(this.tooltip,"none");
		xVisibility(this.tooltip,"visible");
	}
};