/*:._.:*:._.:*:._.:*:._.:*:._.:*:._

Flash Detection & Insertion Class

Copyright (c) 2004-2007 Polychrome Studio, Inc.
http://www.polychrome.com/

Not for redistribution.

- - - - - - - - - - - - -
Created: 2004/08/06
Author:  Christian Rocha
- - - - - - - - - - - - -

CHANGELOG:

2005/10/30
    .flashDetect() is now a method of the Flash object and is now
    called 'detect()'.
2006/04/10
	.fixed a serious bug where flashVars were being improperly inserted in the
	<embed> tag. The fix is a solid one, but should be implemented in a
	cleaner fashion down the road.
2006/08/09
    .added HTTPS support so that IE won't complain when flash is embedded over
    a secure connection.
2007/02/12
    .added add addParam() method to add miscellaneous parameters to the object
    and embed objects.
    .added a makeTransparent() method to make the background of the movie
    transparent.
2007/02/14
    .fixed a bug where not setting any flashVars or setting mulitple flashVars
    resulted in a malformed flashVars attribute in the embed element.
2007/02/17
    .detect() is now a static method
2007/04/17
    .added an ID variable to attach an ID to the <object> and <embed> elements
2007/05/03
    .added a method to set embedded movie's background color

*:._.:*:._.:*:._.:*:._.:*:._.:*:._.:*/

/**
 * Constructor
 */
function Flash(url, width, height, quality, isSecure)
{
    // Public Variables
    
    this.url      = url       || '';     // URL to the flash movie
    this.width    = width     || '';     // width of the flash movie
    this.height   = height    || '';     // height of the flash movie
    this.quality  = quality   || 'HIGH'; // quality of the flash movie
    this.isSecure = isSecure  || false;  // whether or not to use https in the codebase URL (to avoid IE complaining when loading SWF files over a secure connection)
    
    this.id; // ID attribute to add to the <object> and <embed> elements
    
    // Private Variables
    
    this.flashHTML; // stores HTML string for output
    
    this.flashVarsObject = ''; // stores FlashVars data for the <object> element
    this.flashVarsEmbed  = ''; // stores FlashVars data for the <embed> element
    
    this.objectParams = ''; // data for the <object> element
    this.embedParams  = ''; // data for the <embed> element
    
};

/**
 * Detect Flash Player version
 * Returns false if player is not present, otherwise returns the version number
 */
Flash.detect = function()
{
    var version = 0;
    
    // Most browsers store plugin information in navigator.plugins...
    if (navigator.plugins && navigator.plugins.length) {
        
        var n = navigator.plugins["Shockwave Flash"];
        
        if(n && n.description) {
            var d = n.description;
            version = d.charAt(d.indexOf('.') - 1);
        }
    }
    
    // Internet Explorer, however, needs some VBScript for it's detection
    else {
        result = false;
        for(var i = 15; i >= 3 && result != true; i--) {
            execScript('on error resume next: result = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+i+'"))','VBScript');
            version = i;
        }
    }
    
    return version;  
}

/**
 * Methods
 */
Flash.prototype = {
   
    /**
    * Add Flash Variable
    * access public
    */
    addFlashVar : function(key, value)
    {
        this.flashVarsObject += '<param name="FlashVars" value="' + key + '=' + value + '" />';
        this.flashVarsEmbed  += key + '=' + value + '&';
    },
    
    /**
     * Add Parameter
     * access public
     */
    addParam : function(key, value)
    {
        this.objectParams += '<param name="' + key + '" value="' + value + '" />';
        this.embedParams  += ' ' + key + '=' + '"' + value + '"';
    },
    
    /**
     * Make Background Transparent
     * access public
     */
    makeTransparent : function()
    {
        this.addParam('WMODE', 'transparent');
    },
    
    setBackgroundColor : function(hex)
    {
        this.addParam('bgcolor', hex);
    },
    
    /**
     * Outputs the HTML
     * access public
     */
    write : function()
    {
        this.compile();
        document.write(this.flashHTML);
    },

    /**
     * Builds HTML to embed
     * access protected
     */
    compile : function()
    {
        var secure = this.isSecure ? 's' : '';
        /* Modified ID and added NAME attribute for embed - T.Turner */
        var _id = this.id ? ' id="' + this.id + '" ' : 'id="PreviewPlayer"';
        var _name = 'name="PreviewPlayer"';
        
        this.flashHTML = '<object ' + _id + ' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http' + secure + '://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="' + this.width + '" height="' + this.height + '">'
         + '<param name="movie" value="' + this.url + '" />'
         + '<param name="quality" value="' + this.quality + '" />'
         + this.flashVarsObject
         + this.objectParams
         + '<embed ' + _id + ' ' + _name + ' src="' + this.url + '" quality="' + this.quality + '"  flashVars="' + this.flashVarsEmbed + '" ' + this.embedParams + ' pluginspage="http' + secure + '://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="' + this.width + '" height="' + this.height + '"></embed>'
         + '</object>';
    }
};
