/**
 * @name FFlash.js
 * @desc Affichage d'un élément Flash via JavaScript
 * @author Fofif <fofif@mvh-grafx.com>
 * @modified xpLosIve. <nkdeus.sav@gmail.com>
 * @version 1.3 / 24/03/2008
 * 
 * @param string	path Url du swf
 * @param int		width Longueur du swf
 * @param int		height Hauteur du swf
 * @param object	options Options du swf
 * 
 * @method void	write() write([mixed element]) Ecrit le swf dans element ou à la suite
 * @method void	assign() assign(mixed a, [mixed b]) Rajoute une variable flash 
 * @method mixed	get() get(string v) Renvoie la propriété v de l'objet
 * 
 * <code>
 * var flash = new FFlash('test.swf', 50, 50, {
 * 	params: {
 * 		bgcolor: 'ffffff'
 * 	},
 * 	flashVars: { // Pour ajouter des variables flash
 * 		var1: 'test1',
 * 		var2: 'test2'
 * 	}
 * });
 * flash.assign('var3', 'test3'); // Rajout d'une variable
 * flash.write();
 * </code>
 */

var FFlash = function(path, width, height, conteneur, options) {
	// Les informations de l'élément Flash
	this.options = {
		classid: null,
		type: null,
		data: null,
		id: null,
		width: width,
		height: height,
		base: '',
		params: {
			movie: path,
			pluginurl: 'http://www.adobe.com/go/BPCKN',
			quality: 'best',
			wmode: 'transparent',
			bgcolor: 'transparent',
			menu: 'false',
			scale: 'exactfit',
			allowScriptAccess: 'sameDomain',
			flashVars: ''
		}
	};
	
	// Le code final sera stocké dans cette variable
	this.build = null;
	
	// Ajout de variables flash
	this.assign = function(a, b) {
		if (typeof a == 'string' && typeof b != 'undefined') {
			this.options.params.flashVars += (this.options.params.flashVars == '') ? a+'='+b : '&'+a+'='+b;
		}
		else if (typeof a == 'object') {
			for (var v in a) {
				this.options.params.flashVars += (this.options.params.flashVars == '') ? v+'='+a[v] : '&'+v+'='+a[v];
			}
		}
		else if (typeof a == 'string' && typeof b == 'undefined') {
			this.options.params.flashVars += (this.options.params.flashVars == '') ? a : '&'+a;
		}
	};
	
	this.base = function(a) {
		this.options.base = a;
	}
	
	// Assemblage de deux objets (basé sur Mootools)
	this.merge = function() {
		var ret = {};
		
		for (var i = 0; i < arguments.length; i++) {
			for (var property in arguments[i]) {
				var argP = arguments[i][property];
				var retP = ret[property];
				
				if (retP && typeof argP == 'object' && typeof retP == 'object') { // La propriÃ©tÃ© existe dÃ©jÃ 
					ret[property] = this.merge(retP, argP);
				} else {
					ret[property] = argP;
				}
			}
		}
		
		return ret;
	};
	
	// Creation du code d'affichage du flash
	this.parse = function() {
		// Mise a jour des infos
		this.options = this.merge(this.options, options);
		
		// Ajout des variables flash
		if (typeof options == 'object' && typeof options.flashVars != 'undefined') {
			this.options.flashVars = null;
			this.assign(options.flashVars);
		}
		
		if (window.ActiveXObject) { // IE
			this.options.classid = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
		} else {
			this.options.type = 'application/x-shockwave-flash';
			this.options.data = path;
		}
		
		// Ouverture de balise et écriture des attributs
		this.build = '<object';
		for (var attr in this.options) {
			if (this.options[attr] != null && typeof this.options[attr] != 'object') {
				this.build += ' '+attr+'="'+this.options[attr]+'"';
			}
		}
		if(this.id) this.build += ' id="flash-' + this.id + '"';
		this.build += '>\n';
		
		// Si un URL de base est définie
		if(this.options.base != '') {
			this.build += '\t<param name="base" value="' + this.options.base + '"/>\n';
		}
		
		// Ecriture des balises <param>
		for (var name in this.options.params) {
			this.build += '\t<param name="'+name+'" value="'+this.options.params[name]+'" />\n';
		}
		
		// Fermeture balise
		this.build += '</object>\n';
	};
	
	// Fonction d'écriture du code
	this.write = function(element) {
		this.parse();
		
		if (typeof element !== 'undefined') {
			try {
				if (typeof element == 'string') {
					document.getElementById(element).innerHTML = this.build;
				}
				else {
					element.innerHTML = this.build;
				}
			}
			catch(e) {};
		}
		else {
			document.write(this.build);
		}
	};
	
	// Fonction de retour d'une propriete
	this.get = function(v) {
		if (typeof this.options[v] != 'undefined') {
			return this.options[v];
		}
		else if (typeof this[v] != 'undefined') {
			if (v == 'build') this.parse();
			
			return this[v];
		}
	};
	
	return this;
};