var xWindow = function ()
{
	var p = this;
	p.oWindowList = [];
	p.vErrorList = [];
	p.vErrorIndex = null;

	p.vDrag = [];
	p.vDrag['element'] = [];
	p.vDrag['capture'] = [];
	p.vDrag['deltaX'] = [];
	p.vDrag['deltaY'] = [];

	p.vClosePic = '/images/closew.gif';
	p.vControls = [];
	p.vControls['min'] = [];
	p.vControls['min']['active'] = false;
	p.vControls['min']['show'] = false;
	p.vControls['max'] = [];
	p.vControls['max']['active'] = false;
	p.vControls['max']['show'] = false;
	p.vControls['close'] = [];
	p.vControls['close']['active'] = true;
	p.vControls['close']['show'] = true;

	p.xWindow = [];
	p.xWindow.vControlClassName = 'xwin_';

	p.bMultiLang = true;
	p.bAllowClose = true;
	p.bAddDrag = true;
	p.vBlinkInterval = null;
	p.vBlinkCount = 2;
	p.vBlinkCountCurrent = 0;
	p.vBlinkFrequency = 110;
	p.vNeedPrepareSelect = true;

	for (var i in arguments[0])
		p[i] = arguments[0][i];

}

xWindow.prototype = new xAjax;

xWindow.prototype.fAddWin = function ()
{

	var p = this, wIndex;
	if (document.vLanguages)
	{
		p.vCurrentLang = document.vLanguages.vCurrent;
		p.vLangs = document.vLanguages.vLangs;
	}

	if (document.xWin)
		p.oWindowList = document.xWin;

	wIndex = p.oWindowList.length;
	p.oWindowList[wIndex] = [];

	for (var i in arguments[0])
		p[i] = arguments[0][i];

	p.oWindowList[wIndex]['args'] = arguments[0];

	document.xWin = p.oWindowList;
	document.xWinActive = wIndex;

	if (p.bMultiLang && !p.vLangs)
		p.oWindowList[wIndex]['wait_lang'] = true;
	else
		p.fShowWin(wIndex);
	p.vWindowIndex = wIndex;
	return wIndex;
}

xWindow.prototype.fShowWin = function ()
{
	var p = this, wIndex = arguments[0];
	if (p.vNeedPrepareSelect)
		p.fPrepareSelect(false);
	if (p.oWindowList[wIndex] && !p.oWindowList[wIndex]['oWnd'])
	{
		if (p.aOnWindowShow(wIndex))
		{
			var tDiv, tDiv1, tSpan, tMainDiv, tText;

			for (var i in p.oWindowList[wIndex]['args'])
				p[i] = p.oWindowList[wIndex]['args'][i];

			tMainDiv = crEl('DIV');
			tMainDiv.style.position = 'absolute';
			tMainDiv.style.width = (p.vWidth + 16) + 'px';
			tMainDiv.style.heigth = (p.vHeight + 28) + 'px';
			if (p.vTop)
				tMainDiv.style.top = p.vTop;
			else
				tMainDiv.style.top = parseInt(document.body.clientHeight) / 2 - p.vHeight/2 + parseInt(document.body.scrollTop) + 'px'			//p.vBoxHeight/2 + parseInt(document.body.scrollTop) + 'px';
			if (p.vLeft)
				tMainDiv.style.left = p.vLeft;
			else
				tMainDiv.style.left = parseInt(document.body.clientWidth / 2 - p.vWidth/2 + parseInt(document.body.scrollLeft)) + 'px';

			for (var i=0; i<3; i++)
			{
				var tp = (i==0?'t':(i==1?'m':'b'));
				tDiv = crEl('DIV');
				tDiv.className = p.xWindow.vControlClassName + tp;
				tDiv.setAttribute('xIndex', wIndex);
				if (i==0)
					p.fDragInit(tMainDiv, tDiv);

				tDiv1 = crEl('DIV');
				tDiv1.className = p.xWindow.vControlClassName + tp +'left ' + p.xWindow.vControlClassName + 'filter';
				if (i==1)
					tDiv1.style.height = p.vHeight + 'px';
				tDiv.appendChild(tDiv1);

				tDiv1 = crEl('DIV');
				if (i==1)
					tDiv1.style.height = p.vHeight + 'px';
				tDiv1.className = p.xWindow.vControlClassName + tp + 'right ' + p.xWindow.vControlClassName + 'filter';
				tDiv.appendChild(tDiv1);

				if (i!=1)
				{
					tDiv1 = crEl('DIV');
					tDiv1.className = p.xWindow.vControlClassName + tp +'center ' + p.xWindow.vControlClassName + 'filter';
					tDiv1.style.width = p.vWidth + 'px';
					tDiv.appendChild(tDiv1);
					if (i==0)
						p.fAddWinControls(tDiv1, wIndex);
				}

				tSpan = crEl('SPAN');
				tSpan.className = p.xWindow.vControlClassName + tp + 'text';
				if (i==0)
				{
					tText = document.createTextNode(p.tWinTitle);
					tSpan.appendChild(tText);
				}
				if (i==1)
				{
					tSpan.style.width = p.vWidth + 'px';
					tSpan.style.height = p.vHeight + 'px';
					p.oWindowList[wIndex]['oContent'] = tSpan;
				}
				tDiv.appendChild(tSpan);

				tMainDiv.appendChild(tDiv);
			}

			tMainDiv.className = p.xWindow.vControlClassName;

			if (p.vAddForm)
			{
				var tForm = crEl('FORM');
				tForm.onsubmit = function ()
				{
					return p.aOnWindowSubmit();
				};
				tForm.appendChild(tMainDiv);
				document.body.appendChild(tForm);
				p.oWindowList[wIndex]['oForm'] = tForm;
			}
			else
			{
				document.body.appendChild(tMainDiv);
			}

			p.oWindowList[wIndex]['oWnd'] = tMainDiv;
			p.vTopElement = tMainDiv;

			if (p.bMultiLang)
			{
				p.oLangTab = new xTabs;
				p.oLangTab.fInit({vIdElement: p.oWindowList[wIndex]['oContent'], vWidth: p.vWidth, vHeight: p.vHeight});

				for (var i in p.vLangs)
				{
					p.oLangTab.fCreateTab({
							vIndex: i,
							vTitle: p.vLangs[i]
						});
				}
				p.oLangTab.fSetActiveTabByIndex(p.vCurrentLang);
			}

			p.fCheckActiveWin(wIndex);

			p.aOnWindowShowEnd(wIndex);
		}
	}
}

xWindow.prototype.fCheckActiveWin = function ()
{
	var tC = document.xWinActive, p = this;
	if (document.xWin && tC >= 0)
	{
		p.oWindowList = document.xWin;
		for (var i in p.oWindowList)
		{
			if (i != tC)
				p.oWindowList[i]['oWnd'].className = p.xWindow.vControlClassName + 'noa_';
			else
				p.oWindowList[i]['oWnd'].className = p.xWindow.vControlClassName;
		}
	}
}

xWindow.prototype.fAddWinControls = function ()
{
	var tA;
	var p = this;

	if (p.vControls['close']['show'])
	{
		tA = crEl('A');
		tA.setAttribute('xBaseClass', p.xWindow.vControlClassName + 'c_close');
		tA.setAttribute('xWindowIndex', arguments[1]);
		tA.className = p.xWindow.vControlClassName + 'c_close';
		if (p.vControls['close']['active'])
		{
			tA.onclick = function ()
			{
				if (p.aOnWindowControlClick(this, 'close'))
				{
					p.fCloseWindow(this.getAttribute('xWindowIndex'));
				}

			}
			p.fAttachOver(tA);
		}
		arguments[0].appendChild(tA);
	}

	if (p.vControls['max']['show'])
	{
		tA = crEl('A');
		tA.setAttribute('xBaseClass', this.xWindow.vControlClassName + 'c_max');
		tA.setAttribute('xWindowIndex', arguments[1]);
		tA.className = this.xWindow.vControlClassName + 'c_max';
		if (p.vControls['max']['active'])
		{
			tA.onclick = function ()
			{
				if (p.aOnWindowControlClick(this, 'max'))
				{
					p.fWinMaximaze(this.getAttribute('xWindowIndex'));
				}

			}
			p.fAttachOver(tA);
		}
		arguments[0].appendChild(tA);
	}

	if (p.vControls['min']['show'])
	{
		tA = crEl('A');
		tA.setAttribute('xBaseClass', p.xWindow.vControlClassName + 'c_min');
		tA.setAttribute('xWindowIndex', arguments[1]);
		tA.className = p.xWindow.vControlClassName + 'c_min';
		if (p.vControls['min']['active'])
		{
			tA.onclick = function ()
			{
				if (p.aOnWindowControlClick(this, 'min'))
				{
					p.fWinMinimaze(this.getAttribute('xWindowIndex'));
				}

			}
			p.fAttachOver(tA);
		}
		arguments[0].appendChild(tA);
	}
}

xWindow.prototype.fWinMaximaze = function ()
{
	//
}

xWindow.prototype.fWinMinimaze = function ()
{
	//
}

xWindow.prototype.fCloseWindow = function ()
{
	var ind = arguments[0], p = this;
	if (p.vNeedPrepareSelect)
		p.fPrepareSelect(true);
	if (p.aOnWindowClose(ind))
	{
		try{
		p.oWindowList[ind]['oWnd'].parentNode.removeChild(this.oWindowList[ind]['oWnd']);
		}catch(e){};
		delete p.oWindowList[ind];
	}
	document.xWin = p.oWindowList;
}

xWindow.prototype.fWinControlOver = function ()
{
	arguments[0].className = arguments[0].getAttribute('xBaseClass') + (arguments[1]?'_over':'');
}

xWindow.prototype.fDragInit = function ()
{
	var p = this;
	if (document.xWinDrag)
		p.vDrag = document.xWinDrag;

	p.vDragIndex = p.vDrag['element'].length + 1;
	p.vDrag['element'][p.vDragIndex] = arguments[0];
	p.vDrag['element'][p.vDragIndex].style.position = 'absolute';
	p.vDrag['element'][p.vDragIndex].setAttribute('xDragIndex', p.vDragIndex);
	p.vDrag['capture'][p.vDragIndex] = arguments[1];
	p.vDrag['capture'][p.vDragIndex].style.cursor = 'move';
	p.vDrag['capture'][p.vDragIndex].setAttribute('xDragIndex', p.vDragIndex);

	p.vDrag['capture'][this.vDragIndex].onmousedown = function ()
	{
		p.fDisableSelections();
		
		if (this.getAttribute('xIndex') > -1)
		{
			document.xWinActive = this.getAttribute('xIndex');
			p.fCheckActiveWin();
		}

		var e = arguments[0];
		if (!e)
			e = event;

		p.vDragActiveIndex = this.getAttribute('xDragIndex');
		p.fDragStart(e, this.getAttribute('xDragIndex'));
		document.xWinDragActiveIndex = p.vDragActiveIndex;
	}

	p.vDrag['capture'][this.vDragIndex].onmouseup = function ()
	{
		p.fEnableSelections();
		var e = arguments[0];
		if (!e)
			e = event;
		p.fDragStop(e, document.xWinDragActiveIndex);
	}

	p.vDrag['capture'][this.vDragIndex].onmousemove = function ()
	{
		var e = arguments[0];
		if (!e)
			e = event;
		p.fDragMove(e, document.xWinDragActiveIndex);
	}

	if (!document.xWinDrag)
	{
		document.body.onmousemove = function ()
		{
			var e = arguments[0];
			if (!e)
				e = event;
			p.fDragMove(e, document.xWinDragActiveIndex);
		}
	}
	document.xWinDrag = this.vDrag;
}

xWindow.prototype.fDragStart = function ()
{
	var e = arguments[0], i = arguments[1], p = this;
	p.vDragActiveIndex = i;
	p.vDrag['deltaX'][i] = e.clientX - p.fGetLeft(p.vDrag['element'][i]);
	p.vDrag['deltaY'][i] = e.clientY - p.fGetTop(p.vDrag['element'][i]);
	p.vfDragActive = true;
	document.xWinDragActive = true;
	document.xWinDragActiveIndex = i;
}

xWindow.prototype.fDragStop = function ()
{
	this.vfDragActive = false;
	document.xWinDragActive = false;
}

xWindow.prototype.fDragMove = function ()
{
	if (document.xWinDragActive)
	{
		var e = arguments[0], i = arguments[1], p = this;
		p.vDrag['element'][i].style.left = e.clientX - p.vDrag['deltaX'][i];
		p.vDrag['element'][i].style.top = e.clientY - p.vDrag['deltaY'][i];
	}
}

xWindow.prototype.aOnExec = function()
{
	var p = this;
	if (arguments[0] == 'error')
	{
		alert(p.vErrorList[p.vErrorIndex]);
		p.fUnLockControl();
	}
	else if (arguments[0] == 'this.fOnLangLoad()')
	{
		if (p.aOnWindowLangLoad())
		{
			for (var i in p.oWindowList)
				if (p.oWindowList[i]['wait_lang'])
				{
					p.oWindowList[i]['wait_lang'] = false;
					p.fShowWin(i);
				}
		};
	}
}

xWindow.prototype.aOnWindowLangLoad = function ()
{
	return true;
}

xWindow.prototype.aOnWindowShow = function ()
{
	return true;
}

xWindow.prototype.aOnWindowControlClick = function ()
{
	return true;
}

xWindow.prototype.aOnWindowClose = function ()
{
	return true;
}

xWindow.prototype.aOnWindowSubmit = function ()
{
	return false;
}

xWindow.prototype.fBlink = function ()
{
	var p = this, tTopElement, tWin;

	if (p.vTopElement)
	{
		tTopElement = p.vTopElement;
		tWin = p;
	}
	else if (p.oWindow.vTopElement)
	{
		tTopElement = p.oWindow.vTopElement;
		tWin = p.oWindow;
	}

	if (tTopElement)
	{
		document.xWinActive = tWin.vWindowIndex;
		tWin.fCheckActiveWin(tWin.tWindowIndex);
		tWin.vBlinkInterval = setInterval(function ()
		{
			if (tTopElement.className != tWin.xWindow.vControlClassName)
			{
				tTopElement.className = tWin.xWindow.vControlClassName;
				tWin.vBlinkCountCurrent++;
			}
			else
				tTopElement.className = tWin.xWindow.vControlClassName + 'noa2_';
			
			if (tWin.vBlinkCountCurrent == tWin.vBlinkCount)
			{
				tWin.vBlinkCountCurrent = 0;
				clearInterval(tWin.vBlinkInterval);
			}
		}
		, tWin.vBlinkFrequency);
	}
}

xWindow.prototype.fDisableSelections = function ()
{
	var d = document;
	if (!d.oDragDropDiv)
	{
		d.oDragDropDiv = crEl('div');
		d.oDragDropDiv.className = this.xWindow.vControlClassName + 'dragdropdiv';
		d.body.appendChild(d.oDragDropDiv);
	}
	d.oDragDropDiv.style.display = '';
}

xWindow.prototype.fEnableSelections = function ()
{
	document.oDragDropDiv.style.display = 'none';
}

xWindow.prototype.aOnWindowShowEnd = function (){}