/*
	Flashbox by Pascal-berkhahn is a part of pb-embedFlash for Wordpress and based on Slimbox and Videobox. It uses the mootools framework and the SWFObject 2.0  class.
	Released under MIT License.
*/

var Flashbox = {

	init: function(options){

		var links = $$('a');
		this.anchors = [];
		links.each(function(el, i){
			if (el.rel && el.href && el.rel.test(/^flashbox/i)){
				this.create = true;
					el.addEvent('click', function(e){
						e = new Event(e); e.stop(); this.open (el.href, el.title, el.rel);
					}.bind(this));
					this.anchors.push(el);
				}
			}, this);

		if(this.create){
			this.overlay = new Element('div', {'id': 'fbOverlay', 'styles': {'opacity': 0, 'display': 'none'}}).injectInside(document.body);
			this.center  = new Element('div', {'id': 'fbCenter', 'styles': {'opacity': 0, 'display': 'none'}}).injectInside(document.body);
			this.bottomContainer = new Element('div', {'id': 'fbBottomContainer', 'styles': {'display': 'none'}}).injectInside(document.body);
			this.bottom  = new Element('div', {'id': 'fbBottom'}).injectInside(this.bottomContainer);
			this.caption = new Element('div', {'id': 'fbCaption'}).injectInside(this.bottom);
			new Element('a', {'id': 'fbCloseLink', 'href': '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
			new Element('div', {'styles': {'clear': 'both'}}).injectInside(this.bottom);
			this.fx = {
				overlay: this.overlay.effect('opacity', {duration: 400}).hide(),
				center: this.center.effects({duration: 300}),
				bottom: this.bottom.effects({duration: 300})
			};
		}
	}, // init
	
	open: function(linkHref, linkTitle, linkRel){
		this.vars = linkRel.match(/[0-9]+/g);
		this.left = (window.getWidth()-this.vars[0])/2;
		this.top = (window.getHeight()-this.vars[1])/2;
		
		this.overlay.setStyle('display', '');
		this.center.setStyles({'left': this.left+'px', 'top': this.top+'px', 'width': parseInt(this.vars[0])+'px', 'height': parseInt(this.vars[1])+'px', 'display': ''});
		this.fx.overlay.start(0, 0.7);
		this.fx.center.start({'opacity': [0, 1]});
		if (linkTitle || this.vars[2] == 1){
			this.caption.innerHTML = linkTitle;
			this.bottomContainer.setStyles({'left': this.left, 'top': (this.top + this.center.clientHeight)+'px', 'height': '', 'width': this.vars[0]+'px', 'display': ''});
			this.fx.bottom.start({'margin-top': 0, 'opacity': [0, 0.85]});
		}
		new Element('div', {'id': 'fbFlashbox'}).injectInside(this.center);
		var flashvars = {}; var params = {}; params.wmode = 'transparent'; var attributes = {}; attributes.id = "flashboxObject";
		swfobject.embedSWF(linkHref, 'fbFlashbox', this.vars[0], this.vars[1], '9.0.0', false, flashvars, params, attributes);
	}, // open

	close: function(){
		this.bottomContainer.setStyle('display', 'none');
		this.center.setStyles({'opacity': 0, 'display': 'none'});
		this.center.innerHTML = this.caption.innerHTML = '';
		this.fx.overlay.start(0);
		this.overlay.setStyle('display', 'none');
		return false;
	} // close
};
window.addEvent('domready', Flashbox.init.bind(Flashbox));