﻿/* GLANCE CODE */
var IsIE, IsFirefox, IsWindows, BrowserVer;
var ClientVer = 0;

function glanceStart(pin) {
    // get or create pin
    if (pin == null) {
        pin = ('' + Math.random()).substring(2, 6);
        //RN=Math.floor(Math.random()*9999);
        while (pin.length < 4) {
            pin = '0' + pin;
        }
    }

    // detect browser
    InitBrowserInfo();

    // get glance version
    if (IsWindows && IsIE) {
        //the function is under glance.vbs 
        ClientVer = GetGlanceClientVersionIE();
    } else {
        //e.g apple mac, vbs does not seem to work on mac.    
        ClientVer = GetGlanceClientVersion();
    }
    ClientVer = parseFloat(ClientVer);
 
    if (ClientVer < 2.4) {
        // show error message
        if (confirm('Glance version could not be detected, you must have version 2.4 or greater to use this feature.\n\nIf you do not have Glance installed, click Ok to start the download now or click Cancel to return.')) {
            document.location.href = 'http://' + window.location.hostname + '/en-GB/downloads/Glance/GlanceSetup.exe';
        }
        return false;
    } else if (ClientVer >= 2.4 && ClientVer < 2.5) {
        document.location.href = 'glances:start/' + pin;
    } else if (ClientVer >= 2.5) {
        document.location.href = 'glance://startssn/www.glance.net?key=' + pin;
    }
    return true;
}

function glanceStop() {
    if (ClientVer < 2.4) {
        // do nothing
    } else if (ClientVer >= 2.4 && ClientVer < 2.5) {
        document.location.href = 'glances:end/randomkey';
    } else if (ClientVer >= 2.5) {
        document.location.href = 'glance://endssn';
    }
}

function detectPlugin() {
    navigator.plugins.refresh();
    for (n = 0; n < navigator.plugins.length; n++) {
        if (navigator.plugins[n].name.indexOf("Glance") >= 0) return true;
    }
    return false;
}

function writePlugin() {
    // Check for plugin already written
    if (document.getElementById('glancePlugin')) {
        //alert("plugin already created");
        return true;
    }

    // Only write plugin if it already exists to avoid puzzle piece
    if (detectPlugin()) {
        // For some reason, creating an object element directly didnt work...had to create a div and set innerHTML          
        var objdiv = document.createElement("div");
        document.body.appendChild(objdiv);
        objdiv.innerHTML = '<object id="glancePlugin" type="application/x-vnd-glance"></object>';
        //alert("created plugin!");
        return true;
    }
    return false;
}

function GetGlanceClientVersion() {   
    try {
        navigator.plugins.refresh();
        var mt = navigator.mimeTypes["application/x-vnd-glance"];
        var p = mt.enabledPlugin;
        if (p == null) {
            //cannot find plugin
            return "0";
        } 
        //p.name currently in format of 'Glance Plug-In 2.5'     
        return p.name.substring(p.name.lastIndexOf(' '));                             
    } catch (err) {
        //return 0 - this allows the other function to inform customer to download glance.
        return "0";
    }
}

function InitBrowserInfo() {
    var firefoxPos = navigator.userAgent.indexOf("Firefox");
    var msiePos = navigator.userAgent.indexOf("MSIE");

    IsIE = msiePos != -1;
    IsFirefox = firefoxPos != -1;
    IsWindows = navigator.userAgent.indexOf("Win") != -1;

    if (IsIE) {
        BrowserVer = navigator.userAgent.substr(msiePos + 5, 3);
    } else if (IsFirefox) {
        BrowserVer = navigator.userAgent.substr(firefoxPos + 8);
    }
}

/* END OF GLANCE CODE */
