/*
 * These are function to detect the browser plugin
 * Notice that these functions does NOT work on
 * a HTTPS connection, since most browsers
 * will complain about insecure content on a secure
 * connection
 */

function rewriteTestCanvas() {
    try {
        var target = document.getElementById("plugin-status");
        target.parentNode.removeChild(target);
        writeTestCanvas();
    } catch (e) {
        alert('Exception: '+e.message);
    }
}

function writeTestCanvas() {

    try {
        var sibling = document.getElementById("container");
        var target = sibling.parentNode;

        var div = document.createElement('div');
        div.id = "plugin-status";
        div.style.display = "none";

        var loc = ""+window.location;
        if (!loc.match(/^https/i)) {
            var img2 = document.createElement('img');
            img2.src = 'nabto://self/show_image?name=logo.png';
            div.appendChild(img2);

            var img3 = document.createElement('img');
            img3.src = 'nabto://self/feature_enabled?feature=sso';
            div.appendChild(img3);
        }

        target.insertBefore(div, sibling);
    } catch (e) {
        alert('Exception: '+e.message);
    }

}

/* Kudos to http://talideon.com/weblog/2005/02/detecting-broken-images-js.cfm */
function isImageOk(i) {
    var mydiv = document.getElementById("plugin-status");
    var images = mydiv.getElementsByTagName("img");

    // Avoid Safari errors.
    if (images.length - 1 < i) {
        return false;
    }

    img = images[i];

    // During the onload event, IE correctly identifies any images that
    // weren't downloaded as not complete. Others should too. Gecko-based
    // browsers act like NS4 in that they report this incorrectly.
    if (!img.complete) {
        return false;
    }

    // However, they do have two very useful properties: naturalWidth and
    // naturalHeight. These give the true size of the image. If it failed
    // to load, either of these should be zero.
    if (typeof img.naturalWidth!= undefined && img.naturalWidth== 0) {
        return false;
    }

    // No other way of checking: assume it's ok.
    return true;
}

function getPluginStatus() {
    var msg = "";

    msg += "Plugin detection has been tested in MSIE7/XP, Firefox3/MacOSX and Safari3/MacOSX.<br>";

    msg += "<li>nabto:// is ";
    msg += (isImageOk(0)) ? "supported" : "unsupported";
    msg += "</li>";
    msg += "</ul>";

    return msg;
}

function hasNabtoPlugin() {
    return isImageOk(0);
}

function hasNabtoPluginWithSSO() {
    return isImageOk(1);
}

function redirect() {
    // Replace all the urls &amp; with &
    // &amp; is the correct use of & on a html page
    // but we cannot redirect to this raw
    window.location.replace(url.replace(new RegExp("&amp;", "g"), "&"));
}

function addEvent(obj, evType, fn){ 
    if (obj.addEventListener){ 
        obj.addEventListener(evType, fn, false); 
        return true; 
    } else if (obj.attachEvent){ 
        var r = obj.attachEvent("on"+evType, fn); 
        return r; 
    } else { 
        return false; 
    } 
}

/*
var loc = ""+window.location;
if (loc.match(/^https/i)) {
    loc = loc.replace(/^https/i, "http");
    window.location.replace(loc);
}
*/


