Popup = function(aeItems) {
	this.ePage = $('#page');
	this.ePopup = this.ePage.find("div.popup");
	this.eClose = this.ePopup.find(".close");
	this.eContent = this.ePopup.find(".content");
	this.eText = this.eContent.find(".text");
	this.aeLinks = aeItems.find('a:not(.clickable)');

	this.init();
}

Popup.prototype = {
	init: function() {
		this.get_click();
		this.prepareDom();
	},

	get_click: function() {
		var oThis = this;

		this.aeLinks.click(function() {
			oThis.dimensions($(this));
			oThis.text_info($(this));
			return false;
		});
	},
	
	prepareDom : function(){
		this.jImage = $("<img />");
		this.eContent.append(this.jImage);
	},
	
   	dimensions: function(oLink) {
		var
			oThis = this,
			oImage = new Image();
		
		oImage.onload = function(){
			var
				popup_width = oImage.width + 92,
				popup_height = oImage.height + 94;
			
			oThis.jImage.attr("src", oImage.src);

			oThis.ePopup[0].style.width = popup_width + 'px';
			oThis.ePopup[0].style.height = popup_height + 'px';
			oThis.position(popup_width, popup_height);
			oThis.show();
		}
		
		oImage.src = oLink[0].href;
	},

	text_info: function(oLink) {
		var ePopup_info = oLink.parents('.wrap').find('.popup_info').clone();

		this.eText.empty().prepend(ePopup_info);
	},

	position: function(popup_width, popup_height) {
		this.ePopup[0].style.marginRight = - popup_width/2 + 'px';

		var
			iScroll = document.documentElement.scrollTop,
			iViewport_height = document.documentElement.clientHeight;

		/* Изъебство для Safari, который не понимает document.documentElement.scrollTop */
		if(iScroll == 0) {
			iScroll = document.body.scrollTop;
		}

		this.ePopup[0].style.top = iScroll + iViewport_height/2 - this.ePopup[0].offsetHeight/2 + 'px';
	},

	popup_drag: function() {
		var that = this;
		
		this.IS_DRAG = false;
		this.MouseX = 0;
		this.MouseY = 0;

		this.ePopup.mousedown(
			function(evt){
				that.start_drag(evt);

				return false;
			}
		);

		$(document)
			.mousemove(
				function(evt){
					return that.drag_handle(evt);
				}
			)
			.mouseup(
				function(){
					that.stop_drag();
				}
			);

		this.eText.mousedown(
			function(evt){
				that.stop_drag();
				evt.stopPropagation();
			}
		);
	},

	start_drag: function(evt) {
		this.startX = evt.pageX;
		this.startY = evt.pageY;

		this.popup_left = this.ePopup[0].offsetLeft;
		this.popup_top = this.ePopup[0].offsetTop;

		this.IS_DRAG = true;
	},

	drag_handle: function(evt) {
		if(this.IS_DRAG) {
			var
				currentX = evt.pageX,
				currentY = evt.pageY,
				deltaX = currentX - this.startX,
				deltaY = currentY - this.startY;

			this.ePopup[0].style.left = this.popup_left + deltaX + 'px';
			this.ePopup[0].style.top = this.popup_top + deltaY + 'px';
		}

		//evt.preventDefault();
		return false;
	},

    stop_drag: function(evt) {
		this.IS_DRAG = false;
	},

	show: function() {
		this.ePopup.removeClass('hidden');
		this.close();
		this.popup_drag();
	},

	close: function() {
		var oThis = this;

		this.eClose.click(function() {
			oThis.ePopup.addClass('hidden');
		});
	}
}