﻿var Floatup = Class.create();
Floatup.prototype = {
	
	initialize : function (el, ancor) {
		
		// private constants
		this.CLOSE_EL_TAGNAME	= 'a';
		this.CLOSE_EL_CLASSNAME = 'closeEl';
		
		// private members
		this.element	= null;
		this.ancor		= null;
		this.closeEl	= null;
		
		
		// public events
		this.onFloat		= null;
		this.onBeforeFloat	= null;
		this.onHide			= null;
		
		
		// ctor
		
		this.element = $(el);
		this.ancor = $(ancor);
		
		var divs = this.element.getElementsByTagName(this.CLOSE_EL_TAGNAME);
		
		if (divs.length < 1)
			throw new Error('Ошибка инициализации всплывающего элемента');
			
		for (var i = 0; i < divs.length; i++)
			if ($(divs[i]).hasClassName(this.CLOSE_EL_CLASSNAME))
				if (this.closeEl == null)
					this.closeEl = $(divs[i]);
				else throw new Error('Ошибка инициализации всплывающего элемента');
		
		if (this.element == null)
			throw new Error('Ошибка инициализации всплывающего элемента');
		
		if (this.ancor == null)
			throw new Error('Ошибка инициализации всплывающего элемента');

		if (this.closeEl == null)
			throw new Error('Ошибка инициализации всплывающего элемента');

		this.addBehavior();	
	
	},
	


	// private methods
	
	addBehavior : function () {
		this.ancor.observe('click', this.pop.bindAsEventListener(this));
		this.closeEl.observe('click', this.hide.bindAsEventListener(this));
	},
	
	
	// public methods
	
	pop : function (e) {	
		if (this.onBeforeFloat)
			this.onBeforeFloat();

		this.element.style.visibility = 'visible';

		if (this.onFloat)
			this.onFloat();
		
		if (e)
			e.stop()
	},
	
	hide : function (e) {
		this.element.style.visibility = 'hidden';

		if (this.onHide)
				this.onHide();

		if (e)
			e.stop();
	}
	
}