/*
	This popup script by Pascal-berkhahn is a part of pb-embedFlash for Wordpress.
	Released under MIT License.
*/

var FlashPopup = {
	init: function(options) {
		var links = $$('a');
		this.anchors = [];
		links.each(function(el, i) {
			if (el.rel && el.href && el.rel.test(/^popup/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);
		return false;
	}, // init
	open: function(linkHref, linkTitle, linkRel) {
		var vars = linkRel.split(' ');
		var offset = 15;
		var width = parseInt(vars[1]);
		var height = parseInt(vars[2]);
		
		var flashvars = {}; var params = {}; var attributes = {};
		attributes['id'] = 'flashObject';

		for (i=3; i<=5; 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];
					}
				}
			}
		}

		var popup = new Element('div', {'id': 'popupbox', 'styles': {'display': 'none'}}).injectInside(document.body);
		new Element('div', {'id': 'popupcontent'}).injectInside(popup);

		swfobject.embedSWF(linkHref, 'popupcontent', width+'', height+'', '9.0.0', false, flashvars, params, attributes);

		flashpopup = window.open('about:blank', 'flashpopup', 'width='+(width+offset)+',height='+(height+offset)
		+',toolbar=no,menubar=no,location=no,status=no,scrollbars=no,resizable=yes,screenX='+((screen.width-(width+offset))/2)+',screenY='+((screen.height-(height+offset))/2));
		flashpopup.document.write('<html><head><title>'+linkTitle+'</title></head><body style="text-align:left;vertical-align:top;">'+document.getElementById('popupbox').innerHTML+'</body></html>');

		document.body.removeChild(document.getElementById('popupbox'));

		return false;
	} // open
};
window.addEvent('domready', FlashPopup.init.bind(FlashPopup));