//-----------------------------------------------------------------------------------------|
//-----------------------------------------------------------------------------------------|
// AUTHOR:                  Jeff Kody (kodyj@nlg.com)
// CREATED:                 7/20/2005
// MODIFICATION HISTORY:
// PURPOSE:                 this file contains objects and static methods for displaying
//							dynamic inline message windows.  It requires 
//-----------------------------------------------------------------------------------------|
//-----------------------------------------------------------------------------------------|

//-----------------------------------------------------------------------------------------|
//---Window class and associated members
function Window() {
	this.DEFAULT_WIDTH = 175;					//---the width of the window if not otherwise specified
	this.DEFAULT_X_OFFSET = 21;					//---the number of pixels on the X axis that the window will appear away from the mouse
	this.DEFAULT_Y_OFFSET = 5;					//---the number of pixels on the Y axis that the window will appear away from the mouse
	this.DEFAULT_WIN_ID = "infoBox";			//---the default ID of the window div tag
	this.DEFAULT_HEADER_ID = "infoHeaderText";	//---the default ID of the window header text div
	this.DEFAULT_BODY_ID = "infoBoxBody";		//---the default ID of the window body text div
	this.DEFAULT_Z_INDEX = 5;					//---the default z-index of the window
	this.DO_EXPAND = true;						//---whether or not expand animation should be used
	this.EXPAND_GROWTH_SIZE = 20;				//---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.RegisterMarkup = registerMarkup();
	this.WinEl = GetElement(this.DEFAULT_WIN_ID);
	this.ShimEl = GetElement(this.DEFAULT_WIN_ID + "_shim");
	this.HeaderEl = GetElement(this.DEFAULT_HEADER_ID);
	this.BodyEl = GetElement(this.DEFAULT_BODY_ID);
	
	this.Show = showInfoBox;
	this.Hide = hideInfoBox;
	this.DragStart = pickupInfoBox;
	this.Drag = dragInfoBox;
	this.DragStop = releaseInfoBox;	
	this.Expand = expand;	
	this.Broswer = new Browser();
}

function registerMarkup() {
    var sb = new Array();
    
    sb.push("<div id='infoBox' style='position: absolute; display: none;'>");
    sb.push("<div id='infoBoxHeader' class='infoBoxHeader' onmousedown=\"pickupInfoBox(event, 'infoBox');\">");
    sb.push("<span id='infoHeaderText' class='ibHeader'></span>");
    sb.push("</div>");
    sb.push("<div id='infoBoxBody' class='ibBody'></div>");
    sb.push("<div id='infoBoxFooter' class='infoBoxFooter'><a href='javascript: InfoBox.Hide();'>[close]</a></div>");
    sb.push("</div>");
    sb.push("<iframe id='infoBox_shim' scrolling='no' frameborder='0' style='position:absolute; top:0px; left:0px; display:none;'></iframe>");
    document.write(sb.join(''));
}		

function showInfoBox(ao_evt, as_HeaderText, as_InnerText, ai_Width) {
	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;	
		
	
	this.Y = parseInt(this.WinEl.style.top);
	this.X = parseInt(this.WinEl.style.left);
	this.Width = ai_Width ? ai_Width + "px" : this.DEFAULT_WIDTH + "px";
	this.WinEl.style.width = this.Width;
	this.WinEl.style.top = GetElementTop(ao_evt) + "px";
	this.WinEl.style.left = GetElementLeft(ao_evt) + "px";
	
	this.WinEl.style.zIndex = this.DEFAULT_Z_INDEX;
	
	/*
	SetOpacity(this.WinEl, 85);
	SetOpacity(this.BodyEl, 85);
	SetOpacity(this.HeaderEl, 85);
	SetOpacity(this.ShimEl, 85);
	*/
	
	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 hideInfoBox(evt) {
 this.WinEl.style.display = "none";
 this.ShimEl.style.display = "none";
}



function GetElementTop(ao_evt) {
	var y = 0;
	var ls_WorkingHeight = window.InfoBox.WinEl.clientHeight < window.InfoBox.MAX_HEIGHT ? window.InfoBox.WinEl.clientHeight : window.InfoBox.MAX_HEIGHT;
	
	if((GetMouseY(ao_evt) + parseInt(ls_WorkingHeight)) + (window.InfoBox.DEFAULT_Y_OFFSET * 2) > WindowY()) {
		y =  GetMouseY(ao_evt) - parseInt(ls_WorkingHeight) - window.InfoBox.DEFAULT_Y_OFFSET;
	} else {
		y =  GetMouseY(ao_evt) + window.InfoBox.DEFAULT_Y_OFFSET;
	}
	
	return y > 0 ? y : window.InfoBox.DEFAULT_Y_OFFSET;
}

function GetElementLeft(ao_evt) {
	var x = 0;
	
	if((GetMouseX(ao_evt) + parseInt(window.InfoBox.Width) + (window.InfoBox.DEFAULT_X_OFFSET * 2)) > WindowX())
		x =  GetMouseX(ao_evt) - (parseInt(window.InfoBox.Width) + window.InfoBox.DEFAULT_X_OFFSET);
	else
		x =  GetMouseX(ao_evt) + window.InfoBox.DEFAULT_X_OFFSET;
		
	return x > 0 ? x : window.InfoBox.DEFAULT_X_OFFSET;
}

function expand() {
	var lb_continue = false;

	if((parseInt(window.InfoBox.WinEl.clientWidth) + 2 >= window.InfoBox.EXPAND_X) || (parseInt(window.InfoBox.WinEl.clientHeight) + 2 >= window.InfoBox.EXPAND_Y)) { 
		window.InfoBox.WinEl.style.clip = GetClipString(window.InfoBox.EXPAND_X, window.InfoBox.EXPAND_Y);
		window.InfoBox.ShimEl.style.clip = GetClipString(window.InfoBox.EXPAND_X, window.InfoBox.EXPAND_Y);
		
		window.InfoBox.EXPAND_X += window.InfoBox.EXPAND_GROWTH_SIZE;
		window.InfoBox.EXPAND_Y += window.InfoBox.EXPAND_GROWTH_SIZE;
		lb_continue = true;
	}
	  
	if(lb_continue) {				
		setTimeout("expand()", this.EXPAND_GROWTH_TIMING);
	} else {			
		window.InfoBox.WinEl.style.clip = "rect(auto, auto, auto, auto)";
		window.InfoBox.ShimEl.style.clip = "rect(auto, auto, auto, auto)";	
	}
}
 
function GetClipString(ai_x, ai_y) {
	
	if(parseInt(ai_x) >= window.InfoBox.EXPAND_X)
		ai_x = window.InfoBox.EXPAND_X + 10;
  
	if(parseInt(ai_y) >= window.InfoBox.EXPAND_Y)
		ai_y = window.InfoBox.EXPAND_Y + 10;
    
    //alert(ai_x + ' ' + ai_y);
    
	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(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(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;
}

function GetElement(as_el) {
	var lo_el = document.getElementById(as_el);
	
	if(!lo_el)
		return null;
		
	if(lo_el.nodeType == 3)
		return lo_el.parentNode;
	else
		return lo_el;
}

function GetMouseX(ao_evt) {
	// Handles the way events are fired (especially when dealing with all CSS layouts)
	var e = GetEvent(ao_evt);
	if (e.pageX) {
		return e.pageX;
	} else if (e.clientX) {
	   return 	e.clientX + (document.documentElement.scrollLeft ?
	   						 document.documentElement.scrollLeft :
	   						 document.body.scrollLeft);
	} else {
		return null;
	}
}

function GetMouseY(ao_evt) {
	// Handles the way events are fired (especially when dealing with all CSS layouts)
	var e = GetEvent(ao_evt);
	if (e.pageY) {
		return e.pageY;
	} else if (e.clientY) {
		return e.clientY + (document.documentElement.scrollTop ?
	   	        		    document.documentElement.scrollTop :
	   						document.body.scrollTop);
	} else {
		return null;
	}
}

function SetOpacity(ao_el, ai_Opacity) {
	ao_el.style.opacity = (ai_Opacity / 100);
   	ao_el.style.MozOpacity = (ai_Opacity / 100);
  	ao_el.style.KhtmlOpacity = (ai_Opacity / 100);
   	ao_el.style.filter = "alpha(opacity=" + ai_Opacity + ")";
}
//---end static Window helper methods
//-----------------------------------------------------------------------------------------|


//-----------------------------------------------------------------------------------------|
//---Static drag-drop members
function pickupInfoBox(ao_evt) {
	var e = GetEvent(ao_evt);

	window.InfoBox.Y = parseInt(window.InfoBox.WinEl.style.top);
	window.InfoBox.X = parseInt(window.InfoBox.WinEl.style.left);
	window.InfoBox.CursorLeft = GetMouseX(ao_evt);
	window.InfoBox.CursorTop = GetMouseY(ao_evt);
	
	window.InfoBox.WinEl.style.zIndex = window.InfoBox.DEFAULT_Z_INDEX;	
	
	window.InfoBox.OffX = window.InfoBox.CursorLeft - window.InfoBox.X;
	window.InfoBox.OffY = window.InfoBox.CursorTop - window.InfoBox.Y; 	
				
	if(Browser.isIE) {
		document.attachEvent("onmousemove", dragInfoBox);
		document.attachEvent("onmouseup", releaseInfoBox);
		e.cancelBubble = true;
		e.returnValue = false;
	} else {
		document.addEventListener("mousemove", dragInfoBox, true);
	    document.addEventListener("mouseup", releaseInfoBox, true);
	    e.preventDefault();
	}
	
	SetOpacity(window.InfoBox.WinEl, 50);
	SetOpacity(window.InfoBox.BodyEl, 50);
	SetOpacity(window.InfoBox.HeaderEl, 50);
	SetOpacity(window.InfoBox.ShimEl, 50)
}

function dragInfoBox(ao_evt) {
	var e = GetEvent(ao_evt);

	window.InfoBox.CursorLeft = GetMouseX(ao_evt);
	window.InfoBox.CursorTop = GetMouseY(ao_evt);
	
	var y = window.InfoBox.CursorTop - window.InfoBox.OffY;
	var x = window.InfoBox.CursorLeft - window.InfoBox.OffX;
	
	window.InfoBox.WinEl.style.top = y + "px";
	window.InfoBox.WinEl.style.left = x + "px";
	window.InfoBox.ShimEl.style.top = y + "px";
	window.InfoBox.ShimEl.style.left = x + "px";
	
	if(Browser.isIE) {
		e.cancelBubble = true;
		e.returnValue = false;
	} else {
		e.preventDefault()
	}
}

function releaseInfoBox(ao_evt) {
	var e = GetEvent(ao_evt);	
	
	SetOpacity(window.InfoBox.WinEl, 100);
	SetOpacity(window.InfoBox.BodyEl, 100);
	SetOpacity(window.InfoBox.HeaderEl, 100)
	SetOpacity(window.InfoBox.ShimEl, 100);		
	
	if(Browser.isIE) {
		document.detachEvent("onmousemove", dragInfoBox);
		document.detachEvent("onmouseup", releaseInfoBox);	
		e.cancelBubble = true;
		e.returnValue = false;
	} else {
		document.removeEventListener("mousemove", dragInfoBox, true);
  			document.removeEventListener("mouseup", releaseInfoBox, true);
	}
}	
//---end static drag-drop members
//-----------------------------------------------------------------------------------------|

function initInfoBox() {
    window.InfoBox = new Window();
    window.Browser = new Browser();
}

