/*
* Popup
* ----------------------
* @last_modified: 2007-07-18
* @author: Filatov Dmitry <alpha@design.ru>
*/

function Popup(oElement) {

	Popup.baseConstructor.call(this);

	this.oElement = oElement;
	this.oCurrentParentElement = oElement.parentNode;
	
	this.oContentElement = Common.Dom.getElementsByClassName(
		oElement,
		Popup.CLASS_NAME_CONTENT		
		)[0];
		
	this.aCloserElements = Common.Dom.getElementsByClassName(
		oElement,
		Popup.CLASS_NAME_CLOSER		
		);
		
	this.bOpened = false;	
	
	this.init();

	Popup.aInstances.push(this);
		
}

Popup.CLASS_NAME_CONTENT   = 'content-inner';
Popup.CLASS_NAME_CLOSER    = 'closer';
Popup.CLASS_NAME_INVISIBLE = 'invisible';
Popup.CLASS_NAME_TOP       = 'top';

Popup.EVENT_TYPE_ON_CLOSE  = 'onclose';


Popup.aInstances = [];
Popup.iTopIndex = -1;


Popup.inheritFrom(
	Observable,
	{

		init : function() {
		
			var oThis = this;
		
			Common.Event.add(
				this.aCloserElements.concat([document]),
				'click',
				function() {
				
					oThis.close();

				}
				);
			
			Common.Event.add(
				this.oElement,
				'click',
				function(oEvent) {
				
					var oEvent = Common.Event.normalize(oEvent);
				
					oEvent.cancelBubble = true;

					oThis.top();
				
				}
				);
					
		},
	
		isOpened : function() {
		
			return this.bOpened;
		
		},
	
		setContent : function(sContent) {
		
			this.oContentElement.innerHTML = sContent;
		
		},
	
		move : function(
			sStyle,
			oParentElement
			) {
			
			if(oParentElement && this.oElement.parentNode != oParentElement) {
			
				Common.Class.remove(
					this.oCurrentParentElement,
					Popup.CLASS_NAME_TOP
					);
			
				this.oElement = oParentElement.appendChild(this.oElement.parentNode.removeChild(this.oElement));
			
				this.oCurrentParentElement = oParentElement;
			
				Common.Class.add(
					this.oCurrentParentElement,
					Popup.CLASS_NAME_TOP
					);
			
			}
		
			if(sStyle) {
				Common.Dom.setStyle(
					this.oElement,
					sStyle
					);
			}
			
		},

		top : function() {

			if(Popup.iTopIndex > -1) {
				Popup.aInstances[Popup.iTopIndex].removeClass(Popup.CLASS_NAME_TOP);
			}

			this.addClass(Popup.CLASS_NAME_TOP);

			Popup.iTopIndex = Popup.aInstances.indexOf(this);

		},
	
		open : function() {
		
			this.bOpened = true;
		
			this.removeClass(Popup.CLASS_NAME_INVISIBLE);
		
		},
	
		close : function() {
		
			this.addClass(Popup.CLASS_NAME_INVISIBLE);
			
			this.bOpened = false;

			this.notify(
				Popup.EVENT_TYPE_ON_CLOSE,
				this
				);
		
		},
	
		toggle : function() {
		
			if(this.isOpened()) {
				this.close();
			}
			else {
				this.open();
			}
		
		},

		addClass : function(sClassName) {

			Common.Class.add(
				this.oElement,
				sClassName
				);

		},

		removeClass : function(sClassName) {

			Common.Class.remove(
				this.oElement,
				sClassName
				);

		}

	}	
	);