function FlashTag(src, width, height, version)
{
    if (arguments.length < 4)
    {
        throw new Exception('RequiredParameterException',
                            'You must pass in a src, width, height, and version when creating a FlashTag.');
    }

    // Required
    this.src            =  src;
    this.width          =  width;
    this.height         =  height;
    this.version        =  version;

    this.id             =  null;
    this.flashVars      =  null;
    this.flashVarsStr   =  null;
    this.genericParam   = new Object();
    this.ie = (navigator.appName.indexOf ("Microsoft") != -1) ? 1 : 0;
}

/**
 * Specifies the location (URL) of the Flash content to be loaded.
 */
FlashTag.prototype.setSource = function(src)
{
    this.src = src; 
}

/**
 * Specifies the width of the Flash content in either pixels or percentage of browser window. 
 */
FlashTag.prototype.setWidth = function(w)
{
    this.width = width; 
}

/**
 * Specifies the height of the Flash content in either pixels or percentage of browser window. 
 */
FlashTag.prototype.setHeight = function(h)
{
    this.h = height; 
}

/**
 * The required version of the Flash Player for the specified content. 
 */
FlashTag.prototype.setVersion = function(v)
{
    this.version = v;
}

/**
 * Identifies the Flash content to the host environment (a web browser, for example) so that
 * it can be referenced using a scripting language. This value will be used for both the 'id'
 * and 'name' attributes depending on the client platform and whether the object or the embed
 * tag are used.
 */
FlashTag.prototype.setId = function(id)
{
    this.id = id;
}

/**
 * Specifies the background color of the Flash content. Use this attribute to override the background
 * color setting specified in the Flash file. This attribute does not affect the background
 * color of the HTML page. 
 */
FlashTag.prototype.setBgcolor = function(bgc)
{
    if (bgc.charAt(0) != '#')
    {
        bgc = '#' + bgc;
    }
    this.genericParam['bgcolor'] = bgc;
}

/**
 * Allows you to set multiple Flash vars at once rather than adding them one at a time. The string
 * you pass in should contain all your Flash vars, properly URL encoded. This function can be used in
 * conjunction with addFlashVar.
 */
FlashTag.prototype.addFlashVars = function(fvs)
{
    this.flashVarsStr = fvs;
}

/**
 * Used to send root level variables to the Flash content. You can add as many name/value pairs as
 * you want. The formatting of the Flash vars (turning them into a query string) is handled automatically.
 */
FlashTag.prototype.addFlashVar = function(n, v)
{
    if (this.flashVars == null)
    {
        this.flashVars = new Object();
    }

    this.flashVars[n] = v;
}

/**
 * Used to remove Flash vars. This is primarily useful if you want to reuse an instance of the FlashTag
 * but you don't want to send the same variables to more than one piece of Flash content. 
 */
FlashTag.prototype.removeFlashVar = function(n)
{
    if (this.flashVars != null)
    {
        this.flashVars[n] = null;
    }
}

/**
 * (true, false) Specifies whether the browser should start Java when loading the Flash Player for the first time.
 * The default value is false if this property is not set. 
 */
FlashTag.prototype.setSwliveconnect = function(swlc)
{
    this.genericParam['swliveconnect'] = swlc;
}

/**
 * (true, false) Specifies whether the Flash content begins playing immediately on loading in the browser.
 * The default value is true if this property is not set. 
 */
FlashTag.prototype.setPlay = function(p)
{
    this.genericParam['play'] = p;
}

/**
 * (true, false) Specifies whether the Flash content repeats indefinitely or stops when it reaches the last frame.
 * The default value is true if this property is not set. 
 */
FlashTag.prototype.setLoop = function(l)
{
    this.genericParam['loop'] = l;
}

/**
 * (true,false) Whether or not to display the full Flash menu. If false, displays a menu that contains only
 * the Settings and the About Flash options. 
 */
FlashTag.prototype.setMenu = function(m)
{
    this.genericParam['menu'] = m;
}

/**
 * (low, high, autolow, autohigh, best) Sets the quality at which the Flash content plays.
 */
FlashTag.prototype.setQuality = function(q)
{
    if (q != 'low' && q != 'high' && q != 'autolow' && q != 'autohigh' && q != 'best')
    {
        throw new Exception('UnsupportedValueException',
                            'Supported values are "low", "high", "autolow", "autohigh", and "best".');
    }
    this.genericParam['quality'] = q;
}

/**
 * (showall, noborder, exactfit) Determins how the Flash content scales.
 */
FlashTag.prototype.setScale = function(sc)
{
    if (sc != 'showall' && sc != 'noborder' && sc != 'exactfit')
    {
        throw new Exception('UnsupportedValueException',
                            'Supported values are "showall", "noborder", and "exactfit".');
    }
    this.genericParam['scale'] = sc;
}

/**
 * (l, t, r, b) Align the Flash content along the corresponding edge of the browser window and crop
 * the remaining three sides as needed.
 */
FlashTag.prototype.setAlign= function(a)
{
    if (a != 'l' && a != 't' && a != 'r' && a != 'b')
    {
        throw new Exception('UnsupportedValueException',
                            'Supported values are "l", "t", "r" and "b".');
    }
    this.genericParam['align'] = a;
}

/**
 * (l, t, r, b, tl, tr, bl, br) Align the Flash content along the corresponding edge of the browser
 * window and crop the remaining three sides as needed.
 */
FlashTag.prototype.setSalign= function(sa)
{
    if (sa != 'l' && sa != 't' && sa != 'r' && sa != 'b' && sa != 'tl' && sa != 'tr' && sa != 'bl' && sa != 'br')
    {
        throw new Exception('UnsupportedValueException',
                            'Supported values are "l", "t", "r", "b", "tl", "tr", "bl" and "br".');
    }
    this.genericParam['salign'] = sa;
}

/**
 * (window, opaque, transparent) Sets the Window Mode property of the Flash content for transparency,
 * layering, and positioning in the browser. 
 */
FlashTag.prototype.setWmode = function(wm)
{
    if (wm != 'window' && wm != 'opaque' && wm != 'transparent')
    {
        throw new Exception('UnsupportedValueException',
                            'Supported values are "window", "opaque", and "transparent".');
    }
    this.genericParam['wmode'] = wm;
}

/**
 * Specifies the base directory or URL used to resolve all relative path statements in your Flash content.
 */
FlashTag.prototype.setBase = function(base)
{
    this.genericParam['base'] = base;
}

/**
 * (never, always) Controls the ability to perform outbound scripting from within Flash content. 
 */
FlashTag.prototype.setAllowScriptAccess = function(sa)
{
    if (sa != 'never' && sa != 'always')
    {
        throw new Exception('UnsupportedValueException',
                            'Supported values are "never" and "always".');
    }
    this.genericParam['allowScriptAccess'] = sa;
}

/**
 * Get the Flash tag as a string. 
 */
FlashTag.prototype.toString = function()
{
    var flashTag = new String();
    if (this.ie)
    {
        flashTag += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
        if (this.id != null)
        {
            flashTag += 'id="'+this.id+'" ';
        }
        flashTag += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+this.version+'" ';
        flashTag += 'width="'+this.width+'" ';
        flashTag += 'height="'+this.height+'">';
        flashTag += '<param name="movie" value="'+this.src+'"/>';

        for (var n in this.genericParam)
        {
            if (this.genericParam[n] != null)
            {
                flashTag += '<param name="'+n+'" value="'+this.genericParam[n]+'"/>';
            }
        }

        if (this.flashVars != null)
        {
            var fv = this.getFlashVarsAsString();
            if (fv.length > 0)
            {
                flashTag += '<param name="flashvars" value="'+fv+'"/>';
            }
        }
        flashTag += '</object>';
    }
    else
    {
        flashTag += '<embed src="'+this.src+'"';
        flashTag += ' width="'+this.width+'"';
        flashTag += ' height="'+this.height+'"';
        flashTag += ' type="application/x-shockwave-flash"';
        if (this.id != null)
        {
            flashTag += ' name="'+this.id+'"';
        }

        for (var n in this.genericParam)
        {
            if (this.genericParam[n] != null)
            {
                flashTag += (' '+n+'="'+this.genericParam[n]+'"');
            }
        }

        if (this.flashVars != null)
        {
            var fv = this.getFlashVarsAsString();
            if (fv.length > 0)
            {
                flashTag += ' flashvars="'+fv+'"';
            }
        }
        flashTag += ' pluginspage="http://www.macromedia.com/go/getflashplayer">';
        flashTag += '</embed>';
    }
    return flashTag;
}

/**
 * Write the Flash tag out. Pass in a reference to the document to write to. 
 */
FlashTag.prototype.write = function(doc)
{
    doc.write(this.toString());
}

/**
 * Write the Flash tag out. Pass in a reference to the document to write to. 
 */
FlashTag.prototype.getFlashVarsAsString = function()
{
    var qs = new String();
    for (var n in this.flashVars)
    {
        if (this.flashVars[n] != null)
        {
            qs += (escape(n)+'='+escape(this.flashVars[n])+'&');
        }
    }

    if (this.flashVarsStr != null)
    {
        return qs + this.flashVarsStr;
    }

    return qs.substring(0, qs.length-1);
}

function Exception(name, message)
{
    if (name)
        this.name = name;
    if (message)
        this.message = message;
}

/**
 * Set the name of the exception. 
 */
Exception.prototype.setName = function(name)
{
    this.name = name;
}

/**
 * Get the exception's name. 
 */
Exception.prototype.getName = function()
{
    return this.name;
}

/**
 * Set a message on the exception. 
 */
Exception.prototype.setMessage = function(msg)
{
    this.message = msg;
}

/**
 * Get the exception message. 
 */
Exception.prototype.getMessage = function()
{
    return this.message;
}

function FlashSniffer() {
	var platform = window.navigator.platform;
			
	this.isWin = (platform.indexOf("Win")!=-1);
	this.isMac = (platform.indexOf("Mac")!=-1);

	if (this.isWin && document.all) {
		document.write(
			'<scr' + 'ipt language="javascript">' +
				'window.flashSniffer_getFlashVersion = function() {' +
				'	var version = new flashSniffer_Version(0, 0);' +
				'	try {' +
				'		var sf = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");' +
				'		var bin = sf.FlashVersion();' +
				'		bin = flashSniffer_toBinary(bin, 32);' +
				'		var maj = bin.substr(0, 16);' +
				'		maj = flashSniffer_toDecimal(maj);' +
				'		var min = bin.substr(16);' +
				'		min = flashSniffer_toDecimal(min);' +
				'		version.major = maj;' +
				'		version.minor = min;' +
				'	}' +
				'	catch (e) {;}' +
				'	return version;' +
				'};' +
			'</scr' + 'ipt>'
		);
	}
	else {
		window.flashSniffer_getFlashVersion = function() {
			var version = new flashSniffer_Version(0, 0);
			if (navigator.plugins) {
				var regExp = /^Shockwave Flash (\d+)\.(\d+)\s*r(\d+)/;
				for(var i=0; i<navigator.plugins.length; i++) {
					var p = navigator.plugins[i];
					var match = regExp.exec(p.description);
					if (match) {
						version.major = parseInt(match[1]);
						version.minor = parseInt(match[2]);
						version.revision = parseInt(match[3]);
						break;
					}
				}
			}
			return version;
		};
	}
	if (flashSniffer_getFlashVersion) {
		this.version = flashSniffer_getFlashVersion();
	}
	else {
		this.version = new flashSniffer_Version(0, 0);
	}
}
		
function flashSniffer_Version(major, minor, revision) {
	this.major = major;
	this.minor = minor;
	this.revision = revision;
	this.toString = function() {
		if (this.revision==null) {
			return this.major + "." + this.minor;
		}
		else {
			return this.major + "." + this.minor + " r" + this.revision;
		}
	};
	this.getValue = function() {
		return parseFloat(this.major + "." + this.minor);
	};
}
		
function flashSniffer_toBinary (num, placeCount) {
	var max = Math.floor(Math.log(num)/Math.log(2));
	if (max<placeCount) max = placeCount;
	var s = "";
	for (var i=max-1; i>=0; i--) {
		var val = Math.pow(2, i);
		if (num & val) {
			s += "1";
		}
		else {
			s += "0"
		}
	}
	return s;
}
function flashSniffer_toDecimal(num) {
	var value = 0;
	for (var i=1; i<=num.length; i++) {
		var index = num.length - i;
		if (num.substr(index, 1)=="1") {
			var exp = Math.pow(2, i-1);
			value += exp;
		}
	}
	return value;
}
