$(document).ready( function(){
	window.zWindow = new ZWindow();
} );

//{{{ ZWindow()
function ZWindow( Id )
{
	if ( typeof( Id ) == 'undefined' ) {
		Id = 'zWindow';
	}
	this.jEl = $( '<div class="z-wnd z-wnd-zh" id="'+Id+'" style="display:none"><table class="z-wnd-tbl">'
			+'<tr><td class="z-wnd-tl"><div></div></td><td class="z-wnd-t"><img src="/i/0.gif"/></td><td class="z-wnd-tr"></td></tr>'
			+'<tr><td class="z-wnd-l"></td><td class="z-wnd-c"><div class="z-wnd-body"></div></td><td class="z-wnd-r"></td></tr>'
			+'<tr><td class="z-wnd-bl"></td><td class="z-wnd-b"><img src="/i/0.gif"/></td><td class="z-wnd-br"></td></tr>'
			+'</table></div>'
		).prependTo( document.body ).addClass();
	this.jBody = this.jEl.find( '.z-wnd-body' );
// Способ позиционирования элемента: по левому верхнему углу, по правому верхнему углу и т.д.
	this.pos = ''; // Варианты: tl(по умолчанию), tr, bl, br
// Фиксированное положение на странице
	this.fixed = ''; // Варианты: top, bottom ( считать сверху, считать снизу )
	this.left = 300;
	this.top = 300;
	this.maxHeight = 300;
	this.html = '';
};
//===========================================================================}}}

ZWindow.prototype = {

	show: function( X, Y, Pos, Fixed )
	{
		if ( typeof( Fixed ) == 'undefined' ) {
			Fixed = '';
		}
		if ( this.pos ) {
			this.jEl.removeClass( 'z-wnd-pos-'+this.pos );
		};
		this.pos = Pos ? Pos : '';
		if ( this.pos ) {
			this.jEl.addClass( 'z-wnd-pos-'+this.pos );
		}
		this.left = X;
		this.top = Y;
		this.fixed = Fixed;

		this.jEl.css( { left:'-10000px', top:'-10000px' } );
		this.jEl.show();
		this.tunePosition();
	},
	load: function( Url, Params, OnLoad )
	{
		this.jBody.html('<img src="/i/0.gif" width="25" height="20"/>');
		this.jEl.addClass( 'progress' );
		var me = this;
		if ( typeof( Params ) == 'function' ) {
			this.html = $.get( Url, function( html ) { me.html = html; me.onLoad(); Params(); } );
		}
		else {
			this.html = $.get( Url, Params, function( html ) { me.html = html; me.onLoad(); if ( OnLoad ) OnLoad(); } );
		}
	},
	hide: function()
	{
		this.jEl.hide();
		this.jEl.find( '.confirm-action' ).hide();
		this.obj = null;
	},
	html: function( Html )
	{
		this.jBody.html( Html );
		this.onLoad();
	},
	setClass: function( ClassName )
	{
		if ( this.className == ClassName ) {
			return false;
		}
		if ( this.className ) {
			this.jEl.removeClass( this.className );
		}
		this.jEl.addClass( ClassName );
		this.className = ClassName;
	},
	tunePosition: function( Delayed )
	{
		var top = this.top;
		var left = this.left;

	// Настраиваем размеры
		if ( this.maxHeight && this.jBody[0].offsetHeight > this.maxHeight ) {
			this.jBody.css( 'height', this.maxHeight+'px' );
		}
		else {
			this.jBody.css( 'height', 'auto' );
		}

	// Настраиваем позицию
		if ( this.pos )
		{
		// Для ie6 как всегда всё по-особенному
			if ( $.browser.msie && parseInt( $.browser.version ) < 7 && this.pos.indexOf( 'b' ) >= 0 && this.fixed == 'bottom' ) {
				top = this.top + this.jEl.height();
			}
		// Позиционируем по нижнему краю или по верхнему для случая fixed=bottom
			else if ( this.pos.indexOf( 'b' ) >= 0 && this.fixed != 'bottom' || this.pos.indexOf( 't' ) >= 0 && this.fixed == 'bottom' ) {
				top = this.top - this.jEl.height();
			}
		// Позиционируем по правому краю
			if ( this.pos.indexOf( 'r' ) >= 0 ) {
				left = this.left - this.jEl.width();
			}
		}
	// Если мы вылезаем за края экрана, нужно исправить
		if ( left < 20 ) {
			left = 20;
		}

		var windowWidth = $(window).width();
	// Опера неправильно определяет размер jEl, если окно выходит за рамки экрана. Используем чилдрена
		var elWidth = this.jEl.children().width();
		if ( left + elWidth + 10 > windowWidth ) {
			left = windowWidth - elWidth - 10;
		}

	// Обрабатываем fixed
		if ( this.fixed )
		{
			if ( $.browser.msie && parseInt( $.browser.version ) < 7 )
			{
				if ( this.fixed == 'bottom' )
				{
					this.jEl.css( { left:left+'px', bottom:''} );
					this.jEl[0].style.setExpression( 'top', "document.getElementsByTagName( 'body' )[0].scrollTop + document.getElementsByTagName( 'body' )[0].clientHeight - "+top+" + 'px'" );
				}
				else
				{
					this.jEl.css( { left:left+'px', bottom:''} );
					this.jEl[0].style.setExpression( 'top', "document.getElementsByTagName( 'body' )[0].scrollTop + "+top+" + 'px'" );
				}
				document.recalc && document.recalc( true );
			}
			else
			{
				this.jEl.css( 'position', 'fixed' )
				if ( this.fixed == 'bottom' ) {
					this.jEl.css( { left:left+'px', top:'', bottom:top+'px'} );
				}
				else {
					this.jEl.css( { left:left+'px', top:top+'px', bottom:''} );
				}
			}
		}
		else {
			this.jEl.css( { left:left+'px', top:top+'px', bottom:''} );
		}

		if ( ( $.browser.mozilla ) && ( typeof(Delayed)!='undefined' && Delayed ) )
		{
			var me = this;
			setTimeout( function(){ me.tunePosition( false ); }, 100 );
			return;
		}

	},
	onLoad: function()
	{
		this.jEl.removeClass( 'in-progress' );
		if ( this.pos ) {
			this.jEl.css( { left:'-10000px', top:'-10000px' } );
		}
		this.jBody.html( this.html );
		this.tunePosition( true );
	},
	setAutoClose: function()
	{
		if ( ! this.onDocumentClick )
		{
			var me = this;
			this.onDocumentClick = function( Evt ) {
				var el = Evt.target;
				while ( el )
				{
					if ( el === me.jEl[0] ) {
						return;
					}
					el = el.parentNode;
				}
				me.hide();
			};
			$(document).click( this.onDocumentClick );
		}
	}
};
