// Aniel Santos
// 2:44 PM 8/23/2006. 5:35 PM 1/29/2007. 12:43 PM 6/19/2007
// ============================================================================
// ##<t> FloatBox2 Object
// ##<v> 2.2.1

function FloatBox2(xObjID){
	// ##<m> CONSTRUCTOR

	// Private Attributes.
	// --------------------------------------------------------------------
	var containerObj = null;
	
	// Public Methods.
	// --------------------------------------------------------------------
	this.SetContainerObj = function(xObj){ containerObj=xObj; }
	// ##<m> Set the HTML tag of the floating box by passing the tag object.
	this.SetContainerID = function(xObjID){ containerObj=document.getElementById(xObjID); }
	// ##<m> Set the HTML tag of the floating box by passing the tag ID.
	this.GetBrowser = function(){
		// ##<m> Returns a string identifying the browser being used.<br /><pre><b>Output:</b><br />     Opera<br />     IE<br />     NS6+<br />     FF<br />     UNKNOWN</pre>
		// document.all -- IE, NS6+
		alert(navigator.userAgent);
		var browser = 'UNKNOWN';
		if(navigator.userAgent.indexOf("Opera")>-1){
			browser = 'Opera';
		}else if(navigator.userAgent.indexOf("MSIE")>-1 && navigator.appName=='Microsoft Internet Explorer'){
			browser = 'IE';
		}else if(navigator.appName=='Netscape' && document.all){
			browser = 'NS6+';
		}else if(navigator.userAgent.indexOf("Firefox")>-1){
			browser = 'FF'; 
		}
		return browser;
	}
	// Coordinate functions.
	this.GetCursorCoord = function(xEvent){
		// ##<m> Returns the coordinate of the cursor.<br /><pre><b>Output:</b><br />     <i>output</i>.x<br />     <i>output</i>.y</pre>
		//javascript events needs to be used to use the 'event' variable.
		// event.clientX -- IE
		// event.screenX -- IE
		// xEvent.pageX -- FF
		// xEvent.screenX -- FF, IE
		var x = 0;
		var y = 0;
		if(typeof(window['event'])!='undefined'){
			//x = event.screenX;
			//y = event.screenY;
			var sc = this.GetScroll();
			x = event.clientX + sc.x;
			y = event.clientY + sc.y;
		}else{
			//x = xEvent.screenX;
			//y = xEvent.screenY;
			x = xEvent.pageX;
			y = xEvent.pageY;
		}
		return {x:x, y:y};
	}
	this.GetCoordByObj = function(xObj){
		// ##<m> Returns top-left coordinate of an HTML object.<br /><pre><b>Output:</b><br />     <i>output</i>.x<br />     <i>output</i>.y</pre>
		var left = 0;
		var top = 0;
		if(xObj.offsetParent){
			left = xObj.offsetLeft;
			top = xObj.offsetTop;
			while(xObj=xObj.offsetParent){
				left += xObj.offsetLeft;
				top += xObj.offsetTop;
			}
		}
		return {x:left, y:top};
	}
	this.GetCoordByID = function(xObjID){
		// ##<m> Returns top-left coordinate of an HTML object.<br /><pre><b>Output:</b><br />     <i>output</i>.x<br />     <i>output</i>.y</pre>
		return this.GetCoordByObj(document.getElementById(xObjID));
	}
	// Dimension functions.
	this.GetDimByObj = function(xObj){
		// ##<m> Returns the dimensions of an HTML object.<br /><pre><b>Output:</b><br />     <i>output</i>.x<br />     <i>output</i>.y</pre>
		var x = xObj.offsetWidth;
		var y = xObj.offsetHeight
		return {x:x, y:y};
	}
	this.GetDimByID = function(xObjID){
		// ##<m> Returns the dimensions of an HTML object.<br /><pre><b>Output:</b><br />     <i>output</i>.x<br />     <i>output</i>.y</pre>
		return this.GetDimByObj(document.getElementById(xObjID));
	}
	// Container control.
	this.SetHTML = function(xHTML){ containerObj.innerHTML=xHTML; }
	// ##<m> Set the HTML script to display.
	this.ClearHTML = function(){ containerObj.innerHTML=''; }
	// ##<m> Clear the HTML script to display.
	this.AppendHTML = function(xHTML){ containerObj.innerHTML+=xHTML; }
	// ##<m> Append HTML script to the current HTML script.
	this.Show = function(xCoord, yCoord){
		// ##<m> Show the floating box using the input coordinates.
		containerObj.style.left = xCoord+'px';
		containerObj.style.top = yCoord+'px';
		containerObj.style.visibility = 'visible';
	}
	this.Hide = function(){
		// ##<m> Hide the floating box.
		containerObj.style.visibility = 'hidden';
	}
	this.ScrollTo = function(xOffset, yOffset){
		// ##<m> Scroll the page based on the input offset values.
		this.ScrollToObj(containerObj, xOffset, yOffset);
	}
	// Preset locations based on cursor position.
	this.ShowTL = function(xEvent){
		// ##<m> Show the floating box on the top-left of the cursor.
		var coord = this.GetCursorCoord(xEvent);
		var dim = this.GetDimByObj(containerObj);
		this.Show(coord.x-dim.x-3, coord.y-dim.y-3);
	}
	this.ShowTM = function(xEvent){
		// ##<m> Show the floating box on the top-middle of the cursor.
		var coord = this.GetCursorCoord(xEvent);
		var dim = this.GetDimByObj(containerObj);
		this.Show(coord.x-(dim.x/2), coord.y-dim.y-5);
	}
	this.ShowTR = function(xEvent){
		// ##<m> Show the floating box on the top-right of the cursor.
		var coord = this.GetCursorCoord(xEvent);
		var dim = this.GetDimByObj(containerObj);
		this.Show(coord.x+3, coord.y-dim.y-3);
	}
	this.ShowMR = function(xEvent){
		// ##<m> Show the floating box on the middle-right of the cursor.
		var coord = this.GetCursorCoord(xEvent);
		var dim = this.GetDimByObj(containerObj);
		this.Show(coord.x+5, coord.y-(dim.y/2));
	}
	this.ShowBR = function(xEvent){
		// ##<m> Show the floating box on the bottom-right of the cursor.
		var coord = this.GetCursorCoord(xEvent);
		this.Show(coord.x+5, coord.y+5);
	}
	this.ShowBM = function(xEvent){
		// ##<m> Show the floating box on the bottom-middle of the cursor.
		var coord = this.GetCursorCoord(xEvent);
		var dim = this.GetDimByObj(containerObj);
		this.Show(coord.x-(dim.x/2), coord.y+5);
	}
	this.ShowBL = function(xEvent){
		// ##<m> Show the floating box on the bottom-left of the cursor.
		var coord = this.GetCursorCoord(xEvent);
		var dim = this.GetDimByObj(containerObj);
		this.Show(coord.x-dim.x-5, coord.y+3);
	}
	this.ShowML = function(xEvent){
		// ##<m> Show the floating box on the middle-left of the cursor.
		var coord = this.GetCursorCoord(xEvent);
		var dim = this.GetDimByObj(containerObj);
		this.Show(coord.x-dim.x-5, coord.y-(dim.y/2));
	}
	// Scroll functions.
	this.GetScroll = function(){
		// ##<m> Returns the current browser scroll values.<pre><b>Output:</b><br />     <i>output</i>.x<br />     <i>output</i>.y</pre>
		// document.body.scrollLeft -- IE; *NOTE: Using DOCTYPE, need to use document.documentElement.scrollTop.
		// document.documentElement.scrollLeft -- IE, FF; *See NOTE above.
		// window.pageXOffset -- NS, FF
		var x = 0;
		var y = 0;
		if(x+y==0 && document.documentElement){
			x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
		}
		if(x+y==0 && document.body){
			x = document.body.scrollLeft;
			y = document.body.scrollTop;
		}
		if(x+y==0 && window){
			x = window.pageXOffset;
			y = window.pageYOffset;
		}
		return {x:x, y:y}
	}
	this.ScrollToObj = function(xObj, xOffset, yOffset){
		// ##<m> Scroll to an HTML object with the input offset values.
		//var xOffset = (xOffset==null)?0:xOffset;
		//var yOffset = (yOffset==null)?0:yOffset;
		var coord = this.GetCoordByObj(xObj);
		window.scroll(coord.x+xOffset, coord.y+yOffset);
	}
	this.ScrollToID = function(xObjID, xOffset, yOffset){ this.ScrollToObj(document.getElementById(xObjID), xOffset, yOffset); }
	// ##<m> Scroll to an HTML object by ID with the input offset values.
	this.VerticalScrollToObj = function(xObj, yOffset){
		// ##<m> Vertically scroll to an HTML object with the input offset value.
		//var yOffset = (yOffset==null)?0:yOffset;
		var coord = this.GetCoordByObj(xObj);
		window.scroll(0, coord.y+yOffset);
	}
	this.VerticalScrollToID = function(xObjID, yOffset){ this.VerticalScrollToObj(document.getElementById(xObjID), yOffset); }
	// ##<m> Vertically scroll to an HTML object by ID with the input offset value.
	this.HorizontalScrollToObj = function(xObj, xOffset){
		// ##<m> Horizontally scroll to an HTML object with the input offset value.
		//var xOffset = (xOffset==null)?0:xOffset;
		var coord = this.GetCoordByObj(xObj);
		window.scroll(coord.x+xOffset, 0);
	}
	this.HorizontalScrollToID = function(xObjID, xOffset){ this.HorizontalScrollToObj(document.getElementById(xObjID), xOffset); }
	// ##<m> Horizontally scroll to an HTML object by ID with the input offset value.
	this.ScrollToCoordinate = function(xScroll, yScroll){ window.scroll(xScroll, yScroll); }
	// ##<m> Scroll to specified coordinates.
	this.SaveCurrentScroll = function(){
		// ##<m> Saves the current scroll data as COOKIE variables.
		var sc = this.GetScroll();
		document.cookie = 'fbScrollX='+sc.x;
		document.cookie = 'fbScrollY='+sc.y;
	}
	this.GotoSavedScroll = function(){
		// ##<m> Scroll to saved position saved using SaveCurrentScroll().
		var scrollX = 0;
		var scrollY = 0;
		if(document.cookie.length>0){
			var startPos = document.cookie.indexOf('fbScrollX=');
			if(startPos!=-1){
				startPos = startPos+10;
				endPos = document.cookie.indexOf(';', startPos);
				if(endPos==-1){ endPos=document.cookie.length; }
				scrollX = unescape(document.cookie.substring(startPos, endPos));
			}
			var startPos = document.cookie.indexOf('fbScrollY=');
			if(startPos!=-1){
				startPos = startPos+10;
				endPos = document.cookie.indexOf(';', startPos);
				if(endPos==-1){ endPos=document.cookie.length; }
				scrollY = unescape(document.cookie.substring(startPos, endPos));
			}
		}
		this.ScrollToCoordinate(scrollX, scrollY);
	}
	// Other functions.
	this.AddFormField = function(xForm, xFieldName, xFieldValue){
		// ###<m> Add a field to the specified form.
		var elem = document.createElement('input');
		elem.type = 'hidden';
		elem.name = xFieldName;
		elem.value = xFieldValue;
		if(typeof(xForm)=='object'){ xForm.appendChild(elem); }
		else{ document.getElementById(xForm).appendChild(elem); }
	}
	
	// Constructor.
	// --------------------------------------------------------------------
	if(xObjID!=null){ this.SetContainerID(xObjID); }
}

// ----------------------------------------------------------------------------

/******************************************************************************

	_div("id floatBox | style width:auto; height:auto; border:1px solid #404040; background-color:#f0e0ff; padding:10px; line-height:25px; position:absolute; left:10px; top:10px; visibility:hidden;", '-');
	_js("
		var fb = new FloatBox2('floatBox');
	");
	
	$html = HTMLEncode(str_replace(array("'", "\n"), array("\\\'", ''), $html);
	_a("style font-size:0.7em; | href # | onclick fb.SetHTML('$html'); fb.ShowBR(event); return false;", '[options]');

******************************************************************************/
