window['windows']=Array();
window['lockedWindows']=Array();
function $window(){}
$extend($window,{
	element:null,
	minW: 150,
	minH: 40,
	width: 'auto',
	height: 'auto',
	left: 0,
	top: 0,
	title: new String("RDA Comunicações"),
	zIndex: new Number(100),
	target: new String(),
	bg: new String(),
	lock: new Boolean(false),
	unique: new Boolean(false),
	closeable: new Boolean(false),
	windowID: 0,
	createWindow: function (url,variables,params) {
	if(params.title){ this.title = params.title;}
	var _unique = false;//Array().exists(window['windows'],this.title);
	for(var i=0;i<window['windows'].length;i++){
		if(window['windows'][i].title==this.title) {_unique=true;break;}
	}	
	if(params.unique) { this.unique = params.unique; }
	if(this.unique == true && _unique == true) return false;
//	window['windows'].push(this.title);
	if(params.w!=null){this.width = (/([%]+|px)/.test(params.w))?params.w:(params.w>this.minW?params.w:this.minW);}
	if(params.h!=null){this.height = (/([%]+|px)/.test(params.h))?params.h:(params.h>this.minH?params.h:this.minH);}	
	if(params.target!="" && params.target!="undefined"){ this.target = params.target; }
	if(params.z){ this.zIndex = params.z;}
	if(params.l!=null){this.left=params.l;}
	if(params.t!=null){this.top=params.t;}
	if(params.lock) { this.lock = params.lock; }
	if(params.bg) { this.bg = params.bg; }
	if(params.cantClose) { this.closeable = params.cantClose; }
	if(params.method) { method = params.method; }

	this.windowID = Math.floor(Math.random()*9999);
	
	var lockFrame = $('window_lockWindows');
	if(!lockFrame) checkLockFrame();
	
	this.element=document.createElement("DIV");
	var theWindow = this.element;
	theWindow.id="window"+this.windowID;
	theWindow.className = "window_element";
	theWindow.style.height = this.height+(typeof this.height!='string'?'px':'');
	theWindow.style.width = this.width+(typeof this.width!='string'?'px':'');
	
	var windowTitle = document.createElement("DIV");	
	windowTitle.innerHTML = '<div class="window_title_label"><span ondblclick="Win().minimize(this,\''+theWindow.id+'\',true);return false;">'+this.title+'</span></div><div class="window_systemmenu">'+
	'<a href="javascript:;" class="button_mini" onclick="Win().minimize(this,\''+theWindow.id+'\');return false;"> &ndash; </a>'+
	'<a href="javascript:;" class="button_close" onclick="Win().destroyWindow(\''+theWindow.id+'\');return false;"> &times; </a></div>';
	windowTitle.className = "window_title";
	windowTitle.onmousedown = function(){Drag.drag((theWindow.id).toString()); }
	theWindow.appendChild(windowTitle);
	windowTitle=null;
	
	//WINDOW CLIENT SPACE
	var windowClient = document.createElement("DIV");
	windowClient.innerHTML = "&nbsp;";
	windowClient.style.height = (typeof this.height =='string'?this.height:(this.height - 32)+"px");
	windowClient.className = "window_client";
	if(this.bg!="" && this.bg!="undefined") { windowClient.style.backgroundColor=this.bg; }
	windowClient.id = "winClient"+this.windowID;
	theWindow.appendChild(windowClient);
	windowClient = null;
	
	var ns = (navigator.appName.indexOf("Netscape") != -1);
	if(this.left==0) {
		if(typeof this.width=='string' && (/[%]/).test(this.width)) {
			var vl=this.width.replace(/[%]/,'');
			vl=100-vl;
			this.left = (vl/2)+'%';
		} else {
			this.left = ns ? innerWidth : (document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth);
			this.left = (this.left-this.width)/2;
		}
	}
	if(this.top==0) {
		if(typeof this.height=='string' && (/[%]/).test(this.height)) {
			var vt=this.height.replace(/[%]/,'');
			vt=100-vt;
			this.top = (vt/2)+'%';
		} else {
			this.top = ns ? innerHeight : (document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight);
			this.top = (this.top - this.height)/2;
		}
	}
	theWindow.style.left = (typeof this.left=='string'?this.left:Math.floor(this.left)+'px');
	theWindow.style.top = (typeof this.top=='string'?this.top:Math.floor(this.top)+'px');
	this.left = 0;
	this.top = 0;
	if(this.target == "" || this.target == "undefined" || this.target == null) {
		document.body.appendChild(theWindow);
	} else {
		var target=$(this.target);
		try {
			target.innerHTML='';
			target.appendChild(theWindow);
		} catch(e) {
			document.body.appendChild(theWindow);
		}
	}
	
	var realWidth = 0, realHeight=0, realLeft=0, realTop=0;
	var clientH=ns ? innerHeight : (document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight),
	clientW= ns ? innerWidth : (document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth);
	if(/[%]+/.test(this.width)) {
		realWidth = clientW;
		realWidth *= parseInt(this.width.replace(/[%]/,''))/100;
	} else if(/px/.test(this.width)) {
		realWidth = parseInt(this.width.replace(/px/,''));
	} else realWidth = this.width;
	if(/[%]+/.test(this.height)) {
		realHeight = clientH;
		realHeight *= parseInt(this.height.replace(/[%]/,''))/100;
	} else if(/px/.test(this.width)) {
		realHeight = parseInt(this.height.replace(/px/,''));
	} else realHeight = this.height;
//	alert(realWidth+','+realHeight);
	Fadder.fadeIn(theWindow);
	var v = Object.copy(Sizer);
/*	var w = '';
	for (var i in v) w+=i+'\n';
	alert(w);*/
//	Move().setPos(theWindow,Vector.vx(Math.floor(clientW/2),Math.floor(clientH/2)));
//	Move().moveTo(client
//	v.shrinkFx(theWindow,0.2,Vector(realWidth,realHeight)); 
	//Vector.vx(parseInt(realWidth*0.8),parseInt(realHeight*0.8))
	if(url!='') {AjaxGet(url,variables,'winClient'+this.windowID);}
	
	if(this.zIndex>0) {
		theWindow.style.zIndex=this.zIndex;
	} else {
		theWindow.style.zIndex=getMaxIndex()+1;
	}
	if(this.lock==true) {
		window['lockedWindows'].push(theWindow.id);
		if(lockFrame!=null) {
			lockFrame.style.display='block';
			lockFrame.style.zIndex = getMaxIndex();
		}
		theWindow.style.zIndex=getMaxIndex()+1;
	} else {
		window['lockedWindows'].popValue(theWindow.id);
	}
	window['windows'].push(this);
	return theWindow;
},
restore: function (object,windowID) {
	var w = $(windowID);
	w.childNodes[1].style.display = 'block';
	var h='auto';
	for(var i=0;i<window['windows'].length;i++) {
		if(window['windows'][i].windowID==windowID.replace(/window/,'')){h=window['windows'][i].height; break;}
	}	
	object.onclick = function () {Win().minimize(object,windowID);}
	w.style.height = h;
},
minimize: function (object,windowID,double) {
	var w = $(windowID);
	w.childNodes[1].style.display = 'none';
	if(double)
	  object.ondblclick = function () {Win().restore(object,windowID);}
	else
	  object.onclick = function () {Win().restore(object,windowID);}
	w.style.height = 'auto';
},
destroy: function (windowID) {
	var obj = null;
	for(var i=0;i<window['windows'].length;i++) {
		if(window['windows'][i].id==windowID) {obj=window['windows'][i];break;}
	}
	$(windowID).parentNode.removeChild($(windowID));
	window['lockedWindows'].popValue(windowID);
	for(var i=0;i<window['windows'].length;i++) {
		if(window['windows'][i].windowID==(windowID.replace(/window/,''))) window['windows'].popValue(window['windows'][i]);//[i]=null;
	}
//	window['windows'].clean();
},
destroyWindow: function (object) {
	object=$(object);
	while(object.className != "window_element" && object.nodeName != "BODY" && object.parentNode != document)
	  object = object.parentNode;
	if(object) 
	  this.destroy(object.id);
}
});

function Win(){return $window}
function checkLockFrame() {	
	var lockFrame = null,
	ie=(document.all!=null);
	var de=null;
	if(document.documentElement) var de=document.documentElement;
	try {lockFrame = $('window_lockWindows');} catch(e){}
	if(lockFrame==null) {
		lockFrame = document.createElement("DIV");
		lockFrame.className = 'window_lock_windows';
		lockFrame.id='window_lockWindows';
		lockFrame.style.zIndex=getMaxIndex();
		lockFrame.style.height = (navigator.appName.indexOf("Netscape") != -1) ? innerHeight : (document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight) + 'px';
		if(document.body) document.body.appendChild(lockFrame);
	}
	if(window['lockedWindows'].length>0 && lockFrame!=null) {
		if(lockFrame.style.display!='block') {
			lockFrame.style.display='block';
			lockFrame.style.height = ( (de!=null) ? (de.clientHeight?de.clientHeight:de.scrollHeight) : (ie && document.body.innerHeight ? document.body.innerHeight : document.body.offsetHeight)) + 'px';
		}
	}
	else if(window['lockedWindows'].length==0 && lockFrame!=null) lockFrame.style.display='none';
}
function destroyWindow (node) {
	return Win.destroyWindow(node);
}
function getWindowClient(object) {
	while(object.parentNode.nodeName !="BODY" && object.className != "window_client") {
		object = object.parentNode;
	}
	if(object.className == "window_client") { return object.id; } else { return null; }
}
function getMaxIndex() {
	var list=document.getElementsByTagName('div');
	var maxIndex=0;
	for(var i=0;i<list.length;i++) {
		if(RegExp('/window([0-9]+)/','i').test(list[i].id) && list[i].style.zIndex>0 && maxIndex<list[i].style.zIndex) maxIndex=list[i].style.zIndex;
	}
	return maxIndex;
}	

setInterval('checkLockFrame();',300);
