// Solid State Networks ION installation script 

if (typeof Solidspace == "undefined")
    var Solidspace = new Object();
if (typeof Solidspace.Utility == "undefined")
    Solidspace.Utility = new Object();
if (typeof Solidspace.Nav == "undefined")
    Solidspace.Nav = new Object();

Solidspace.Vars = function() { }
Solidspace.Vars.StagePlacementId = "ssn_stage_placement";
Solidspace.Vars.StageId = "ssn_stage";
Solidspace.Vars.StageBodyId = "ssn_stage_body";
Solidspace.Vars.MainPanel = "ssn_panel";
Solidspace.Vars.CodebaseUrl = "http://www.solidaxision.com/setup/solidstateion.cab#Version=0,8,9,8";
Solidspace.Vars.XpiUrl = "http://www.solidaxision.com/setup/solidstateion.xpi";
Solidspace.Vars.MinimumVersion = "0.898";
Solidspace.Vars.AgreeCookie = "ssn_cookie_agree";
Solidspace.Vars.AgreeButton = "ssn_button_agree";
Solidspace.Vars.AgreePanel = "ssn_panel_agree";
Solidspace.Vars.AgreeUrl = "http://www.solidaxision.com/setup/agree.htm";
Solidspace.Vars.AboutUrl = "http://www.solidaxision.com/setup/about.htm";
Solidspace.Vars.TermsUrl = "http://www.solidaxision.com/setup/terms.htm";
Solidspace.Vars.FaqUrl = "http://www.solidaxision.com/setup/faq.htm";
Solidspace.Vars.SuccessUrl = "http://www.solidaxision.com/setup/success.htm";
Solidspace.Vars.SuccessCookie = "ssn_cookie_success";
Solidspace.Vars.UnsupportedUrl = "http://www.solidaxision.com/setup/unsupported.htm";
Solidspace.Vars.UnsupportedSpan = "ssn_unsupported_msg";
Solidspace.Vars.InstructionsUrl = "http://www.solidaxision.com/setup/install.htm";
Solidspace.Vars.InstructionsPanel = "ssn_panel_instructions";
Solidspace.Vars.VersionRequiredSpan = "ssn_version_required";
Solidspace.Vars.VersionInstalledSpan = "ssn_version_installed";

Solidspace.PlayerVersion = function(Version) {
    this.Major    = (Version[0] != null) ? parseInt(Version[0]) : 0;
    this.Minor    = (Version[1] != null) ? parseInt(Version[1]) : 0;
    this.Revision = (Version[2] != null) ? parseInt(Version[2]) : 0;
}
Solidspace.PlayerVersion.prototype = {
    IsValid: function(Version)
    {
        if (!Version) 
            return false;
            
        //alert('Installed Version: ' + this.toString() + ' Minimum Version: ' + Version.toString());        
        
        if (this.Major < Version.Major)          return false;
        if (this.Major > Version.Major)          return true;
        if (this.Minor < Version.Minor)          return false;
        if (this.Minor > Version.Minor)          return true;
        if (this.Revision < Version.Revision)    return false;
        
        return true;
    },
    IsInstalled: function() 
    { 
        return ((this.Major + this.Minor) > 0); 
    },
    toString: function()    
    { 
        return (this.Major + '.' + this.Minor + '.' + this.Revision); 
    }
}

Solidspace.Utility = {
    IsIE: function()        
    { 
        return (navigator.userAgent.indexOf('MSIE') != -1); 
    },
    IsFirefox: function()
    {
        return (navigator.userAgent.indexOf('Firefox') != -1);
    },
    IsWindows: function()
    {
        return (navigator.userAgent.indexOf('Windows') != -1);
    },
    IsMac: function()
    {
        return (navigator.userAgent.indexOf('Mac') != -1);
    },
    IsFlashInstalled: function()
    {
        if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"])
            {
            var Plugin = navigator.plugins["Shockwave Flash"];
            
            if (Plugin && Plugin.description && Plugin.description.charAt(Plugin.description.indexOf('.')) - 1 < 9)
                return true;
                
            return (navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin);
            }
        else if (Solidspace.Utility.IsIE())
            {
            for (var i = 12; i >= 9; i -= 1)
                {
                try {
                    new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
                    return true;
                    }
                catch(e)
                    {
                    }
                }
            }
            
        return false;
    },
    GetUrlParameter: function(QueryName)
    {
        var Regex   = new RegExp("[\\?&]" + QueryName + "=([^&#]*)");
        var Results = Regex.exec(window.location.href);
        
        if (Results == null)
            return "";
            
        return Results[1];
    },
    GetUrlParameters: function()
    {
        var Url = window.location.href;
        var Params = new Array();
        var Start = Url.indexOf("?");
        if (Start == -1)
            return null;
        var Pairs = Url.substring(Start + 1).split("&");
        
        for(i in Pairs)
            {
            var KeyVal = Pairs[i].split("=");
            Params[KeyVal[0]] = decodeURIComponent(KeyVal[1]);
            }
            
        return Params;
    },    
    GetPlayerVersion: function()
    {
        if (navigator.plugins && navigator.mimeTypes.length)
            {
            var Plugin = navigator.plugins["Solid State ION Mozilla Plugin"];
            
            if (Plugin && Plugin.description)
                {
                var Version = Plugin.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split(".");
                return new Solidspace.PlayerVersion(Version);
                }
            }
        else
            {
            try {
                var Plugin = new ActiveXObject("SolidStateNetworks.SolidStateIONIE.1");
                return new Solidspace.PlayerVersion(Plugin.Version().split("."));
                }
            catch(err)  
                { 
                //alert(err.description);
                }
            }

        return new Solidspace.PlayerVersion([0,0,0]);
    },    
    AppendHtml: function(Html)
    {
        var Element;


        Element = document.createElement("div");
        Element.innerHTML = Html;
        
        Solidspace.Utility.InsertElement(Element);
        return;
    },
    InsertElement: function (Element)
    {
        var Script, Scripts;
        var Placement;
        
        Scripts = document.getElementsByTagName('script'); 
        Placement = document.getElementById(Solidspace.Vars.StagePlacementId);
        
        if (Placement)
            {
            Placement.appendChild(Element);
            }
        else if (Scripts && Scripts.length >= 1)
            {
            Script = Scripts[Scripts.length - 1];
            Script.parentNode.insertBefore(Element, Script);   
            }
        else
            {
            if (document.body.insertAdjacentElement)
                document.body.insertAdjacentElement('afterBegin', Element);
            else
                document.body.appendChild(Element);
            }
            
        return;
    },
    HideElement: function(Element)
    {
        Element.style.visibility = "hidden";
        Element.style.display    = "none";
    },
    ShowElement: function(Element)
    {    
        Element.style.visibility = "visible";
        Element.style.display    = "block";    
    },
    SetCookie: function(Name, Value, Days)
    {
        var Expires = "";

    	if (Days) 
            {
    		var CookieDate = new Date();
    		CookieDate.setTime(CookieDate.getTime() + (Days*24*60*60*1000));
    		Expires = "; expires=" + CookieDate.toGMTString();
            }
            
    	document.cookie = Name + "=" + Value + Expires + "; path=/;";

        return document.cookie;
    },
    GetCookie: function(Name) 
    {
    	var NameEQ = Name + "=";
    	var Ca = document.cookie.split(';');

    	for (var i = 0; i < Ca.length; i += 1) 
            {
    		var c = Ca[i];
    		while (c.charAt(0) == ' ') 
                c = c.substring(1, c.length);
    		if (c.indexOf(NameEQ) == 0)
                return c.substring(NameEQ.length, c.length);
            }
            
    	return null;
    },
    DeleteCookie: function(Name)
    {
        var CookieDate = new Date();
        CookieDate.setTime(CookieDate.getTime() - 1);
        document.cookie = Name + "=0;expires=" + CookieDate.toGMTString() + ";" + ";";

        return document.cookie;
    }
}

Solidspace.Nav.OnLoad = function()
{
    var RequiredVersion = null;
    var Params = null;
    

    if (window.location.href.indexOf(Solidspace.Vars.AgreeUrl) != -1)
        {
        Params = Solidspace.Utility.GetUrlParameters();
        
        if (Params != null)
            RequiredVersion = Params["RequiredVersion"];
        
        if (!RequiredVersion)
             RequiredVersion = Solidspace.Vars.RequiredVersion;

        if (Solidspace.Utility.GetCookie(Solidspace.Vars.AgreeCookie) == 1)
            {
            Solidspace.Nav.GotoUrlFramed(Solidspace.Vars.InstructionsUrl);
            return;
            }        
         
        Solidspace.Utility.SetCookie(Solidspace.Vars.SuccessCookie, 1, 1);
                        
        var Panel = document.getElementById(Solidspace.Vars.MainPanel);
        
        if (Panel)
            Solidspace.Utility.ShowElement(Panel);
        
        var VersionInstalled = document.getElementById(Solidspace.Vars.VersionInstalledSpan);
        
        if (VersionInstalled)
            {
            VersionInstalled.innerHTML = Solidspace.Utility.GetPlayerVersion().toString();
            if (VersionInstalled.innerHTML == "0.0.0")
                VersionInstalled.innerHTML = "Not Installed";
            }
        
        var VersionRequired = document.getElementById(Solidspace.Vars.VersionRequiredSpan);
        
        if (VersionRequired)
            {
            VersionRequired.innerHTML = RequiredVersion;
            }

        var AgreeButton = document.getElementById(Solidspace.Vars.AgreeButton);
        
        if (AgreeButton)
            {        
            AgreeButton.onclick = function() 
                {
                Solidspace.Utility.SetCookie(Solidspace.Vars.AgreeCookie, 1, 1);
                Solidspace.Nav.GotoUrlFramed(Solidspace.Vars.InstructionsUrl);
                return true; 
                };
            }
        }
    else if (window.location.href.indexOf(Solidspace.Vars.InstructionsUrl) != -1)
        {
        var InstructionsPanelName = Solidspace.Vars.InstructionsPanel;
        
        
        if (Solidspace.Utility.IsIE())
            InstructionsPanelName += "_ie";
        else
            InstructionsPanelName += "_mo";
            
        var InstructionsPanel = document.getElementById(InstructionsPanelName);
        if (InstructionsPanel)
            {
            Solidspace.Utility.ShowElement(InstructionsPanel);
            }
            
        var UnsupportedSpan = document.getElementById(Solidspace.Vars.UnsupportedSpan);
        if (UnsupportedSpan)
            {
            var AlternativeUrl = Solidspace.Utility.GetUrlParameter('alturl');
            
            if (AlternativeUrl && AlternativeUrl.length != 0)
                {
                Solidspace.Utility.ShowElement(UnsupportedSpan);
                }
            }
        }
        
    return;
}
Solidspace.Nav.GotoUrl = function(Url)
{
    if (!Url || Url == '') 
        return;

    window.location = Url;
    return;
}
Solidspace.Nav.GotoUrlFramed = function(Url)
{
    if (!Url || Url == '') 
        return;
        
    var FrameBody = document.getElementById(Solidspace.Vars.StageBodyId);
               
    if (FrameBody) // we are inside of iframe
        {
        Solidspace.Nav.GotoUrl(Url);
        return; 
        }
    
    var Frame = document.getElementById(Solidspace.Vars.StageId);
    
    if (!Frame)
        {
        Frame = document.createElement("iframe");        
        Frame.id = Solidspace.Vars.StageId;
        Frame.frameBorder = "0px";
        Solidspace.Utility.InsertElement(Frame);
        }
       
    if (Frame.contentWindow)
        Frame.contentWindow.location.replace(Url);
    else
        Frame.src = Url;
        
    return;
}

Solidspace.Base = function(DataSource, MetaFileSource, RequiredVersion)
{
    if (!document.getElementById)
        return;
    
    if (this.Attributes == null)
        this.Attributes = new Array();
    if (this.UserParams == null)
        this.UserParams = new Array();
        
    if (this.GetAttribute('DataSource') == undefined && DataSource != undefined)         
        this.SetAttribute('DataSource', DataSource);
    if (this.GetAttribute('MetaFileSource') == undefined && MetaFileSource != undefined)     
        this.SetAttribute('MetaFileSource', MetaFileSource);
    
    if (this.GetAttribute('RequiredVersion'))
        RequiredVersion = this.GetAttribute('RequiredVersion');
    if (!RequiredVersion)   
        RequiredVersion = Solidspace.Vars.MinimumVersion;
    
    this.SetAttribute('RequiredVersion', RequiredVersion);
    
    if (this.GetAttribute('Version') == undefined)
        this.SetAttribute('Version', new Solidspace.PlayerVersion(RequiredVersion.toString().split(".")));
    if (this.GetAttribute('Stream') == undefined)
        this.SetAttribute('Stream', false);
    if (this.GetAttribute('AlternativeUrl') == undefined)
        this.SetAttribute('AlternativeUrl', '');
    if (this.GetAttribute('MetaFileFree') == undefined)
        this.SetAttribute('MetaFileFree', false);

    this.InstalledVersion = Solidspace.Utility.GetPlayerVersion();
}
Solidspace.Base.prototype = {
    
    SetStream: function(Value)                  { this.SetAttribute('Stream', Value); },
    SetAlternativeUrl: function(Value)          { this.SetAttribute('AlternativeUrl', Value); },
    SetMetaFileFree: function(Value)            { this.SetAttribute('MetaFileFree', Value); },
    SetUserParam: function(Name, Value)         { this.UserParams[Name] = Value; },
    GetUserParam: function(Name)                { return this.UserParams[Name]; },
    GetUserParams: function()                   { return this.UserParams; },
    SetUserParams: function(Params)             { this.UserParams = Params; },    
    SetAttribute: function(Name, Value)         { this.Attributes[Name] = Value; },
    GetAttribute: function(Name)                { return this.Attributes[Name]; },
    SetAttributes: function(Attribs)            { this.Attributes = Attribs; },
    GetHtml: function()
    {
        var Node = "", Name, Params = this.GetUserParams();
        
        if (Solidspace.Utility.IsIE())
            {
            Node += '<object codebase="' + Solidspace.Vars.CodebaseUrl + '" ';   
            Node += ' CLASSID="CLSID:BD08A9D5-0E5C-4f42-99A3-C0CB5E860557" width=0 height=0 border=0>';
            Node += '<param name="SkinSource" value="'+ this.GetAttribute('DataSource') +'" />';
            
            if (this.GetAttribute('MetaFileFree'))
                Node += '<param name="ContentSource" value="'+ this.GetAttribute('MetaFileSource') + '" />';
            else
                Node += '<param name="MetaFileSource" value="'+ this.GetAttribute('MetaFileSource') + '" />';
                
            Node += '<param name="IsInstalled" value="' + this.InstalledVersion.IsInstalled() + '"/>';

            if (this.GetAttribute('Stream')) 
                Node += '<param name="Stream" value="1"/>';
            for (Name in Params) 
                Node += '<param name="User.' + Name + '" value="' + Params[Name] + '"/>'; Node += "</object>";
            }
        else
            {
            Node += '<embed type="application/x-solidstate-ion" width=0 height=0 border=0 ';
            Node += ' SkinSource="' + this.GetAttribute('DataSource') + '" ';
            
            if (this.GetAttribute('MetaFileFree'))
                Node += ' ContentSource="' + this.GetAttribute('MetaFileSource') + '" ';
            else
                Node += ' MetaFileSource="' + this.GetAttribute('MetaFileSource') + '" ';

            if (this.GetAttribute('Stream')) 
                Node += ' Stream=1 ';
            for (Name in Params) 
                Node += ' User.' + Name + '="' + Params[Name] + '"'; Node += '/>';
            }

        return Node;
    },
    IsSupported: function() 
    {
        if (Solidspace.Utility.IsWindows())
            {
            if (Solidspace.Utility.IsIE())
                return true;
            if (Solidspace.Utility.IsFirefox())
                return true;
            }
            
        return false;
    },
    IsInstalled: function() 
    {
        return (this.skipDetect || this.InstalledVersion.IsValid(this.GetAttribute('Version')));
    },
    InstallPlugin: function(Html)
    {       
        if (Solidspace.Utility.IsIE())
            {           
            this.WritePlugin();
            }
        else
            {
            // Use setTimeout to let the rest of the page load first before we redirect
            setTimeout("document.location.replace(Solidspace.Vars.XpiUrl)", 1000);
            }
            
        return true;
    },
    Write: function()
    {        
        if (!this.IsSupported())
            {
            Solidspace.Nav.GotoUrlFramed(Solidspace.Vars.UnsupportedUrl, this.Attributes, this.UserParams);
            return true;
            }

        if (this.IsInstalled())
            {
            if (Solidspace.Utility.GetCookie(Solidspace.Vars.AgreeCookie) == 1)
                Solidspace.Utility.SetCookie(Solidspace.Vars.AgreeCookie, 0, 1);
            
            if (!this.GetAttribute('Stream'))
                {
                if (Solidspace.Utility.GetCookie(Solidspace.Vars.SuccessCookie) == 1)
                    {
                    Solidspace.Utility.SetCookie(Solidspace.Vars.SuccessCookie, 0, 1);
                    Solidspace.Nav.GotoUrlFramed(Solidspace.Vars.SuccessUrl);
                    }
                }
            
            this.WritePlugin();
            }
        else
            {
            Solidspace.Nav.GotoUrlFramed(Solidspace.Vars.AgreeUrl + "?RequiredVersion=" + this.GetAttribute('RequiredVersion'));
            }

        return true;
    },    
    WritePlugin: function()
    {
        //alert(this.GetHtml());
        Solidspace.Utility.AppendHtml(this.GetHtml());
    }    
}
Solidspace.Stream = function(DataSource, MetaFileSource, RequiredVersion) {
    var args = Array.prototype.slice.call(arguments);
    Solidspace.Base.apply(this, args);
    this.SetStream(true);    
}
Solidspace.Skin = function(DataSource, MetaFileSource, RequiredVersion) { 
    var args = Array.prototype.slice.call(arguments);
    Solidspace.Base.apply(this, args);
}
Solidspace.Stream.prototype = new Solidspace.Base();
Solidspace.Stream.prototype.constructor = Solidspace.Stream;
Solidspace.Skin.prototype = new Solidspace.Base();
Solidspace.Skin.prototype.constructor = Solidspace.Skin;

// add Array.push if needed (ie5)
if (Array.prototype.push == null) 
    {
    Array.prototype.push = function(item) {
        this[this.length] = item;
        return this.length;
        }
    }
    
if ((window.location.href.indexOf(Solidspace.Vars.AgreeUrl) != -1) || 
    (window.location.href.indexOf(Solidspace.Vars.InstructionsUrl) != -1))
    {
    if (window.addEventListener)
        window.addEventListener("load", Solidspace.Nav.OnLoad, false);
    else if (window.attachEvent)
        window.attachEvent("onload", Solidspace.Nav.OnLoad);
    else
        window.onload = Solidspace.Nav.OnLoad;
    }