var Floaters =
{
	startingZ : 999,
	floaterZ : this.startingZ,
	floaters : new Array(),

	zCheck : function()
	{
		if( !( Floaters.floaterZ ) )
		{
			Floaters.floaterZ = Floaters.startingZ;
		}
	},

	swapFocus : function( fObj )
	{
		//reorganize floater z-indexes starting with the current one
		Floaters.zCheck();
		Floaters.floaterZ = Floaters.startingZ;
		var fhit = false;
		for( fcount = 0; fcount < Floaters.floaters.length - 1; fcount++ )
		{
			if( Floaters.floaters[fcount].getRootElem().id == fObj.getRootElem().id || fhit )
			{
				Floaters.floaters[fcount] = Floaters.floaters[fcount+1];
				fhit = true;
			}
		}
		Floaters.floaters.pop();
		Floaters.floaters.push( fObj );

		for( fcount = 0; fcount < Floaters.floaters.length; fcount++ )
		{
			Floaters.floaters[fcount].getRootElem().style.zIndex = Floaters.floaterZ;
			Floaters.floaterZ += 10;
		}
	},

	close : function( fName )
	{
		Floaters.zCheck();
		if( document.getElementById( 'floater-' + fName ) )
		{
			document.getElementById( 'floater-' + fName ).parentNode.removeChild( document.getElementById( 'floater-' + fName ) );
		}
		if( document.getElementById( 'floater-shim-' + fName ) )
		{
			document.getElementById( 'floater-shim-' + fName ).parentNode.removeChild( document.getElementById( 'floater-shim-' + fName ) );
		}

		//remove from array to track the floaters on the page
		var fhit = false;
		for( fcount = 0; fcount < Floaters.floaters.length - 1; fcount++ )
		{
			if( Floaters.floaters[fcount].getRootElem().id == "floater-" + fName || fhit )
			{
				Floaters.floaters[fcount] = Floaters.floaters[fcount+1];
				fhit = true;
			}
		}
		Floaters.floaters.pop();
	},

	closeMostRecent : function()
	{
		Floaters.zCheck();
		if( Floaters.floaters.length > 0 )
		{
			Floaters.close( Floaters.floaters[Floaters.floaters.length - 1].getName() );
		}
	},

    keyCheck : function( evtObj )
	{
		Floaters.zCheck();
		var key = window.event ? event.keyCode : evtObj.keyCode;
		switch( key )
		{
			case 27:
				//escape key
				Floaters.closeMostRecent();
				break;
		}
	},

	createNew : function( fName, fClassName, fTitleHTML, fContentHTML, fWidth, fHeight, startX, startY, fXMin, fXMax, fYMin, fYMax, dragAnchor, showTitlebar, moveable )
	{
		Floaters.zCheck();
		//provide a few defaults
		fName = fName == null ? "default" : fName;
		fClassName = fClassName == null ? "default" : fClassName;
		fWidth = isNaN( fWidth ) ? 500 : fWidth;
		fHeight = isNaN( fHeight ) ? 300 : fHeight;
		fXMin = isNaN( fXMin ) ? null : fXMin;
		fXMax = isNaN( fXMax ) ? null : fXMax;
		fYMin = isNaN( fYMin ) ? null : fYMin;
		fYMax = isNaN( fYMax ) ? null : fYMax;
		showTitlebar = showTitlebar === false ? false : true;
		dragAnchor = showTitlebar == false ? "window" : dragAnchor == "titlebar" || dragAnchor == "window" ? dragAnchor : "titlebar";
		moveable = moveable === false ? false : true;

		var f = null;
		var fouter = null;
		var finner = null;
		var fwrapper = null;
		var ftitlewrapper = null;
		var ftitle = null;
		var fclose = null;
		var fsmall = null;
		var fcontentwrapper = null;
		var fcontent = null;
		var iframeshim = null;
		fWidth = ( fWidth == null ? 500 : fWidth );
		fHeight = ( fHeight == null ? 300 : fHeight );
		var xpos = startX == null ? ( Floaters.pageWidth( ) / 2 ) - ( Math.floor( fWidth / 2 ) ) : startX;
		var ypos = startY == null ? Floaters.posTop( ) + ( Floaters.pageHeight( ) / 2 - ( Math.floor( fHeight / 2 ) ) ) : startY;

		if( document.getElementById( "floater-" + fName ) )
		{
			Floaters.close( fName );
		}

		//if( !document.getElementById("floater-" + fName) )
		//{
			f = document.createElement("div");
			f.id = "floater-" + fName;
			f.className = "floater-" + fClassName + ( moveable === true && dragAnchor == "window" ? " floater-moveable-" + fClassName : "" );
			f.style.width = fWidth + "px";
			f.style.height = fHeight + "px";
			f.style.position = "absolute";
			f.style.top = ypos + "px";
			f.style.left = xpos + "px";
			f.style.zIndex = Floaters.floaterZ;

			fouter = document.createElement("div");
			fouter.className = "floater-outer-" + fClassName;
			fouter.style.height = fHeight + "px";
			fouter.style.width = fWidth + "px";

			finner = document.createElement("div");
			finner.className = "floater-inner-" + fClassName;

			fwrapper = document.createElement("div");
			fwrapper.className = "floater-wrapper-" + fClassName;

			ftitlewrapper = document.createElement("div");
			ftitlewrapper.className = "floater-title-wrapper-" + fClassName + ( moveable === true && dragAnchor == "titlebar" ? " floater-moveable-" + fClassName : "" );;

			ftitle = document.createElement("div");
			ftitle.className = "floater-title-" + fClassName;
			ftitle.id = "floater-title-" + fName;
			ftitle.innerHTML = fTitleHTML;

			fclose = document.createElement("div");
			fclose.className = "floater-close-" + fClassName;
			fclose.onmouseover = function( )
			{
				this.className = 'floater-close-hover-' + fClassName;
			}
			fclose.onmouseout = function( )
			{
				this.className = 'floater-close-' + fClassName;
			}
			fclose.onclick = function( )
			{
				Floaters.close( fName );
			}

			if( showTitlebar !== true )
			{
				ftitlewrapper.style.display = "none";
			}
			//fclose.onmouseover = "javascript:this.className='floater-close-hover';";
			//fclose.onmouseout = "javascript:this.className='floater-close';";
			//fclose.onclick = "javascript:Floater.close( 'floater-' + fName );";

			fsmall = document.createElement("div");
			fsmall.className = "floater-spacer-" + fClassName;

			fcontentwrapper = document.createElement("div");
			fcontentwrapper.className = "floater-content-wrapper-" + fClassName;

			fcontent = document.createElement("div");
			fcontent.id = "floater-content-" + fName;
			fcontent.className = "floater-content-" + fClassName;
			fcontent.innerHTML = fContentHTML;

			fcontentwrapper.appendChild( fcontent );
			ftitlewrapper.appendChild( ftitle );
			fwrapper.appendChild( ftitlewrapper );
			fwrapper.appendChild( fclose );
			fwrapper.appendChild( fsmall );
			fwrapper.appendChild( fcontentwrapper );
			f.appendChild( fouter );
			f.appendChild( finner );
			f.appendChild( fwrapper );

			document.body.appendChild( f );

			var fcontentwrapperheight = fHeight - Floaters.getElementHeight( ftitlewrapper ) - 9;
			fcontentwrapper.style.height = fcontentwrapperheight + "px";
			finner.style.height = fcontentwrapperheight + "px";
			finner.style.width = ( fWidth - 6 ) + "px";
			finner.style.top = ( Floaters.getElementHeight( ftitle ) + 6 ) + "px";
			finner.style.left = "3px";

			//insert an i-frame shim so it will show above ads and flash content
			iframeshim = document.createElement('iframe');
			iframeshim.id = "floater-shim-" + fName;
			iframeshim.className = "floater-shim-" + fClassName;
			iframeshim.src = '/images/blank.gif';
			iframeshim.style.position = "absolute";
			iframeshim.style.top = ypos + "px";
			iframeshim.style.left = xpos + "px";
			iframeshim.style.height = fHeight + "px";
			iframeshim.style.width = fWidth + "px";
			iframeshim.style.border = "0px";
			/*iframeshim.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
			iframeshim.style.mozOpacity="0";
			iframeshim.style.opacity="0";*/
			document.body.insertBefore( iframeshim, f );

			Floaters.floaterZ += 10;

			var dragTargets = new Array();
			var dragAnchorObj = f;
			if( moveable === true )
			{
				dragTargets = new Array( f, iframeshim );
				dragAnchorObj = dragAnchor == "titlebar" ? ftitlewrapper : f;
			}

			var floaterObj = new Floater( fName, fClassName, f, dragAnchorObj, dragTargets, fXMin, fXMax, fYMin, fYMax );

			var swapFunc = function( floaterObj )
			{
				return function()
				{
					Floaters.swapFocus( floaterObj );
				};
			}( floaterObj );
			if( f.addEventListener )
			{
				f.addEventListener( "mousedown", swapFunc, true );
			}
			else
			{
				f.attachEvent( "onmousedown", swapFunc );
			}
			if( moveable === true )
			{
				floaterObj.dragInit();
			}
			Floaters.floaters.push( floaterObj );
		//}
	},

	//HELPER FUNCTIONS
	isArray : function( obj )
	{
		try
		{
			if( obj.constructor.toString().indexOf( "Array" ) == -1 )
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		catch( e )
		{
			return false;
		}
	},

	getElementHeight : function( element )
	{
		return element.style.pixelHeight ? element.style.pixelHeight : element.offsetHeight;
	},

	getElementWidth : function( element )
	{
		return element.style.pixelWidth ? element.style.pixelWidth : element.offsetWidth;
	},

	pageWidth : function()
	{
		return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
	},

	pageHeight : function()
	{
		return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;
	},

	posLeft : function()
	{
		return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
	},

	posTop : function()
	{
		return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
	},

	posRight : function()
	{
		return Floaters.posLeft() + Floaters.pageWidth();
	},

	posBottom : function()
	{
		return Floaters.posTop() + Floaters.pageHeight();
	},

	getElementLeft : function( element, isInline )
	{
		var xPos = element.offsetLeft;
		if( !isInline )
		{
			var temp = element.offsetParent;
  			while( temp != null )
			{
  				xPos += temp.offsetLeft;
  				temp = temp.offsetParent;
 			}
		}
		return xPos;
	},

	getElementTop : function( element )
	{
		var yPos = element.offsetTop;
		var temp = element.offsetParent;
		while( temp != null )
		{
	  		yPos += temp.offsetTop;
  			temp = temp.offsetParent;
		}
		return yPos;
	},

	getElementsByClass : function( searchClass, node )
	{
		var classElements = new Array();
		if( node == null )
		{
			node = document;
		}
		for( var i = 0, j = 0; i < node.childNodes.length; i++ )
		{
			if( node.childNodes[i].className == searchClass )
			{
				classElements[j] = node.childNodes[i];
				j++;
			}
		}
		return classElements;
	},

	getChildNodes : function( node )
	{
		var nodes = new Array();
		for( i = 0, j = 0; i < node.childNodes.length; i++ )
		{
			if( node.childNodes[i].tagName != undefined )
			{
			nodes[j] = node.childNodes[i];
				j++;
			}
		}
		return nodes;
	}
};

//class used merely to track and group certain elements of a floater div,
// as well as perform certain functions
//should be used more as a reference than anything
var Floater = function( fName, fClassName, rootElem, dragAnchor, dragTargets, fXMin, fXMax, fYMin, fYMax )
{
	var lastMouseX = 0;
	var lastMouseY = 0;
	var minMouseX = null;
	var maxMouseX = null;
	var minMouseY = null;
	var maxMouseY = null;
	var thisref = this;

	this.getName = function()
	{
		return fName;
	}

	this.getClassName = function()
	{
		return fClassName;
	}

	this.getRootElem = function()
	{
		return rootElem;
	}

	this.getDragAnchor = function()
	{
		return dragAnchor;
	}

	this.getDragTargets = function()
	{
		return dragTargets;
	}

	this.dragInit = function()
	{
		dragAnchor.onmousedown = this.dragStart;

		for( var rcount = 0; rcount < dragTargets.length; rcount++ )
		{
			if( isNaN( parseInt( dragTargets[rcount].style.left ) ) )
			{
				dragTargets[rcount].style.left = "0px";
			}
			if( isNaN( parseInt( dragTargets[rcount].style.top ) ) )
			{
				dragTargets[rcount].style.top = "0px";
			}

			//dragAnchor.onDragstart = new Function();
			//dragAnchor.onDragend = new Function();
			//dragAnchor.onDrag = new Function();
		}
	}

	this.dragStart = function( eventObj )
	{
		//disable a few things first
		document.body.onmousedown = function(){ return false; }
		document.body.ondragstart = function(){ return false; }
		document.body.onselectstart = function(){ return false; }

		eventObj = findEventObj( eventObj );
		var x = 0;
		var y = 0;

		y = parseInt( rootElem.style.top );
		x = parseInt( rootElem.style.left );

		//dragAnchor.onDragStart( x, y );
		lastMouseX = eventObj.clientX;
		lastMouseY = eventObj.clientY;

		if( fXMin != null )
		{
			minMouseX = eventObj.clientX - x + fXMin;
		}
		if( fXMax != null )
		{
			maxMouseX = minMouseX + fXMax - fXMin;
		}

		if( fYMin != null )
		{
			minMouseY = eventObj.clientY - y + fYMin;
		}
		if( fYMax != null )
		{
			maxMouseY = minMouseY + fYMax - fYMin;
		}

		//window.status = "x:" + x + " ;y:" + y + " ;lastMouseX:" + lastMouseX + " ;lastMouseY:" + lastMouseY + " ;minMouseX:" + minMouseX + " ;minMouseY:" + minMouseY + " ;maxMouseX:" + maxMouseX + " ;maxMouseY:" + maxMouseY;
		document.onmousemove = function( objref )
		{
			return objref.drag;
		}( thisref );
		document.onmouseup = function( objref )
		{
			return objref.dragEnd;
		}( thisref );

		return false;
	}

	this.drag = function( eventObj )
	{
		//disable a few things first
		document.body.onmousedown = function(){ return false; }
		document.body.ondragstart = function(){ return false; }
		document.body.onselectstart = function(){ return false; }

		eventObj = findEventObj( eventObj );

		var ex = eventObj.clientX;
		var ey = eventObj.clientY;
		var x = 0;
		var y = 0;

		x = parseInt( rootElem.style.left );
		y = parseInt( rootElem.style.top );

		var nx, ny;
		if( fXMin != null )
		{
			ex = Math.max( ex, minMouseX );
		}
		if( fXMax != null )
		{
			ex = Math.min( ex, maxMouseX );
		}
		if( fYMin != null )
		{
			ey = Math.max( ey, minMouseY );
		}
		if( fYMax != null )
		{
			ey = Math.min( ey, maxMouseY );
		}

		nx = x + ( ex - lastMouseX );
		ny = y + ( ey - lastMouseY );

		for( var rcount = 0; rcount < dragTargets.length; rcount++ )
		{
			dragTargets[rcount].style.left = nx + "px";
			dragTargets[rcount].style.top = ny + "px";
			lastMouseX = ex;
			lastMouseY = ey;
			//dragTargets[rcount].onDrag( nx, ny );
		}

		return false;
	}

	this.dragEnd = function( eventObj )
	{
		document.body.onmousedown = function(){ return true; }
		document.body.ondragstart = function(){ return true; }
		document.body.onselectstart = function(){ return true; }

		eventObj = findEventObj( eventObj );

		document.onmousemove = null;
		document.onmouseup = null;
		/*for( var rcount = 0; rcount < dragTargets.length; rcount++ )
		{
			dragTargets[rcount].onDragEnd( parseInt( dragAnchor.dragTargets[rcount].style.left ), parseInt( dragAnchor.dragTargets[rcount].style.top ) );
		}*/
	}

	function findEventObj( eventObj )
	{
		if( typeof eventObj == "undefined" )
		{
			eventObj = window.event;
		}
		if( typeof eventObj.layerX == "undefined" )
		{
			eventObj.layerX = eventObj.offsetX;
		}
		if( typeof eventObj.layerY == "undefined" )
		{
			eventObj.layerY = eventObj.offsetY;
		}
		return eventObj;
	}
}

//finally, add a key listener
if( document.addEventListener )
{
	document.addEventListener( "keyup", Floaters.keyCheck, true );
}
else
{
	document.attachEvent( "onkeyup", Floaters.keyCheck );
}

