var opera = (navigator.userAgent.indexOf('Opera') != -1);
/**
 * @param {window} wnd
 * @return {Integer}
 */
function Wnd(wnd)
{
	this.top = function(){
		return (Bereg.isIE
			? (wnd.document.documentElement ? wnd.document.documentElement.scrollTop : wnd.document.body.scrollTop)
			: wnd.pageYOffset)};
	this.left = function(){
		return (Bereg.isIE
			? (wnd.document.documentElement ? wnd.document.documentElement.scrollLeft : wnd.document.body.scrollLeft)
			: wnd.pageXOffset)};
	this.height = function(){
		return (Bereg.isIE
			? (wnd.document.documentElement ? wnd.document.documentElement.clientHeight : wnd.document.body.clientHeight)
			: wnd.innerHeight)};
	this.width = function(){
		return (Bereg.isIE
			? (wnd.document.documentElement ? wnd.document.documentElement.clientWidth : wnd.document.body.clientWidth)
			: wnd.innerWidth)};
}

function Shadow(color)
{
	var self = this;
	this.isLoading = false;
	this.isVisible = false;
	this.isPreLoading = false;
	this.wnd = new Wnd(window);

	this._shadow_pos = function(){
		this.shadow.style.top = '0px';//this.wnd.top()+'px';
		this.shadow.style.left = '0px';
		this.shadow.style.height = Math.max(
			document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight,
			document.documentElement ? document.documentElement.scrollHeight : document.body.scrollHeight)+'px';
		this.shadow.style.width = Math.max(
			document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth,
			document.documentElement ? document.documentElement.scrollWidth : document.body.scrollWidth)+'px';
	};

	this.close = function()
	{
		document.body.removeChild(this.shadow);
	};

	this._loading_create = function()
	{
		this.loading = document.createElement('img');
		this.loading.id = 'loading';
		this.loading.src = this.preLoading.src;
		this.loading.style.top = Math.round(this.wnd.top() + (this.wnd.height() - this.preLoading.height) / 2) +'px';
		this.loading.style.left = Math.round(this.wnd.left() + (this.wnd.width() - this.preLoading.width) / 2) +'px';
		this.loading.width = this.preLoading.width;
		this.loading.height = this.preLoading.height;
		this.shadow.appendChild(this.loading);
		this.isLoading = true;
	};

	this.loading_visible = function(value)
	{
		if (value)
		{
			if (this.isPreLoading)
				this._loading_create();
			else
				this.preLoading.onload =
					function(){
						self.preLoading.onload = null; //ie
						self._loading_create()};
		}
		else
		{
			if (this.isLoading)
			{
				this.shadow.removeChild(this.loading);
				this.isLoading = false;
			}
		}
	};

	this.visible = function(value)
	{
		if (value)
		{
			this._shadow_pos();
			this.shadow.style.display = '';
		}
		else
			this.shadow.style.display = 'none';
		this.isVisible = value;
	}

	this.shadow = document.createElement('div');
	this.shadow.className = 'shadow';
	this.shadow.style.background = color;
	document.body.appendChild(this.shadow);

	this.preLoading = new Image();
	this.preLoading.onload = function(){
		self.preLoading.onload = null; //ie
		self.isPreLoading = true};
	this.preLoading.src = '/imgs/loading.gif';

	attach_event(window, 'resize', function(){
		if (self.isVisible)
			self._shadow_pos()});
}

function Dialog2(url)
{
	var self = this;
	var DLG_CLOSE = false;
	this.shadow = new Shadow('#24396a');
	this.shadow.visible(true);
	this.shadow.loading_visible(true);

	this.close = function()
	{
		if (DLG_CLOSE)
			document.body.removeChild(this.btnClose);
		document.body.removeChild(this.pdialog);
		this.shadow.close();
	};

	this.set_close = function(value)
	{
		if (DLG_CLOSE === value) return;
		if (value)
		{
			this.btnClose = document.createElement('img');
			this.btnClose.id = 'btnClose';
			this.btnClose.style.position = 'absolute';
			this.btnClose.style.cursor = 'pointer';
			this.btnClose.src = '/imgs/btnClose.png';
			this.btnClose.style.left = (this.shadow.wnd.left()+this.shadow.wnd.width()-50)+'px';
			this.btnClose.style.top = (this.shadow.wnd.top()+this.shadow.wnd.height()-40)+'px';
			this.btnClose.width = 21;
			this.btnClose.height = 21;
			document.body.appendChild(this.btnClose);
			this.shadow.shadow.onclick = function(){self.close()};
			this.btnClose.onclick = function(){self.close()};
		}
		else
		{
			document.body.removeChild(this.btnClose);
			this.shadow.shadow.onclick = null;
			this.btnClose.onclick = null;
		}
		DLG_CLOSE = value;
	}

	this.pdialog = document.createElement('div');
	this.pdialog.id = 'pdialog';
	this.dialog = document.createElement('iframe');
	this.dialog.id = 'dialog';
	this.dialog.frameBorder = '0';
	document.body.appendChild(this.pdialog);
	this.pdialog.appendChild(this.dialog);

	this.dialog.contentWindow.location.href = url;
	attach_event(this.dialog, 'load',
		function(){self.shadow.loading_visible(false)});
}

function Dialog()
{
	var self = this;
	this.shadow = new Shadow('#24396a');
	this.shadow.visible(true);
	this.shadow.loading_visible(true);
	this.styles = [];
	this.IS_PAGE = false;

	this.close = function()
	{
		document.body.removeChild(this.btnClose);
		document.body.removeChild(this.pdialog);
		this.disable.close();
		this.shadow.close();
		if (this.IS_PAGE)
		{
			for (var i=0; i<=1; i++)
				document.getElementsByTagName('head')[0].removeChild(this.styles[i]);
			this.IS_PAGE = false;
		}
	};

	this.set_page = function(value)
	{
		var img = 0;
		var imgs = value.page.match(/\<img[^\>]+\>/ig);
		if ((imgs === null) || (imgs.length === 0))
			this._set_page(value);
		else
		{
			img = imgs.length;
			for (var i=0; i<imgs.length; i++)
			{
				var src = imgs[i].match(/src="(.+?)"/i)[1];
				imgs[i] = new Image();
				imgs[i].onload = function(){
					img--;
					this.onload = null; //ie. this=Image
					if (img === 0)
					{
						self._set_page(value);
					}
				}
				imgs[i].src = src;
			}
		}
	}

	this._set_page = function(value)
	{
		if (this.IS_PAGE)
		{
			this.disable.loading_visible(false);
			this.disable.visible(false);
			//this.pdialog.style.top = '10000px';//прячем
			this.pdialog.style.visibility = 'hidden';//прячем
			this.dialog.innerHTML =  '';
			for (var i=0; i<=1; i++)
				document.getElementsByTagName('head')[0].removeChild(this.styles[i]);
			this.IS_PAGE = false;
		}
		else
			this.shadow.loading_visible(false);

		for (var i=0; i<=1; i++)
		{
			//this.styles[i] = document.createElement('link');
			//this.styles[i].rel = 'stylesheet';
			this.styles[i] = document.createElement('style');
			this.styles[i].type = 'text/css';
			this.styles[i].media = ((i===0) ? 'screen' : 'print');
			//this.styles[i].href = '/index.html?index=css&css='+value.css+'&media='+((i===0) ? 'screen' : 'print');
			if (Bereg.isIE)
				this.styles[i].styleSheet.cssText = ((i===0) ? value.cssScreen : value.cssPrint);
			else
			{
				var css = document.createTextNode(((i===0) ? value.cssScreen : value.cssPrint));
				this.styles[i].appendChild(css);
			}
			document.getElementsByTagName('head')[0].appendChild(this.styles[i]);
		}

		var scripts = value.page.match(/\<script[^\>]+\>[^\x00]*?\<\/script\>/gi);
		if (scripts === null)
			this.dialog.innerHTML = value.page;
		else
		{
			value.page = value.page.replace(/\<script[^\>]+\>[^\x00]*?\<\/script\>/gi, '');
			this.dialog.innerHTML = value.page;
			for (var i=0; i<scripts.length; i++)
			{
				var script = document.createElement('script');
				script.type = 'text/javascript';
				if (/src=".+?"/.test(scripts[i]))
					script.src = scripts[i].match(/src="(.+?)"/i)[1];
				else
					script.text = scripts[i].match(/\<script[^\>]+\>([^\x00]*?)\<\/script\>/i)[1];
				this.dialog.appendChild(script);
			}
		}

		this.resize(
			(typeof(value.dlgWidth) === 'undefined') ? 0 : value.dlgWidth,
			(typeof(value.dlgHeight) === 'undefined') ? 0 : value.dlgHeight);
		this.IS_PAGE = true;
	}

	this.set_close = function(value)
	{
		if (value)
		{
			this.btnClose.style.visibility = 'visible';
			this.shadow.shadow.onclick = function(){self.close()};
			this.btnClose.onclick = function(){self.close()};
		}
		else
		{
			this.btnClose.style.visibility = 'hidden';
			this.shadow.shadow.onclick = null;
			this.btnClose.onclick = null;
		}
	}

	this.resize = function(aWidth, aHeight)
	{
		var wnd = new Wnd(window);
		var wh = [];
		wh['d_height'] = aHeight;
		this.dialog.style.width = (aWidth == '0') ? 'auto' : aWidth+'px';

		aWidth = this.dialog.offsetWidth + (this.pdialog.offsetWidth - this.pdialog.clientWidth);
		aHeight = this.dialog.offsetHeight + (this.pdialog.offsetHeight - this.pdialog.clientHeight);
		wh['height'] = ((wh['d_height'] == '0') ? aHeight : wh['d_height']);
		this.pdialog.style.left = Math.max(wnd.left(), Math.round(wnd.left() + (wnd.width() - aWidth) / 2)) + 'px';
		this.pdialog.style.top = Math.max(wnd.top(), Math.round(wnd.top() + (wnd.height() - wh['height']) / 2)) + 'px';
		this.pdialog.style.height = ((wh['d_height'] == '0') ? 'auto' : wh['height'] + 'px');
		this.pdialog.style.visibility = 'visible';//прячем
	}

	this.btnClose = document.createElement('img');
	//this.btnClose.id = 'btnClose';
	this.btnClose.style.position = 'absolute';
	this.btnClose.style.cursor = 'pointer';
	this.btnClose.src = '/imgs/btnClose.png';
	this.btnClose.style.left = (this.shadow.wnd.left()+this.shadow.wnd.width()-50)+'px';
	this.btnClose.style.top = (this.shadow.wnd.top()+this.shadow.wnd.height()-40)+'px';
	this.btnClose.width = 21;
	this.btnClose.height = 21;
	this.btnClose.style.visibility = 'hidden';
	document.body.appendChild(this.btnClose);

	this.pdialog = document.createElement('div');
	this.pdialog.id = 'pdialog';
	//this.pdialog.style.top = '10000px';//прячем
	this.pdialog.style.visibility = 'hidden';//прячем
	document.body.appendChild(this.pdialog);

	this.dialog = document.createElement('div');
	this.dialog.id = 'dlg';
	this.dialog.style.border = '3px double #24396a';
	this.dialog.style.padding = '10px';
	this.pdialog.appendChild(this.dialog);

	this.disable = new Shadow('#e0e0e0');
}

function dialog_submit()
{
	if (Bereg.isIE6) parent.document.body.focus();
	var shadow = new Shadow('#e0e0e0');
	shadow.visible(true);
	return shadow;
}

function dialog_close()
{
	if (Bereg.isIE6) parent.document.body.focus();
	parent.Bereg.dialog.close();
}

function dialog_size(wxh)
{
	var wnd = new Wnd(parent);
	var pdialog = parent.document.getElementById('pdialog');
	var dialog = pdialog.firstChild;//parent.document.getElementById('dialog');
	if (pdialog && dialog)
	{
		var el = document.getElementById('main');
		if (el)
		{
			var wh = wxh.match(/\d+/ig);
			wh['d_height'] = wh[1];
			if (wh[0] != '0')
				el.style.width = wh[0]+'px';

			wh[0] = el.offsetWidth + 20 - (Bereg.isIE6 ? 3 : 0);
			wh[1] = el.offsetHeight + 20 - (Bereg.isIE6 ? 3 : 0);

			pdialog.style.top = '10000px';//прячем
			dialog.style.width = wh[0] + 'px';
			dialog.style.height = wh[1] + 'px';

			wh[0] = wh[0] + (pdialog.offsetWidth - pdialog.clientWidth);
			wh[1] = wh[1] + (pdialog.offsetHeight - pdialog.clientHeight);
			wh['height'] = ((wh['d_height'] == '0') ? wh[1] : wh['d_height']);
			pdialog.style.left = Math.max(wnd.left(), Math.round(wnd.left() + (wnd.width() - (wh[0])) / 2)) + 'px';
			pdialog.style.top = Math.max(wnd.top(), Math.round(wnd.top() + (wnd.height() - wh['height']) / 2)) + 'px';
			pdialog.style.height = ((wh['d_height'] == '0') ? 'auto' : wh['height'] + 'px');

			window.onunload = function() {
				pdialog.style.top = '10000px';
			}
		}
	}
}
