﻿if (Array.prototype.push==null)
{
    Array.prototype.push=function(item)
    {
        this[this.length]=item;
        return this.length;
    }
}

function PlayerVersion(_arrVersion)
{
    this.major=parseInt(_arrVersion[0]) || 0;
    this.minor=parseInt(_arrVersion[1]) || 0;
    this.rev=parseInt(_arrVersion[2]) || 0;
    
    this.versionIsValid=function(_needVersion)
    {
        if (this.major<_needVersion.major)
        {
            return false;
        }
        if (this.major>_needVersion.major)
        {
            return true;
        }
        if (this.minor<_needVersion.minor)
        {
            return false;
        }
        if (this.minor>_needVersion.minor)
        {
            return true;
        }
        if (this.rev<_needVersion.rev)
        {
            return false;
        }
        return true;
    }
    
    this.toString=function()
    {
        return this.major+"."+this.minor+"."+this.rev;
    }
}

var FlashUtil=new function()
{
    this.getPlayerVersion=function(_reqVer,_xiInstall)
    {
        var _playerVersion=new PlayerVersion([0,0,0]);
        if (navigator.plugins && navigator.mimeTypes.length)
        {
            var x = navigator.plugins["Shockwave Flash"];
            if (x && x.description)
            {
                _playerVersion=new PlayerVersion(x.description.replace(/([a-z]|[A-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
            }
        }
        else
        {
            try
            {
                var axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
                for (var i=3;axo!=null;i++)
                {
                    axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
                    _playerVersion=new PlayerVersion([i, 0, 0]);
                }
            }
            catch (e) {}
            if (_reqVer && _playerVersion.major>_reqVer.major)
            {
                return _playerVersion;
            }
            if (!_reqVer || ((_reqVer.minor!=0 || _reqVer.rev!=0) && _playerVersion.major==_reqVer.major) || _playerVersion.major!=6 || _xiInstall)
            {
                try
                {
                    _playerVersion=new PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));
                }
                catch (e){}
            }
        }
        return _playerVersion;
    }
}

var Util=new function()
{
    this.getRequestParameter=function(param)
    {
        var q=document.location.search || document.location.href.hash;
        if (q)
        {
            var startIndex=q.indexOf(param+"=");
            var endIndex=(q.indexOf("&",startIndex)>-1)?q.indexOf("&", startIndex):q.length;
            if (q.length>1 && startIndex>-1)
            {
                return q.substring(q.indexOf("=",startIndex)+1,endIndex);
            }
        }
        return "";
    }
}

function Flash(_swf,_id,_width,_height,_version,_bgColor,_useExpressInstall,_quality,_xiRedirectUrl,_redirectUrl,_detectKey)
{
    this.DETECT_KEY=_detectKey?_detectKey:"detectflash";
    this.skipDetect=Util.getRequestParameter(this.DETECT_KEY);
    
    this.params = new Object();
    this.variables = new Object();
    this.attributes=new Array();
    
    this.useExpressInstall=_useExpressInstall;
    
    this.init=function()
    {        
        this.setAttribute("swf",(_swf)?_swf:"");        
        this.setAttribute("id",(_id)?_id:"");
        this.setAttribute("width",(_width)?_width:"");
        this.setAttribute("height",(_height)?_height:"");
        this.setAttribute("version",(_version)?new PlayerVersion(_version.toString().split(".")):[0,0,0]);
        this.installedVer=FlashUtil.getPlayerVersion(this.getAttribute("version"),_useExpressInstall);
        this.addParam("bgcolor",(_bgColor)?_bgColor:"#FFFFFF");
        this.addParam("quality",(_quality)?_quality:"high");
        this.setAttribute("xiRedirectUrl",(_xiRedirectUrl)?_xiRedirectUrl:window.location);
        this.setAttribute("redirectUrl",(_redirectUrl)?_redirectUrl:"");
    }
    
    this.setAttribute=function(_name,_value)
    {
        this.attributes[_name]=_value;
    }
    this.getAttribute=function(_name)
    {
        return this.attributes[_name];
    }    
    this.addParam=function(_name,_value)
    {
        this.params[_name]=_value;
    }
    this.getParams=function()
    {
        return this.params;
    }
    this.addVariable=function(_name,_value)
    {
        this.variables[_name]=_value;
    }
    this.getVariable=function(_name)
    {
        return this.variables[_name];
    }
    this.getVariables=function()
    {
        return this.variables;
    }    
    this.createParamTag=function(_name,_value)
    {
        var _p=document.createElement("param");
        _p.setAttribute("name",_name);
        _p.setAttribute("value",_value);
        return _p;
    }
    this.getVariablePairs=function()
    {
        var variablePairs=new Array();
        var key;
        var variables=this.getVariables();
        for (key in variables)
        {
            variablePairs.push(key+"="+variables[key]);
        }
        return variablePairs;
    }
    this.getFlashHTML=function()
    {
        var flashNode="";
        if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length)
        {
            if (this.getAttribute("doExpressInstall"))
            {
                this.addVariable("MMplayerType","PlugIn");
            }
            flashNode="<embed type=\"application/x-shockwave-flash\" src=\""+this.getAttribute("swf")+"\" width=\""+this.getAttribute("width")+"\" height=\""+this.getAttribute("height")+"\"";
            flashNode += " id=\""+this.getAttribute("id")+"\" name=\""+this.getAttribute("id")+"\" ";
            var params=this.getParams();
            for (var key in params)
            {
                flashNode += [key]+"=\""+params[key]+"\" ";
            }
            var pairs=this.getVariablePairs().join("&");
            if (pairs.length > 0)
            {
                flashNode += "flashvars=\""+pairs+"\"";
            }
            flashNode += "/>";
        }
        else
        {
            if (this.getAttribute("doExpressInstall"))
            {
                this.addVariable("MMplayerType","ActiveX");
            }
            flashNode="<object id=\""+this.getAttribute("id")+"\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\""+this.getAttribute("width")+"\" height=\""+this.getAttribute("height")+"\">";
            flashNode += "<param name=\"movie\" value=\""+this.getAttribute("swf")+"\" />";
            var params=this.getParams();
            for (var key in params)
            {
                flashNode += "<param name=\""+key+"\" value=\""+params[key]+"\" />";
            }
            var pairs=this.getVariablePairs().join("&");
            if (pairs.length > 0)
            {
                flashNode += "<param name=\"flashvars\" value=\""+pairs+"\" />";
            }
            flashNode += "</object>";
        }
        return flashNode;
    }
    this.write=function(_container)
    {
        if (this.useExpressInstall)
        {
            var expressInstallReqVer=new PlayerVersion([6,0,65]);
            if (this.installedVer.versionIsValid(expressInstallReqVer) && !this.installedVer.versionIsValid(this.getAttribute("version")))
            {
                this.setAttribute("doExpressInstall",true);
                this.addVariable("MMredirectURL",escape(this.getAttribute("xiRedirectUrl")));
                document.title=document.title.slice(0,47)+" - Flash Player Installation";
                this.addVariable("MMdoctitle",document.title);
            }
        }
        else
        {
            this.setAttribute("doExpressInstall",false);
        }        
        if (this.skipDetect || this.getAttribute("doExpressInstall") || this.installedVer.versionIsValid(this.getAttribute("version")))
        {
            var n=(typeof _container=="string")?document.getElementById(_container):_container;
            n.innerHTML=this.getFlashHTML();
        }
        else
        {
            alert("Flash播放器版本不够，请下载新版播放器。");
            if (this.getAttribute("redirectUrl")!="")
            {
                document.location.replace(this.getAttribute("redirectUrl"));
            }
        }
    }
    this.init();
}