Function.prototype.inherit = function(parent) {
	var childPrototype = this.prototype;
	this.prototype = new parent();
	this.prototype.parent = parent.prototype;
	for(var property in childPrototype)
		this.prototype[property] = childPrototype[property];
	this.prototype.constructor = this;
}


function uPopup() {

	this.width = 'auto';
	this.height = 'auto';

	this.position = 'fixed';

	this.verticalAlign = 'middle';

	this.popup = false;
	this.content = false;

	this.hover = false;

	this.frameSides =  {

		0: {
			top: -5,
			right: 0,
			left: -8,
			height: 5,
			backgroundX: '-11px',
			backgroundY: '-10px'
		},

		1: {
			top: -5,
			right: -11,
			bottom: 0,
			width: 11,
			backgroundX: 0,
			backgroundY: '-10px'
		},

		2: {
			right: -11,
			bottom: -11,
			left: 0,
			height: 11,
			backgroundX: '100%',
			backgroundY: 0
		},

		3: {
			top: 0,
			bottom: -11,
			left: -8,
			width: 8,
			backgroundX: -11,
			backgroundY: '100%'
		}
	};

	this.imagesURI = 'http://img.66.ru/js/u-popup';
}

uPopup.prototype = {

	toString: function() {
		return 'uPopup';
	},

	init: function(options) {

		if(options) {

			if(typeof options.width != 'undefined')
				this.width = options.width;

			if(typeof options.height != 'undefined')
				this.height = options.height;

			if(typeof options.position != 'undefined')
				this.position = options.position;

			if(typeof options.verticalAlign != 'undefined')
				this.verticalAlign = options.verticalAlign;
		}

		this.draw();
	},

	draw: function() {

		var me = this;

		this.popup = document.createElement('div');

		this.popup.className = 'u-popup';
		this.popup.style.position = this.position;
		if(this.position == 'fixed') {
			if(this.verticalAlign == 'top')
				this.popup.style.top = '4%';
			if(this.verticalAlign == 'middle')
				this.popup.style.top = '50%';
			if(this.verticalAlign == 'bottom')
				this.popup.style.bottom = '4%';
		}
		this.popup.style.left = '50%';
		this.popup.style.zIndex = 999;
		this.popup.style.display = 'none';
		this.popup.style.padding = '20px';
		this.popup.style.backgroundColor = '#FFFFFF';

		if(this.position == 'fixed' && jQuery.browser.msie) {
			if(!document.body.style.backgroundImage) {
				document.body.style.backgroundImage = 'url(\'' + this.imagesURI + '/u-popup-empty.gif\')';
				document.body.style.backgroundAttachment = 'fixed';
			}
			this.popup.style.position = 'absolute';
			if(this.verticalAlign == 'bottom') {
				window.onresize = document.body.onscroll = function() {
					me.popup.style.top = document.body.scrollTop +
					                     document.body.clientHeight -
										 me.popup.scrollHeight -
										 parseInt(document.body.clientHeight / 25) + 'px';
				};

			}
			else {
				if(this.verticalAlign == 'top') {
					var addition = parseInt(document.body.clientHeight / 25);
				}
				if(this.verticalAlign == 'middle') {
					var addition = parseInt(document.body.clientHeight / 2);
				}
				window.onresize = document.body.onscroll = function() {
					me.popup.style.top = document.body.scrollTop +
										 addition + 'px';
				};
			}
		}

		this.content = document.createElement('div');
		this.content.width = this.width;
		this.content.height = this.height;

		jQuery(this.content).appendTo(this.popup);

		var closeButton = document.createElement('button');

		closeButton.style.position = 'absolute';
		closeButton.style.top = '11px';
		closeButton.style.right = '11px';
		closeButton.style.width = '25px';
		closeButton.style.height = '18px';
		closeButton.style.background = 'url(\'' + this.imagesURI + '/u-popup-close-button.png\') no-repeat';
		closeButton.style.border = '0';
		closeButton.style.cursor = 'pointer';

		jQuery(closeButton).click(function() { me.hide(); }).appendTo(this.popup);

		for(var i = 0; i < 4; i++) {

			var frameSide = this.frameSides[i].element = document.createElement('div');

			var frameImage = 'u-popup-frame-shadowed.png';
			if(jQuery.browser.msie && jQuery.browser.version <= 6)
				frameImage = 'u-popup-frame.png';

			frameSide.style.position = 'absolute';
			frameSide.style.backgroundImage = 'url(\'' + this.imagesURI + '/' + frameImage + '\')';
			frameSide.style.backgroundPosition = this.frameSides[i].backgroundX + ' ' +
			                                     this.frameSides[i].backgroundY;
			frameSide.style.lineHeight = '1px';
			frameSide.style.fontSize = '1px';

			if(typeof(this.frameSides[i].width) != 'undefined')
				frameSide.style.width = this.frameSides[i].width + 'px';

			if(typeof(this.frameSides[i].height) != 'undefined')
				frameSide.style.height = this.frameSides[i].height + 'px';

			if(typeof(this.frameSides[i].top) != 'undefined')
				frameSide.style.top = this.frameSides[i].top + 'px';

			if(typeof(this.frameSides[i].right) != 'undefined')
				frameSide.style.right = this.frameSides[i].right + 'px';

			if(typeof(this.frameSides[i].bottom) != 'undefined')
				frameSide.style.bottom = this.frameSides[i].bottom + 'px';

			if(typeof(this.frameSides[i].left) != 'undefined')
				frameSide.style.left = this.frameSides[i].left + 'px';

			jQuery(frameSide).appendTo(this.popup);
		}

		jQuery(this.popup).appendTo(document.body);
		jQuery(this.popup).hover(function() { me.hover = true; },
								 function() { me.hover = false; });
	},

	addContent: function(content) {

		if(!content) return;
		jQuery(this.content).html('').append(content);
		this.refresh();
	},
	
	addContentFromAjax: function(url, vars)
	{
		var req = new JsHttpRequest();
		var obj = this;
		
		jQuery(obj.content).html('<span class="text">Загрузка...</span>');
		obj.refresh();
		
        req.onreadystatechange = function() {
            if (req.readyState == 4){
        		jQuery(obj.content).html(req.responseText);
        		obj.refresh();
            }
        }

        req.open( 'POST', url , true);         
        req.send( vars );    
	},
	

	show: function(content) {

		this.addContent(content);
		this.popup.style.display = 'block';
		if(this.position == 'absolute') {
			if(this.verticalAlign == 'top')
				this.popup.style.top = document.body.scrollTop +
				                       parseInt(document.body.clientHeight / 25) + 'px';
			if(this.verticalAlign == 'middle')
				this.popup.style.top = document.body.scrollTop +
				                       parseInt(document.body.clientHeight / 2) + 'px';
			if(this.verticalAlign == 'bottom')
				this.popup.style.bottom = document.body.scrollTop +
					                      document.body.clientHeight -
										  me.popup.scrollHeight -
										  parseInt(document.body.clientHeight / 25) + 'px';
		}
		
		var me = this;
		setTimeout(function() {
			jQuery(document.body).bind('click.u-popup', function(e) {
				if(!me.hover) me.hide();
			});
		}, 100);
	},
	
	showBG: function(content) {

		this.addContent(content);
		this.popup.style.display = 'block';
		if(this.position == 'absolute') {
			if(this.verticalAlign == 'top')
				this.popup.style.top = document.body.scrollTop +
				                       parseInt(document.body.clientHeight / 25) + 'px';
			if(this.verticalAlign == 'middle')
				this.popup.style.top = document.body.scrollTop +
				                       parseInt(document.body.clientHeight / 2) + 'px';
			if(this.verticalAlign == 'bottom')
				this.popup.style.bottom = document.body.scrollTop +
					                      document.body.clientHeight -
										  me.popup.scrollHeight -
										  parseInt(document.body.clientHeight / 25) + 'px';
		}
	},	

	hide: function() {

		this.popup.style.display = 'none';
		jQuery(document.body).unbind('click.u-popup');
	},

	refresh: function() {

		if(!this.popup) return;

		var currentStyleDisplay = this.popup.style.display;

		if(currentStyleDisplay != 'block') {
			this.popup.style.visibility = 'hidden';
			this.popup.style.display = 'block';
		}

		this.popup.style.marginLeft = Math.floor(this.popup.clientWidth / 2) * -1;

		if(this.verticalAlign == 'middle') {

			this.popup.style.marginTop = Math.floor(this.popup.scrollHeight / 2) * -1;
		}

		if(jQuery.browser.msie) for(var i = 0; i < 4; i++) {

			var frameSide = this.frameSides[i].element;

			if(typeof(this.frameSides[i].top) != 'undefined' &&
			   typeof(this.frameSides[i].bottom) != 'undefined') {

				var height = parseInt(this.popup.scrollHeight);

				if(jQuery.browser.version < 8 && this.popup.scrollHeight % 2 != 0)
					height--;

				var top = 0;

				if(typeof(this.frameSides[i].top) != 'undefined')
					top = this.frameSides[i].top;

				var bottom = 0;

				if(typeof(this.frameSides[i].bottom) != 'undefined')
					bottom = this.frameSides[i].bottom;

				frameSide.style.height = (height - top - bottom) + 'px';
			}

			if(typeof(this.frameSides[i].right) != 'undefined' &&
			   typeof(this.frameSides[i].left) != 'undefined') {

				var width = parseInt(this.popup.clientWidth);

				if(jQuery.browser.version < 8 && this.popup.clientWidth % 2 != 0)
					width--;

				var right = 0;

				if(typeof(this.frameSides[i].right) != 'undefined')
					right = this.frameSides[i].right;

				var left = 0;

				if(typeof(this.frameSides[i].left) != 'undefined')
					left = this.frameSides[i].left;

				frameSide.style.width = (width - right - left) + 'px';
			}
		}

		if(currentStyleDisplay != 'block') {
			this.popup.style.visibility = 'visible';
			this.popup.style.display = 'none';
		}
	}
};