var startingZ = 999;
var floaterZ = floaterZ;
var floaters = new Array();

var Floater = {
	init : function( fName, fTitleHTML, fContentHTML, fWidth, fHeight )
	{
		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 = ( pageWidth( ) / 2 ) - ( Math.floor( fWidth / 2 ) );
		var ypos = posTop( ) + ( pageHeight( ) / 2 - ( Math.floor( fHeight / 2 ) ) );

		if( !document.getElementById("floater-" + fName) )
		{
			f = document.createElement("div");
			f.id = "floater-" + fName;
			f.className = "floater";
			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 = floaterZ;
			f.onmousedown = function( )
			{
				Floater.swapFocus( this );
			}
			
			fouter = document.createElement("div");
			fouter.className = "floater-outer";
			fouter.style.height = fHeight + "px";
			fouter.style.width = fWidth + "px";

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

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

			ftitlewrapper = document.createElement("div");
			ftitlewrapper.className = "floater-title-wrapper lightblueback";

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

			fclose = document.createElement("div");
			fclose.className = "floater-close";
			fclose.onmouseover = function( )
			{
				this.className = 'floater-close-hover';
			}
			fclose.onmouseout = function( )
			{
				this.className = 'floater-close';
			}
			fclose.onclick = function( )
			{
				Floater.close( fName );
			}
			//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 = "small";

			fcontentwrapper = document.createElement("div");
			fcontentwrapper.className = "floater-content-wrapper";
			
			fcontent = document.createElement("div");
			fcontent.className = "floater-content";
			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 - getElementHeight( ftitlewrapper ) - 9;
			fcontentwrapper.style.height = fcontentwrapperheight + "px";
			finner.style.height = fcontentwrapperheight + "px";
			finner.style.width = ( fWidth - 6 ) + "px";
			finner.style.top = ( getElementHeight( ftitle ) + 6 ) + "px";
			finner.style.left = "3px";
			
			if( ie6 || ie55 )
			{
				iframeshim = document.createElement('iframe');
				iframeshim.id = "floater-shim-" + fName;
				iframeshim.src = 'javascript:;';
				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.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
				document.body.insertBefore( iframeshim, f );
			}

			floaterZ += 10;
			Drag.init( ftitlewrapper, ( ie6 || ie55 ? new Array(f, iframeshim) : f ) );
			floaters.push( f );
		}
	},

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

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

	close : function( fName )
	{
		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.length - 1; fcount++ )
		{
			if( floaters[fcount].id == "floater-" + fName || fhit )
			{
				floaters[fcount] = floaters[fcount+1];
				fhit = true;
			}
		}
		floaters.pop();
	}
};

