/**
*@addtogroup COMPOSANT
*
*Comp_Swf
*Script permettant de gérer les objets SWF.
*Les fonctionnalités relatives aux tests de version du plugin SWF sont une adaptation du Flash Player Detection Kit [http://www.adobe.com/products/flashplayer/download/detection_kit/]
*Le concept, bien que différent, est inspiré de SWFobject [http://blog.deconcept.com/swfobject/].
*
*@author François Béliveau <fbeliveau@mexup-soft.com>
*@copyright 2006 MEXUP <http://www.mexup-soft.com>
*@license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
*@version 0.1
*/
function Comp_Swf()
{
	//singleton
	obj_Pointeur = this.constructor;
	if(obj_Pointeur.obj_Instance === undefined)
	{
		//propriétés
		this.bool_Compatible = null;
		this.array_Requis = [
			document.getElementById,
			document.firstChild,
			document.removeChild
		];

		this.array_VersionPlugin = null;

		obj_Pointeur.obj_Instance = this;
	}
	else
	{
		return obj_Pointeur.obj_Instance;
	}
}

/*------------------------------------------------------------
METHODES PRIVEES
------------------------------------------------------------*/

/**
*check_Compatible
*Spécifit si le navigateur du client est compatible ou non avec le code de la classe.
*Un système de "cache" est utilisé pour stocker le résultat dans une variable.
*Ce qui permet d'appeler autant de fois que l'on souhaite la fonction sans pour autant refaire le test complètement
*
*@return     boolean
*/
Comp_Swf.prototype.check_Compatible = function()
{
	if(this.bool_Compatible !== true && this.bool_Compatible !== false)
	{
		this.bool_Compatible = true;
		try
		{
			var int_NbRequis = this.array_Requis.length - 1;
			for(var int_I = 0; int_I<int_NbRequis; int_I++)
			{
				if(!this.array_Requis[int_I])
				{
					this.bool_Compatible = false;
					break;
				}
			}
		}
		catch(obj_Erreur)
		{
			this.bool_Compatible = false;
		}
	}
	return this.bool_Compatible;
};

/**
*get_VersionControlPluginSwf
*Retourne la version du plugin SWF installé sur le poste client à partir d'un activeX.
*
*@return     array
*/
Comp_Swf.prototype.get_VersionControlPluginSwf = function()
{
	var str_Resultat;
	var obj_ActiveX;
	var e;

	// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry
	try
	{
		// version will be set for 7.X or greater players
		obj_ActiveX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		str_Resultat = obj_ActiveX.GetVariable("$version");
	}catch(e){}

	if(!str_Resultat)
	{
		try
		{
			// version will be set for 6.X players only
			obj_ActiveX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");

			// installed player is some revision of 6.0
			// GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
			// so we have to be careful.

			// default to the first public version
			str_Resultat = "WIN 6,0,21,0";

			// throws if AllowScripAccess does not exist (introduced in 6.0r47)
			obj_ActiveX.AllowScriptAccess = "always";

			// safe to call for 6.0r47 or greater
			str_Resultat = obj_ActiveX.GetVariable("$version");

		}catch(e){}
	}

	if(!str_Resultat)
	{
		try
		{
			// version will be set for 4.X or 5.X player
			obj_ActiveX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			str_Resultat = obj_ActiveX.GetVariable("$version");
		} catch(e){}
	}

	if(!str_Resultat)
	{
		try
		{
			// version will be set for 3.X player
			obj_ActiveX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			str_Resultat = "WIN 3,0,18,0";
		}
		catch(e){}
	}

	if(!str_Resultat)
	{
		try
		{
			// version will be set for 2.X player
			obj_ActiveX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			str_Resultat = "WIN 2,0,0,11";
		}catch(e){str_Resultat = -1;}
	}

	var array_Version = [0,0,0];
	if(str_Resultat !== -1)
	{
		var array_Temp = str_Resultat.split(" ");
		var str_Temp = array_Temp[1];
		array_Version = str_Temp.split(",");
	}

	return array_Version;
};

/**
*exec_StructureHtml
*Execute la création de la structure HTML de l'objet SWF.
*Il semble qu'une construction DOM de la structure ne marche que sur Firefox et IE???
*
*@param     object[element HTML]
*@param     object
*@param     object
*
*@return     void
*/
Comp_Swf.prototype.exec_StructureHtml = function(obj_Cible, obj_Params, obj_Variables)
{
	var str_Structure;

	//on supprime tout ce qu'il y a dans la cible
	while(obj_Cible.firstChild)
	{
		var obj_Noeud = obj_Cible.firstChild;
		obj_Cible.removeChild(obj_Noeud);
	}

	if(navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length)
	{
		str_Structure = "<embed type=\"application/x-shockwave-flash\"";

		//paramètres
		for(str_Param in obj_Params)
		{
			str_Structure += " "+str_Param+"=\""+obj_Params[str_Param]+"\"";
		}

		//variables flash
		str_Variables = "";
		for(str_Variable in obj_Variables)
		{
			if(str_Variables !== "")
			{
				str_Variables += '&';
			}
			str_Variables += str_Variable+"="+obj_Variables[str_Variable];
		}
		if(str_Variables !== "")
		{
			str_Structure += " flashvars=\""+str_Variables+"\"";
		}

		str_Structure += " />";
	}
	else
	{
		str_Structure = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" id=\""+obj_Params["id"]+"\" width=\""+obj_Params["width"]+"\" height=\""+obj_Params["height"]+"\">";
		str_Structure += "<param name=\"movie\" value=\""+obj_Params["src"]+"\" />";
		obj_Params["height"] = null;
		obj_Params["width"] = null;
		obj_Params["id"] = null;
		obj_Params["src"] = null;

		//paramètres
		for(str_Param in obj_Params)
		{
			if(obj_Params[str_Param] !== null)
			{
				str_Structure += "<param name=\""+str_Param+"\" value=\""+obj_Params[str_Param]+"\" />";
			}
		}

		//variables flash
		str_Variables = "";
		for(str_Variable in obj_Variables)
		{
			if(str_Variables !== "")
			{
				str_Variables += '&';
			}
			str_Variables += str_Variable+"="+obj_Variables[str_Variable];
		}
		if(str_Variables !== "")
		{
			str_Structure += "<param name=\"flashvars\" value=\""+str_Variables+"\" />";
		}
	}
	obj_Cible.innerHTML = str_Structure;
};

/*------------------------------------------------------------
METHODES PUBLIQUES
------------------------------------------------------------*/

/**
*get_VersionPluginSwf
*Retourne la version du plugin SWF installé sur le poste client.
*
*@return     string
*/
Comp_Swf.prototype.get_VersionPluginSwf = function()
{
	var str_Description;
	var array_Description;
	var int_VersionMajeur;
	var int_VersionMineur;

	if(this.array_VersionPlugin === null)
	{
		var array_Resultat = [0,0,0];

		if(navigator.plugins !== null && navigator.plugins.length > 0)
		{
			if(navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"])
			{
				if(navigator.plugins["Shockwave Flash 2.0"])
				{
					str_Description = navigator.plugins["Shockwave Flash 2.0"].description;
				}
				else
				{
					str_Description = navigator.plugins["Shockwave Flash"].description;
				}
				array_Description = str_Description.split(" ");

				//version majeur et mineur
				array_Temp = array_Description[2].split(".");
				int_VersionMajeur = array_Temp[0];
				int_VersionMineur = array_Temp[1];

				//version révision
				if(array_Description[3] !== "" )
				{
					array_Temp = array_Description[3].split("r");
				}
				else
				{
					array_Temp = array_Description[4].split("r");
				}
				var int_VersionRevision = array_Temp[1] > 0 ? array_Temp[1] : 0;

				array_Resultat = [int_VersionMajeur, int_VersionMineur, int_VersionRevision];
			}
		}
		else if(navigator.userAgent.toLowerCase().indexOf("webtv/2.6") !== -1)
		{
			array_Resultat = [4, 0, 0]; // MSN/WebTV 2.6 supports Flash 4
		}
		else if(navigator.userAgent.toLowerCase().indexOf("webtv/2.5") !== -1)
		{
			array_Resultat = [3, 0, 0]; // WebTV 2.5 supports Flash 3
		}
		else if(navigator.userAgent.toLowerCase().indexOf("webtv") !== -1)
		{
			array_Resultat = [2, 0, 0]; // older WebTV supports Flash 2
		}
		else
		{
			array_Resultat = this.get_VersionControlPluginSwf();
		}

		this.array_VersionPlugin = array_Resultat;
	}
	return this.array_VersionPlugin;
};

/**
*check_Version
*Spécifit si la version du plugin SWF passé en paramètre est valide par rapport à la version du plugin SWF installé sur le poste client.
*
*@return     boolean
*/
Comp_Swf.prototype.check_Version = function(int_Majeur, int_Mineur, int_Revision)
{
	var array_Version = this.get_VersionPluginSwf();
	var int_VersionMajeur      = array_Version[0];
	var int_VersionMineur      = array_Version[1];
	var int_VersionRevision   = array_Version[2];

	if(int_VersionMajeur === 0 && int_VersionMineur === 0 && int_VersionRevision === 0)
	{
		return false;
	}
	else
	{
		if (int_VersionMajeur > parseFloat(int_Majeur))
		{
			return true;
		}
		else if(int_VersionMajeur === parseFloat(int_Majeur))
		{
			if(int_VersionMineur > parseFloat(int_Mineur))
			{
				return true;
			}
			else if(int_VersionMineur === parseFloat(int_Mineur))
			{
				if (int_VersionRevision >= parseFloat(int_Revision))
				{
					return true;
				}
			}
		}
	}
	return false;
};

/**
*check_Version
*Spécifit si la version du plugin SWF passé en paramètre est valide par rapport à la version du plugin SWF installé sur le poste client.
*
*@param     float
*@param     float
*@param     float
*
*@return     boolean
*/
Comp_Swf.prototype.check_Version = function(float_Majeur, float_Mineur, float_Revision)
{
	var array_Version = this.get_VersionPluginSwf();
	var int_VersionMajeur      = array_Version[0];
	var int_VersionMineur      = array_Version[1];
	var int_VersionRevision   = array_Version[2];

	if(int_VersionMajeur === 0 && int_VersionMineur === 0 && int_VersionRevision === 0)
	{
		return false;
	}
	else
	{
		if (int_VersionMajeur > parseFloat(float_Majeur))
		{
			return true;
		}
		else if(int_VersionMajeur === parseFloat(float_Majeur))
		{
			if(int_VersionMineur > parseFloat(float_Mineur))
			{
				return true;
			}
			else if(int_VersionMineur === parseFloat(float_Mineur))
			{
				if (int_VersionRevision >= parseFloat(float_Revision))
				{
					return true;
				}
			}
		}
	}
	return false;
};

/**
*exec_Initialisation
*Initialise et intègre (si compatible) un objet SWF dans la page HTML.
*
*@param     string
*@param     string
*@param     string
*@param     mixed
*@param     mixed
*@param     object
*
*@return     boolean
*/
Comp_Swf.prototype.exec_Initialisation = function(str_Cible, str_UrlSwf, str_IdSwf, mix_Largeur, mix_Hauteur, obj_Options)
{
	var obj_Version;
	var int_Majeur = 0;
	var int_Mineur = 0;
	var int_Revision = 0;
	var obj_Params;
	var obj_ParamsTemp;
	var obj_Variables;

	if(!this.check_Compatible())
	{
		return;
	}

	var obj_Cible = document.getElementById(str_Cible);
	if(!obj_Cible)
	{
		return;
	}

	//initialisation de la version
	try
	{
		obj_Version = obj_Options.version;
	}catch(e){obj_Version = false;}

	//test de la version
	if(obj_Version)
	{
		int_Majeur = obj_Version.majeur ? obj_Version.majeur : 0;
		int_Mineur = obj_Version.mineur ? obj_Version.mineur : 0;
		int_Revision = obj_Version.revision ? obj_Version.revision : 0;
		if(!this.check_Version(int_Majeur, int_Mineur, int_Revision))
		{
			return;
		}
	}

	//initialisation des paramètres
	try
	{
		obj_Params = obj_Options.parametres;
	}catch(e){obj_Params = {};}

	//on s"assure que les paramètres soit en minuscule
	obj_ParamsTemp = obj_Params;
	obj_Params = {};
	for(var str_Param in obj_ParamsTemp)
	{
		obj_Params[str_Param.toLowerCase()] = obj_ParamsTemp[str_Param];
	}
	obj_Params.src = str_UrlSwf;
	obj_Params.id = str_IdSwf;
	obj_Params.width = mix_Largeur;
	obj_Params.height = mix_Hauteur;
	if(!obj_Params.quality)
	{
		obj_Params.quality = "high";
	}

	try
	{
		obj_Variables = obj_Options.variables;
	}catch(e){obj_Variables = false;}

	this.exec_StructureHtml(obj_Cible, obj_Params, obj_Variables);
};
