var $ = function(o){return document.getElementById(o);};
var ie = (/msie/i.test(navigator.userAgent));

if(ie){
	document.getElementsByTagName('html')[0].className = 'ie';
}
var _XWIN = function(){
	return {
		dr	: 1,
		width	: 200,
		height	: 200,
		resize	: false,
		title	: 'System info',
		easing	: Tween.elasticEaseOut,
		fscreen		: false, // Full Screen
		html	: 'Hello world', // content
		wins	: [],
		open	: function(e,params){
			if(this.win) return;
			var srcEl = $('myflash');
			if(!e) e=window.event;
			this.x = srcEl.ofssetLeft;//e.pageX || e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;
			this.y = srcEl.ofssetLeft; //e.pageY || e.clientY+document.body.scrollTop+document.documentElement.scrollTop;

			if (srcEl.nodeType == 3){ // defeat Safari bug
				srcEl = srcEl.parentNode;
			};
			this.srcEl = srcEl;
			this.x = srcEl.offsetLeft;
			this.y = srcEl.offsetTop;
			this.ew = srcEl.offsetWidth;
			this.eh = srcEl.offsetHeight;
			
			
			if(params){
				this.r = params.r||false;
				if(params.id){
					this.wid = params.id;
					if(!this.wins[params.id]){
						this.wins[params.id] = {};
					};
					this.w = this.wins[params.id].lw||(params.w||this.width);
					this.h = this.wins[params.id].lh||(params.h||this.height);
				}else{
					this.w = params.w||this.width;
					this.h = params.h||this.height;					
				};
				
				this.t = params.title||this.title;
				this.c = params.html||this.html;
				this.f = params.fs||this.fscreen;
				
			}else{
				this.w = this.width;
				this.h = this.height;
				this.r = this.resize;
				this.t = this.title;
				this.c = this.html;
				this.f = this.fscreen;
				this.wid = null;
			};
			this.win = $('win');
			this.win.innerHTML = this.create();

			$('xwin-title').innerHTML = this.t;
			this.drag();
			this.tween();
			
			
		}
		
		,keyControl: function(e){
			if(XWIN.closed){return;};
			var k=false;			
			if (window.event){k = window.event.keyCode;}
			else if(e){k = e.which;}
			if(k==27){XWIN.close();};			
		}
		,create : function(){
			var code = '<div id="xwin-proxy" class="proxy-drop"><table id="xwin-skin" cellpadding="0" cellspacing="0" border=0>'
							+'<tr>'
								+'<td class="left-top">&#160;</td>'
								+'<td class="top" id="xwin-bar">'
								+'<div id="xwin-title" class="drsMoveHandle"></div>'
								+'<div class="close" onclick="XWIN.close();"></div>';
							if(this.f){
								code += '<div id="xwin-expand" class="expand" onclick="XWIN.toggle();"></div>';
							};
							code +='</td>'
								+'<td class="right-top">&#160;</td>'
							+'</tr>'
							+'<tr>'
								+'<td class="left">&#160;</td>'
								+'<td height="100%">'
								+'<div id="xwin-content-wrap" class="content-wrap">'
									+'<div id="xwin-content" class="content">'+this.c+'</div>'
								+'</div></td>'
								+'<td class="right">&#160;</td>'
							+'</tr>'
							+'<tr>'
								+'<td class="left-bottom"></td>'
								+'<td class="bottom"></td>'
								+'<td class="right-bottom"></td>'
							+'</tr>'
						+'</table></div>';
			return code;
		}
		,drag : function(){
		
			xwdrag = new DragResize('xwdrag', { minWidth: 120, minHeight: 80, minLeft: 20, minTop: 20, maxLeft: (screen.width), maxTop: screen.height });
			
			xwdrag.isElement = function(elm){
				if(XWIN.fs) return false;
				if (elm.className && elm.className.indexOf('drsElement') > -1) return true;
			};
			xwdrag.isHandle = function(elm){
			 if (elm.className && elm.className.indexOf('drsMoveHandle') > -1) return true;
			};


			xwdrag.ondragfocus = function() {			
				if(!XWIN.r){
					var h = XWIN.win.getElementsByTagName('div');
					var n = h.length;
					for(var i=0;i<n;i++){
						if(h[i].className.indexOf('xwdrag-')!=-1){
							h[i].style.visibility = 'hidden';
						};
					};
				};
			};
									
			xwdrag.ondragstart = function(isResize) {XWIN.setProxy(true);/*$('xwin-content-wrap').className='content-drag';*/};
			xwdrag.ondragmove = function(isResize) { };
			xwdrag.ondragend = function(isResize) {XWIN.setResized();XWIN.setProxy();/*$('xwin-content-wrap').className='content-wrap';*/};
			xwdrag.ondragblur = function() { };
			xwdrag.apply(document);
		}
		
		,setResized : function(){
			if(this.wins[this.wid]){
				this.wins[this.wid].lw = this.win.offsetWidth;
				this.wins[this.wid].lh = this.win.offsetHeight;
				this.wins[this.wid].lx = this.win.offsetLeft;
				this.wins[this.wid].ly = this.win.offsetTop;
			};
		}
		,setProxy : function(d){
		
			$('xwin-proxy').className = d ? 'proxy-drag' : 'proxy-drop';
			$('xwin-skin').style.visibility = d ? 'hidden' : 'visible';
			//$('xwin-proxy').style.width = (this.win.offsetWidth)+'px';
			//$('xwin-proxy').style.height = (this.win.offsetHeight)+'px';
			//$('xwin-proxy').style.zIndex = d?10:0;
			
		}
		,tween : function(full){
			this.closed = false;
			this.win.style.display='block';
			var  w = this.w, h = this.h, d = document.documentElement;
			var ix = this.wins[this.wid]&&this.wins[this.wid].lx?this.wins[this.wid].lx:null;
			var iy = this.wins[this.wid]&&this.wins[this.wid].ly?this.wins[this.wid].ly:null;
			this.tox = full ? 0 : (ix||(d.clientWidth/2 - w/2));
			this.toy = full ? 0 : (iy||(d.clientHeight/2 - h/2)-100);
			//this.toy = this.toy<0?50:this.toy;
			//alert(document.documentElement.clientHeight)
			this.anime(this.x,this.tox,this.y,this.toy,0,w,0,h);
			
		}
		,anime : function(x,tox,y,toy,w,tow,h,toh){
			var e=this.easing, dr=this.dr;
			var tl = new Tween(this.win.style,'left',e,x,tox,dr,'px');
			tl.start();
			var tt = new Tween(this.win.style,'top',e,y,toy,dr,'px').start();
			var tw =  new Tween(this.win.style,'width',e,w,tow,dr,'px').start();
			var th =  new Tween(this.win.style,'height',e,h,toh,dr,'px').start();			
			if(this.closed){
				tl.onMotionFinished = function(){
					XWIN.win.style.display='none';
					XWIN.win = null;
				};
			}else{
				document.onkeydown = this.keyControl;
			};
		
		}
		,close : function(){
			this.closed = true;
			this.fs = false;
			this.setProxy(true);
			try{xwdrag.deselect(true);}catch(e){};
			var minW = ie?110:110, minH = ie?50:30;
			var x=parseInt(this.win.style.left),y=parseInt(this.win.style.top),tow = this.ew, toh = this.eh;
			if(tow<minW) tow=minW;
			if(toh<minH) toh=minH;			
			this.anime(x,this.x,y,this.y,this.w,tow,this.h,toh);
			var ot = new OpacityTween($('xwin-proxy'),Tween.regularEaseIn, 100, 0, this.dr/2);
			//ot.onMotionFinished = function(){};
			ot.start();
		}
		,toggle : function(){
			var d = document.documentElement;
			if(!this.fs){
				document.body.style.overflow = 'hidden';
				this.anime(this.win.offsetLeft,0,this.win.offsetTop,0,this.win.offsetWidth,d.clientWidth,this.win.offsetHeight,d.clientHeight-(ie?40:0));
				$('xwin-expand').className = 'collapse';
				this.fs = true;
			}else{
				document.body.style.overflow = 'auto';
				this.anime(0,this.tox,0,this.toy,this.win.offsetWidth,this.w,this.win.offsetHeight,this.h);
				$('xwin-expand').className = 'expand';
				this.fs = false;
			};
		}

		
	};
};

var XWIN = new _XWIN();
var cnt = 0;
function debug(str){
	$('dbg').innerHTML += cnt+' > '+str+'<br>';
	cnt++;
}