	var trayMaxLength = 20;
	var resizeBorder = 3;
	
	var movingWindow = "";
	var resizingWindow = "";
	var direction = "";
	var windows = new Array();
	
	//**********Window Class**************
	function WindowCLS(name, title, width, height, resizeable) 
	{
		//****animation
		this.isAnimating = false;
		this.animationTime = 1000; // 3 sec
		this.frames = 15;
		this.cnt = 0;
		this.opacity = 0.3;
		//*************
		this.name = name;
		this.title = title;
		this.contentID = name + '_content';
		this.isMoving = false;
		this.minWidth = width;
		this.width = width;
		this.minHeight = height;
		this.height = height;
		this.top = 0;
		var winContainer;
		if (winContainer = getElem("windowsContainer"))
		{
			var Zindex = -1;
			for (var id in windows)
				Zindex = Math.max(windows[id].Zindex, Zindex);
			Zindex++;
			Zindex = (Zindex) ? Zindex : getMaxZindex(document.getElementsByTagName('body')[0], 0);
			this.Zindex = Zindex;
			var resizeContent = "";
			var resizeWidth = 0;
			var resizeHeight = 0;
			if (resizeable)
			{
				resizeWidth = 2*resizeBorder;
				resizeHeight = resizeBorder;
				resizeContent = "<table id='" + name + "' onmousedown='winUp(this);' style='float: left; position: absolute; width: " + width + "px; height: " + height + "px; border: 1px solid #000; background-color: #000000; z-index: " + Zindex + "' cellspacing='0' cellpadding='0' border='0' align='center' width='100%' height='100%'><tr><td colspan='3' id='" + name + "_head' style='width: 100%;cursor: default; background-image: url(/img/windows/window_head.jpg); background-position: center top; background-repeat: repeat-x;' ><table cellspacing='0' cellpadding='0' border='0' height='100%'><tr><td width='100%' id='" + name + "_title' style='height: 20px; overflow: hidden; font-weight: bold; color: #000; padding-left: 3px;' onmousedown='movingWindow=\"" + name + "\"; mouseDown(event);' onmouseup='mouseUp(event);'>" + title + "</td><td align='right' style='padding-right: 5px; display: inline;' class='activeLink'><a href='' onclick='windows." + name + ".close(); return false;' title='" + "close" + "'>&times;</a></td></tr></table></td></tr><tr height='100%'><td height='100%'><div style='background-color: #000000; height: 100%; width: " + resizeBorder + "px; cursor: w-resize;' onmousedown='resizingWindow=\"" + name + "\"; direction=\"west\"; document.body.style.cursor = \"w-resize\"; mouseDown(event);' onmouseup='mouseUp(event);'></div></td><td><div id='" + name + "_contentBorder' style='padding: 0px; overflow: auto; height: 100%; width: " + (width - resizeWidth) + "px;'><div id='" + this.contentID + "' style='padding: 3px; color: #000; height: " + (height - 30 - resizeHeight - 6 - 2) + ";'></div></div></td><td><div style='background-color: #000000; height: 100%; width: 3px; cursor: e-resize;' onmousedown='resizingWindow=\"" + name + "\"; direction=\"east\"; document.body.style.cursor = \"e-resize\"; mouseDown(event);' onmouseup='mouseUp(event);'></div></td></tr><tr><td><div style='background-color: #000000; width: " + resizeBorder + "px; height: " + resizeBorder + "px; cursor: sw-resize;' onmousedown='resizingWindow=\"" + name + "\"; direction=\"south-west\"; document.body.style.cursor = \"sw-resize\"; mouseDown(event);' onmouseup='mouseUp(event);'></div></td><td><div style='background-color: #000000; height: " + resizeBorder + "px; cursor: s-resize;' onmousedown='resizingWindow=\"" + name + "\"; direction=\"south\"; document.body.style.cursor = \"s-resize\"; mouseDown(event);' onmouseup='mouseUp(event);'></div></td><td><div style='background-color: #000000; width: " + resizeBorder + "px; height: " + resizeBorder + "px; width: " + resizeBorder + "px; cursor: se-resize;' onmousedown='resizingWindow=\"" + name + "\"; direction=\"south-east\"; document.body.style.cursor = \"se-resize\"; mouseDown(event);' onmouseup='mouseUp(event);'></div></td></tr></table>";
				/*<a href='' onclick='windows." + name + ".hide(); return false;' title='" + "minimize" + "'>_</a>&nbsp;*/
			}
			else
			{
				var size = "";
				var content = "";
				if (width)
					size += width + "px; ";
				if (height)
					size += height + "px;";
				resizeContent = "<div id='" + name + "' style='position: absolute; z-index: " + Zindex + "; " + size + "'><table style='width: 100%; font-size: 14px; ' cellspacing='0' cellpadding='0'  id='" + name + "_head'><tr><td><div style='width: 6px; height: 24px; background: url(/img/windows/border/left_top.png) left top no-repeat;'></div></td><td style='width: 100%; background:url(/img/windows/border/win_header_bg.jpg) center top repeat-x; border-top: 1px solid #d5d5d5; overflow: hidden; height: 23px;'><table cellspacing='0' cellpadding='0' height='23px'><tr><td style='width: 100%; height: 23px; overflow: hidden; font-weight: bold; color: #000; font-size: 14px; ' id='" + name + "_title' onmousedown='movingWindow=\"" + name + "\"; mouseDown(event);' onmouseup='mouseUp(event);'>" + title + "</td><td style='display: inline;'><div style='width: auto;'><a href='' onclick='windows." + name + ".close(); return false;' title='" + "Закрыть" + "' style='text-decoration: none; color: #000; font: bold; font-size: 14px; '>&times;</a></div></td></tr></table></td><td><div style='width: 6px; height: 24px; background:url(/img/windows/border/right_top.png) right top no-repeat;'></div></td></tr></table><table style='width: 100%; border-left: 1px solid #d5d5d5; border-right: 1px solid #d5d5d5; background-color: #fff;' cellspacing='0' cellpadding='0'><tr><td id='" + name + "_content_overflow' style='padding: 0px; overflow: auto; text-align: center; vertical-align: top;'><div id='" + this.contentID + "' style='color: #000; text-align: center;'>" + content + "</div></td></tr></table><table style='width: 100%; height: 7px;' cellspacing='0' cellpadding='0'><tr><td><div style='width: 6px; height: 7px; background: url(/img/windows/border/left_bot.png) left bottom no-repeat;'></div></td><td style='width: 100%; height: 7px; background: #fff url(/img/windows/border/border.png) center bottom repeat-x;'></td><td><div style='width: 6px; height: 7px; background:url(/img/windows/border/right_bot.png) right bottom no-repeat;'></div></td></tr></table></div>";
			}
			winContainer.innerHTML += resizeContent;
			this.container = getElem(name);
		}
	}
	//**********starts animation*************
	WindowCLS.prototype.animate = function()
	{
		if (this.isAnimating)
			return;
		this.isAnimating = true;
		this.stPos = getElementPosition(this.name + "_hidden");
		var target = getElem(this.name);
		if (getElem(this.name + "_hidden"))
			alert(this.stPos['top']);
		this.deltaX = parseInt(target.style.left) - this.stPos['left'];
		this.deltaY = parseInt(target.style.top) - this.stPos['top'];
		this.deltaWidth = Math.abs(parseInt(target.style.width) - this.stPos['width']);
		this.deltaHeight = Math.abs(parseInt(target.style.height)  - this.stPos['height']);
		target.style.width = '40px';
		target.style.height = '20px';
		this.cnt = 0;
		this.animID = setInterval("windows." + this.name + ".animating()", this.animationTime/this.frames);
	}
	//*****************************************
	WindowCLS.prototype.setPos_parent = function(parent)
	{
		var pos = getElementObjPosition(parent);
		var left = pos['left'] - pos['width'];
		var top = pos['top'] + pos['height'];
		this.setPos(left, top);
	}	
	//*****************************************
	WindowCLS.prototype.animating = function()
	{
		var target = getElem(this.name)
		if (isIE)
		{
			target.style.width = this.deltaWidth/this.frames*(this.cnt+1) + 'px';
			target.style.height = this.deltaHeight/this.frames*(this.cnt+1) + 20 + 'px';
		}
		else
		{
			target.style.width = this.deltaWidth/this.frames*(this.cnt+1) + 'px';
			target.style.height = this.deltaHeight/this.frames*(this.cnt+1) + 20 + 'px';
		}
		target.style.top = this.stPos['top'] + this.deltaY/this.frames*(this.cnt+1) + 'px';
		target.style.left = this.stPos['left'] + this.deltaX/this.frames*(this.cnt+1) + 'px';
		getElem("debug").innerHTML = " w: " + this.deltaX + " | cW: " + this.stPos['top'];
		if (isIE)
			target.style.filter = "alpha(opacity=" + 100*(this.opacity + (1 - this.opacity)/this.frames*(this.cnt + 1)) + ")";
		else
			target.style.opacity = (this.opacity + (1 - this.opacity)/this.frames*(this.cnt + 1));
		if (!this.cnt)
			windows[this.name].show(); 
		this.cnt++;
		if ( (this.cnt >= this.frames) || (!this.isAnimating) )
		{
			this.isAnimating = false;		
			clearInterval(this.animID);
			//target.style.overflow = "visible";
			//getElem("msg_header").style.overflow = "visible";				
			//getElem("msg_container").style.overflow = "auto";
		}
	}	
	//*****************************************
	WindowCLS.prototype.setContent = function(text) 
	{ 
		var content;
		if (content = getElem(this.name + "_content"))
		{
			content.innerHTML = text;
		}
	};	
	//*****************************************
	WindowCLS.prototype.setTitle = function(text) 
	{ 
		this.title = text;
		var Title;
		if (Title = getElem(this.name + "_title"))
		{
			Title.innerHTML = text;
		}
	};	
	//*****************************************
	WindowCLS.prototype.setContentBG = function(text) 
	{ 
		var content;
		if (content = getElem(this.name + "_content"))
		{
			content.style.background = text;
		}
	};	
	//*****************************************
	WindowCLS.prototype.setTitleBG = function(text) 
	{ 
		var Title;
		if (Title = getElem(this.name + "_head"))
		{
			Title.style.background = text;
		}
	};
	//*****************************************
	WindowCLS.prototype.setOpacity = function(opacity) 
	{ 
		var Container;
		if (Container = getElem(this.name))
		{
			Container.style.opacity = opacity;
			Container.style.filter = "alpha(opacity=" + opacity*100 + ")";
		}
	};
	//*****************************************
	WindowCLS.prototype.setBorderStyle = function(text) 
	{ 
		var Container;
		if (Container = getElem(this.name))
		{
			Container.style.border = text;
		}
	};
	//*****************************************
	WindowCLS.prototype.setStyle = function(text) 
	{ 
		var Content;
		if (Content = getElem(this.name + "_content"))
		{
			Content.style = text;
		}
	};
	//*****************************************
	WindowCLS.prototype.setWidth = function(width) 
	{ 
		var Content;
		var Win;
		if ( (Content = getElem(this.name + "_content_overflow")) && (Win = getElem(this.name)) )
		{
			Win.style.width = width + 'px';
			Content.style.width = width + 'px';
			this.width = width;
		}
	};
	//*****************************************
	WindowCLS.prototype.setHeight = function(height) 
	{ 
		var Content;
		var Win;
		if ( (Content = getElem(this.name + "_content_overflow")) && (Win = getElem(this.name)) )
		{
			Win.style.height = height + 'px';
			Content.style.height = (height - 20) + 'px';
			this.height = height;
		}
	};
	//*****************************************
	WindowCLS.prototype.setDefaultSize = function() 
	{ 
		this.setWidth(400);
		this.setHeight(200);
	};
	//*****************************************
	WindowCLS.prototype.setScrollStyle = function(text) 
	{ 
		var Content;
		if (Content = getElem(this.name + "_contentBorder"))
		{
			Content.scrollbar = text;
		}
	};
	//*****************************************
	WindowCLS.prototype.setCenter = function(vertHeight) 
	{
		vertHeight = (vertHeight) ? true :false;
		this.setPos((getClientWidth() - this.width)/2 + defScroll().x , (vertHeight) ? (getClientHeight() - this.height)/2 + defScroll().y : this.top);
	};
	//*****************************************
	WindowCLS.prototype.setPos = function(left, top) 
	{ 
		var Content;
		if (Content = getElem(this.name))
		{
			Content.style.left = left + 'px';
			Content.style.top = top + 'px';
		}
	};
	//*****************************************
	WindowCLS.prototype.remove = function() 
	{ 
		var winContainer;
		if (winContainer = getElem("windowsContainer"))
		{
			var win2remove;
			if (win2remove = getElem(this.name))
			{
				winContainer.removeChild(win2remove);
				delete windows[this.name];
			}
		}
	};
	//*****************************************
	WindowCLS.prototype.close = function() 
	{ 
		var win2close;
		if (win2close = getElem(this.name))
		{
			win2close.style.display = "none";
		}
	};
	//*****************************************
	WindowCLS.prototype.hide = function() 
	{ 
		var win2hide;
		if (win2hide = getElem(this.name))
		{
			win2hide.style.display = "none";
			add2tray(this.name);
		}
	};	
	//*****************************************
	WindowCLS.prototype.show = function() 
	{ 
		var win2show;
		if (win2show = getElem(this.name))
		{
			winUp(win2show);
			win2show.style.display = "";
		}
	};
	//*********additional functions********
	//***********add mini icon to tray when window is minimized***************
	function add2tray(winID)
	{
		var trayContainer;
		if (trayContainer = getElem("tray"))
		{
			var title = "";
			if (windows[winID].title.length > trayMaxLength)
			{
				title = windows[winID].title.substr(0, trayMaxLength - 3) + "...";
			}
			else
			{
				title = windows[winID].title;
				for (var i = 0; i < (trayMaxLength - windows[winID].title.length); i++)
					title += "&nbsp;";
			}
			trayContainer.innerHTML += "<div id='" + winID + "_hidden' class='activeLink' style='display: inline; height: 20px; padding-left: 3px; padding-right: 3px; margin: 2px; margin-bottom: 0px; background-color: black; color: #ffffff; cursor: default; background-image: url(/img/windows/window_head.jpg); background-position: center top; background-repeat: repeat-x;'><a href='' style='color: #ffffff; text-decoration: none; font-weight: bold;' onclick='windows." + winID + ".animate(); delFromTray(\"" + winID + "\"); return false;' title='maximize'>" + title + "</a>&nbsp;<a href='' onclick=' delFromTray(\"" + winID + "\"); return false;' title='" + "close" + "'>&times;</a></div>";
		}
	}
	//**********remove mini icon from tray when window is minimized************
	function delFromTray(winID)
	{
		var trayContainer;
		if (trayContainer = getElem("tray"))
		{
			var trayItem;
			if (trayItem = getElem(winID + "_hidden"))
				trayContainer.removeChild(trayItem);
		}
	}
	//***************************************
	function debug()
	{
		var debug = getElem("debug");
		debug.innerHTML = "";
		for (var id in windows)
			debug.innerHTML += "id: " + id + " Zindex: " + windows[id].Zindex + "<br/>";
	}
	//**********checks window with this name on existing************	
	function isWindow(name)
	{
		for (var id in windows)
			if (id == name)
				return true;
		return false;
	}
	//**********creates new window and adds it into array `windows`. access: `windows.name`********	
	function add_window(name, title, width, height, resizeable)
	{
		if (!isWindow(name))
			windows[name] = new WindowCLS(name, title, width, height, resizeable);
		return windows[name];
	}
	//***********gets current window up************
	function winUp(up)
	{
		var Zindex = -1;
		var win2up = up.id;
		for (var id in windows)
		{
			if (id != win2up)
			{
				if (windows[id].Zindex > windows[win2up].Zindex)
				{
					Zindex = Math.max(windows[id].Zindex, Zindex);
					windows[id].Zindex--;
					getElem(id).style.zIndex = windows[id].Zindex;	
				}
			}
		}
		if (Zindex != -1)
		{
			windows[win2up].Zindex = Zindex;
			getElem(windows[win2up].name).style.zIndex = Zindex;		
		}
	}
	//*****************************
	function returnFalse(e){
	  return false;
	}
	function cancelEvent(e){
	  if(e.preventDefault)e.preventDefault();
	  else e.returnValue=false;
	}
	function addHandler(e,event,action,param){
	  if(document.addEventListener)e.addEventListener(event,action,param);
	  else if(document.attachEvent)e.attachEvent('on'+event,action);
	  else e['on'+event]=action;
	}
	function removeHandler(e,event,action,param){
	  if(document.addEventListener)e.removeEventListener(event,action,param);
	  else if(document.attachEvent)e.detachEvent('on'+event,action);
	  else e['on'+event]=returnFalse;
	}
	addHandler(document,'mouseup',mouseUp,false);
	function mouseDown(e){
	  e=e||event;
	  cancelEvent(e);
	  addHandler(document,'selectstart',returnFalse,false);
	}
	function mouseUp(e){
	  removeHandler(document,'selectstart',returnFalse,false);
	  movingWindow = "";
	  resizingWindow = "";
	  document.body.style.cursor = "default";
	}
	//***************
	/*prvmousex = mousex = 0
	prvmousey = mousey = 0
	if (document.all && document.all.item) //ie
	{
	  document.onmousemove=function()
	  {    
		prvmousex = mousex;
		prvmousey = mousey;
		mousex=event.clientX+document.body.scrollLeft;
		mousey=event.clientY+document.body.scrollTop;
		if (movingWindow)
			moveWindow();
		if (resizingWindow)
			resizeWindow(direction);
		return true
	  }
	}
	else // !ie
	{
	  document.onmousemove=function(e) 
	  {
		prvmousex = mousex;
		prvmousey = mousey;
		mousex = e.pageX
		mousey = e.pageY
		if (movingWindow)
			moveWindow();
		if (resizingWindow)
			resizeWindow(direction);
		return true
	  }  
	}*/
	//*******************************************************
	var isIE = checkBrowser();	
	function checkBrowser() 
	{
		if (navigator.appName.indexOf ("Microsoft") !=-1) 
		{
			return true //IE
		}	
		return false;
	}
	//*****************************************
	function getElementPosition(elemId)   
	{   
		var elem = getElem(elemId);   
		   
		var w = elem.offsetWidth;   
		var h = elem.offsetHeight;   
		   
		var l = 0;   
		var t = 0;   
		   
		while (elem)   
		{   
			l += elem.offsetLeft;   
			t += elem.offsetTop;   
			elem = elem.offsetParent;   
		}   
	  
		return {"left":l, "top":t, "width": w, "height":h};   
	}
//****************************************
	function moveWindow()
	{
		var target = getElem(movingWindow);		
		if (target.offsetTop > 0) 
			target.style.top = (target.offsetTop + mousey - prvmousey) + 'px';
		else
			target.style.top = '1px';
		if (target.offsetLeft + target.offsetWidth > 40) 
			target.style.left = (target.offsetLeft + mousex - prvmousex) + 'px';
		else
			target.style.left = (40 + 1 - target.offsetWidth) + 'px';
	}
	//****************************************
	function resizeWindow(_direction)
	{
		var target;
		switch (_direction)
		{
			case 'east':
						if (target = getElem(resizingWindow + "_contentBorder"))
						{
							var width = target.offsetWidth + mousex - prvmousex;
							if (windows[resizingWindow].minWidth <= width + 2*resizeBorder)
								target.style.width = width + 'px'; 
						}
						break;
			case 'south':
						if (target = getElem(resizingWindow + "_contentBorder"))
						{
							var paddingHeight = 0;
							if (isIE)
								paddingHeight = 10;
							var height = target.offsetHeight + mousey - prvmousey;
							if (windows[resizingWindow].minHeight <= height + resizeBorder + 30 + paddingHeight)
								target.style.height = height + 'px'; 
						}
						break;
			case 'west':
						if (target = getElem(resizingWindow + "_contentBorder"))
						{
							var width = target.offsetWidth - (mousex - prvmousex);
							if (windows[resizingWindow].minWidth <= width + 2*resizeBorder)
							{
								target.style.width = width + 'px'; 
								getElem(resizingWindow).style.left = getElem(resizingWindow).offsetLeft + (mousex - prvmousex);
							}
						}
						break;
			case 'south-west':
						resizeWindow('south');
						resizeWindow('west');
						break;
			case 'south-east':
						resizeWindow('south');
						resizeWindow('east');
						break;
		}
	}
	var cnt = 0;
	function getMaxZindex(parent, zIndex)
	{
		var tmpZindex = parseInt('0' + getElementComputedStyle(parent, "z-index"))
		zIndex = Math.max(tmpZindex, zIndex);
		cnt++;
		if (parent.style.zIndex > 0)
			alert(parent.style.zIndex)
		for (var i = 0; i < parent.children.length; i++)
			zIndex = getMaxZindex(parent.children[i], zIndex);
		return zIndex + 1;
	}
	
	function getElementComputedStyle(elem, prop)
	{
		if (typeof elem != "object") 
			elem = getElem(elem);
	  
		// external stylesheet for Mozilla, Opera 7+ and Safari 1.3+
		if (document.defaultView && document.defaultView.getComputedStyle)
		{
			if (prop.match(/[A-Z]/)) 
				prop = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
			return document.defaultView.getComputedStyle(elem, "").getPropertyValue(prop);
		}	  
		// external stylesheet for Explorer and Opera 9
		if (elem.currentStyle)
		{
			var i;
			while ((i=prop.indexOf("-"))!=-1) 
				prop = prop.substr(0, i) + prop.substr(i+1,1).toUpperCase() + prop.substr(i+2);
			return elem.currentStyle[prop];
		}	  
		return "";
	}
