﻿// Add a stylesheet to the document and populate it with the specified styles.
// The styles argument can be a string or an object. If it is a string, it
// is treated as the text of the stylesheet. If it is an object, then each
// property defines a style rule to be added to the stylesheet. Property 
// names are selectors and their values are the corresponding styles
function addStyles(styles) {
    // First, create a new stylesheet
    var styleElt, styleSheet;
    if (document.createStyleSheet) { // If the IE API is defined, use it
        styleSheet = document.createStyleSheet();
    }
    else {
        var head = document.getElementsByTagName("head")[0]
        styleElt = document.createElement("style"); // New <style> element
        head.appendChild(styleElt);                 // Insert it into <head>
        // Now the new stylesheet should be the last one
        styleSheet = document.styleSheets[document.styleSheets.length-1]
    }

    // Now insert the styles into it
    if (typeof styles === "string") {
        // The argument is stylesheet text
        if (styleElt) styleElt.innerHTML = styles; 
        else styleSheet.cssText = styles;           // The IE API
    }
    else {
        // The argument is an object of individual rules to insert
        var i = 0;
        for(selector in styles) {
            if (styleSheet.insertRule) {
                var rule = selector + " {" + styles[selector] + "}";
                styleSheet.insertRule(rule, i++);
            }
            else {
                styleSheet.addRule(selector, styles[selector], i++);
            }
        }
    }
}

function changeStyle(title) 
{
    var lnks = document.getElementsByTagName('link');
    for (var i = lnks.length - 1; i >= 0; i--) 
    {
        if (lnks[i].getAttribute('rel').indexOf('style')> -1 && lnks[i].getAttribute('title')) 
        {
            lnks[i].disabled = true;
            if (lnks[i].getAttribute('title') == title) lnks[i].disabled = false;
        }
    }
}

    function checkBrowser()
    {
        if(browser.name.indexOf("msie",0)>-1)
        {
            changeStyle("main");
        }
        else
        {
            changeStyle("alt1");
        }
    }

// Define browser.name and browser.version for client sniffing, using code 
// derived from jQuery 1.4.1. Both the name and number are strings, and both 
// may differ from the public browser name and version. Detected names are: 
// 
// "webkit": Safari or Chrome; version is WebKit build number 
// "opera": the Opera browser; version is the public version number 
// "mozilla": Firefox or other gecko-based browsers; version is Gecko version 
// "msie": IE; version is public version number 
// 
// Firefox 3.6, for example, returns: { name: "mozilla", version: "1.9.2" }. 
var browser = (function() 
{ 
    var s = navigator.userAgent.toLowerCase(); 
    var match = /(webkit)[ \/]([\w.]+)/.exec(s) || /(opera)(?:.*version)?[ \/]([\w.]+)/.exec(s) || /(msie) ([\w.]+)/.exec(s) || !/compatible/.test(s) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec(s) || []; 
    return { name: match[1] || "", version: match[2] || "0" }; }());
