/**
 *
 * Copyright (c) 2007 by Matthew Shwartz Design Studio.
 *
 *   -- Crafted with pride by Stephan Florquin <stephan@kreaflux.com>.
 */
new Namespace('Safe.IframePopup', new Class({
	/**
	 * Default options.
	 */
	getOptions: function() {
		return {
			className: 'jsPopup',
			opacity: 1,
			durationOpen: 400,
			durationClose: 400,
			transitionOpen: Fx.Transitions.linear,
			transitionClose: Fx.Transitions.linear,
			closeTxt: 'Close Video',
			height: 380
		};
	},
	/**
	 * Constructor.
	 */
	initialize: function(options) {
		this.setOptions(this.getOptions());
		this.setOptions(options);
		this.popupUrl = this.options.element.href;
		this.options.element.href = 'javascript:;';
		this.options.element.addEvent('click', this.openPopup.bindWithEvent(this));
		this.fx = false;
		this.fx2 = false;
		this.initialHeight = this.options.container.getStyle('height').toInt();
	},
	/**
	 * Opens the popup.
	 */
	openPopup: function(event) {
		if(Safe.IframePopup.openPopup.popupUrl == this.popupUrl) {
			Safe.IframePopup.openPopup.closePopup();
			return;
		}
		if(Safe.IframePopup.openPopup) {
			Safe.IframePopup.openPopup.closePopup();
		}
		this.createNode();
		//this.options.container.setStyle('height', this.options.height + 'px');
		var height = this.options.height;
		if(this.fx) {
			this.fx.stop();
		}
		if(this.fx2) {
			this.fx2.stop();
		}
		this.node.setStyle('left', this.options.container.getLeft() + 'px');
		this.node.setStyle('top', this.options.container.getTop() + 'px');
		this.fx = new Fx.Styles(this.node, {
			duration: this.options.durationOpen,
			transition: this.options.transitionOpen
		});
		this.fx.start({
			width: [0, this.options.container.getSize().size.x],
			height: [0, height],
			//left: [event.page.x, this.options.container.getLeft()],
			//top: [event.page.y, this.options.container.getTop()],
			opacity: [0, this.options.opacity]
		});
		this.fx2 = new Fx.Style(this.options.container, 'height', {
			duration: this.options.durationOpen,
			transition: this.options.transitionOpen
		});
		this.fx2.start(this.options.container.getStyle('height'), height);
		(function() {this.iframe.src = this.popupUrl;}).delay(this.options.durationOpen, this);
		document.body.appendChild(this.node);
		Safe.IframePopup.openPopup = this;
	},
	/**
	 * Closes the popup.
	 */
	closePopup: function(event) {
		this.iframe.remove();
		this.titleBar.remove();
		if(this.fx) {
			this.fx.stop();
		}
		if(this.fx2) {
			this.fx2.stop();
		}
		this.fx = new Fx.Styles(this.node, {
			duration: this.options.durationClose,
			transition: this.options.transitionClose
		});
		this.fx.start({
			//left: [this.options.container.getLeft(), event.page.x],
			//top: [this.options.container.getTop(), event.page.y],
			width: [this.options.container.getSize().size.x, 0],
			height: [this.options.container.getSize().size.y, 0],
			opacity: [this.options.opacity, 0]
		});
		(function() {this.node.remove();}).delay(this.options.durationClose, this);
		this.fx2 = new Fx.Style(this.options.container, 'height', {
			duration: this.options.durationOpen,
			transition: this.options.transitionOpen
		});
		this.fx2.start(this.options.container.getStyle('height'), this.initialHeight);
		(function() {this.iframe.src = this.popupUrl;}).delay(this.options.durationOpen, this);
		document.body.appendChild(this.node);
		Safe.IframePopup.openPopup = false;
		//this.options.container.setStyle('height', 'auto');
	},
	/**
	 * Creates the iframe.
	 */
	createNode: function() {
		this.iframe = $(Lib.Builder.node('iframe', {
			frameBorder: 0,
			scrolling: 'no'
		}));
		this.titleBar = $(Lib.Builder.node('a', this.options.closeTxt));
		this.titleBar.href = 'javascript:;';
		this.titleBar.addEvent('click', this.closePopup.bindWithEvent(this));
		this.node = $(Lib.Builder.node('div', {
			className: this.options.className
			}, [
			this.iframe,
			this.titleBar
		]));
		this.node.setStyle('position', 'absolute');
		this.node.setStyle('width', '1px');
		this.node.setStyle('height', '1px');
	}
}));

Safe.IframePopup.implement(new Options);
Safe.IframePopup.openPopup = false;