/**
*	TooltipHandler Class
*
*	Provides scripts to handle tooltips.  This functionality also provided
*	by the FormHandler class.
*/

function TooltipHandler(container){
	//	Establish superclass and inherit methods, StageElement contains 
	//	helper methods for objects that work with the screen
	this.inheritFrom = StageElement;
	this.inheritFrom(container);
	
	this.id="TooltipHandler";
	
	this.tooltips={};
	
	//	Tooltip mouse over function strings
	this.ttPrefix="return escape('";
	this.ttSuffix="');";
	this.ttTextColor="#000066";
	this.ttTextErrorColor="#330000";
	this.errorTitleBg="#660000";
	this.errorTitle="field error";
	
	//	Find a way to add application path to absolute
	//	url if we're in an application...
	absoluteUrlHack=document.location.pathname;
	if(absoluteUrlHack.substring(0,8)=="/ilendit"){
		var iconPath="/ilendit/webwork/tooltip/";
		}
	else if(absoluteUrlHack.substring(0,8)=="/offerri"){
		var iconPath="/offerrings/webwork/tooltip/";
		}
	else{var iconPath="/webwork/tooltip/";}
	
	//	Define the tooltip icons to 
	//	be used and cache the images
	this.icons={
		hidden:{off:new Image(),over:new Image()},
		normal:{off:new Image(),over:new Image()},
		required:{off:new Image(),over:new Image()},
		complete:{off:new Image(),over:new Image()},
		error:{off:new Image(),over:new Image()}
		};
	this.icons.hidden.off.src=iconPath+"spacer.gif";
	this.icons.hidden.over.src=iconPath+"spacer.gif";
	this.icons.normal.off.src=iconPath+"scndLevForm_blue.gif";
	this.icons.normal.over.src=iconPath+"scndLevForm_blue_f2.gif";
	this.icons.required.off.src=iconPath+"scndLevForm_yellow.gif";
	this.icons.required.over.src=iconPath+"scndLevForm_yellow_f2.gif";
	this.icons.complete.off.src=iconPath+"scndLevForm_green.gif";
	this.icons.complete.over.src=iconPath+"scndLevForm_green_f2.gif";
	this.icons.error.off.src=iconPath+"scndLevForm_red.gif";
	this.icons.error.over.src=iconPath+"scndLevForm_red_f2.gif";
	
	
	this.initTooltips=TooltipHandler_initTooltips;
	this.setToolTip=TooltipHandler_setToolTip;
	this.setToolTipTitle=TooltipHandler_setToolTipTitle;
	this.setImageState=TooltipHandler_setImageState;
	this.overImage=TooltipHandler_overImage;
	this.offImage=TooltipHandler_offImage;
	
	
	if(document.ttipObjNames){
		this.initTooltips(document.ttipObjNames);
		}//tt_handler
	
}	//	TooltipHandler

/*
<div id="ttimg_testFormAction_fieldValue_field1_tt" 
	style="position: absolute; z-index: 1010; left: -304px; top: -64px; width: 304px; visibility: hidden; opacity: 0.85;">
	<table style="background: rgb(120, 148, 175) none repeat scroll 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" bgcolor="#7894af" border="0" cellpadding="0" cellspacing="0" width="300">
	<tbody><tr style="background-color: rgb(120, 148, 175);" id="ttimg_testFormAction_fieldValue_field1_tttittd">
	<td style="padding-left: 3px; padding-right: 3px;" align="left">
	<font class="toolTipTitle" color="#ffffff" face="arial,helvetica,sans-serif"><b>
	<div id="ttimg_testFormAction_fieldValue_field1_tttitle" class="toolTipTitle">field help</div>
	</b></font>
	</td><td style="text-align: right; padding-right: 4px;" width="35%"><font class="toolTipTitleReq" color="#ffffff" face="arial,helvetica,sans-serif"><div id="ttimg_testFormAction_fieldValue_field1_tttitlereq"></div></font></td></tr><tr><td colspan="2"><table border="0" cellpadding="6" cellspacing="1" width="100%"><tbody><tr><td style="padding: 6px; text-align: left;" align="left" bgcolor="#ffffcc"><font style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif; font-size: 11px; font-weight: normal;" color="#000000" face="arial,helvetica,sans-serif"><div id="ttimg_testFormAction_fieldValue_field1_tttxt">This is the first test field.</div></font></td></tr></tbody></table></td></tr></tbody></table><div id="ttimg_testFormAction_fieldValue_field1_ttR" style="background: rgb(120, 148, 175) none repeat scroll 0%; overflow: hidden; position: absolute; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; left: 300px; top: 5px; width: 4px; height: 49px; opacity: 0.55;"></div><div style="background: rgb(120, 148, 175) none repeat scroll 0%; overflow: hidden; position: relative; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; left: 5px; top: 0px; width: 295px; height: 4px; opacity: 0.55;"></div></div>
*/


/**
*	initTooltips
*	Initialize Tooltips
*/
function TooltipHandler_initTooltips(tooltips){
	var ttname;
	
	for(var i=0; i < tooltips.length; i++){
		ttname = tooltips[i];
		//alert("Adding tooltip " + ttname);
		//	Find related divs and objects for tooltip
		//	Try to get a reference to the tooltip objects in this document
		var tooltip={};
		tooltip.text=this.findObj(ttname+"_tttxt");
		tooltip.div=this.findObj(ttname+"_tt");
		//alert(ttname + " = " + tooltip.div);
		tooltip.title=this.findObj(ttname+"_tttitle");
		tooltip.titletd=this.findObj(ttname+"_tttittd");
		tooltip.titleRight=this.findObj(ttname+"_tttitleright");
		tooltip.defTitleBg=((tooltip.titletd&&tooltip.titletd.style)?tooltip.titletd.style.backgroundColor:null);
		tooltip.defTitle=(tooltip.title?tooltip.title.innerHTML:null);
		//	Create tooltip image data object
		tooltip.img={obj:this.findObj(ttname),defstate:"normal",curstate:"normal"};
		this.tooltips[ttname]=tooltip;
		}	
}	//	initTooltips


/**
*	setToolTip
*	Sets the text to display for a tooltip.
*/
function TooltipHandler_setToolTip(name,msg,textColor,title,titleBg,titleRight){
	if(!this.tooltips[name])return;
	var tooltip=this.tooltips[name];
	if(!textColor){textColor = this.ttTextColor;}
	//if(msg&&(msg!=="")){tt=this.ttPrefix+msg+this.ttSuffix;}
	tooltip.div.style.color=textColor;
	tooltip.text.innerHTML=msg;
	this.setToolTipTitle(name,title,titleBg,titleRight);
}	//	setToolTip


/**
*	setToolTipTitle
*	Sets the tooltip title and title right text.
*/
function TooltipHandler_setToolTipTitle(name,title,titleBg,titleRight){
	if(!this.tooltips[name])return;
	var tooltip=this.tooltips[name];
	if(title&&tooltip.title){tooltip.title.innerHTML=title;}
	else if(tooltip.defTitle&&tooltip.title){tooltip.title.innerHTML=tooltip.defTitle;}
	//	Change background color of tooltip if specified
	//this.log.debug("Setting title params for name=" + name + " to title=" + title + ", titleBg= " +titleBg+ ", right title=" + titleRight);
	if(titleBg&&tooltip.titletd&&tooltip.titletd.style){tooltip.titletd.style.backgroundColor=titleBg;}
	else if(tooltip.defTitleBg&&tooltip.titletd.style){tooltip.titletd.style.backgroundColor=tooltip.defTitleBg;}
	if(titleRight&&tooltip.titleRight){tooltip.titleRight.innerHTML=titleRight;}
	else {tooltip.titleRight.innerHTML="";}
}	//	setToolTipTitle

/**
*	setImageState
*	Sets the tooltip image to one of the preloaded icon states (hidden, normal, required, error,etc)
*/
function TooltipHandler_setImageState(name,state){
	if(!this.tooltips[name]){this.log.error("setImageState() No tooltip with name " + name);return;}
	var tooltip=this.tooltips[name];
	if(!state){state="normal";}
	if(!this.icons||!this.icons[state]){this.log.error("setImageState() Can't find icons or state " + state);return;}
	tooltip.img.curstate=state;
	tooltip.img.obj.src=this.icons[state][(tooltip.img.obj.over?"over":"off")].src;
}	//	setImageState


/**
*	overImage
*	Over mouse handler for tooltip images.
*/
function TooltipHandler_overImage(img){
	var src=img.src;
	if(!src){return;}
	if(img.id==this.curOverImg){return;}
	this.curOverImg=img.id;
	//	Add in over image "_f2"
	var pos=src.lastIndexOf(".");
	if(pos<0){return;}
	img.src=src.substring(0,pos)+"_f2"+src.substring(pos);
}	//	overImage


/**
*	offImage
*	Off mouse handler for tooltip images.
*/
function TooltipHandler_offImage(img){
	var src=img.src;
	if(!src){return;}
	this.curOverImg=null;
	//	Add in over image "_f2"
	var pos=src.lastIndexOf("_f2");
	if(pos<0){return;}
	//alert(src.substring(0,pos)+src.substring(pos+"_f2".length));
	img.src=src.substring(0,pos)+src.substring(pos+"_f2".length);
}	//	offImage

