// Style sheet selector based on "A list apart" http://www.alistapart.com/articles/alternate/
function setActiveStyleSheet(styleSheet) {
    var i, a;
    var sitename="nicolas.brodu.numerimoire.net";
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            a.disabled = true;
            var astylesheet = a.getAttribute("href");
            astylesheet = astylesheet.substring(astylesheet.lastIndexOf("/")+1,astylesheet.length);
            if(astylesheet == styleSheet) a.disabled = false;
        }
    }
    // Added part that propagates decision on whole site
    for(i=0; (a = document.links[i]); i++) {
        // relative address to this site?
        if (a.href.indexOf(sitename) != -1) {
            var idx = a.href.indexOf("?style=");
            if (idx!=-1) {
                a.href = a.href.substring(0,idx) + "?style="+encodeURIComponent(styleSheet);
            }
            else a.href += "?style="+encodeURIComponent(styleSheet);
        }
    }
}

// Style sheet selector based on "A list apart" http://www.alistapart.com/articles/alternate/
function getPreferredStyleSheet() {
    var i, a;
    // retreive preference, if any
    if ((i=document.URL.indexOf("?style="))!=-1) {
        a = decodeURIComponent(document.URL.substring(i+7,document.URL.length));
        return a;
    }
    // No preference? Maybe a cookie was saved (always explicitly, don't bother user)
    if ((a=readCookie("style"))!=null) return a;
    // fallback to detection in document
    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")
        ) {
            var astylesheet = a.getAttribute("href");
            return astylesheet.substring(astylesheet.lastIndexOf("/")+1,astylesheet.length);
        }
    }
    return null;
}

/* Share code here only once instead of inserting it in each web page
   And be nice to lynx, etc: do not even display a form if javascript is disabled!
*/
function createStyleForm() {
    var i, a;
    var preferred = getPreferredStyleSheet();
    if (preferred!=null) setActiveStyleSheet(preferred);

    var formcode = "<form action=\"#\"><select onChange=\"setActiveStyleSheet(this.form.styleSelect.options[this.form.styleSelect.options.selectedIndex].title)\" name=\"styleSelect\" id=\"styleSelect\" size=\"1\">";

    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {

        // at the risk of a little confusion, use link "title" as value (langage dependent)
        // but use option "title" as the real selector (style sheet)
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            var astylesheet = a.getAttribute("href");
            astylesheet = astylesheet.substring(astylesheet.lastIndexOf("/")+1,astylesheet.length);
            formcode += "<option title=\""+astylesheet+"\"";
            if (astylesheet==preferred) formcode+=" selected=\"selected\"";
            formcode += ">"+ a.getAttribute("title") +"</option>";
        }
    }
    formcode += "</select>";

    // Do not display "save" button if we're sure it won't work
    if(navigator.cookieEnabled == true) {
        formcode += "<input type=\"button\" id=\"styleButton\" value=\""+saveButtonValue+"\" onClick=\"saveStyle(this.form.styleSelect.options[this.form.styleSelect.options.selectedIndex].title)\" title=\""+saveButtonTitle+"\" />";
    }
    formcode += "</form>";
    document.write(formcode);

}

// Cookie code now only used as fallback. Users hate cookies!!!

function saveStyle(value) {
    createCookie("style", value, 365);
}

// "A list apart" cookie code http://www.alistapart.com/articles/alternate/ 
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=/";
}

// "A list apart" cookie code http://www.alistapart.com/articles/alternate/ 
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;
}
