var Popup = Class.create({
	initialize : function(container,options) {
		if((typeof container === 'string') && $$(container).size()==0) { return; }
		this.options = Object.extend({
			actor : '.popup-header',
			onEvent : 'click',
			animate : false,
			tab : false,
			position : 'absolute',
			closer : null
		}, options || {});

		this.container = (typeof container=='object') ? $(container) : $$(container)[0];
		this.actor = $$(this.options.actor)[0];
		this.closer = $$(this.options.closer)[0];
		
		this.height = this.container.getHeight();
		this.width = this.container.getWidth();
		
		if (this.options.tab) {
			this.container.setStyle({
				width: this.actor.getWidth() + "px",
				height: this.actor.getHeight() + "px",
				overflow: 'hidden'
			})
		} else {
			this.container.hide();	
		}
		
		this.position();
		Event.observe(this.actor,this.options.onEvent,this.action.bind(this),false)
		if(this.closer!=null) {
			Event.observe(this.closer,this.options.onEvent,this.onClose.bind(this),false)	
		}
	},
	position : function() {
		if (this.options.position == 'absolute') {
			Popup.Utilities.makeAbsolute(this.container)
		} else {
			Popup.Utilities.makeRelative(this.actor,this.container)
		}
	},
	animate : function(_scale,options) {
		new Effect.Scale(this.container, _scale, {
			scaleContent: false,
			scaleX: false,
			scaleMode: {
				originalHeight: this.container.getHeight(),
				originalWidth: this.container.getWidth()
			}
		});
		//console.log(this.container.getHeight())
	},	
	action : function(e) {
		Event.stop(e);
		if (!this.container.hasClassName('popup-expanded')) {
			this.onOpen(e);
		} else {
			this.onClose(e);
		}
	},
	attachClass : function (_class) {
		var _className = _class || 'popup-expanded';
		this.container.addClassName(_className);	
	},
	removeClass : function (_class) {
		var _className = _class || 'popup-expanded';
		this.container.removeClassName('popup-expanded');
	},
	onOpen : function() {
		if (this.options.animate) {
			var _scaleUp = (this.height / this.actor.getHeight()) * 100;
			this.animate(_scaleUp);
		} else if(this.options.tab) {
			this.container.setStyle({
				width: this.width + "px",
				height: this.height + "px"
			});
		} else {
			this.container.show();
		}
		this.attachClass('popup-expanded');
	},
	onClose : function(e) {
		Event.stop(e);
		if (this.options.animate) {
			var _scaleDown = ((this.actor.getHeight() * 100) / this.height);
			this.animate(_scaleDown);
		} else if(this.options.tab) {
			this.container.setStyle({
				width: this.actor.getWidth() + "px",
				height: this.actor.getHeight() + "px"
			})
		} else {
			this.container.hide();
		}
		this.removeClass('popup-expanded');
	}
});
Popup.Utilities = {
	makeAbsolute : function(element) {
		if(element.getStyle('position')=='absolute') return;
		
		var offset = element.positionedOffset();
		var left = offset.left;
		var top = offset.top;
		var height = element.getHeight();
		
		element.style.position = 'absolute';
		element.setStyle({
			bottom : element.getOffsetParent().getHeight() - (top+height) +"px",
			left : left + "px"
		})
		
		return element;
	},
	makeRelative : function(actor,element) {
		if(element.getStyle('position')!='absolute') element.absolutize();
		Element.clonePosition(element, actor, {
			offsetLeft: parseInt(-(element.getWidth()-actor.getWidth())),
			/*offsetTop: (actor.cumulativeOffset().top + element.getHeight())<$(document.body).getHeight() ? 0 : (-element.getHeight()+actor.getHeight()),*/
			offsetTop: 0,
			setWidth: false,
			setHeight: false
		})
	}
}
