/**
 * A javascript file used to detect the current browser's platform.  Offers a couple
 * of utility functions for modifying the paralog.net 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('download') != -1) {
                lists[i].setAttribute('class', lists[i].getAttribute('class') + " " + cssClass);
            }

            // For IE
            if (lists[i].getAttribute('className') && lists[i].getAttribute('className').indexOf('download') != -1) {
                lists[i].setAttribute('className', lists[i].getAttribute('className') + " " + cssClass);
            }
                    
        }
    }
}
