var _dragWindow  = null;
var _mouseOffset = null;

/*
	class Window
*/
function Window(id, title, content, width, height, x, y)
{
	// member variables
	this.winElem = null;
	this.releftionElem = null;
	this.windowId = false;
	
	
	// static member variables
	var windows = new Array();
	var maxZIndex = 250;
	var windowIds = 0;
	var reflectionHeight = 89;
	
	if (typeof(_window_prototype_called) == 'undefined')
  {
     _window_prototype_called = true;
     
     // member functions
     Window.prototype.setContent = setContent;
     Window.prototype.setTitle = setTitle;
     Window.prototype.setPosition = setPosition;
     Window.prototype.getPosition = getPosition;
     Window.prototype.setWidth = setWidth;
     Window.prototype.setHeight = setHeight;
     Window.prototype.getWidth = getWidth;
     Window.prototype.getHeight = getHeight;
     Window.prototype.setSize = setSize;
     Window.prototype.toFront = toFront;
     Window.prototype.redraw = redraw;
     Window.prototype.close = close;
     
     // private member functions
     Window.prototype._newWindow = _newWindow; // constructor
     Window.prototype._toBack = _toBack;
     Window.prototype._setVisibilityMedia = _setVisibilityMedia;
     
     // private static functions
     Window.redrawAll = redrawAll;
     Window.getWindowById = getWindowById;
     Window.getContainingWindow = getContainingWindow;
     Window.test = test;
  }
	this._newWindow(id, title, content, width, height, x, y);
	
	function _newWindow(id, title, content, width, height, x, y)
	{
		if (windows.length > 0)
			windows[windows.length-1]._toBack();
		
		if (id == '_blank')
		{
			windowIds++;
			id = '__win_' + windowIds;
		}
				
		this.windowId = id;
		windows[windows.length] = this;
		var body = document.getElementsByTagName('body')[0];
		
		// create window
		var div = document.createElement('div');
	  div.setAttribute('id', this.windowId);
	  body.appendChild(div);
	  this.winElem = new Element(this.windowId);
	  this.winElem.setClassname('window');
	  maxZIndex++;
	  this.winElem.setStyleAttribute('zIndex', maxZIndex);
	  
	  var table = '';
	  table += '<table cellspacing="0" class="window">';
	  table += '<tr><td class="windowTitle" id="'+this.windowId+'_titlebar">';
	  table += '<div class="windowTitle" id="'+this.windowId+'_title">Loading...</div></td>';
	  table += '<td class="windowButtons" id="'+this.windowId+'_buttons">';
	  table += '<img src="graphics/button_close_22x19.gif" class="windowButton" id="'+this.windowId+'_closeButton" />';
	  table += '</td></tr>';
	  table += '<tr><td colspan="2" class="windowContent" id="'+this.windowId+'_contentHolder">';
	  table += '<div class="windowContent" id="'+this.windowId+'_content">Loading...</div></td></tr>';
	  table += '</table>';
	  this.winElem.setValue(table);
	  
	  
	  var titleBar = getElem(this.windowId+'_titlebar');
	  titleBar.window = this;
	  titleBar.onmousedown = function(ev)
	  {
			_dragWindow = this.window;
			_dragWindow.toFront();
			_mouseOffset = _getMouseOffset(this.window, ev);
			return false;
		}
		
		var titleButtons = getElem(this.windowId+'_buttons');
	  titleButtons.window = this;
	  titleButtons.onmousedown = function(ev)
	  {
			_dragWindow = this.window;
			_dragWindow.toFront();
			_mouseOffset = _getMouseOffset(this.window, ev);
			return false;
		}
		
		var contentHolder = getElem(this.windowId+'_contentHolder');
	  contentHolder.window = this;
	  contentHolder.onmouseup = function(ev)
	  {
			this.window.toFront();
			return false;
		}
		
		var contentDiv = getElem(this.windowId+'_content');
	  contentDiv.window = this;
		
		// handle closeButton
		var closeButton = getElem(this.windowId+'_closeButton');
	  closeButton.window = this;
	  closeButton.onmouseup = function(ev)
	  {
			this.window.close();
			return false;
		}
		
		// create reflection
		div = document.createElement('div');
	  div.setAttribute('id', this.windowId+'_reflection');
	  body.appendChild(div);
	  this.reflectionElem = new Element(this.windowId+'_reflection');
	  this.reflectionElem.setClassname('reflection');
	  
	  table = '';
	  table += '<table cellspacing="0" class="reflection"><tr>';
	  table += '<td class="reflection">&nbsp;</td></tr></table>';
	  
	  this.reflectionElem.setValue(table);

		//alert(width + ',' + height +  ',' + x + ',' + y);
		this.setSize(width, height);
		this.setPosition(x, y);
		this.setTitle(title);
		this.setContent(content);
		
  } // newWindow
  
	/*
		Member functions
	*/

	function test()
	{
		alert('testing...');
	}

	function close()
	{
		this.winElem.hide();
		this.reflectionElem.hide();
		this.setContent('');
		this.winElem.getElement().parentNode.removeChild(this.winElem.getElement());
		this.reflectionElem.getElement().parentNode.removeChild(this.reflectionElem.getElement());
		
		// put second window in front, if this window was in front
		if (windows[windows.length-1] == this && windows.length > 1)
  		windows[windows.length-2].toFront();
		
		// remove window from array
		// find this window first
  	var i=0; 
  	for (;i<windows.length; i++)
  	{
  		if (windows[i] == this)
  			break;
  	}
  	i++;
	  	
  	// shift all windows
  	for (; i<windows.length; i++)
  	{
  		windows[i-1] = windows[i];
  	}
  	
  	// remove last (duplicate) window
  	windows.length--;
	} // close

  function setContent(content)
  {
  	var winContent = new Element(this.windowId + '_content');
  	winContent.setValue(content);
  } // setContent
  
  function setTitle(title)
  {
  	// IE fix for buttons
  	if (title.length < 8)
  		title += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  	var winContent = new Element(this.windowId + '_title');
  	winContent.setValue(title);
  } // setContent
  
  function setPosition(x, y)
  {
  	
  	var reflectionSpace = 20;
  	var minX = 0;
  	var maxX = _getBrowserWidth()-this.getWidth();
  	var minY = 0;
  	var maxY = _getBrowserHeight()-reflectionSpace-reflectionHeight-this.getHeight();
  	
  	
  	if (x > maxX)
  		x = maxX;
  	
  	if (x < minX)
  		x = minX;
  	
  	if (y > maxY)
  		y = maxY;
  	
  	if (y < minY)
  		y = minY;
  		
  	this.winElem.setPosition(x, y);
  	this.reflectionElem.setPosition(x, _getBrowserHeight()-reflectionSpace-reflectionHeight);
  	
  	
  	//this.winElem.setPosition(0, 0);
  	//this.reflectionElem.setPosition(0,0);
  } // setPosition
  
  function getPosition()
  {
  	return this.winElem.getPosition();
  } // getPosition
  
  function setSize(w, h)
  {
  	this.setWidth(w);
  	this.setHeight(h);
  } // setSize
  
  function setWidth(w)
  {
  	this.winElem.setWidth(w);
  	this.reflectionElem.setWidth(w);
  } // setWidth
  
  function setHeight(h)
  {
  	this.winElem.setHeight(h);
  } // setHeight

	function getWidth()
  {
  	return this.winElem.getWidth();
  } // getWidth
  
  function getHeight(h)
  {
  	return this.winElem.getHeight();
  } // getHeight

  function toFront()
  {
  	if (windows[windows.length-1] != this)
  	{
	  	maxZIndex++;
	  	this.winElem.setStyleAttribute('zIndex', maxZIndex);
	  	if (windows.length > 0)
				windows[windows.length-1]._toBack();
	  	
	  	// fix position in windows array
	  	// - this window goes on last place
	  	// - all other windows shift one
	  	
	  	// find this window first
	  	var i=0; 
	  	for (;i<windows.length; i++)
	  	{
	  		if (windows[i] == this)
	  			break;
	  	}
	  	i++;
	  	
	  	// shift all windows
	  	for (; i<windows.length; i++)
	  	{
	  		windows[i-1] = windows[i];
	  	}
	  	
	  	// put this window in front
	  	windows[windows.length-1] = this;
	  	
	  	/*var mes = '';
	  	for (i=0; i<windows.length; i++)
	  		mes += windows[i].windowId + ', ';
	  	alert(mes);*/
	  	
	  	this._setVisibilityMedia(true);
	  	new Element(this.windowId + '_titlebar').setClassname('windowTitle');
	  	new Element(this.windowId + '_title').setClassname('windowTitle');
	  	new Element(this.windowId + '_buttons').setClassname('windowButtons');
	  }
  } // toFront
  
  function _toBack()
  {
  	this._setVisibilityMedia(false);
  	new Element(this.windowId + '_titlebar').setClassname('windowTitleBack');
	  new Element(this.windowId + '_title').setClassname('windowTitleBack');
	  new Element(this.windowId + '_buttons').setClassname('windowButtonsBack');
  } // _toBack
  
  function _setVisibilityMedia(visible)
  {
		var div = this.winElem.getElement();
		if (!div)
			return;
		
		var tags = new Array('applet');
		
		for (i=0; i<tags.length; i++)
		{
			var media = div.getElementsByTagName(tags[i]);
			for (j=0; j<media.length; j++)
			{
				if (visible)
					media[j].style.visibility = 'visible';
				else
					media[j].style.visibility = 'hidden';
			}
		}
  } // _setVisibilityMedia
  
  function redraw()
  {
  	var pos = this.getPosition();
  	this.setPosition(pos.x, pos.y);
  } // redraw
  
  function redrawAll()
  {
  	for (i=0; i<windows.length; i++)
			windows[i].redraw();
  } // redrawAll
  
  function getWindowById(windowId)
  {
  	for (i=0; i<windows.length; i++)
  	{
  		if (windows[i].windowId == windowId)
  			return windows[i];
		}
		return false;
  } // getWindowById
  
  function getContainingWindow(elem)
  {
  	
  	while (elem)
  	{
  		if (elem.window)
  		{
  			return elem.window;
  		}
  		
  		elem = elem.parentNode;
  	}
  	return false;
  } // getContainingWindow
  
} // class Window

/*
	Window drag functions
*/
function _getMouseCoords(ev)
{
	if(ev.pageX || ev.pageY)
	{
		return {x:ev.pageX, y:ev.pageY};
	}
	
	return	{
						x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
						y:ev.clientY + document.body.scrollTop  - document.body.clientTop
					};
} // _getMouseCoords

function _getMouseOffset(target, ev)
{
	ev = ev || window.event;

	var docPos    = target.getPosition();
	var mousePos  = _getMouseCoords(ev);
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
} // _getMouseOffset

function _mouseMove(ev)
{
	ev = ev || window.event;
	var mousePos = _getMouseCoords(ev);

	if(_dragWindow)
	{
		_dragWindow.setPosition(mousePos.x - _mouseOffset.x, mousePos.y - _mouseOffset.y);		
		return false;
	}
} // _mouseMove

function _mouseUp()
{
	_dragWindow = null;
} // _mouseUp

/*
 Other functions
*/

var desktopTopBackground = new Element('desktopTopBackground');
function resizeDesktopBackground()
{
	desktopTopBackground.setSize(_getBrowserWidth(), _getBrowserHeight()/2);
} // resizeDesktopBackground

function _resize()
{
	if (Window.redrawAll)
		Window.redrawAll();
	resizeDesktopBackground();
} // _resize

function _getBrowserWidth()
{
	if(document.layers||(document.getElementById&&!document.all))
		browserWidth = parseInt(window.innerWidth);
	else if(document.all)
		browserWidth = parseInt(document.body.clientWidth);
	
	return browserWidth;
} // _getBrowserWidth

function _getBrowserHeight()
{
	if(document.layers||(document.getElementById&&!document.all))
		browserHeight = parseInt(window.innerHeight);
	else if(document.all)
		browserHeight = parseInt(document.body.clientHeight);
	
	return browserHeight;
} // _getBrowserHeight


__icons = new Array();

function _preloadImage(icon)
{
	if (__icons[icon] == null)
	{
		__icons[icon] = new Image();
		__icons[icon].src = baseurl+'/graphics/'+icon+'.jpg';
		__icons[icon+'_hover'] = new Image();
		__icons[icon+'_hover'].src = baseurl+'/graphics/'+icon+'_hover.jpg';
	}
} // preloadImage

function makeSwappable(icon, elem)
{
	_preloadImage(icon);
	elem.icon = icon;
	elem.onmouseover = function()
	{
		this.src = __icons[this.icon+'_hover'].src;
	}
	elem.onmouseout = function()
	{
		this.src = __icons[this.icon].src;
	}
} // makeSwappable

document.onmousemove = _mouseMove;
document.onmouseup = _mouseUp;
window.onresize = _resize;
