﻿/* 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) {
	    ClientVer = GetGlanceClientVersionIE();
    } else {
	    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 + '/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')) 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>';
        return true;
    }
    return false;
}

function GetGlanceClientVersion() {
    try {
        if (!writePlugin()) return '0';
        var thePlugin = document.getElementById('glancePlugin');
        return thePlugin.Version(); 
    } catch (err) {
		//document.write("Error calling plugin: " + err + "<br/>");
        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 */