function setActiveStyleSheet(title)
{
    var i, a, main;
    
    currentSelection = title;
    
    if(title == "Default")
        title = defaultStyle;
    
    for(i = 0; (a = document.getElementsByTagName("link")[i]); i++)
    {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
        {
            a.disabled = true;
            
            if(a.getAttribute("title") == title)
                a.disabled = false;
        }
    }
}

function getActiveStyleSheet()
{
    var i, a;
    
    for(i = 0; (a = document.getElementsByTagName("link")[i]); i++)
    {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled)
            return a.getAttribute("title");
    }
    
    return null;
}

function getPreferredStyleSheet()
{
    var i, a;
    
    for(i = 0; (a = document.getElementsByTagName("link")[i]); i++)
    {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
      )
        return a.getAttribute("title");
    }
    return null;
}

function createCookie(name,value,days)
{
    if(days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days * 24 * 60 * 60 * 1000));
        var expires = "; expires="+date.toGMTString();
    }
    else
      expires = "";
    
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    
    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
        
        while(c.charAt(0)==' ')
            c = c.substring(1,c.length);
        
        if(c.indexOf(nameEQ) == 0)
            return c.substring(nameEQ.length,c.length);
    }
    
    return null;
}

window.onload = function(e)
{
    var cookie = readCookie("style");
    var title = cookie ? cookie : "Default";
    setActiveStyleSheet(title);
    
    for(i = 0; (a = document.getElementsByTagName("option")[i]); i++)
    {
        if(a.getAttribute("value"))
        {
            if(a.getAttribute("value") == title)
                a.setAttribute("selected", "selected");
        }
    }
}

window.onunload = function(e)
{
    createCookie("style", currentSelection, 365);
}

// Konami code
var completion = 0;
document.onkeydown = function(e)
{
    if(!e)
        e = window.event;
    
    var key = e.keyCode;
    
    if(completion == 10)
    {
        switch(key)
        {
        case 13:
            setActiveStyleSheet("Midnight");
            break;
        case 72: // H
            setActiveStyleSheet("Halloween");
            break;
        case 87: // W
            setActiveStyleSheet("Christmas");
            break;
        default:
            // do nothing
        }
    }
    
    if(completion == 0 && key == 38 ||
       completion == 1 && key == 38 ||
       completion == 2 && key == 40 ||
       completion == 3 && key == 40 ||
       completion == 4 && key == 37 ||
       completion == 5 && key == 39 ||
       completion == 6 && key == 37 ||
       completion == 7 && key == 39 ||
       completion == 8 && key == 66 ||
       completion == 9 && key == 65)
    {
        completion++;
    }
    else
        completion = 0;
};

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

