/*
	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 = [];
		var create = false;
		links.each(function(el, i) {
			if (el.rel && el.href && el.rel.test(/^flashbox/i)) {
				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 (create == true) {
			this.overlay = new Element('div', {'id': 'fbOverlay', 'styles': {'opacity': 0, 'display': 'none'}}).injectInside(document.body);
			this.center  = new Element('div', {'id': 'fbCenter'}).injectInside(this.overlay);
			this.bottomContainer = new Element('div', {'id': 'fbBottomContainer', 'styles': {'display': 'none'}}).injectInside(this.overlay);
			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: 300}).hide()
//				center: center.effects({duration: 300}),
//				bottom: bottom.effects({duration: 300})
			};
		}
		return false;
	}, // init
	open: function(linkHref, linkTitle, linkRel) {
//		var vars = linkRel.match(/[0-9]+/g);
		var vars = linkRel.split(' ');
		var left = (window.getWidth()-vars[1])/2;
		var top = (window.getHeight()-vars[2])/2;
		this.overlay.setStyles({'display': ''});
		this.center.setStyles({'left': left+'px', 'top': top+'px', 'width': parseInt(vars[1])+'px', 'height': parseInt(vars[2])+'px'});
		this.fx.overlay.start(0, 0.8);
//		fx.center.start({'opacity': [0, 1]});
		if (linkTitle || vars[3] == 1) {
			this.caption.innerHTML = linkTitle;
			this.bottomContainer.setStyles({'left': left, 'top': (top + this.center.clientHeight)+'px', 'height': '', 'width': vars[1]+'px', 'display': ''});
//			fx.bottom.start({'margin-top': 0, 'opacity': [0, 1]});
		}
		new Element('div', {'id': 'fbFlashbox'}).injectInside(this.center);
		var flashvars = {}; var params = {}; var attributes = {};
		attributes['id'] = 'flashboxObject';
//		params['wmode'] = 'transparent'; 

		for (i=4; i<=6; i=i+1) 
		{
			if (vars[i] && /^f\[(.+)\]$/i.test(vars[i]))
			{
				var result = /^f\[(.+)\]$/i.exec(vars[i]);
				var f = result[1].split('&');

				for (var item in f)
				{
					if (f[item] && /^(.+)\[(.+)\]$/i.test(f[item]))
					{
						var result = /^(.+)\[(.+)\]$/i.exec(f[item]);
						flashvars[result[1]] = result[2];
					}
				}
			}

			if (vars[i] && /^p\[(.+)\]$/i.test(vars[i]))
			{
				var result = /^p\[(.+)\]$/i.exec(vars[i]);
				var p = result[1].split('&');

				for (var item in p)
				{
					if (p[item] && /^(.+)\[(.+)\]$/i.test(p[item]))
					{
						var result = /^(.+)\[(.+)\]$/i.exec(p[item]);
						params[result[1]] = result[2];
					}
				}
			}
			
			if (vars[i] && /^a\[(.+)\]$/i.test(vars[i]))
			{
				var result = /^a\[(.+)\]$/i.exec(vars[i]);
				var a = result[1].split('&');

				for (var item in a)
				{
					if (a[item] && /^(.+)\[(.+)\]$/i.test(a[item]))
					{
						var result = /^(.+)\[(.+)\]$/i.exec(a[item]);
						attributes[result[1]] = result[2];
					}
				}
			}
		}

		swfobject.embedSWF(linkHref, 'fbFlashbox', vars[1], vars[2], '9.0.0', false, flashvars, params, attributes);
		return false;
	}, // open
	close: function() {
		this.overlay.setStyles({'opacity': 0, 'display': 'none'});
		this.bottomContainer.setStyle('display', 'none');
		this.center.innerHTML = this.caption.innerHTML = '';
		return false;
	} // close
};
window.addEvent('domready', Flashbox.init.bind(Flashbox));