﻿var acceptedBrowsers = [ "msie 8", "msie 7", "msie 6", "firefox/2", "firefox/3"];
var acceptedOS = ["windows nt 6.1", "windows nt 6.0", "windows nt 5.2", "windows nt 5.1"];

//remove leading zeros from version number 
//so ParseInt function works correctly
function removeLeadingZeros(numString)
{
    var pos = 0;
    for (i=0; i < numString.length; i++)
    {
        if (numString.substr(i, 1) == '0')
        {
            pos++;
        }
        else
        {
            break;
        }
    }
    return numString.substr(pos);
}

//takes string like '42' and returns '0042' 
//where desired width is 4 in the above example.
function zeroPadFront(inputString, desiredWidth)
{
    var paddedString = inputString;
    for (var i = 0; i < desiredWidth - inputString.length; ++i)
    {
        paddedString = '0' + paddedString;
    }
    return paddedString;
}

function convertDotVersionStringToZeroPaddedString(s)
{
    if(s.indexOf('.') == -1)
        return s;
    
    var dottedVersion = s.split('.'); 
    var zeroPaddedVersionString = "";
    for (var i = 0; i < dottedVersion.length; ++i) 
    {
        // Note that we need to pad the D of A.B.C.D to 5 digits to be compatible with pre-M11
        // versioning.
        var width = (dottedVersion.length - 1 == i)? 5 : 4;
        zeroPaddedVersionString = zeroPaddedVersionString + zeroPadFront(dottedVersion[i], width);
    }
    return zeroPaddedVersionString;
}

function parseFirefoxVersionString(s)
{
    //string is formatted like "Photosynth 1234.1234.1234.12345"
    var parts = s.split(' ');
    return convertDotVersionStringToZeroPaddedString(parts[parts.length - 1]); 
}

function getPhotosynthInfo()
{
    var info = new Object();
    info.isPresent = false;
    info.version = 0;
    info.browser = 'unknown';
    
    if (isMSIE())
    {
        info.browser = 'ie';
        try
        {
            var ps = new ActiveXObject('Photosynth.Photosynth.2');
            if (ps)
            {
                info.isPresent = true;
                try
                {
                    info.version = parseInt(removeLeadingZeros(convertDotVersionStringToZeroPaddedString(ps.buildnum.toString())));
                }
                catch(pe)
                {
                    info.version = 0;
                }
            }
        }
        catch(ex)
        {
        }
    }
    if (isFirefox())
    {
        info.browser = 'firefox';          
        navigator.plugins.refresh();
        var plugin = navigator.plugins["Photosynth"];
        if (plugin != null && 
            plugin["application/x-photosynth2"] != null &&
            plugin["application/x-photosynth2"].type == "application/x-photosynth2")
        {
            info.isPresent = true;
            if (plugin.description.indexOf('.') > 0)
            {
                try
                {
                    info.version = parseInt(removeLeadingZeros(parseFirefoxVersionString(plugin.description)));
                }
                catch(ex)
                {
                    info.version = 0;
                }
            }
        }              
    }
    return info;    
}

function insertViewer()
{       
    if (typeof(_psConfig) == "undefined")  
    {
        if (isMSIE())
        {
            createAndInsertInstance('ie', false);            
        }    
        else if (isFirefox())
        {
            createAndInsertInstance('firefox', false);
        }
    }
    else
    {
        createAndInsertInstance(_psConfig.Browser, _psConfig.IsDebug); 
    }           
    
    function createAndInsertInstance(browserType, debug)
    {
        var psContainer = document.getElementById("photosynthbox");

        if (browserType == 'ie')
        {
            var viewer = document.createElement("OBJECT");
            try
            {
                viewer.classid = (debug == true) ? 'clsid:0426FA36-2272-4960-806E-12EAB1E7C633' : 'clsid:8D2B8AEE-6F3E-41FE-9F33-53A399E2F180';
            }
            catch (err)
            {
               //Unable to create classid so suppress any error. Will be redirected to create.aspx
            }
            viewer.id =  "Photosynth";        
            viewer.height = "100%";
            viewer.width = "100%";  

            if (psContainer)
            {
                var fc = psContainer.firstChild;
                psContainer.replaceChild(viewer, fc);           
            }  
        } 
        else if (browserType == 'firefox')
        {
            if (psContainer)
            {
                if (debug)
                {                
                    psContainer.innerHTML = '<embed id="Photosynth" type="application/x-photosynth2_d" width="100%" height="100%"\n>';
                }
                else
                {
                    psContainer.innerHTML = '<embed id="Photosynth" type="application/x-photosynth2" width="100%" height="100%"\n>';                
                }
            }
        }    
    }  
}

function isOSSupported() 
{
    var userAgent = navigator.userAgent.toLowerCase();

    for (i=0; i < acceptedOS.length; i++)
    {
        if (userAgent.indexOf(acceptedOS[i]) != -1)
        {
            return true;
        }
    }
    return false;
}

function isBrowserSupported()
{
    var userAgent = navigator.userAgent.toLowerCase(); 
    for (i=0; i < acceptedBrowsers.length; i++)
    {
        if (userAgent.indexOf(acceptedBrowsers[i]) != -1)
        {
            return true;
        }    
    }
    return false;
}

function isFirefox()
{
    return (navigator.userAgent.toLowerCase().indexOf("firefox") + navigator.userAgent.toLowerCase().indexOf("safari") >= 0);
}

function isMSIE()
{
    return (navigator.userAgent.toLowerCase().indexOf("msie") != -1);
}

function isMSIE7()
{
    return (navigator.userAgent.toLowerCase().indexOf("msie 7") != -1);
}

function loadCollection()
{
    var ps = document.getElementById("Photosynth"); 
    if (ps && _collectionInfo)
    {
        ps.collection = _collectionInfo.Url;
    } 
}

function launchSynther()
{
    var ps = document.getElementById('Photosynth');
    if (ps)
    {
        try
        {
            ps.launchSynther();
        }
        catch(e)
        {
            if (ps.launchSynther == undefined)
            {
                window.location = 'create.aspx';
            }
            else
            {
                window.location = 'error.aspx?EID=' + _LaunchSyntherError;
            }
        }
    }
}
  
function launchSyntherFromContentPage()
{
    insertViewer(); 
    setTimeout(launchSynther, 1000);   
}
