/* 
	TODO:
		- Resize windows on browser resize (currently commented out)
*/

var WindowSet = {
	_minZIndex : 0,
	_taskbar : 0,
	_constrainWindows : false,
	_position : 'absolute',
	_displayWindow : window,
	_windows : new Array()
};

WindowSet.setup = function(options)
{
	this._ie6 = (!window.innerWidth) && navigator.userAgent.match(/MSIE 6\.0/);
	
	if(!!options.minZIndex && !isNaN(parseInt(options.minZIndex)))
		this._minZIndex = options.minZIndex;
	
	if(!!options.taskbar && !isNaN(parseInt(options.taskbar)))
		this._taskbar = parseInt(options.taskbar);
	
	if(options.constrainWindows==true)
		this._constrainWindows=true;
	
	if (options.position=='fixed' && !this._ie6)
		this._position='fixed';
	
	if(!!options.displayWindow)
		this._displayWindow = options.displayWindow;
	
	this._setAvailWidthHeight();
	
	var modalOverlay = document.createElement('div');
	modalOverlay.className = 'windowSetModalOverlay';
	modalOverlay.style.display = 'none';
	modalOverlay.onmousedown = function() {return false;};
	modalOverlay.onclick = function() {return false;};
	this._modalOverlay = this._displayWindow.document.body.appendChild(modalOverlay);
	
	var iframeOverlay = document.createElement('div');
	iframeOverlay.className = 'windowSetIframeOverlay';
	iframeOverlay.style.display = 'none';
	this._iframeOverlay = this._displayWindow.document.body.appendChild(iframeOverlay);
}

WindowSet._setAvailWidthHeight = function()
{
	var w = 0, h = 0;
	if(typeof(this._displayWindow.innerWidth) == 'number') //Non-IE
	{
		w = this._displayWindow.innerWidth;
		h = this._displayWindow.innerHeight;
	}
	else if(this._displayWindow.document.documentElement && (this._displayWindow.document.documentElement.clientWidth || this._displayWindow.document.documentElement.clientHeight)) //IE 6+ in strict mode
	{
		w = this._displayWindow.document.documentElement.clientWidth;
		h = this._displayWindow.document.documentElement.clientHeight;
	}
	else if(this._displayWindow.document.body && (this._displayWindow.document.body.clientWidth || this._displayWindow.document.body.clientHeight)) //IE 4 compatible
	{
		w = this._displayWindow.document.body.clientWidth;
		h = this._displayWindow.document.body.clientHeight;
	}
	this._availWidth = w;
	this._availHeight = h;
}

WindowSet.loadTheme = function(theme)
{
	var headTag = this._displayWindow.document.getElementsByTagName('head')[0];         
	var linkNode = document.createElement('link');
	linkNode.type = 'text/css';
	linkNode.rel = 'stylesheet';
	linkNode.href = 'WindowSet/themes/'+theme+'/theme.css';
	for(var i in headTag.childNodes)
	{
		if(!!headTag.childNodes[i].href && headTag.childNodes[i].href.match(/\/themes\/[^\/]+\/theme\.css$/))
		{
			headTag.replaceChild(linkNode, headTag.childNodes[i]);
			return;
		}
	}
	headTag.appendChild(linkNode);
}

WindowSet.Win = function()
{
	this.ready = false;
	this._minimized = false;
	this._maximized = false;
	this._visible = true;
	this._modal = false;
	this.resizable = true;
	this.movable = true;
	
	this.onresize = function() {;};
	this.onmove = function() {;};
	this.onclose = function() {;};
}

/*
	Object options
	{
		name: string (required),
		title: string,
		content: string,
		contentUrl: string (url),
		contentFrame: string (url),
		width: string ('250' or '50%'),
		height: string ('250' or '50%'),
		fitWidth: boolean,
		fitHeight: boolean,
		top: string ('250' or '50%'),
		left: string ('250' or '50%'),
		modal: boolean,
		resizable: boolean,
		moveable: boolean,
		minimizeButton: boolean,
		maximizeButton: boolean,
		closeButton: boolean,
		onload: function,
		onresize: function,
		onmove: function,
		onclose: function,
		
	}
*/
WindowSet.addWindow = function(options)
{
	if(!options.name)
		return false;
	var name=options.name;
	if(!!this[name]) 
	{
		if(this[name]._minimized)
			this[name].restore();
		this[name].focus();
		return false;
	}
	
	var n = this._windows.push(new this.Win());
	this[name] = this._windows[n-1];
	this[name].name=options.name;
	
	var div = document.createElement('div');
	div.className = 'winContainer';
	div.style.position = this._position;
	
	if (options.visible===false)
	{
		this[name]._visible = false;
		div.style.display = 'none';
	}
	
	if (options.modal===true)
		this[name]._modal=true;
	
	if (options.resizable===false)
		this[name].resizable=false;
	
	if (options.movable===false)
		this[name].movable=false;
	
	if (!!options.onresize)
		this[name].onresize=options.onresize;
	
	if (!!options.onmove)
		this[name].onmove=options.onmove;
	
	if (!!options.onclose)
		this[name].onclose=options.onclose;
	
	if (!!options.width && !isNaN(parseInt(options.width)))
	{
		if ((parseInt(options.width)+'%') == options.width)
		{
			var scale = parseInt(options.width)/100;
			div.style.width = parseInt(scale*this._availWidth)+'px';
		}
		else
			div.style.width = parseInt(options.width)+'px';
	}
	else
		div.style.width = '250px';
	if (!!options.height && !isNaN(parseInt(options.height)))
	{
		if ((parseInt(options.height)+'%') == options.height)
		{
			var scale = parseInt(options.height)/100;
			div.style.height = parseInt(scale*(this._availHeight-this._taskbar))+'px';
		}
		else
			div.style.height = parseInt(options.height)+'px';
	}
	else
		div.style.height = '250px';
		
	if (!!options.top && !isNaN(parseInt(options.top)))
	{
		if ((parseInt(options.top)+'%') == options.top)
		{
			var scale = parseInt(options.top)/100;
			div.style.top = parseInt((scale*(this._availHeight-this._taskbar))-(parseInt(div.style.height)*scale))+'px';
		}
		else
			div.style.top = parseInt(options.top)+'px';
	}
	else
		div.style.top = '0px';
	if (!!options.left && !isNaN(parseInt(options.left)))
	{
		if ((parseInt(options.left)+'%') == options.left)
		{
			var scale = parseInt(options.left)/100;
			div.style.left = parseInt((scale*this._availWidth)-(parseInt(div.style.width)*scale))+'px';
		}
		else
			div.style.left = parseInt(options.left)+'px';
	}
	else
		div.style.left = '0px';
	
	div.style.zIndex = this._minZIndex+this._windows.length-1;
	
	div.innerHTML = '<div class="winTop" onmousedown="WindowSet.'+ name +'.focus();">'+
						'<div class="winButtonBar" onmousedown="return false;">'+
							'<div class="minimize" onclick="WindowSet.'+ name +'.minimize(event);"></div>'+
							'<div class="maximize" onclick="WindowSet.'+ name +'.maximize(event);"></div>'+
							'<div class="close" onclick="WindowSet.'+ name +'.close(event);"></div>'+
						'</div>'+
						'<div class="winTitleBar" onmousedown="return WindowSet.'+ name +'._dragMoveStart(event);" onclick="WindowSet.'+ name +'.restore();" ondblclick="return (WindowSet.'+ name +'._maximizeButton.style.display==\'\' && WindowSet.'+ name +'.maximize());">'+ (!!options.title ? options.title : '') +'</div>'+
					'</div>'+
					'<div class="winMiddle" onmousedown="WindowSet.'+ name +'.focus();">'+
						'<div class="winMiddleLeft" onmousedown="return WindowSet.'+ name +'._dragResizeStart(event, true, true, true, false);"></div>'+
						'<div class="winMiddleCenter">'+ (!!options.content ? options.content : '') +'</div>'+
						'<div class="winMiddleRight" onmousedown="return WindowSet.'+ name +'._dragResizeStart(event, true, false, true, true);"></div>'+
					'</div>'+
					'<div class="winBottom" onmousedown="WindowSet.'+ name +'.focus();">'+
						'<div class="winBottomLeft" onmousedown="return WindowSet.'+ name +'._dragResizeStart(event, true, true, false, false);"></div>'+
						'<div class="winBottomCenter" onmousedown="return WindowSet.'+ name +'._dragResizeStart(event, true, true, false, true);"></div>'+
						'<div class="winBottomRight" onmousedown="return WindowSet.'+ name +'._dragResizeStart(event, true, false, false, true);"></div>'+
					'</div>';
	this[name].div = this._displayWindow.document.body.appendChild(div);
	
	this[name]._top = this[name].div.childNodes[0];
	this[name]._buttonBar = this[name]._top.childNodes[0];
	this[name]._minimizeButton = this[name]._buttonBar.childNodes[0];
	this[name]._maximizeButton = this[name]._buttonBar.childNodes[1];
	this[name]._closeButton = this[name]._buttonBar.childNodes[2];
	this[name]._titleBar = this[name]._top.childNodes[1];
	
	this[name]._middle = this[name].div.childNodes[1];
	this[name]._middleLeft = this[name]._middle.childNodes[0];
	this[name]._middleCenter = this[name]._middle.childNodes[1];
	this[name]._middleRight = this[name]._middle.childNodes[2];
	
	this[name]._bottom = this[name].div.childNodes[2];
	this[name]._bottomLeft = this[name]._bottom.childNodes[0];
	this[name]._bottomCenter = this[name]._bottom.childNodes[1];
	this[name]._bottomRight = this[name]._bottom.childNodes[2];
	
	this[name].content = this[name]._middleCenter;
	
	this[name].content.style.width = (parseInt(this[name].div.style.width)-14)+'px';
	this[name]._bottomCenter.style.width = (parseInt(this[name].div.style.width)-14)+'px';
	this[name]._middle.style.height = (parseInt(this[name].div.style.height)-32)+'px';
	
	if(this[name]._modal)
	{
		this[name]._minimizeButton.style.display = 'none';
		this[name]._maximizeButton.style.display = 'none';
		this[name]._closeButton.style.display = 'none';
	}
	
	if(options.minimizeButton===true)
		this[name]._minimizeButton.style.display = '';
	else if(options.minimizeButton===false)
		this[name]._minimizeButton.style.display = 'none';
	if(options.maximizeButton===true)
		this[name]._maximizeButton.style.display = '';
	else if(options.maximizeButton===false)
		this[name]._maximizeButton.style.display = 'none';
	if(options.closeButton===true)
		this[name]._closeButton.style.display = '';
	else if(options.closeButton===false)
		this[name]._closeButton.style.display = 'none';
	
	if(!!options.contentFrame)
	{
		var iframe = document.createElement('iframe');
		iframe.style.width = '100%';
		iframe.style.height = '100%';
		iframe.style.border = 'none';
		iframe.scrolling = 'no';
		iframe.border = '0';
		iframe.frameBorder = '0';
		iframe.src = options.contentFrame;
		iframe.onload = function()
		{
			/*
			TODO: Fix this
			
			var width=null, height=null;
			
			if(!!options.width && options.width=='auto')
				width=parseInt(WindowSet[name].iframe.contentDocument.body.scrollWidth);
			if(!!options.height && options.height=='auto')
				height=parseInt(WindowSet[name].iframe.contentDocument.body.scrollHeight);
			WindowSet[name].resize(width+14, height+32);*/
			
			if(!!options.onload)
				options.onload;
			WindowSet[name].ready = true;
		}
		//iframe.onreadystatechange=iframe.onload;
		
		while(this[name].content.firstChild) 
			this[name].content.removeChild(this[name].content.firstChild);
		this[name].iframe=this[name].content.appendChild(iframe);
	}
	else if(typeof(Ajax)!='undefined' && !!options.contentUrl)
	{
		var ajax=new Ajax(true, 'txt');
		ajax.get(options.contentUrl, function()
		{
			if(ajax.ready() && ajax.status()==200)
			{
				var topPct=false, leftPct=false;
				
				WindowSet[name].content.innerHTML=ajax.response();
				
				if(options.fitWidth===true || options.fitHeight===true)
				{
					if(!!options.top && parseInt(options.top)+'%'==options.top)
						topPct=options.top;
					if(!!options.left && parseInt(options.left)+'%'==options.left)
						leftPct=options.left;
					WindowSet[name].fitContent(!!options.fitWidth, !!options.fitHeight, topPct, leftPct);
				}
				
				if(!!options.onload)
					options.onload();
				WindowSet[name].ready = true;
			}
		});
	}
	else
	{
		var topPct=false, leftPct=false;
		
		if(options.fitWidth===true || options.fitHeight===true)
		{
			if(!!options.top && parseInt(options.top)+'%'==options.top)
				topPct=options.top;
			if(!!options.left && parseInt(options.left)+'%'==options.left)
				leftPct=options.left;
			this[name].fitContent(!!options.fitWidth, !!options.fitHeight, topPct, leftPct);
		}
		
		if(!!options.onload)
			options.onload();
		this[name].ready = true;
	}
	
	this[name].focus();
}

WindowSet.Win.prototype.resize = function(width, height, forceConstrainWindow)
{
	var result = {
		width : false,
		height : false
	};
	var difference = {
		width : 0,
		height : 0
	};
	if(forceConstrainWindow !== true && forceConstrainWindow !== false)
		constrainWindow = WindowSet._constrainWindows;
	else
		constrainWindow = forceConstrainWindow;
	
	if(width !== false)
	{
		if(width === true)
			result.width = WindowSet._availWidth;
		else if ((parseInt(width)+'%') == width)
		{
			var scale = parseInt(width)/100;
			result.width = parseInt(scale*(WindowSet._availWidth))+'px';
		}
		else
		{
			result.width = width;
			if (forceConstrainWindow !== false)
			{
				if (result.width < 100)
				{
					difference.width = Math.abs(100 - result.width);
					result.width = 100;
				}
			}
			if(constrainWindow)
			{
				if(result.width+parseInt(this.div.style.left) > WindowSet._availWidth)
				{
					difference.width = (result.width+parseInt(this.div.style.left))-WindowSet._availWidth;
					result.width = WindowSet._availWidth-parseInt(this.div.style.left);
				}
			}
		}
	}
	if(height !== false)
	{
		if(height === true)
			result.height = WindowSet._availHeight-WindowSet._taskbar;
		else if ((parseInt(height)+'%') == height)
		{
			var scale = parseInt(height)/100;
			result.height = parseInt(scale*(WindowSet._availHeight-WindowSet._taskbar))+'px';
		}
		else
		{
			result.height = height;
			if (forceConstrainWindow !== false)
			{
				if (result.height < 56)
				{
					difference.height = Math.abs(56 - result.height);
					result.height = 56;
				}
			}
			if(constrainWindow)
			{
				if(result.height+parseInt(this.div.style.top) > WindowSet._availHeight-WindowSet._taskbar)
				{
					difference.height = (result.height+parseInt(this.div.style.top))-(WindowSet._availHeight-WindowSet._taskbar);
					result.height = (WindowSet._availHeight-WindowSet._taskbar)-parseInt(this.div.style.top);
				}
			}
		}
	}
	
	if(result.width !== false)
	{
		this.div.style.width = parseInt(result.width)+'px'
		this.content.style.width = (parseInt(result.width)-14 > 0 ? parseInt(result.width)-14 : 0)+'px';
		this._bottomCenter.style.width = (parseInt(result.width)-14 > 0 ? parseInt(result.width)-14 : 0)+'px';
	}
	if(result.height !== false)
	{
		this.div.style.height = parseInt(result.height)+'px'
		this._middle.style.height = (parseInt(result.height)-32 > 0 ? parseInt(result.height)-32 : 0)+'px';
	}
	
	if(!!this.onresize)
		this.onresize();
	
	return difference;
}

WindowSet.Win.prototype.move = function(top, left, forceConstrainWindow)
{
	var result = {
		top : false,
		left : false
	};
	var difference = {
		top : 0,
		left : 0
	};
	
	if(forceConstrainWindow !== true && forceConstrainWindow !== false)
		constrainWindow = WindowSet._constrainWindows;
	else
		constrainWindow = forceConstrainWindow;
	
	if(top !== false)
	{
		if ((parseInt(top)+'%') == top)
		{
			var scale = parseInt(top)/100;
			result.top = parseInt((scale*(WindowSet._availHeight-WindowSet._taskbar))-(parseInt(this.div.style.height)*scale))+'px';
		}
		else
		{
			result.top = top;
			if(result.top < 0)
			{
				difference.top = Math.abs(result.top);
				result.top = 0;
			}
			if(constrainWindow && result.top+parseInt(this.div.style.height) > WindowSet._availHeight-WindowSet._taskbar)
			{
				difference.top = (result.top+parseInt(this.div.style.height))-(WindowSet._availHeight-WindowSet._taskbar);
				result.top = (WindowSet._availHeight-WindowSet._taskbar)-parseInt(this.div.style.height);
			}
			else if(forceConstrainWindow !== false && result.top+25 > WindowSet._availHeight-WindowSet._taskbar)
			{
				difference.top = (result.top+25)-(WindowSet._availHeight-WindowSet._taskbar);
				result.top = (WindowSet._availHeight-WindowSet._taskbar)-25;
			}
		}
	}
	if(left !== false)
	{
		if ((parseInt(left)+'%') == left)
		{
			var scale = parseInt(left)/100;
			result.left = parseInt((scale*WindowSet._availWidth)-(parseInt(this.div.style.width)*scale))+'px';
		}
		else
		{
			result.left = left;
			if(constrainWindow)
			{
				if(result.left < 0)
				{
					difference.left = Math.abs(result.left);
					result.left = 0;
				}
				if(result.left+parseInt(this.div.style.width) > WindowSet._availWidth)
				{
					difference.left = (result.left+parseInt(this.div.style.width))-WindowSet._availWidth;
					result.left = WindowSet._availWidth-parseInt(this.div.style.width);
				}
			}
		}
	}
	
	if(result.top !== false)
		this.div.style.top = parseInt(result.top)+'px'
	if(result.left !== false)
		this.div.style.left = parseInt(result.left)+'px'
	
	if(!!this.onmove)
		this.onmove();
	
	return difference;
}

WindowSet.Win.prototype.fitContent = function(fitWidth, fitHeight, topPercent, leftPercent)
{
	var width=false, height=false;
	
	if(fitWidth)
	{
		this.content.style.overflow='hidden';
		this.content.scrollWidth; // Hack for IE. Yes, I know it doesn't LOOK like it's doing anything. But it is.
		width=parseInt(this.content.scrollWidth)+14;
	}
	if(fitHeight)
	{
		this.content.style.overflow='hidden';
		this.content.scrollHeight; // Another hack for IE. I don't know why this works. IE, you make me want to punch babies.
		height=parseInt(this.content.scrollHeight)+32;
	}
	this.resize(width, height);
	
	if(!!topPercent || !!leftPercent)
		this.move(topPercent, leftPercent);
	// TODO: Move it if the top and/or left were specified in %
}

WindowSet.Win.prototype.focus = function()
{
	if(!!this._flashInterval)
	{
		clearInterval(this._flashInterval);
		this._flashInterval=null;
	}
	
	var indices=new Array();
	var z=WindowSet._minZIndex;
	var numMinimized=0;
	
	while(indices.length < WindowSet._windows.length-1)
	{
		for(var i=0; i < WindowSet._windows.length; i++)
		{
			if(WindowSet._windows[i]!=this)
			{
				if(WindowSet._windows[i]._modal)
					return false;
				if(WindowSet._windows[i].div.style.zIndex==z)
					indices.push(WindowSet._windows[i]);
			}
		}
		z++;
	}
	
	for(var i=0; i<indices.length; i++)
	{
		if(indices[i]._minimized)
			numMinimized++;
	}
	
	indices.push(this);
	
	z=WindowSet._minZIndex;
	for(var i=0; i < indices.length; i++)
	{
		if(indices[i]._minimized)
		{
			indices[i].div.style.zIndex=WindowSet._minZIndex+indices.length;
			indices[i]._focused=false;
		}
		else if(indices[i]._modal)
		{
			WindowSet._modalOverlay.style.zIndex=WindowSet._minZIndex+indices.length+1;
			indices[i].div.style.zIndex=WindowSet._minZIndex+indices.length+2;
			indices[i].div.className='winContainer';
			indices[i]._focused=true;
		}
		else
		{
			indices[i].div.style.zIndex=z++;
			if(i < indices.length-numMinimized-1)
			{
				indices[i].div.className='winContainerBlurred';
				indices[i]._focused=false;
			}
			else
			{
				indices[i].div.className='winContainer';
				indices[i]._focused=true;
			}
		}
	}
	if(this._modal)
		WindowSet._modalOverlay.style.display='';
	else
		WindowSet._modalOverlay.style.display='none';
	
	WindowSet._iframeOverlay.style.zIndex=WindowSet._minZIndex+indices.length+3;
	
	return true;
}

WindowSet.Win.prototype.flash = function()
{
	if(this._focused)
		return false;
	
	if(!!this._flashInterval)
		clearInterval(this._flashInterval);
	this._flashInterval = setInterval('WindowSet.'+this.name+'._doFlash(WindowSet.'+this.name+')', 700);
}

WindowSet.Win.prototype._doFlash = function(win)
{
	if(win.div.className=='winContainer')
		win.div.className='winContainerBlurred';
	else
		win.div.className='winContainer';
}

WindowSet.Win.prototype.show = function()
{
	this.div.style.display = 'block';
	this._visible = true;
}

WindowSet.Win.prototype.hide = function()
{
	this.div.style.display = 'none';
	this._visible = false;
}

WindowSet.Win.prototype.maximize = function()
{
	if(this._modal)
		return false;
	
	if(this._maximized)
	{
		this.resize(this._maxState.width, this._maxState.height, false);
		this.move(this._maxState.top, this._maxState.left, true);
		this._maximizeButton.style.backgroundPosition = '57px 0';
		this._maximized = false;
	}
	else
	{
		this._maxState = {
			top : this.div.style.top,
			left : this.div.style.left,
			width : this.div.style.width,
			height : this.div.style.height
		};
		this.move(0, 0, false);
		this.resize(true, true, false);
		this._maximizeButton.style.backgroundPosition = '38px 0';
		this._maximized = true;
	}
	
	this.focus();
}

WindowSet.Win.prototype.minimize = function()
{
	if(this._modal)
		return false;
	
	if(this._minimized)
		return false;
	
	this._minState = {
		top : this.div.style.top,
		left : this.div.style.left,
		width : this.div.style.width,
		height : this.div.style.height
	};
	
	var mins = 0;
	for(var i=0; i < WindowSet._windows.length; i++)
	{
		if(WindowSet._windows[i]._minimized)
			mins++;
	}
	this.move(WindowSet._availHeight-25, (mins*125), false);
	this.resize(123, 25, false);
	this._buttonBar.style.display = 'none';
	this.div.className = 'winContainer';
	this._minimized = true;
	
	var maxZ = -1;
	var topWin = 0;
	for(var i in WindowSet._windows)
	{
		if(WindowSet._windows[i].div.style.zIndex > maxZ && !WindowSet._windows[i]._minimized)
		{
			maxZ = WindowSet._windows[i].div.style.zIndex;
			topWin = i;
		}
	}
	WindowSet._windows[topWin].focus();
}

WindowSet.Win.prototype.restore = function()
{
	if(!this._minimized)
		return false;
	
	for(var i=0; i < WindowSet._windows.length; i++)
	{
		if(WindowSet._windows[i]._minimized && (parseInt(WindowSet._windows[i].div.style.left) > parseInt(this.div.style.left)))
			WindowSet._windows[i].move(false, parseInt(WindowSet._windows[i].div.style.left)-125, false);
	}
	
	this.move(this._minState.top, this._minState.left, true);
	this.resize(this._minState.width, this._minState.height, true);
	this._buttonBar.style.display = '';
	this._minimized = false;
	
	this.focus();
}

WindowSet.Win.prototype.close = function()
{
	document.body.removeChild(this.div);
	for(var i in WindowSet._windows)
	{
		if(WindowSet._windows[i]==this)
			WindowSet._windows.splice(i, 1);
	}
	WindowSet[this.name]=null;
	
	if(WindowSet._windows.length > 0)
	{
		var maxZ = -1;
		var topWin = 0;
		for(var i in WindowSet._windows)
		{
			if(WindowSet._windows[i].div.style.zIndex > maxZ && !WindowSet._windows[i]._minimized)
			{
				maxZ = WindowSet._windows[i].div.style.zIndex;
				topWin = i;
			}
		}
		WindowSet._windows[topWin].focus();
	}
	else
		WindowSet._modalOverlay.style.display='none';
	
	if(!!this.onclose)
		this.onclose();
}

WindowSet.Win.prototype._dragMoveStart = function(e)
{
	if(this._maximized || this._minimized || !this.movable)
		return false;
	
	e = (!!e)?e:event;
	WindowSet._drag = {
		startX : e.clientX,
		startY : e.clientY,
		startTop : parseInt(this.div.style.top),
		startLeft : parseInt(this.div.style.left),
		target : this
	}
	this.focus();
	document.onmousedown = function() {return false;};
	document.onmouseup = WindowSet._dragEnd;
	document.onmousemove = WindowSet._dragMove;
	
	WindowSet._iframeOverlay.style.cursor='';
	WindowSet._iframeOverlay.style.display='';
}

WindowSet._dragMove = function(e)
{
	e = (!!e)?e:event;
	var drag = WindowSet._drag;
	drag.target.move(drag.startTop+(e.clientY-drag.startY), drag.startLeft+(e.clientX-drag.startX));
	return false;
}

WindowSet.Win.prototype._dragResizeStart = function(e, t, r, b, l)
{
	if(this._maximized || this._minimized || !this.resizable)
		return false;
	
	e = (!!e)?e:event;
	WindowSet._drag = {
		startX : e.clientX,
		startY : e.clientY,
		startWidth : parseInt(this.div.style.width),
		startHeight : parseInt(this.div.style.height),
		startTop : parseInt(this.div.style.top),
		startLeft : parseInt(this.div.style.left),
		anchorTop : t,
		anchorRight : r,
		anchorBottom : b,
		anchorLeft : l,
		target : this
	}
	this.focus();
	document.onmousedown = function() {return false;};
	document.onmouseup = WindowSet._dragEnd;
	document.onmousemove = WindowSet._dragResize;
	
	if(t && b && r)
		WindowSet._iframeOverlay.style.cursor='w-resize';
	else if(t && b && l)
		WindowSet._iframeOverlay.style.cursor='e-resize';
	else if(t && l && r)
		WindowSet._iframeOverlay.style.cursor='s-resize';
	else if(t && r)
		WindowSet._iframeOverlay.style.cursor='sw-resize';
	else if(t && l)
		WindowSet._iframeOverlay.style.cursor='se-resize';
	WindowSet._iframeOverlay.style.display='';
		
}

WindowSet._dragResize = function(e)
{
	e = (!!e)?e:event;
	var drag = WindowSet._drag;
	
	if (!drag.anchorLeft)
	{
		if(drag.startWidth-(e.clientX-drag.startX) >= 100)
		{
			var diff = drag.target.move(false, drag.startLeft+(e.clientX-drag.startX));
			drag.target.resize(drag.startWidth-(e.clientX-drag.startX)-diff.left, false);
		}
	}
	else if (!drag.anchorRight)
		drag.target.resize(drag.startWidth+(e.clientX-drag.startX), false);
	
	if (!(drag.anchorTop && drag.anchorBottom))
		drag.target.resize(false, drag.startHeight+(e.clientY-drag.startY));
	
	return false;
}

WindowSet._dragEnd = function()
{
	if(!!WindowSet._drag)
		WindowSet._drag = null;
	document.onmousedown = null;
	document.onmouseup = null;
	document.onmousemove = null;
	WindowSet._iframeOverlay.style.display='none';
}

/*window.onresize=function()
{
	var percents=new Array();
	
	for (var i in WindowSet._windows)
	{
		percents[i] = {
			top : 0,
			left : 0,
			width : 0,
			height : 0
		};
		if(parseInt(WindowSet._windows[i].div.style.top) != 0)
			percents[i].top = Math.round(100*parseInt(WindowSet._windows[i].div.style.top)/(WindowSet._availHeight-WindowSet._taskbar-parseInt(WindowSet._windows[i].div.style.height)));
		if(parseInt(WindowSet._windows[i].div.style.left) != 0)
			percents[i].left = Math.round(100*parseInt(WindowSet._windows[i].div.style.left)/(WindowSet._availWidth-parseInt(WindowSet._windows[i].div.style.width)));
			
		if(parseInt(WindowSet._windows[i].div.style.width) != 0)
			percents[i].width = Math.round(100*parseInt(WindowSet._windows[i].div.style.width)/(WindowSet._availWidth));
		if(parseInt(WindowSet._windows[i].div.style.height) != 0)
			percents[i].height = Math.round(100*parseInt(WindowSet._windows[i].div.style.height)/(WindowSet._availHeight-WindowSet._taskbar));
		
		WindowSet._windows[i].move(0, 0, false);
		WindowSet._windows[i].resize(0, 0, false);
	}
	
	WindowSet._setAvailWidthHeight();
	
	for (var i in percents)
	{
		WindowSet._windows[i].resize(percents[i].width+'%', percents[i].height+'%', null);
		WindowSet._windows[i].move(percents[i].top+'%', percents[i].left+'%', null);
	}
}*/