﻿/**
 * the FlashEmbedder class
 * replaces link-element with Flash object specified in href, if required Flash version is present
 *
 * @author Klaas Dieleman, <klaas[AT]efocus.nl>
 * @since dec 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 Flash file to be replaced by Flash file
 * @param String unique id for <object>-element (optional but recommended)
 *
 */
function FlashEmbedder(elLink, uid){
	
	if(typeof(elLink) == 'object' && FlashDetect.major >= 9) {
		this.elLink = elLink;
		this.uid = uid || 0;
		
		this.options = {
			width		: 320,
			height		: 200,
			flashfile	: ''
		};
	
		this.initialize();
	} else {
		/**
		* removes the a-tag which wraps the image
		*/
		elLink.children().first().unwrap();
	}
	
}

FlashEmbedder.prototype = {
		
	/**
	 * intializes player with requested options and inserts it
	 */
	initialize: function() {

		this.options.width = this.elLink.children().first().outerWidth() || this.options.width;
		this.options.height = this.elLink.children().first().outerHeight() || this.options.height;
		this.options.flashfile = jQuery(this.elLink).attr('href') || this.options.flashfile;
		
		this.createPlaceholder();
		this.insertFlash();
			
    },
    
 	/**
     * creates placeholder and replaces link element with it
     */
	createPlaceholder: function() {
	
		this.container = jQuery('<span></span>');
		this.container.attr('id', ('flashfile_' + this.uid));
		this.elLink.after(this.container);
		this.elLink.remove();
		
	},
	
	/**
     * embeds Flash file on placeholder
     */
	insertFlash: function() {
	
		var params = {
			'allowfullscreen'	: true,
			'allowscriptaccess'	: 'always',
			'wmode'				: 'transparent',
			'scale'				: 'ExactFit'
		};
		
		var attributes = {
			'id'				: this.container.attr('id'),
			'name'				: this.container.attr('id')
		}

		swfobject.embedSWF(this.options.flashfile, this.container.attr('id'), this.options.width, this.options.height, '9.0.0', false, false, params, attributes);
		
	}
}

