/**
 * A javascript file used to detect the current browser's platform.  Offers a couple
 * of utility functions for modifying the mozilla.com pages to offer the best
 * downloads for visitors. 
 *
 */



/*
 * Do platform detection
 */
var PLATFORM_OTHER    = 0;
var PLATFORM_WINDOWS  = 1;
var PLATFORM_LINUX    = 2;
var PLATFORM_MACOSX   = 3;
var PLATFORM_MAC      = 4;

var gPlatform = PLATFORM_WINDOWS;

if (navigator.platform.indexOf("Win32") != -1)
  gPlatform = PLATFORM_WINDOWS;
else if (navigator.platform.indexOf("Linux") != -1)
  gPlatform = PLATFORM_LINUX;
else if (navigator.userAgent.indexOf("Mac OS X") != -1)
  gPlatform = PLATFORM_MACOSX;
else if (navigator.userAgent.indexOf("MSIE 5.2") != -1)
  gPlatform = PLATFORM_MACOSX;
else if (navigator.platform.indexOf("Mac") != -1)
  gPlatform = PLATFORM_MAC;
else
  gPlatform = PLATFORM_OTHER;

/**
 * When a user comes to our download page, we try to detect their platform.  If
 * successful, this function will hide all the links for other platforms in an effort
 * to give people the best choice to download.
 *
 * @param string tag ID to search for <ul>'s in that we want to adjust (normally, this is main-feature)
 */
function offerBestDownloadLink(tagId) {

    var parent = document.getElementById(tagId);

    if (parent && gPlatform) {
        switch (gPlatform) {

            case PLATFORM_WINDOWS:
                setDownloadListClass(parent, 'os_windows');
                break;
            case PLATFORM_LINUX:
                setDownloadListClass(parent, 'os_linux');
                break;
            case PLATFORM_MACOSX:
                setDownloadListClass(parent, 'os_osx');
                break;
            default:
                // Leave all the links present and let the user choose
                break;
        }
    }
}

/**
 * Will set the download class to the input.  Used for hiding links to download for
 * other platforms.
 *
 * @param object the parent class for the download's <ul>
 * @param string class to add
 */
function setDownloadListClass(parent, cssClass) {

    if (parent) {
        var lists = parent.getElementsByTagName('ul');
        for (var i=0; i < lists.length; i++) {
            if (lists[i].getAttribute('class') && lists[i].getAttribute('class').indexOf('home-download') != -1) {
                lists[i].setAttribute('class', lists[i].getAttribute('class') + " " + cssClass);
            }

            // For IE
            if (lists[i].getAttribute('className') && lists[i].getAttribute('className').indexOf('home-download') != -1) {
                lists[i].setAttribute('className', lists[i].getAttribute('className') + " " + cssClass);
            }
                    
        }
    }
}

/**
 * Internet Explorer throws up the privacy bar if we try to offer a download after
 * the page is loaded.  This means by default we offer a direct link to the download,
 * and if they are using a browser that is not IE, we'll replace the download link to
 * the friendly "Thanks for downloading <3" page.
 * 
 * @param id string id of the tag to look in
 */
function replaceDownloadLinksForId(id) {

    if ( (window.location.protocol == "file:") ||                // Testing locally
         (window.navigator.userAgent.indexOf("SV1") != -1) ||    // IE6/SP2
         (window.navigator.userAgent.indexOf("MSIE 7") != -1)) { // IE7

        // Don't mess with the download URL.
        return;
    }

    var element = document.getElementById(id);

    if (element) {
        var links = element.getElementsByTagName('a');

        // For each link, split on the question mark and replace the beginning with a
        // local page instead of the download.mozilla.org hostname.  
        for (var i=0; i < links.length; i++) {
            var href = links[i].getAttribute('href');

            if (href.indexOf('?') != -1) {
                var temp = href.split('?');
                if (temp[0].indexOf('http://download.mozilla.org') == 0) {
                    links[i].setAttribute('href','/products/download.html?' + temp[1]);
                }
            }

        }
    }
}
