//Initialize variables
var netscape = (navigator.appName=="Netscape") ? 'yes' : 'no';
var hintSuffix = '_hint';
var mouseLeft = -1;
var mouseTop = -1;

var browser=navigator.appName;
	var b_version=navigator.appVersion;
	var version=parseFloat(b_version);

//Create and return the hint window using the passed parameters.
function createHint( controlID, width, height, title, content )
{
	if ( document.getElementById( controlID ) != null )
	{	
		//alert(controlID);	
		attachHintToControl( document.getElementById( controlID ) );
		return new FerantDHTMLWindow( GetParams( controlID + hintSuffix, width, height, title, content ) );			
	}
}

//Function to retrieve default set of parameters. Parameters that should
//be different for each hint window should be passed to this function.
function GetParams( id, width, height, title, content )
{
	//Set the default width and height if no custom width or height is provided
	//or if they are 0.
	w = parseInt(width);
	if ( isNaN(w) || w <= 0 ) w = 266;
	h = 0;
//	h = parseInt(height);
//	if ( isNaN(h) || h <= 0 ) h = 100;

	params = 
	{
		BorderColor : 'Transparent', 
		BorderWidth : 0, 
		CloseBoxHeight : 15, 
		CloseBoxSrc : '', 
		CloseBoxWidth : 15, 
		ContentColor : Hint_ContentColor, 
		ContentHTML: content,
		ContentLeftBorderColor : Hint_ContentLeftBorderColor, 
		ContentLeftBorderWidth : 2, 
		ContentRightBorderColor : Hint_ContentRightBorderColor, 
		ContentRightBorderWidth : 2, 
		Height : h, 
		InnerBorderColor : Hint_InnerBorderColor, 
		InnerBorderWidth : 2, 
		OuterBorderColor : Hint_OuterBorderColor, 
		OuterBorderWidth : 0, 
		Resizable : 'none', 
		StatusBarHeight : 0,
		//StatusBarHTML : '<img alt=\"\"  src=\'../Images/Hints/box_bottom_l.gif\' height=\'20\'><img alt=\"\"  src=\'../Images/Hints/box_bottom.gif\' border=\'0\' width=\'' + String(parseInt(w)-16) + '\' height=\'20\'><img alt=\"\"  src=\'../Images/Hints/box_bottom_r.gif\' height=\'20\'>', 
		StatusBarHTML : '', 		
		StatusBarTextMargin : 6, 
		StatusColor : 'Transparent', 
		TitleBarHeight : 20, 
		//TitleBarHTML : '<img alt=\"\"  src=\'../Images/Hints/box_top_l.gif\'><span style=\'position:absolute; left: 10; top: 4; font-family : verdana, helvetica, arial; font-size : 10px; z-index: 3;\'>' + title + '</span><img alt=\'\'  src=\'../Images/Hints/box_top.gif\' border=\'0\' width=\'' + String(parseInt(w)-22) + '\' height=\'20\'><img alt=\"\"  src=\'../Images/Hints/box_top_r.gif\'>', 
		TitleBarHTML : '<span style=\'position:absolute; left: 6; top: 4; font-family : verdana, helvetica, arial; font-size : 9px; z-index: 3;\'>' + title + '</span><img alt=\'\'  src=\'../Images/Hints/box_top.gif\' border=\'0\' width=\'' + w + '\' height=\'20\'>', 
		TitleColor : 'Transparent', 
		Width : w,
		ContentFontSize : 9, 
		Id : id
	}
	return params;
}

//Attach the OnMouseOver and OnMouseOut events for the passed control
//to the corresponding eventhandlers.
function attachHintToControl( control )
{
	if(control.addEventListener)
	{
	   control.onmouseover = MouseOver;
	   control.onmouseout = MouseOut;
	   control.onkeyoress = KeyPress;
		//control.addEventListener( 'onmouseover', MouseOver, false );
		//control.addEventListener( 'onmouseout', MouseOut, false );	
		//control.addEventListener( 'onkeypress', KeyPress, false );	
	}
	else
	{
		control.attachEvent( 'onmouseover', MouseOver );
		control.attachEvent( 'onmouseout', MouseOut );	
		control.attachEvent( 'onkeypress', KeyPress );
	}
}

//Eventhandler for the document.OnMouseMove event. Should be attached
//to the OnMouseMove event, after all hint windows are created for the
//current page. See f.i. frm_gsmrepairorder_mutate_hint.js.
function MouseMoveAllHints()
{
	//Get the inner width and height of the browser.
	innerWidth = (netscape=="yes") ? window.innerWidth : document.body.offsetWidth;
	innerHeight = (netscape=="yes") ? window.innerHeight : document.body.offsetHeight;
	scrollHeight = (netscape=="yes") ? window.pageYOffset : document.body.scrollTop;

	//Get all div elemens to iterate through.
	divs = document.getElementsByTagName( 'div' ); 
	for(i=0; i < divs.length; i++)
	{
		//If the div element's ID starts with _807 it is a hint window.
		strID = new String( divs[i].id );
		if ( strID.indexOf( '_807' ) != -1 )
		{
			//Get the width and the height of the hint window. To get the integer value
			//we need to remove the suffix px from these properties.
			strExp = /px/gi;
			strWidth = new String( document.getElementById( strID ).style.width );
			width = Number( strWidth.replace( strExp, '' ) );
			strHeight = new String( document.getElementById( strID ).style.height );
			height = Number( strHeight.replace( strExp, '' ) );
		
			//Check if the hint window fits in the current width of the browser.
			//If not, move the hint window to the left of the mouse pointer, else move it to
			//the right of the mouse pointer.
			if ( event.x + width + 45 > innerWidth )
			{
				document.getElementById( strID ).style.left = event.x - width - 20;
				mouseLeft = event.x - width - 20;
			}
			else
			{
				document.getElementById( strID ).style.left = event.x + 20;
				mouseLeft = event.x + 20
			}
			
			//Check if the hint window fits in the current height of the browser.
			//If not, move the hint window above the mouse pointer, else move it below
			//the mouse pointer.
			if ( event.y + height > innerHeight )	
			{							
				document.getElementById( strID ).style.top = event.y - height + scrollHeight;									
				mouseTop = event.y - height + scrollHeight;
			}
			else
			{
				document.getElementById( strID ).style.top = event.y + scrollHeight;
				mouseTop = event.y + scrollHeight;
			}
			
			if ( document.getElementById( strID ).style.display != 'none' )
				UpdateSelects( event.srcElement.id, 'hidden' );				
		}
	}
}

//Eventhandler for the document.OnMouseMove event. Should be attached
//to the OnMouseMove event, after all hint windows are created for the
//current page. See f.i. frm_gsmrepairorder_mutate_hint.js.
function MouseMove(e)
{
	//alert('MouseMove fired!');
	
	//Get the inner width and height of the browser.
	innerWidth = (netscape=="yes") ? window.innerWidth : document.body.offsetWidth;
	innerHeight = (netscape=="yes") ? window.innerHeight : document.body.offsetHeight;
	scrollHeight = (netscape=="yes") ? window.pageYOffset : document.body.scrollTop;
	
	//if ((browser=="Netscape"||browser=="Microsoft Internet Explorer")  &amp;&amp; (version&gt;=4))
	
	if(browser=="Microsoft Internet Explorer")
	{	
		if ( event.srcElement != null && event.srcElement.id != '' && document.getElementById( '_807' + event.srcElement.id + hintSuffix ) != null )
		{
			//If the div element's ID starts with _807 it is a hint window.
			strID = new String( '_807' + event.srcElement.id + hintSuffix );
			
			//Get the width and the height of the hint window. To get the integer value
			//we need to remove the suffix px from these properties.
			strExp = /px/gi;
			strWidth = new String( document.getElementById( strID ).style.width );
			width = Number( strWidth.replace( strExp, '' ) );
			strHeight = new String( document.getElementById( strID ).style.height );
			height = Number( strHeight.replace( strExp, '' ) );
			
			//Check if the hint window fits in the current width of the browser.
			//If not, move the hint window to the left of the mouse pointer, else move it to
			//the right of the mouse pointer.
			if ( event.x + width + 45 > innerWidth )
			{
				document.getElementById( strID ).style.left = event.x - width - 20;
				mouseLeft = event.x - width - 20;
			}
			else
			{
				document.getElementById( strID ).style.left = event.x + 20;
				mouseLeft = event.x + 20
			}
			
			//Check if the hint window fits in the current height of the browser.
			//If not, move the hint window above the mouse pointer, else move it below
			//the mouse pointer.
			if ( event.y + height > innerHeight )	
			{							
				document.getElementById( strID ).style.top = event.y - height + scrollHeight;									
				mouseTop = event.y - height + scrollHeight;
			}
			else
			{
				document.getElementById( strID ).style.top = event.y + scrollHeight;
				mouseTop = event.y + scrollHeight;
			}
			
			if ( document.getElementById( strID ).style.display != 'none' )
				UpdateSelects( event.srcElement.id, 'hidden' );				
		}
	}
	else
	{
		//alert('Error on mouse move!!!')		
		//debugger;
		
		var node = e.target;
		
		while(node.nodeType != node.ELEMENT_NODE)
			node = node.parentNode;
			
		if ( node != null && node.id != '' && document.getElementById( '_807' + node.id + hintSuffix ) != null )
		{
			//If the div element's ID starts with _807 it is a hint window.
			strID = new String( '_807' + node.id + hintSuffix );
			
			//Get the width and the height of the hint window. To get the integer value
			//we need to remove the suffix px from these properties.
			strExp = /px/gi;
			strWidth = new String( document.getElementById( strID ).style.width );
			width = Number( strWidth.replace( strExp, '' ) );
			strHeight = new String( document.getElementById( strID ).style.height );
			height = Number( strHeight.replace( strExp, '' ) );
			
			//Check if the hint window fits in the current width of the browser.
			//If not, move the hint window to the left of the mouse pointer, else move it to
			//the right of the mouse pointer.
			if ( e.pageX + width + 45 > innerWidth )
			{
				document.getElementById( strID ).style.left = e.pageX - width - 20;
				mouseLeft = e.pageX - width - 20;
			}
			else
			{
				document.getElementById( strID ).style.left = e.pageX + 20;
				mouseLeft = e.pageX + 20
			}
			
			//Check if the hint window fits in the current height of the browser.
			//If not, move the hint window above the mouse pointer, else move it below
			//the mouse pointer.
			if ( e.pageY + height > innerHeight )	
			{							
				document.getElementById( strID ).style.top = e.pageY - height + scrollHeight;									
				mouseTop = e.pageY - height + scrollHeight;
			}
			else
			{
				document.getElementById( strID ).style.top = e.pageY + scrollHeight;
				mouseTop = e.pageY + scrollHeight;
			}
			
			if ( document.getElementById( strID ).style.display != 'none' )
				UpdateSelects( node.id, 'hidden', e);				
		}	
	}
}

//Eventhandler for the OnMouseOver event of controls that have a hint window.
//This eventhandler is attached to the event using the function attachHintToControl().
function MouseOver(e)
{
	//alert('Mouse over fired!!!');
		if(browser=="Microsoft Internet Explorer")	
		{
			eval( event.srcElement.id + hintSuffix ).OpenWindow(); 
		}
		else
		{
			//alert('Error!!!');
			var node = e.target;
			
			while(node.nodeType != node.ELEMENT_NODE)
				node = node.parentNode;		
			//debugger;			
			eval( node.id + hintSuffix ).OpenWindow(); 
		}
}

//Eventhandler for the OnMouseOut event of controls that have a hint window.
//This eventhandler is attached to the event using the function attachHintToControl().
function MouseOut(e)
{
	if(browser=="Microsoft Internet Explorer")	
	{
		eval( event.srcElement.id + hintSuffix ).CloseWindow(); 
	}
	else
	{
		var node = e.target;
		
		while(node.nodeType != node.ELEMENT_NODE)
			node = node.parentNode;
		eval( node.id + hintSuffix ).CloseWindow(); 
	}
}

//Eventhandler for the OnKeyPress event of controls that have a hint window. 
//This eventhandler is attached to the event using the function attachHintToControl().
function KeyPress()
{
	//Get all div elemens to iterate through.
	divs = document.getElementsByTagName( 'div' ); 
	for(i=0; i < divs.length; i++)
	{
		//If the div element's ID starts with _807 it is a hint window.
		strID = new String( divs[i].id );
		if ( strID.indexOf( '_807' ) != -1 )
		{
			document.getElementById( strID ).style.display = 'none';
		}
	}
}

//Called each time a hint window is displayed to hide <SELECT> html controls
//since they never appear below a hint window.
function UpdateSelects( focusedControlID, display, e)
{

	//Call the required functions in ferantlib.js to hide those <SELECT>
	//controls which overlap the hint window.
	_905[eval( focusedControlID + hintSuffix)._809] = mouseLeft;
	_904[eval( focusedControlID + hintSuffix)._809] = mouseTop;	
	
	//Since the height is auto-sized, set the height to a reasonable number. In the next version,
	//this should be changed to the actual height of the current hintwindow. No idea at the moment
	//how this is retrieved/calculated.
	_903[eval( focusedControlID + hintSuffix)._809] = 200;
	
	if(browser=="Microsoft Internet Explorer")
	{		
		_156( event.srcElement.id );
	}
	else
	{
		var node = e.target;
		
		while(node.nodeType != node.ELEMENT_NODE)
			node = node.parentNode;
		
		_156( node.id );
	}
	
	//If the control the currently displayed hint window belongs to is a <SELECT>
	//control, set this <SELECT> control back to visible.
	document.getElementById( focusedControlID ).style.visibility = "";
}
