/* RayV Embed Library */

/* Defaults - Start */
var TutorialURL = "http://static.rayv-inc.com/ViewerEmbed/tutorial_[LANG].swf";
var ActiveXURL = "http://update.rayv.com/viewer/webinstall/ActiveXInstall1.0/rayvactivex.cab";
var PluginURL = "http://update.rayv.com/viewer/getnewestversion/setup?affiliate=[AFFILIATE]"; //"http://update.rayv.com/viewer/webinstall/rayvplugin.xpi";
/* Defaults - End */

var Rayv = function() {};
Rayv.DetectableWithVB = false;
Rayv.SessionActivated = false;

/* API - Start */
Rayv.Embed = function(containerElementId,embedParams)
{
    var rayv = new Rayv();
    Rayv.control = rayv;
    
    rayv.containerElement = document.getElementById(containerElementId);
    if (rayv.containerElement != null)
    {
        rayv.setDefaultParams(embedParams);
        rayv.sortEmbedParams();
        rayv.embed();
    }
}

Rayv.IsPlatformSupported = function()
{
    var os = Rayv.GetOSType();
    var browser = Rayv.GetBrowserType();
    
    if (((os == "X" || os == "V") && (browser == "IE" || browser == "FF")) ||
        ((os == "M") && (browser == "SA"))) {
        return true;
    }
    
    return false;
}

Rayv.GetRayvViewerObject = function()
{
    if (Rayv.control.browserType=="IE")
    {
        return document.getElementById(Rayv.control.embedParams["id"]);
    }
    else
    {
        return document.embeds[Rayv.control.embedParams["name"]];
    }
}
/* API - End */

/* ExternalInterface - Start */
Rayv.InitEmbed = function()
{
    setTimeout("Rayv.control.initEmbed()", 0);
}

Rayv.AddBookmark = function()
{
    title = document.title;
    url = window.location.href;
    
    if (window.sidebar) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(title, url,"");
    } else if (window.external) { // IE Favorite
        window.external.AddFavorite (url, title);
    } else if (window.opera && window.print) { // Opera Hotlist
        return true;
    }
}
/* ExternalInterface - End */

/* Global Functions - Start */
Rayv.GetOSType = function()
{
    var type="O";
    if (navigator.userAgent.toLowerCase().indexOf("windows nt 5")!=-1) type="X";
    else if (navigator.userAgent.toLowerCase().indexOf("windows nt 6")!=-1) type="V";
    else if (navigator.userAgent.indexOf("Mac")!=-1) type="M";
    else if (navigator.userAgent.indexOf("X11")!=-1) type="U";
    else if (navigator.userAgent.indexOf("Linux")!=-1) type="L";
    return type;
}

Rayv.GetBrowserType = function()
{
    var type="OT";
    if (navigator.userAgent.indexOf("Firefox")!=-1) type="FF";
    else if(navigator.userAgent.indexOf("MSIE")!=-1) type="IE";
    else if(navigator.userAgent.indexOf("Safari")!=-1) type="SA";
    return type;
}

Rayv.IsInstalled = function()
{
      var attempt = false
    
      // if plugins array is there and not fake
      if (navigator.plugins && navigator.plugins.length > 0)
      {
            var pluginsArrayLength = navigator.plugins.length;
            // for each plugin...
            for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++)
            {
                  // if desired plugin name is found in either plugin name or description
                  if( (navigator.plugins[pluginsArrayCounter].name.indexOf('RayV') >= 0) || 
                  (navigator.plugins[pluginsArrayCounter].description.indexOf('RayV') >= 0) )
                  {
                        return true;
                  }
            }
            
            attempt = true;
      }
    
      if (Rayv.DetectableWithVB)
      {
            if (detectActiveXControl('rayvactivex.RayVActiveXCtrl'))
                  return true;
                
            attempt = true;
      }

      if (!attempt)
      {
            // If we have no method to detect installed plugins assume it's installed
            return true;
      }
      
      return false;
}

Rayv.MarkInstallationInProgress = function()
{
    if (!Rayv.IsInstallationInProgress())
    {
        document.cookie = 'RayvInstall=true';
        
        if (document.cookie.indexOf('RayvInstall=true')<0)
            window.location = window.location.href+"#ri";
    }
}

Rayv.IsInstallationInProgress = function()
{
    if (document.cookie.indexOf('RayvInstall=true')>=0 || window.location.href.indexOf("#ri")>0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Rayv.CheckInstallerActive = function()
{
    clearInterval(Rayv.InstallerInterval);
    Rayv.InstallerInterval = setInterval("Rayv.CheckInstallerActiveInternal()",1000);
}

Rayv.CheckInstallerActiveInternal = function()
{
    var embed = Rayv.GetRayvViewerObject();
    
    if (!Rayv.SessionActivated && typeof(embed.SessionActive)!="undefined")
    {
        Rayv.SessionActivated = true;
        //custom logic can be added here
    }
    
    if (typeof(embed.version)!="undefined")
    {
        clearInterval(Rayv.InstallerInterval);
        Rayv.control.removeTutorial();
        Rayv.control.setEmbedVisibility(true);
    }
}
/* Global Functions - End */

/* Prototype Functions - Start */
Rayv.prototype.setDefaultParams = function(embedParams)
{
    if (!embedParams) embedParams = new Object();
    this.embedParams = embedParams;
    
    if (typeof(this.embedParams["id"])=="undefined")
        this.embedParams["id"] = this.containerElement.id+"RayvEmbed";
    if (typeof(this.embedParams["name"])=="undefined")
        this.embedParams["name"] = this.embedParams["id"];
    if (this.embedParams["InstallMode"]==undefined || this.embedParams["InstallMode"]=="")
        this.embedParams.InstallMode = "OnClick";
    if (this.embedParams["CoverImageURL"]!=undefined && this.embedParams["CoverImageURL"]!="")
        this.coverImageUrl = this.embedParams["CoverImageURL"];
    if (this.embedParams["Affiliate"]==undefined || this.embedParams["Affiliate"]=="")
        this.embedParams.Affiliate = "RayV";
    if (this.embedParams["Language"]==undefined || this.embedParams["Language"]=="")
        this.embedParams.Language = "en";
    if (this.embedParams["ChannelID"]==undefined || this.embedParams["ChannelID"]=="")
        this.embedParams.ChannelID = "start";
    if (this.embedParams["SessionActive"]==undefined || this.embedParams["SessionActive"]=="")
        this.embedParams.SessionActive = "1";
    if (this.embedParams["InstallBGColorStart"]==undefined || this.embedParams["InstallBGColorStart"]=="")
        this.embedParams.InstallBGColorStart = "2B3B4A";
    if (this.embedParams["InstallBGColorEnd"]==undefined || this.embedParams["InstallBGColorEnd"]=="")
        this.embedParams.InstallBGColorEnd = "6690B3";
    if (this.embedParams["TutorialURL"]==undefined)
        this.embedParams.TutorialURL = TutorialURL.replace("[LANG]", this.embedParams.Language);
    
    this.installMode = this.embedParams.InstallMode.toLowerCase();
    this.osType = Rayv.GetOSType();
    this.browserType = Rayv.GetBrowserType();
    
    this.embedParams["width"] = this.getStyle("width");
    this.embedParams["height"] = this.getStyle("height");
    this.embedParams["classid"] = "CLSID:3B1E1AB9-98C2-4B7E-AE01-59C84302BBDB";
    this.embedParams["codebase"] = ActiveXURL;
    this.embedParams["type"] = "application/x-rayv-plugin" ;
    this.embedParams["pluginspage"] = PluginURL.replace("[AFFILIATE]", this.embedParams.Affiliate);
    if (this.osType=="M") this.embedParams["pluginspage"] += "&product=vmac";
}

Rayv.prototype.sortEmbedParams = function()
{
    this.objectAttributes = new Object();
    this.objectParameters = new Object();
    this.embedAttributes = new Object();
    
    for (var i in this.embedParams)
    {
        switch (i)
        {	
            case "pluginspage":
            case "type":
            case "src":
                this.embedAttributes[i] = this.embedParams[i];
                break;
            case "data":
            case "codebase":
            case "classid":
            case "id":
            case "onafterupdate":
            case "onbeforeupdate":
            case "onblur":
            case "oncellchange":
            case "onclick":
            case "ondblClick":
            case "ondrag":
            case "ondragend":
            case "ondragenter":
            case "ondragleave":
            case "ondragover":
            case "ondrop":
            case "onfinish":
            case "onfocus":
            case "onhelp":
            case "onmousedown":
            case "onmouseup":
            case "onmouseover":
            case "onmousemove":
            case "onmouseout":
            case "onkeypress":
            case "onkeydown":
            case "onkeyup":
            case "onload":
            case "onlosecapture":
            case "onpropertychange":
            case "onreadystatechange":
            case "onrowsdelete":
            case "onrowenter":
            case "onrowexit":
            case "onrowsinserted":
            case "onstart":
            case "onscroll":
            case "onbeforeeditfocus":
            case "onactivate":
            case "onbeforedeactivate":
            case "ondeactivate":
                this.objectAttributes[i] = this.embedParams[i];
                break;
            case "width":
            case "height":
            case "align":
            case "vspace": 
            case "hspace":
            case "class":
            case "title":
            case "accesskey":
            case "name":
            case "tabindex":
                this.embedAttributes[i] = this.objectAttributes[i] = this.embedParams[i];
                break;
            default:
                this.embedAttributes[i] = this.objectParameters[i] = this.embedParams[i];
        }
    }
}

Rayv.prototype.embed = function()
{
    if (Rayv.IsInstalled())
    {
        this.addEmbed(true);
    }
    else if (Rayv.IsInstallationInProgress())
    {
        this.addTutorial(3);
        this.initEmbed();
    }
    else if (this.coverImageUrl && !this.imageElement)
    {
        this.addImage();
    }
    else
    {
        if (this.imageElement)
        {
            this.removeImage();
        }
        
        if (this.installMode=="onload")
        {
            this.addTutorial(2);
        }
        else if (this.installMode=="onclick")
        {
            this.addTutorial(1);
        }
    }
}

Rayv.prototype.getStyle = function(styleProp) {
	var style;
	if (this.containerElement.currentStyle) {
        style = this.containerElement.currentStyle[styleProp];
    } else if (window.getComputedStyle) {
		style = document.defaultView.getComputedStyle(this.containerElement, null).getPropertyValue(styleProp);
    }
	return style;
}

Rayv.prototype.initEmbed = function()
{
    if (this.browserType=="IE")
    {
        this.addEmbed(false);
        Rayv.MarkInstallationInProgress();
        Rayv.CheckInstallerActive();
    }
    else
    {
        //download the exe until plugin is authorized by mozilla
        window.location.href = this.embedParams["pluginspage"];
    }
}

Rayv.prototype.addEmbed = function(visible)
{
    this.embedElement = document.createElement("div");
    this.containerElement.appendChild(this.embedElement);
    this.embedElement.innerHTML = this.generateEmbedHTML();
    this.setEmbedVisibility(visible);
}

Rayv.prototype.setEmbedVisibility = function(visible)
{
    if (visible)
    {
        this.embedElement.style.display = "block";
    }
    else
    {
        this.embedElement.style.display = "none";
    }
}

Rayv.prototype.addTutorial = function(phase)
{
    this.tutorialElement = document.createElement("div");
    this.containerElement.appendChild(this.tutorialElement);
    this.tutorialElement.innerHTML = this.generateTutorialHTML(phase);
}

Rayv.prototype.removeTutorial = function()
{
    if (this.tutorialElement != null)
    {
        this.containerElement.removeChild(this.tutorialElement);
        this.tutorialElement = null;
    }
}

Rayv.prototype.addImage = function()
{
    this.imageElement = document.createElement("div");
    this.containerElement.appendChild(this.imageElement);
    this.imageElement.innerHTML = this.generateImageHTML();
}

Rayv.prototype.removeImage = function()
{
    if (this.imageElement != null)
    {
        this.containerElement.removeChild(this.imageElement);
        this.imageElement = null;
    }
}

Rayv.prototype.generateEmbedHTML = function()
{
    var html="";
    html = '<object ';
    for (var i in this.objectAttributes)
    {
        html += i + '="' + this.objectAttributes[i] + '" ';
    }
    html += '>';
    for (var i in this.objectParameters)
    {
        html += '<param name="' + i + '" value="' + this.objectParameters[i] + '" /> ';
    }
    html += '<embed ';
    for (var i in this.embedAttributes)
    {
        html += i + '="' + this.embedAttributes[i] + '" ';
    }
    html += ' ></embed></object>';
    
    return html;
}

Rayv.prototype.generateTutorialHTML = function(phase)
{
    var width = this.embedParams["width"];
    var height = this.embedParams["height"];
    var url = this.embedParams.TutorialURL;
    var color1 = this.embedParams.InstallBGColorStart;
    var color2 = this.embedParams.InstallBGColorEnd;
    var flashVars = "os="+this.osType+"&browser="+this.browserType+"&phase="+phase+"&color1="+color1+"&color2="+color2;
    
    return '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ' +
        'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" ' +
        'width="'+width+'" height="'+height+'" id="tutorial" align="middle">' +
        '<param name="allowScriptAccess" value="always"/>' +
        '<param name="allowFullScreen" value="false"/>' +
        '<param name="movie" value="'+url+'"/>' +
        '<param name="quality" value="high"/>' +
        '<param name="FlashVars" value="'+flashVars+'"/>' +
        '<embed src="'+url+'" width="'+width+'" height="'+height+'" flashVars="'+flashVars+'" ' +
        'name="tutorial" quality="high" align="middle" allowScriptAccess="always" allowFullScreen="false" ' +
        'type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"/>' +
        '</object>';
}

Rayv.prototype.generateImageHTML = function()
{
    var width = this.embedParams["width"];
    var height = this.embedParams["height"];
    var coverImageUrl = this.coverImageUrl;
    
    return '<a href="javascript:Rayv.control.embed();"><img src="'+coverImageUrl+'" ' +
        'width="'+width+'" height="'+height+'" alt="Click to install RayV Viewer"></img></a>';
}
/* Prototype Functions - End */

if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1))
{
    document.writeln('<script language="VBscript">');

    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
    document.writeln('Rayv.DetectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  Rayv.DetectableWithVB = True');
    document.writeln('End If');

    document.writeln('\'this next function will detect most plugins');
    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If Rayv.DetectableWithVB Then');
    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('\'and the following function handles QuickTime');
    document.writeln('Function detectQuickTimeActiveXControl()');
    document.writeln('  on error resume next');
    document.writeln('  detectQuickTimeActiveXControl = False');
    document.writeln('  If Rayv.DetectableWithVB Then');
    document.writeln('    detectQuickTimeActiveXControl = False');
    document.writeln('    hasQuickTimeChecker = false');
    document.writeln('    Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")');
    document.writeln('    If IsObject(hasQuickTimeChecker) Then');
    document.writeln('      If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then ');
    document.writeln('        detectQuickTimeActiveXControl = True');
    document.writeln('      End If');
    document.writeln('    End If');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('</scr' + 'ipt>');
}