//-----------------------------------------------------------------------------------------|
// this file contains objects and static methods for displaying	dynamic inline message windows.  It requires 
//-----------------------------------------------------------------------------------------|

//---Page-level variables
var po_Window = new Window();
var po_Browser = new Browser();

//-----------------------------------------------------------------------------------------|
//---Window class and associated members
function Window() {
	this.DEFAULT_WIDTH = 175;					//---the width of the window if not otherwise specified
	this.MAX_HEIGHT = 350;						//---if the content of the body area excedes this, scroll bars will appear 
	this.DEFAULT_X_OFFSET = 16;					//---the number of pixels on the X axis that the window will appear away from the mouse
	this.DEFAULT_Y_OFFSET = 16;					//---the number of pixels on the Y axis that the window will appear away from the mouse
	this.DEFAULT_WIN_ID = "helpWin";				//---the default ID of the window div tag
	//this.DEFAULT_HEADER_ID = "helpHeader";		//---the default ID of the window header text div
	this.DEFAULT_BODY_ID = "helpBody";			//---the default ID of the window body text div
	this.DEFAULT_Z_INDEX = 500;					//---the default z-index of the window
	this.DO_EXPAND = false;						//---whether or not expand animation should be used
	this.EXPAND_GROWTH_SIZE = 30;				//---the number of pixels the window grows per evaluation during expand
	this.EXPAND_GROWTH_TIMING = 10;  			//---the number of milliseconds before re-evaluating the window's size
	this.EXPAND_X = 0;							//---used internally to evaluate the expand
	this.EXPAND_Y = 0;							//---used internally to evaluate the expand
	
	this.BodyText = "";
	this.HeaderText = "";
	this.X = 0;
	this.Y = 0;
	this.WinEl = $(this.DEFAULT_WIN_ID);
	this.ShimEl = $("DivShim");
	//this.HeaderEl = $(this.DEFAULT_HEADER_ID);
	this.BodyEl = $(this.DEFAULT_BODY_ID);
	this.Position = "right";
	
	this.Show = showObject;
	this.Hide = hideObject;
	this.Expand = expand;	
	this.Browser = new Browser();	
}		

function showObject(ao_evt, as_InnerText, ai_Width, as_Position) {
	this.BodyEl.style.height = null;
	this.WinEl.style.display = "block";
	this.ShimEl.style.display = "none";
	//this.HeaderEl.innerHTML = as_HeaderText;
	this.BodyEl.innerHTML = as_InnerText;	
	if (as_Position == 'left'){this.Position = "left";}
	
	this.Y = parseInt(po_Window.WinEl.style.top);
	this.X = parseInt(po_Window.WinEl.style.left);
	this.Width = ai_Width ? ai_Width + 'px' : this.DEFAULT_WIDTH + 'px';
	this.WinEl.style.width = po_Window.Width;
	
	var winHeight = document.viewport.getHeight();
    var winWidth = document.viewport.getWidth();
	if((Event.pointerY(ao_evt) + po_Window.DEFAULT_Y_OFFSET + this.BodyEl.offsetHeight) > winHeight)
	{
	    this.WinEl.style.top = (Event.pointerY(ao_evt) - po_Window.DEFAULT_Y_OFFSET - this.BodyEl.offsetHeight) + 'px';	    
	}
	else{
	    this.WinEl.style.top = Event.pointerY(ao_evt) + po_Window.DEFAULT_Y_OFFSET + 'px';
	}	
	if((Event.pointerX(ao_evt) + po_Window.DEFAULT_X_OFFSET + ai_Width) > winWidth)
	{
	    this.Position = "left";
	}
	else{
	    this.Position = "right";
	}
			
	if (this.Position == "left"){
		this.WinEl.style.left = (Event.pointerX(ao_evt) - po_Window.DEFAULT_X_OFFSET - ai_Width) + 'px';
	}else{
	    this.WinEl.style.left = Event.pointerX(ao_evt) + po_Window.DEFAULT_X_OFFSET + 'px';
	}
	
	//this.WinEl.style.top = GetElementTop(ao_evt);
	//this.WinEl.style.left = GetElementLeft(ao_evt);
	
	this.WinEl.style.zIndex = this.DEFAULT_Z_INDEX;
	
	if(this.BodyEl.offsetHeight > this.MAX_HEIGHT) {
		this.BodyEl.style.height = this.MAX_HEIGHT;
		this.BodyEl.style.overflow = 'auto';		
	} else {
		this.BodyEl.style.height = this.BodyEl.offsetHeight;
	}
	
	if(this.DO_EXPAND) {
		this.EXPAND_X = 0;
		this.EXPAND_Y = 0;		
		
		this.Expand();
	}
	
    this.ShimEl.style.width = this.WinEl.offsetWidth;
    this.ShimEl.style.height = this.WinEl.offsetHeight;
    this.ShimEl.style.top = this.WinEl.style.top;
    this.ShimEl.style.left = this.WinEl.style.left;
    this.ShimEl.style.zIndex = this.WinEl.style.zIndex - 1;
    this.ShimEl.style.display = "block";
}		

function hideObject(evt) {
	this.WinEl.style.display = "none";
	this.ShimEl.style.display = "none";
}		

function expand() {
	var lb_continue = false;
	
	if((parseInt(po_Window.WinEl.clientWidth) + 2 >= po_Window.EXPAND_X) || (parseInt(po_Window.WinEl.clientHeight) + 2 >= po_Window.EXPAND_Y)) { 
		po_Window.WinEl.style.clip = GetClipString(po_Window.EXPAND_X, po_Window.EXPAND_Y);
		po_Window.ShimEl.style.clip = GetClipString(po_Window.EXPAND_X, po_Window.EXPAND_Y);
		
		po_Window.EXPAND_X += po_Window.EXPAND_GROWTH_SIZE;
		po_Window.EXPAND_Y += po_Window.EXPAND_GROWTH_SIZE;
		lb_continue = true;
	}
	
	if(lb_continue) {				
		setTimeout("expand()", this.EXPAND_GROWTH_TIMING);
	} else {			
		po_Window.WinEl.style.clip = "rect(auto, auto, auto, auto)";
		po_Window.ShimEl.style.clip = "rect(auto, auto, auto, auto)";	
	}
}

function GetClipString(ai_x, ai_y) {
	if(parseInt(ai_x) >= po_Window.EXPAND_X)
		ai_x = po_Window.EXPAND_X + 10;
		
	if(parseInt(ai_y) >= po_Window.EXPAND_Y)
		ai_y = po_Window.EXPAND_Y + 10;

	return "rect(0px, " + ai_x + "px, " + ai_y + "px, 0px)";	
}
//---end window class
//-----------------------------------------------------------------------------------------|


//-----------------------------------------------------------------------------------------|
//---Browser class and associated members
function Browser() {
	var ua, s, i;

	this.isIE = false;
	this.isNS = false;
	this.version = null;

	ua = navigator.userAgent;

	s = "MSIE";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}

	s = "Netscape6/";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}

	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = 6.1;
		return;
	}
}
//---end Browser class
//-----------------------------------------------------------------------------------------|


//-----------------------------------------------------------------------------------------|
//---static Window helper methods
function WindowX() {
	if(po_Browser.isIE)
		return document.body.offsetWidth + document.documentElement.scrollLeft + document.body.scrollLeft;
	else
		return window.innerWidth + document.documentElement.scrollLeft + document.body.scrollLeft;		
}

function WindowY() {
	if(po_Browser.isIE)
		return document.body.offsetHeight + document.documentElement.scrollTop + document.body.scrollTop;
	else
		return window.innerHeight + document.documentElement.scrollTop + document.body.scrollTop;				
}

function GetEvent(ao_evt) {
	return window.event ? window.event : ao_evt;
}

//---end static Window helper methods

//-----------------------------------------------------------------------------------------|

