﻿var PopHandler = Class.create();
PopHandler.prototype = {

	initialize : function (popContainer) {

		// const
		this.MAX_Y_RATE = 0.8;
		
		
		// private members
		this.holder		= null;
		this.popup		= null;
		this.content	= null;
		this.closeBtn	= null;
		
		
		// ctor
		
		if (!popContainer)
			throw new Error('Не найден контейнер всплывающего окна');

		this.holder = $(popContainer.id);
		
		this.popup = $('popup');
		if (!this.popup)
			throw new Error('Ошибка инициализации менеджера всплывающих окон');
			
		this.content = $('pop-content');
		if (!this.content)
			throw new Error('Ошибка инициализации менеджера всплывающих окон');
			
		this.closeBtn = $('pop-close');
		if (!this.closeBtn)
			throw new Error('Ошибка инициализации менеджера всплывающих окон');
			
		this.addBehavior();
	},
	
	
	// private methods
	
	addBehavior : function () {
		if (this.closeBtn.observe)
			this.closeBtn.observe('click', this.hidePop.bind(this, true));
	},
	
	hidePop : function (stop) {
		$(this.holder.id).style.visibility = 'hidden';
		if (stop && event) Event.stop(event);

		// hot fix :
		$('body').removeClassName('no-print');
		$('cellar').removeClassName('no-print');
	},
	
	pop : function () {
		this.validateHeight();
		this.centerView();
		
		// hot fix :
		$('body').addClassName('no-print');
		$('cellar').addClassName('no-print');
		
		$(this.holder.id).style.visibility = 'visible';
		event.stop();
	},
	
	validateHeight : function () {
		if (this.popup.clientHeight > Math.floor($(this.holder.id).clientHeight * this.MAX_Y_RATE))
		{
			$(this.popup.id).style.overflowX = 'hidden';
			$(this.popup.id).style.overflowY = 'scroll';
			$(this.popup.id).style.height = Math.floor($(this.holder.id).clientHeight * this.MAX_Y_RATE) + 'px';
		}
	},
	
	centerView : function () {
		$(this.popup.id).style.top = Math.floor(($(this.holder.id).clientHeight - $(this.popup.id).clientHeight) / 2) + 'px';
	},

	
	// public methods
	
	handleRef : function (ref) {
		var delim = (ref.href.indexOf('?') == -1)?'?':'&';
		
		new Ajax.Request(ref.href+delim+'action=info&x='+Math.random(),
			{
				method : 'get',
				onSuccess : this.responseHandler.bind(this),
				onFailure : function (transport) { alert('Ошибка запроса'); }
			}
		);
		
		if (event)
			Event.stop(event);
	},
	
	handleClick : function (href) {
		new Ajax.Request(href+'&action=info&x='+Math.random(),
			{
				method : 'get',
				onSuccess : this.responseHandler.bind(this),
				onFailure : function (transport) { alert('Ошибка запроса'); }
			}
		);

		if (event)
			Event.stop(event);
	},
		
	responseHandler : function (transport) {
		$(this.content.id).innerHTML = transport.responseText;

		var pr = $('printBtn');
		if (pr)
			pr.onclick = function () {
				window.print();

				var e = event;
				if (e) {
					if (window.event)
						e.returnValue = false;
					else 
						e.preventDefault();
				}

				e.cancelBubble = true
				if (e.stopPropagation) e.stopPropagation();

				return false;
			}
			
		this.pop();
	}

}