/**
 * @author Szymon
 */
function newWnd(){
	var wndConf = arguments[0];
	var win = {	
		readConf:function(cName){
			
			return (cookies.getCookie(cName))?eval("("+(cookies.getCookie(cName).replace(/\"/g,"'")+")")):{};
		},
		
		writeConf:function(obj,cName){
			cookies.setCookie(cName,toJsonString(obj),360);
		},
		
		delConf:function(cName){
			cookies.setCookie(cName,toJsonString({none:null}),0);
		},
		create:function(){	
			this.wndConf = arguments[0];
			var hash = win.readConf(this.wndConf.prefix + "Wnd"+'_conf');
			if(!cookies.getCookie(this.wndConf.prefix + "Wnd"+'_conf')){cookies.setCookie(this.wndConf.prefix + "Wnd"+'_conf',toJsonString({}),360); }
			if (document.getElementById(this.wndConf.prefix + "Wnd") == null) {
				this.wnd = document.createElement('div');
				this.contW = document.createElement('div');
				var icoPane = document.createElement('div');
				var topW = document.createElement('div');
				var headW = document.createElement('div');
				var sbiw = document.createElement('div');
				var xw = document.createElement('div');
				this.wnd.appendChild(topW);
				this.wnd.id = this.wndConf.prefix + "Wnd";
				headW.id = this.wndConf.prefix + "HeadWnd";
				this.contW.id = this.wndConf.prefix + "ContentWnd";
				this.wnd.className = (this.wndConf.wndC || "wndClass") + " radiusWnd";
				this.wnd.style.top = (cc.defined(hash.position))?hash.position.y+'px':'20%';
				this.wnd.style.left = (cc.defined(hash.position))?hash.position.x+'px':'35%';
				headW.className = this.wndConf.wndHC || "wndHClass";
				this.contW.className = "wndContClass "+this.wndConf.contWC || "wndContClass";
				topW.appendChild(headW);
				topW.id = "wndTop";
				topW.className = this.wndConf.topWC || "wndTop";
				topW.onmouseover = function(){
					dragCore.init(this);
					
				}
				topW.ondblclick = function(){
					win.SB(sbiw);	
				}
				
				topW.onmousemove = function(){
					var prnt = this.parentNode;
					var hash = win.readConf(prnt.id+'_conf');
						hash.position = {};
						hash.position.x = prnt.offsetLeft;
						hash.position.y = prnt.offsetTop;
					win.writeConf(hash,prnt.id+'_conf');
				}
				
				topW.appendChild(icoPane);
				icoPane.id = this.wndConf.prefix + "IcoPaneWnd";
				icoPane.className = "wndIcoPane";
				icoPane.appendChild(xw);
				xw.id = this.wndConf.prefix + "xWnd";
				xw.className = "xw";
				xw.onclick = function(){
					win.X(this);
				}
				icoPane.appendChild(sbiw);
				sbiw.id = this.wndConf.prefix + "Sbiw";
				sbiw.className = "sbiw";
				sbiw.onclick = function(){
					win.SB(this);
				}
				this.wnd.appendChild(this.contW);
				
				
				if (!codexd.defined(this.wndConf.wndParent)) {
					document.body.appendChild(this.wnd);
				}
				else {
					codexd.$cc(this.wndConf.wndParent).appendChild(this.wnd);
				}
		
				if (codexd.defined(this.wndConf.expand)) {
					this.doExpand(this.wndConf.expand);
				}
				if (codexd.defined(hash.expand)) {
					this.doExpand(hash.expand);
				}
			}
		},
		
		doExpand:function(){
			if (arguments[0]) {
				//win.contW.style.height = "auto";
				  win.contW.style.display = "";
				codexd.$cc(this.wndConf.prefix + "Sbiw").style.backgroundImage = 'url(./exec/images/icons/window/small.gif)';
			}else{
				//win.contW.style.height = 0 + "px";
				  win.contW.style.display = "none";
				codexd.$cc(this.wndConf.prefix + "Sbiw").style.backgroundImage = 'url(./exec/images/icons/window/big.gif)';
			}
		},
		
		SB: function(){
			var ico = arguments[0];
			var hash = win.readConf(this.wndConf.prefix + "Wnd"+'_conf');
			if (this.changeIco(ico)) {//small
				//this.contW.style.height = 0 + "px";
				this.contW.style.display = "none";
				hash.expand = false;
			}
			else {//Big
				//this.contW.style.height = "auto";
				this.contW.style.display = "";
				hash.expand = true;
			}
			win.writeConf(hash,this.wnd.id+'_conf');
		},
		
		X: function(){
			if(!arguments[0]){
				if (confirm(w18.alerts.close)) {
					if (!codexd.empty(this.wndConf.wndParent) && !codexd.defined(this.wndConf.wndParent)) {
						win.delConf(this.wnd.id + '_conf');
						document.body.removeChild(this.wnd);					
					}else {
						codexd.$cc(this.wndConf.wndParent).removeChild(this.wnd);
					}
				}
			}else{
				if (!codexd.empty(this.wndConf.wndParent) && !codexd.defined(this.wndConf.wndParent)) {
					win.delConf(this.wnd.id + '_conf');
					document.body.removeChild(this.wnd);					
				}else {
					codexd.$cc(this.wndConf.wndParent).removeChild(this.wnd);
				}
			}
		},
		changeIco: function(){
			var stl = codexd.getStyle(arguments[0]);
			var url = codexd.cleanupUrl(stl.backgroundImage);
			url = (url != null) ? url : codexd.cleanupUrl(arguments[0].style.backgroundImage);
			if(url == null) url = './exec/images/icons/window/small.gif';
			arguments[0].style.backgroundImage = (url.indexOf('small') != -1) ? 'url(' + url.replace('small', 'big') + ')' : 'url(' + url.replace('big', 'small') + ')';
			return (url.indexOf('small') != -1);
		}
	};
	this.win = win;
	this.win.create(wndConf);
}