// notifies google analytics if the user has Silverlight installed
function SetAnalyticsCookieWithSilverlightExistance(tracker) {
    try {
        if (IsSilverlightInstalled()) {
            tracker._setVar('Silverlight');
        }
        else {
            tracker._setVar('NoSilverlight');
        }
    }
    catch (err) {
        alert(err);
    }
}

function InitDownloadButton() {
    try {
        if (GetPlatform() == 'linux') {
            DisableDownloadButton('Sorry, Headup currently works only on Windows or Mac', false);
        }
        else if (IsFirefox() == false) {
            DisableDownloadButton('Sorry, Headup currently works only on Firefox', true);
        }
        else {
            
        }
    }
    catch (err) {
    }
}

function DownloadClick() {
    if (IsFirefox() == false) {
        InitDownloadButton();
    }
    else {
        try {
            pageTracker._trackPageview('/outgoing/xpidownload.com');
            var image = document.getElementById('allowImage');
            if (image != null)
                image.style.visibility = 'visible';
        }
        catch (err) {
        }
        DownloadXPI();
    }
}

function DisableDownloadButton(text, padding) {
    var button2 = document.getElementById('button1');
    button2.setAttribute('src', 'http://www.headup.com/Images/get_headup_disabled.jpg')
    button2.src = 'http://www.headup.com/Images/get_headup_disabled.jpg';
    button2.onmouseout = null;
    button2.onmouseover = null;
    button2.onclick = null;
    button2.style.cursor = null;
    button2.alt = text;
    button2.title = text;

    button2.onclick = function() {
        var ver = document.getElementById('sorryText');
        ver.setAttribute('class', 'no_firefox');
        if (padding)
            ver.innerHTML = '&nbsp; &nbsp; &nbsp; ' + text;
        else
            ver.innerHTML = text;

        var note = document.getElementById('note1');
        if (note != null)
            note.parentNode.removeChild(note);

        var note = document.getElementById('note2');
        if (note != null)
            note.parentNode.removeChild(note);
    };
}

function IsSilverlightInstalled() {
    try {
        return Silverlight.isInstalled('2.0');
    }
    catch (err) {
        // probaly not installed.
        return false;
    }
}

function correctPNG() /* correctly handle PNG transparency in Win IE 5.5 or 6. */
{
    var arVersion = navigator.appVersion.split("MSIE")
    var version = parseFloat(arVersion[1])
    if ((version >= 5.5) && (document.body.filters)) {
        for (var i = 0; i < document.images.length; i++) {
            var img = document.images[i]
            var imgName = img.src.toUpperCase()
            if (imgName.substring(imgName.length - 3, imgName.length) == "PNG") {
                var imgID = (img.id) ? "id='" + img.id + "' " : ""
                var imgClass = (img.className) ? "class='" + img.className + "' " : ""
                var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
                var imgStyle = "display:inline-block;" + img.style.cssText
                if (img.align == "left") imgStyle = "float:left;" + imgStyle
                if (img.align == "right") imgStyle = "float:right;" + imgStyle
                if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
                var strNewHTML = "<span " + imgID + imgClass + imgTitle
    + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
    + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
    + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
                img.outerHTML = strNewHTML
                i = i - 1
            }
        }
    }
}

function InitPage() {
    if (IsSilverlightInstalled() == false) {
        ShowElementById('no_silverlight');
        HideOnNoSilverlight();
    }
    
    InitDownloadButton();
    correctPNG();
    SetAnalyticsCookieWithSilverlightExistance(pageTracker);
}

function HideOnNoSilverlight() {
    var elementsToHide = document.getElementsByName('HideOnNoSilverlight');
    for (var i = 0; i < elementsToHide.length; i++) {
        HideElement(elementsToHide[i]);
    }
}

function GetPlatform() {
    try {
        var platform = navigator.platform.toLowerCase();

        if (platform.indexOf('mac') != -1) {
            return 'mac';
        }

        if (platform.indexOf('linux') != -1) {
            return 'linux';
        }

        var osVersion = navigator.oscpu;
        if (osVersion.indexOf('6.0') != -1) {
            return 'vista';
        }


        return 'xp';
    }
    catch (err) {
        return '';
    }
}

function setHeadupCookie() {
    var args = getArgs();
    if (args['u'] != null) {
        setCookie('u', args['u'], 30);
    }
    else {
        window.location = 'index.html';
    }
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
            ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function getArgs() {
    var args = new Object();
    var query = location.search.substring(1);
    var pairs = query.split("&");
    for (var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
        if (pos == -1) continue;
        var argname = pairs[i].substring(0, pos);
        var value = pairs[i].substring(pos + 1);
        args[argname] = unescape(value);
    }
    return args;
}

function IsFirefox() {
    try {
        var ua = navigator.userAgent.toLowerCase();
        if (ua.indexOf("firefox") == -1) {
            return false;
        }
        else {
            return true;
        }
    }
    catch (err) {
        return false;
    }
}

function IsIE() {
    try {
        var ua = navigator.userAgent.toLowerCase();
        if (ua.indexOf("msie") == -1) {
            return false;
        }
        else {
            return true;
        }
    }
    catch (err) {
        return false;
    }
}

var sanity = 0;

function DownloadXPI() {
    try {
        if (xpiPath != null) {
            window.location = xpiPath;
        }
        else {
            sanity++;
            if (sanity < 5)
                setTimeout(DownloadXPI, 1000);
        }
    }
    catch (err) {
    }
}

function HideElement(element) {
    if (element != null) {
        element.style.display = 'none';
        element.style.height = '0px';
        element.style.width = '0px';
    }
}

function HideElementById(id) {
    HideElement(document.getElementById(id));
}

function ShowElement(element) {
    if (element != null) {
        element.style.display = '';
        element.style.height = '';
        element.style.width = '';
    }
}

function ShowElementById(id) {
    ShowElement(document.getElementById(id));
}

/* Client-side access to querystring name=value pairs
Version 1.3
28 May 2008
    	
License (Simplified BSD):
http://adamv.com/dev/javascript/qslicense.txt
*/
function Querystring(qs) { // optionally pass a querystring to parse
    this.params = {};

    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;

    // Turn <plus> back to <space>
    // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&'); // parse out name/value pairs separated via &

    // split out each name=value pair
    for (var i = 0; i < args.length; i++) {
        var pair = args[i].split('=');
        var name = decodeURIComponent(pair[0]);

        var value = (pair.length == 2)
			    ? decodeURIComponent(pair[1])
			    : name;

        this.params[name] = value;
    }
}

Querystring.prototype.get = function(key, default_) {
    var value = this.params[key];
    return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
    var value = this.params[key];
    return (value != null);
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

    // public method for url encoding
    encode: function(string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode: function(string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}