﻿/**
 * the BannerEmbedder class
 * replaces link-element with Flash object specified in href, if required Flash version is present
 *
 * @author Klaas Dieleman, <klaas[AT]efocus.nl>
 * @author Ralph Meeuws, <ralph.meeuws[AT]efocus.nl>
 * @since 13 apr 2010
 * @version 1.1
 * @copyright eFocus
 *
 * @uses jQuery 1.4.2, <http://www.jquery.com>
 * @uses SWFobject 2.2, <http://code.google.com/p/swfobject>
 * @uses FlashDetect 1.0.4, <http://www.featureblend.com>
 *
 * @param Element a-tag with url to movie to be replaced by JWplayer
 * @param String unique id for <object>-element (optional but recommended)
 *
 * @todo streaming options
 */
function BannerEmbedder(elLink, uid){
	
	if(typeof(elLink) == 'object' && FlashDetect.major >= 9) {
		this.elLink = elLink;
		this.uid = uid || 0;

		this.options = {
			'width'		: 320,
			'height'	: 200,
			'url'		: ''
		};
	
		this.initialize();
	} else {
		/**
		* removes the rel attribute of the a-tag
		*/
		elLink.removeAttr('rel');
	}
	
};

BannerEmbedder.prototype = {
	/**
	* intializes player with requested options and inserts it
	*/
	initialize: function() {
	
		this.parseOptions(this.elLink);
		
		if(this.options.url != '') {
			this.options.width = this.elLink.children().first().outerWidth() || this.options.width;
			this.options.height = this.elLink.children().first().outerHeight() || this.options.height;
			
			this.createPlaceholder();
			this.insertFlash();
		};

	},

	/**
	* adds options to given link, reading from specified options in the 'rel'-attribute
	* @param element; link to add options to
	*/
	parseOptions: function(elLink) {
		elLink.linkOptions = {};
		var arrGivenOptions = [];
		var arrParsedOptions = [];

		arrGivenOptions = String(jQuery(elLink).attr('rel')).replace(/ /g, '').split(';');
		arrGivenOptions.splice(0, 1);

		for (i in arrGivenOptions) {
			var strOptionName = String(arrGivenOptions[i]).split('=')[0];
			var strOptionValue = String(arrGivenOptions[i]).split('=')[1];
			arrParsedOptions[strOptionName] = strOptionValue;
		}

		for (i in this.options) {
			if (arrParsedOptions[i] != 'undefined') {

				var blnOptionFormatInvalid = true;

				switch (i) {
					case 'url':
						arrParsedOptions[i] = decodeURIComponent(arrParsedOptions[i]);
						blnOptionFormatInvalid = false;
						break;
					default:
						this.options[i] = this.options[i];
				}

				if (blnOptionFormatInvalid == false) {
					this.options[i] = arrParsedOptions[i];
				}

			} else {
				elLink.linkOptions[i] = this.options[i];
			}
		}
	},

	/**
	* creates placeholder and replaces link element with it
	*/
	createPlaceholder: function() {

		this.container = jQuery('<span></span>');
		this.container.attr('id', ('banner_' + this.uid));
		this.elLink.after(this.container);
		this.elLink.remove();

	},

	/**
	* embeds Flash file on placeholder
	*/
	insertFlash: function() {

		var params = {
			'allowfullscreen': true,
			'allowscriptaccess': 'always'
		};

		var attributes = {
			'id': this.container.attr('id'),
			'name': this.container.attr('id')
		}
		
		swfobject.embedSWF(this.options.url, this.container.attr('id'), this.options.width, this.options.height, '9.0.0', false, false, params, attributes);

	}
}