
//SOF: flash/Utils.js

/*
Macromedia(r) Flash(r) JavaScript Integration Kit License
Copyright (c) 2005 Macromedia, inc. All rights reserved.
*/

if (!window.WIDGETBOX){
    var WIDGETBOX = {};
}
if (!WIDGETBOX.api){
    WIDGETBOX.api = {}
}
if (!WIDGETBOX.api.flash){
    WIDGETBOX.api.flash = {}
}

if (!WIDGETBOX.api.flash.Utils) (function() {


    var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
    var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
    var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

    WIDGETBOX.api.flash.Utils = {

        isIE : function() {
            return isIE;
        },

        isWin : function() {
            return isWin;
        },

        isOpera : function() {
            return isOpera;
        },

        getMovie : function(movieName){
            if (isIE) {
                return window[movieName]
            }
            else {
                return document[movieName]
            }
        }

    }

    function ControlVersion()
    {
        var version;
        var axo;
        var e;

        try {
            axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
            version = axo.GetVariable("$version");
        } catch (e) {
        }

        if (!version)
        {
            try {
                axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
                version = "WIN 6,0,21,0";
                axo.AllowScriptAccess = "always";
                version = axo.GetVariable("$version");

            } catch (e) {
            }
        }

        if (!version)
        {
            try {
                axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
                version = axo.GetVariable("$version");
            } catch (e) {
            }
        }

        if (!version)
        {
            try {
                axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
                version = "WIN 3,0,18,0";
            } catch (e) {
            }
        }

        if (!version)
        {
            try {
                axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
                version = "WIN 2,0,0,11";
            } catch (e) {
                version = -1;
            }
        }

        return version;
    }

    function GetSwfVer() {
        var flashVer = -1;

        if (navigator.plugins != null && navigator.plugins.length > 0) {
            if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
                var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
                var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
                var descArray = flashDescription.split(" ");
                var tempArrayMajor = descArray[2].split(".");
                var versionMajor = tempArrayMajor[0];
                var versionMinor = tempArrayMajor[1];
                if (descArray[3] != "") {
                    tempArrayMinor = descArray[3].split("r");
                } else {
                    tempArrayMinor = descArray[4].split("r");
                }
                var versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
                var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
            }
        }
        else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
        else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
        else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
        else if (isIE && isWin && !isOpera) {
            flashVer = ControlVersion();
        }
        return flashVer;
    }

    function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
    {
        versionStr = GetSwfVer();
        if (versionStr == -1) {
            return false;
        } else if (versionStr != 0) {
            if (isIE && isWin && !isOpera) {
                tempArray = versionStr.split(" ");
                tempString = tempArray[1];
                versionArray = tempString.split(",");
            } else {
                versionArray = versionStr.split(".");
            }
            var versionMajor = versionArray[0];
            var versionMinor = versionArray[1];
            var versionRevision = versionArray[2];

            if (versionMajor > parseFloat(reqMajorVer)) {
                return true;
            } else if (versionMajor == parseFloat(reqMajorVer)) {
                if (versionMinor > parseFloat(reqMinorVer))
                    return true;
                else if (versionMinor == parseFloat(reqMinorVer)) {
                    if (versionRevision >= parseFloat(reqRevision))
                        return true;
                }
            }
            return false;
        }
    }

    function GetFlashVersionArray() {
        var version = new Array(3);
        version[0] = -1;
        version[1] = -1;
        version[2] = -1;

        var versionStr = GetSwfVer();

        if (isIE) {
            // we're expecting something like WIN 9,0,124,0
            var versionParts = versionStr.split(" ");

            if (versionParts.length == 2) {
                versionParts = versionParts[1].split(",");

                if (versionParts.length == 4) {
                    version[0] = parseInt(versionParts[0]);
                    version[1] = parseInt(versionParts[1]);
                    version[2] = parseInt(versionParts[2]);
                }
            }
        } else {
            // we're expecting something like 9.0.124
            var versionParts = versionStr.split(".");

            if (versionParts.length == 3) {
                version[0] = parseInt(versionParts[0]);
                version[1] = parseInt(versionParts[1]);
                version[2] = parseInt(versionParts[2]);
            }
        }

        return version;
    }

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

    Exception.prototype.setName = function(name)
    {
        this.name = name;
    }

    Exception.prototype.getName = function()
    {
        return this.name;
    }

    Exception.prototype.setMessage = function(msg)
    {
        this.message = msg;
    }

    Exception.prototype.getMessage = function()
    {
        return this.message;
    }

    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.');
        }

        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;
        this.enableFSCommand = false;
    }

    FlashTag.prototype.setSource = function(src)
    {
        this.src = src;
    }

    FlashTag.prototype.setWidth = function(w)
    {
        this.width = width;
    }

    FlashTag.prototype.setHeight = function(h)
    {
        this.h = height;
    }

    FlashTag.prototype.setVersion = function(v)
    {
        this.version = v;
    }

    FlashTag.prototype.setId = function(id)
    {
        this.id = id;
    }

    FlashTag.prototype.setBgcolor = function(bgc)
    {
        if (bgc.charAt(0) != '#')
        {
            bgc = '#' + bgc;
        }
        this.genericParam['bgcolor'] = bgc;
    }

    FlashTag.prototype.addParams = function(params)
    {
        for (var paramName in params) {
            var paramValue = params[paramName];
            this.genericParam[paramName] = paramValue;
        }
    }

    FlashTag.prototype.addFlashVars = function(fvs)
    {
        this.flashVarsStr = fvs;
    }


    FlashTag.prototype.addFlashVarsParams = function(params)
    {
        if (!this.flashVars) this.flashVars = new Object();

        for (var paramName in params) {
            var paramValue = params[paramName];
            this.flashVars[paramName] = paramValue;
        }
    }

    FlashTag.prototype.addFlashVar = function(n, v)
    {
        if (!this.flashVars) this.flashVars = new Object();

        this.flashVars[n] = v;
    }

    FlashTag.prototype.removeFlashVar = function(n)
    {
        if (this.flashVars)
        {
            this.flashVars[n] = null;
        }
    }

    FlashTag.prototype.setSwliveconnect = function(swlc)
    {
        this.genericParam['swliveconnect'] = swlc;
    }

    FlashTag.prototype.setPlay = function(p)
    {
        this.genericParam['play'] = p;
    }

    FlashTag.prototype.setLoop = function(l)
    {
        this.genericParam['loop'] = l;
    }

    FlashTag.prototype.setMenu = function(m)
    {
        this.genericParam['menu'] = m;
    }

    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;
    }

    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;
    }

    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;
    }

    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;
    }

    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;
    }

    FlashTag.prototype.setBase = function(base)
    {
        this.genericParam['base'] = base;
    }

    FlashTag.prototype.setAllowScriptAccess = function(sa)
    {
        if (sa != 'never' && sa != 'always' && sa != 'sameDomain')
        {
            throw new Exception('UnsupportedValueException',
                    'Supported values are "never" and "always", and "sameDomain".');
        }
        this.genericParam['allowScriptAccess'] = sa;
    }

    FlashTag.prototype.setAllowNetworking = function(na)
    {
        if (na != 'all' && na != 'internal' && na != 'none')
        {
            throw new Exception('UnsupportedValueException',
                    'Supported values are "all", "internal", and "none".');
        }
        this.genericParam['allowNetworking'] = na;
    }

    FlashTag.prototype.setEnableFSCommand = function(enableFSCommand)
    {
        this.enableFSCommand = enableFSCommand;
    }

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

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

            if (this.flashVars)
            {
                var fv = this.getFlashVarsAsString();
                if (fv.length > 0)
                {
                    flashTag += '<param name="flashvars" value="' + fv + '"/>';
                }
            }
            flashTag += '</object>';

            if (this.enableFSCommand &&
                navigator.platform.indexOf('Win') != -1 &&
                navigator.userAgent.indexOf('Opera') == -1)
            {
                flashTag += "<script event=\"FSCommand(command,args)\" for=\"" + this.id + "\">\n" +
                             "FlashProxy.callJS(command, args);\n" +
                             "</sc" + "ript>";
            }

        }
        else
        {
            flashTag += '<embed src="' + this.src + '"';
            if (this.width >= 0) {
                flashTag += ' width="' + this.width + '"';
                flashTag += ' height="' + this.height + '"';
            }
            flashTag += ' type="application/x-shockwave-flash"';
            if (this.id)
            {
                flashTag += ' id="' + this.id + '"';
                flashTag += ' name="' + this.id + '"';
            }
            var wmodeExists = false;

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

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

    FlashTag.prototype.write = function(doc)
    {
        doc.write(this.toString());
    }

    FlashTag.prototype.getFlashVarsAsString = function()
    {
        var qs = new String();
        for (var n in this.flashVars)
        {
            if (this.flashVars[n])
            {
                qs += (escape(n) + '=' + escape(this.flashVars[n]) + '&');
            }
        }

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

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


    WIDGETBOX.api.flash.Utils.GetSwfVer = GetSwfVer;
    WIDGETBOX.api.flash.Utils.DetectFlashVer = DetectFlashVer;
    WIDGETBOX.api.flash.Utils.GetFlashVersionArray = GetFlashVersionArray;
    WIDGETBOX.api.flash.Utils.FlashTag = FlashTag;

})()

//EOF: flash/Utils.js

