var arrobjFloatingWindows = new Array();

document.onmouseup = function() {
	for( i in arrobjFloatingWindows ) {
		if( arrobjFloatingWindows[i].stopDrag ) {
			arrobjFloatingWindows[i].stopDrag();
		}
	}
}

FloatingWindow = function ( width, height, startingZ, title, strUrl, windowKey, boolIsScroll ) {

	arrobjFloatingWindows.push( this );
	var uniqueId			= arrobjFloatingWindows.length + '_' + windowKey +'_'+ new Date().getTime();
	var initialized 		= false;
	var dragging 			= false;
	var xOffset 			= undefined;
	var yOffset 			= undefined;
	var xMousePos 			= 0;
	var yMousePos 			= 0;
	var xMousePosMax 		= 0;
	var yMousePosMax 		= 0;
	var windowFrame 		= document.createElement( 'div' );
	var windowFrameShadow 	= document.createElement( 'div' );
	var titleBar 			= document.createElement( 'div' );
	var contents 			= document.createElement( 'div' );
		contents.id 		= uniqueId;
	var button_bar 			= document.createElement( 'div' );
	var save_button 		= new Image( 63, 24 );
		save_button.src 	= '/images/_common/buttons/save.gif';
	var cancel_button 		= new Image( 63, 24 );
		cancel_button.src 	= '/images/_common/buttons/cancel.gif';

	var aForm = undefined;

	var boolIsScroll 		= ( 'true' == boolIsScroll ) ? boolIsScroll : 'false';

	createWindowFrame = function() {
		with( windowFrame ) {
			style.position 	= 'absolute';
			style.zIndex 	= startingZ + arrobjFloatingWindows.length;
			style.width 	= width + 'px';
			style.height 	= height + 'px';
			className 		= 'floatingWindowFrame';
			onmousedown 	= bringToFront;
		}

		document.body.appendChild( windowFrame );

		with( windowFrameShadow ) {
			style.position 	= 'absolute';
			style.zIndex	= startingZ - 1 + arrobjFloatingWindows.length;
			style.width 	= width + 'px';
			style.height 	= height + 'px';
			className 		= 'floatingWindowFrameShadow';
		}

		document.body.appendChild( windowFrameShadow );

		titleBar.className		= 'titleBar';
		titleBar.onmousedown 	= startDrag;
		button_bar.className 	= 'buttonBar';

		windowFrame.appendChild( titleBar );
		windowFrame.appendChild( contents );

		strUrl = strUrl + '&div_id=' +contents.id;
		new Ajax.Updater( contents.id, strUrl, { evalScripts: true, onLoading: showWaitingImage( contents.id ) } );

		if( title == undefined ) title = '';

		titleBar.innerHTML = '<div class="caption">' + title + '</div><div class="buttons" onclick="' + windowKey + '.kill();">[x]</div><div style="clear:both;" />';
		centerWindow( boolIsScroll );
		initialized = false
	}

	centerWindow = function ( boolIsScroll ) {

		if( 'true' == boolIsScroll) {
			position_window(( document.body.clientWidth / 2 ) - ( width / 2 ), ( document.documentElement.scrollTop ) + 100 );
		} else {
			position_window(( document.body.clientWidth / 2 ) - ( width / 2 ), ( document.body.clientHeight / 2 ) - ( height / 2 ));
		}

	}

	bringToFront = function () {
		myZ 	= Number(getZIndex());
		highZ 	= myZ;
		lowZ 	= myZ;

		for( var i in arrobjFloatingWindows ) {
			if( arrobjFloatingWindows[i].getZIndex ) {
				cz = Number( arrobjFloatingWindows[i].getZIndex() );
				if( cz > highZ ) highZ = cz;
				if( cz < lowZ ) lowZ = cz;
			}
		}

		window.status = highZ;

		if(highZ > myZ) {
			setZIndex(highZ+1);
		}
	}

	startDrag = function () {
		document.onmousemove	= captureMousePosition;
		dragging 				= true;
	}

	stopDrag = function () {
		pos = getPosition();

		if( pos.x > document.body.clientWidth || pos.y > document.body.clientHeight ) {
			centerWindow();
		}

		dragging	= false;
		xOffset 	= undefined;
		yOffset 	= undefined;

		document.onmousemove = function() {};
	}

	position_window = function(x,y) {
		with( windowFrame ) {
			style.top	= y + 'px';
			style.left	= x + 'px';
		}

		with( windowFrameShadow ) {
			style.top	= ( y + 3 ) + 'px';
			style.left	= ( x + 3 ) + 'px';
		}
	}

	getPosition = function () {
		pos		= new Array();
		pos.x	= windowFrame.style.left.substring( 0, windowFrame.style.left.indexOf( 'px' ));
		pos.y	= windowFrame.style.top.substring( 0, windowFrame.style.top.indexOf( 'px' ));

		return(pos);
	}

	setZIndex = function ( num ) {
		windowFrame.style.zIndex		= num + 1;
		windowFrameShadow.style.zIndex	= num;
	}

	getZIndex = function () {
		return(windowFrame.style.zIndex);
	}

	captureMousePosition = function ( e ) {
		try{ document.selection.empty(); }catch( e ) {}

		if( window.getSelection ) {
			var selection = window.getSelection();
			 if( selection.removeAllRanges ) { selection.removeAllRanges(); }
		}

		if ( !e ) var e = window.event;

		if ( navigator.appName !="Microsoft Internet Explorer" ) {
			xMousePos		= e.pageX;
			yMousePos		= e.pageY;
			xMousePosMax 	= window.innerWidth + window.pageXOffset;
			yMousePosMax 	= window.innerHeight + window.pageYOffset;
		} else {
			xMousePos 		= window.event.x + document.body.scrollLeft;
			yMousePos 		= window.event.y + document.body.scrollTop;
			xMousePosMax 	= document.body.clientWidth + document.body.scrollLeft;
			yMousePosMax 	= document.body.clientHeight + document.body.scrollTop;
		}

		if( dragging == true ) {

			if( xOffset == undefined || yOffset == undefined ) {
				pos 		= getPosition();
				xOffset 	= xMousePos - pos.x;
				yOffset 	= yMousePos - pos.y;
			}

			position_window( xMousePos - xOffset, yMousePos - yOffset );
		}
	}

	fixSize = function () {
		var divs = windowFrame.getElementsByTagName( 'div' );

		for( var i=0; i < divs.length; ++i ) {
			if( divs[i].className == 'contents' ) {
				var cDiv = divs[i];
			}

			if( divs[i].className == 'buttonBar' ) {
				var bDiv = divs[i];
			}
		}

		if( cDiv && bDiv ) {
			cDiv.style.height	= ( height - titleBar.clientHeight - bDiv.clientHeight - 10 ) + 'px';
		}
	}

	show = function () {
		createWindowFrame();
	}

	kill = function () {
		windowFrame.parentNode.removeChild( windowFrame );
		windowFrameShadow.parentNode.removeChild( windowFrameShadow );
	}

	this.getZIndex	= getZIndex;
	this.setZIndex	= setZIndex;
	this.stopDrag 	= stopDrag;
	this.show 		= show;
	this.kill 		= kill;
}