var window_prefix = '_pane_';
var is_ie = document.all ? true : false;
var z_index_offset = 2;

function Manager( container ) {
	this.container = container;
	this.container_offset_x = 0;
	this.container_offset_y = 0;
	this.moving_elt = null;
	this.num_windows = 0;
	this.offset_x = 0;
	this.offset_y = 0;
	this.window_x = is_ie ? document.body.offsetWidth : window.innerWidth;
	this.window_y = is_ie ? document.body.offsetHeight : window.innerHeight;
	
	Manager.prototype.makeCloseWindow = function( index, action ) {
		var f_this = this;

		return function() {
			f_this.container.removeChild(document.getElementById(window_prefix+index));

			if ( action ) {
				action();
			}
		};
	};
	
	Manager.prototype.makeEndMove = function() {
		var f_this = this;
		
		return function() {
			f_this.moving_elt = null;
		};
	};
	
	Manager.prototype.makeBeginMove = function() {
		var n_this = this;
		return function( event ) {
			if ( !event ) {
				event = window.event;
			}

			if ( event.target ) {
				n_this.moving_elt = event.target;
			} else if ( event.srcElement ) {
				n_this.moving_elt = event.srcElement;
			} else {
				n_this.moving_elt = null;
				return false;
			}
			
			while ( n_this.moving_elt.parentNode &&
							n_this.moving_elt.id.substring(0,window_prefix.length) != window_prefix )
				n_this.moving_elt = n_this.moving_elt.parentNode;
			if ( !n_this.moving_elt.parentNode ) {
				n_this.moving_elt = null;
				return false;
			}

			n_this.container_offset_x = 0;
			n_this.container_offset_y = 0;
			var parent = n_this.moving_elt.offsetParent;
			while ( parent ) {
				n_this.container_offset_x += parseInt(parent.offsetLeft);
				n_this.container_offset_y += parseInt(parent.offsetTop);
				parent = parent.offsetParent;
			}
				
			var m_x, m_y;
			if ( !is_ie ) {
				m_x = event.pageX;
				m_y = event.pageY;
			} else {
				m_x = event.clientX + document.body.scrollLeft;
				m_y = event.clientY + document.body.scrollTop;
			}
			
			var elt_x = n_this.moving_elt.style.left == '' ? 0 : parseInt(n_this.moving_elt.style.left);
			var elt_y = n_this.moving_elt.style.top == '' ? 0 : parseInt(n_this.moving_elt.style.top);
			n_this.offset_x = elt_x-m_x;
			n_this.offset_y = elt_y-m_y;

			return false;
		};
	};
	
	Manager.prototype.makePerformMove = function() {
		var n_this = this;
		return function( event ) {
			if ( n_this.moving_elt == null ) return false;
			if ( document.selection ) document.selection.empty();
			if ( !event ) event = window.event;
			
			if ( !event ) return false;
		
			var m_x, m_y;
			if ( !is_ie ) {
				m_x = event.pageX;
				m_y = event.pageY;
			} else {
				m_x = event.clientX + document.body.scrollLeft;
				m_y = event.clientY + document.body.scrollTop;
			}
			
			if ( n_this.container_offset_x+n_this.offset_x+m_x < 0 )
				m_x = -n_this.offset_x-n_this.container_offset_x;
			if ( n_this.container_offset_y+n_this.offset_y+m_y-5 < 0 )
				m_y = -n_this.offset_y-n_this.container_offset_y;
		
			var elt_x = n_this.moving_elt.style.width == '' ? 0 : parseInt(n_this.moving_elt.style.width);
			var elt_y = n_this.moving_elt.style.height == '' ? 0 : parseInt(n_this.moving_elt.style.height);
	
			n_this.moving_elt.style.left = (n_this.offset_x+m_x)+'px';
			n_this.moving_elt.style.top = (n_this.offset_y+m_y)+'px';
			return false;
		};
	};
	
	Manager.prototype.makeMouseDown = function( index ) {
		var n_this = this;
		return function( event ) {
			var mouse_elt;
			if ( !event ) {
				event = window.event;
				mouse_elt = event.srcElement;
			} else mouse_elt = event.target;
			
			while ( mouse_elt.parentNode &&
							mouse_elt.id.substring(0,window_prefix.length) != window_prefix )
				mouse_elt = mouse_elt.parentNode;
			if ( !mouse_elt.parentNode ) return false;
			
			var z_index = parseInt(mouse_elt.style.zIndex);
			for ( var i = 0; i < n_this.num_windows; ++i ) {
				var tmp_elt = document.getElementById(window_prefix+i);
				
				if ( !tmp_elt ) continue;
				var tmp_z = parseInt(tmp_elt.style.zIndex);
				if ( tmp_z > z_index )
					tmp_elt.style.zIndex = tmp_z-1;
			}
			mouse_elt.style.zIndex = n_this.num_windows+z_index_offset;
		};
	};
	
	Manager.prototype.concatenateFunctions = function( fn_0, fn_1 ) {
		return function( param ) {
			if ( fn_0 != null ) fn_0(param);
			if ( fn_1 != null ) fn_1(param);
		};
	};
	
	Manager.prototype.openWindow = function( content, label, action ) {
		var container_x = this.container.clientWidth;
		var container_y = this.container.clientHeight;

		
		var d_main = document.createElement('div');
		d_main.setAttribute('id',window_prefix+this.num_windows);
		d_main.setAttribute('class','window');
		d_main.className = 'window';	
		d_main.style.zIndex = this.num_windows+z_index_offset;
		d_main.onmousedown = this.makeMouseDown(this.num_windows);
		
		var d_title = document.createElement('div');
		d_title.setAttribute('class','header');
		d_title.className = 'header';
		d_title.onmousedown = this.makeBeginMove();
		d_title.appendChild(document.createTextNode(label));
		
		var i_close = document.createElement('img');
		i_close.setAttribute('class','closeimg');
		i_close.className = 'closeimg';
		i_close.setAttribute('src','inc/images/close.window.png');
		i_close.onclick = this.makeCloseWindow(this.num_windows,action);
		i_close.onmouseover = this.swapImage('inc/images/close.window.over.png');
		i_close.onmouseout = this.swapImage('inc/images/close.window.png');
		
		d_title.appendChild(i_close);
		d_main.appendChild(d_title);
		d_main.appendChild(content);
		
		this.container.appendChild(d_main);
		d_main.style.left = parseInt((container_x-d_main.offsetWidth)/2)+'px';
		d_main.style.top = parseInt((container_y-d_main.offsetHeight)/2)+'px';
		return this.num_windows++;
	};
	
	Manager.prototype.swapImage = function( src ) {
		return function() {
			this.setAttribute('src',src);
		};
	};
		
	document.onmouseup = this.concatenateFunctions(document.onmouseup,this.makeEndMove());
	document.onmousemove = this.concatenateFunctions(document.onmousemove,this.makePerformMove());
}

