/**************************************************************************************
  Description: Class for the embedding and detection of Macromedia Flash.
		 Author: Roarke Lynch
		   Date: 19-Apr-05
		  Notes: 
   Dependencies: Browser.js
**************************************************************************************/

/* Constructor */
Flash = function(swf, width, height, version)
{
    this.swf = swf || "";
    this.width = width || "100%";
    this.height = height || "100%";
    this.requiredVersion = version || 6;
    this.id = "";
    this.params = new Object();
    this.variables = new Object();
};

/* Static Members */
Flash.isInstalled = function(minVersion)
{
	if( (typeof Browser) == 'undefined' ) {
		alert("***Browser.js not found****");
		return;
	}
	var v = ( (typeof minVersion) == 'undefined' ? 4 : minVersion);
	return Browser.detectControl("Shockwave Flash", "ShockwaveFlash.ShockwaveFlash." + v, v);
};

/* Flash Embed */
Flash.embed = function (swf_url, swf_width, swf_height, params, variables, parent_id) 
{
    var player = new Flash(swf_url, swf_width, swf_height);
    
    for(var i in params) {
        player.setParam(i, params[i]);
    }
    for(var i in variables) {
        player.setVariable(i, variables[i]);
    }
    
	try {
		document.write(player);
    }
    catch(e) {
        var canvas = document.getElementById(parent_id);
        var playerElement = player.toXmlElement();
        canvas.appendChild(playerElement);
    }
};

/* Instance Members */
Flash.prototype.getSWF = function() 
{ 
	return this.swf; 
};

Flash.prototype.setSWF = function(swf) 
{
  this.swf = swf;
};

Flash.prototype.getWidth = function()
{
    return this.width;
};

Flash.prototype.setWidth = function(w)
{
    this.width = w;
};

Flash.prototype.getHeight = function()
{
    return this.height;
};

Flash.prototype.setHeight = function(h)
{
    this.height = h;
};

Flash.prototype.getRequiredVersion = function()
{
    return this.requiredVersion;
};

Flash.prototype.setRequiredVersion = function(v)
{
    this.requiredVersion = v;
};

Flash.prototype.getId = function()
{
    return this.id;
};

Flash.prototype.setId = function(id)
{
    this.id = id;
};

Flash.prototype.getParam = function(name)
{
    return this.params[name];
};

Flash.prototype.getParams = function()
{
    return this.params;
};

Flash.prototype.setParam = function(name, value)
{
    this.params[name] = value;
};

Flash.prototype.getVariable = function(name)
{
    return this.variables[name];
};

Flash.prototype.getVariables = function()
{
    return this.variables;
};

Flash.prototype.setVariable = function(name, value)
{
    this.variables[name] = value;
};

Flash.prototype.getVariablePairs = function()
{
    var variablePairs = new Array();
    for (var name in this.getVariables()) {
        variablePairs.push(name + "=" + escape(this.getVariable(name)));
    }
    if (variablePairs.length > 0) {
        return variablePairs.join("&");
    }
    else {
        return null;
    }
};

Flash.prototype.getParamTags = function()
{
    var paramTags = "";
    for (var param in this.getParams()) {
        paramTags += '<param name="' + param + '" value="' + this.getParam(param) + '" />';
    }
    if (paramTags == "") {
        paramTags = null;
    }
    return paramTags;
};

Flash.prototype.toString = function()
{
    var flashHTML = "";
    if (window.ActiveXObject && navigator.userAgent.indexOf('Mac') == -1) { // PC IE
        flashHTML += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + this.getWidth() + '" height="' + this.getHeight() + '" id="' + this.getId() + '">';
        flashHTML += '<param name="movie" value="' + this.getSWF() + '" />';
        if (this.getParamTags() != null) {
            flashHTML += this.getParamTags();
        }
        if (this.getVariablePairs() != null) {
            flashHTML += '<param name="flashVars" value="' + this.getVariablePairs() + '" />';
        }
        flashHTML += '</object>';
    }
    else { // Everyone else
        flashHTML += '<embed type="application/x-shockwave-flash" src="' + this.getSWF() + '" width="' + this.getWidth() + '" height="' + this.getHeight() + '" id="' + this.getId() + '"';
        for (var param in this.getParams()) {
            flashHTML += ' ' + param + '="' + this.getParam(param) + '"';
        }
        if (this.getVariablePairs() != null) {
            flashHTML += ' flashVars="' + this.getVariablePairs() + '"';
        }
        flashHTML += '></embed>';
    }
    
    return flashHTML;
};


Flash.prototype.toXmlElement = function() {
	var xmlElement;
	var paramElement;
	
	if (window.ActiveXObject && navigator.userAgent.indexOf('Mac') == -1) { // PC IE
		xmlElement = document.createElementNS("http://www.w3.org/1999/xhtml", "object");
		xmlElement.setAttribute("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
		xmlElement.setAttribute("width", this.getWidth());
		xmlElement.setAttribute("height", this.getHeight());
		xmlElement.setAttribute("id", this.getId());
				
		paramElement = document.createElementNS("http://www.w3.org/1999/xhtml", "param");
		paramElement.setAttribute("name", "movie");
		paramElement.setAttribute("value", this.getSWF());
		
		xmlElement.appendChild(paramElement);
		
		for (var param in this.getParams()) {
			paramElement = document.createElementNS("http://www.w3.org/1999/xhtml", "param");
			paramElement.setAttribute("name", param);
			paramElement.setAttribute("value", this.getParam(param));
			xmlElement.appendChild(paramElement);
	    }
	

        if (this.getVariablePairs() != null) {
			paramElement = document.createElementNS("http://www.w3.org/1999/xhtml", "param");
			paramElement.setAttribute("name", "flashVars");
			paramElement.setAttribute("value", this.getVariablePairs());
			xmlElement.appendChild(paramElement);
        }
    }
    else { // Everyone else
		xmlElement = document.createElementNS("http://www.w3.org/1999/xhtml", "embed");
		xmlElement.setAttribute("type", "application/x-shockwave-flash");
		xmlElement.setAttribute("src", this.getSWF() );
        xmlElement.setAttribute("width", this.getWidth());
		xmlElement.setAttribute("height", this.getHeight());
		xmlElement.setAttribute("id", this.getId());
		
		
        for (var param in this.getParams()) {
			xmlElement.setAttribute(param, this.getParam(param));
        }
        if (this.getVariablePairs() != null) {
			xmlElement.setAttribute("flashVars", this.getVariablePairs());
        }
    }

	return xmlElement;
}