

/*
http://www.alistapart.com/articles/alternate
http://diveintohtml5.org/storage.html

Persistent sheets are always on.
    <link rel="stylesheet" type="text/css" href="persistent" />
Preferred sheets are on by default.
    <link rel="stylesheet" type="text/css" href="preferred" title="normal" />
Alternate sheets are off by default.
    <link rel="alternate stylesheet" type="text/css" href="alternate" title="alternate v1" />

*/

function supports_html5_storage() {
    try {
        return 'localStorage' in window && window['localStorage'] !== null;
    } catch (e) {
        return false;
    }
}

var style_switch = {
    onload: function() {
        style_switch.create_controls('accessibility');
        var style = style_switch.read();
        style = style ? style : style_switch.get_preferred();
        return style_switch.set_active(style);
    },
    onunload: function() {
        var style = style_switch.get_active();
        return style_switch.store(style);
    },
    create_controls: function(id) {
        return null;
        /*
        var container = app.get(id);
        if (!container) {
            return false;
        }
        var all = style_switch.get_all();
        for for (var style in all) {
          var lnk = document.createElement('a');
          var img = document.createElement('img');
          lnk.onclick = function() {
              style_switch.set_size(style);
              return false;
          }
          Dom.add(el, 'content');
          Event.add(el, 'click', function(e) {
            Dom.remove(this);
          });
            container.appendChild();
        }
        */
    },
    set_active: function(title) {
        var links = document.getElementsByTagName('link');
        var done = false;
        for (var i = 0; i < links.length; i++) {
            var a = links[i];
            if ((a.getAttribute('rel').indexOf('style') != -1) && a.getAttribute('title')) {
                a.disabled = true;
                if (a.getAttribute('title') == title) {
                    a.disabled = false;
                    done = true;
                }
            }
        }
        if (!done) return style_switch.set_active('normal normal');
    },
    get_active: function() {
        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;
    },
    get_preferred: function() {
        var links = document.getElementsByTagName('link');
        for (var i = 0; i < links.length; i++) {
            var a = links[i];
            if ((a.getAttribute('rel').indexOf('style') != -1) && (a.getAttribute('rel').indexOf('alt') == -1) && a.getAttribute('title')) {
                return a.getAttribute('title');
            }
        }
        return null;
    },
    get_all: function() {
        var alt = new Object;
        var links = document.getElementsByTagName('link');
        for (var i = 0; i < links.length; i++) {
            var a = links[i];
            var title = a.getAttribute('title');
            if ((a.getAttribute('rel').indexOf('style') != -1) && title) {
                alt[title] = title;
            }
        }
        return alt;
    },
    store: function(value) {
        if (supports_html5_storage()) {
            localStorage.style = value;
        } else {
            document.cookie = 'style=' + value + '; path=/';
        }
    },
    read: function() {
        if (supports_html5_storage()) {
            return localStorage.getItem('style');
        } else {
            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('style=') == 0) return c.substring(6, c.length);
            }
        }
        return null;
    },
    set_size: function(size) {
        var current = style_switch.get_active();
        var divider = current.indexOf(' ');
        var back = current.substr(0, divider);
        return style_switch.set_active(back + ' ' + size);
    },
    set_back: function(back) {
        var current = style_switch.get_active();
        var divider = current.indexOf(' ');
        var size = current.substr(divider + 1);
        return style_switch.set_active(back + ' ' + size);
    }
};

window.onload = function(e) {
    return style_switch.onload();
};

window.onunload = function(e) {
    return style_switch.onunload();
};
