// Stylesheet Handling
//
// super Erklärungen unter
// http://www.alistapart.com/articles/alternate/
//

// open Code:
// das Stylesheet-Handling soll immer passieren,
// wenn eine Seite geladen wird
// window.onload = style_init ;
window.onunload = style_keep ;

// ---------------------------------------------
// Anwendungen von mir:


// Anzeige des aktuellen Stylesheet durch die Eingabe
// javascript:welches()
function welches() {
  alert("Das gegenwärtig aktive Stylesheet ist\n" + getActiveStyleSheet()) ;
  return ;
}

// Stylesheet gemäss Cookie aktivieren
function style_init() {
  var meins = GetCookie("mst","") ;
  if(meins != "") setActiveStyleSheet(meins) ;
  return ;
}

// das nächste Stylesheet aktivieren
function next_style() {
  var i, a ;                              // laufvariable
  var titlo, titln, titlf ;               // alt, neu, erstes
  var titla = getActiveStyleSheet() ;     // aktiv
  titlo = "" ;
  titlf = "" ;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
      && a.getAttribute("title")) {
      titln = a.getAttribute("title") ;
      if(titln != titlo) {
        if(titlo == "") {                  // das erste stylesheet
          titlf = titln ;                  // merken wir uns
        }
        else {
          if(titlo == titla) {             // das ist das nächste
            setActiveStyleSheet(titln) ;   // --> aktivieren
            SetCookie("mst",titln,1,"/") ; //     und sich merken
            return ;
          }
        }
        titlo = titln ;
      }
    }
  }
  // kein nächstes Stylesheet - also nehmen wir das erste
  setActiveStyleSheet(titlf) ;   // --> aktivieren
  SetCookie("mst",titlf,1,"/") ; //     und sich merken
}

// Das aktive Stylesheet behalten
function style_keep() {
  SetCookie("mst", getActiveStyleSheet(), 1, "/") ;
}

// ---------------------------------------------
// Infrastruktur aus
// http://www.alistapart.com/articles/alternate/

// Aktuelles Stylesheet ermitteln
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;
}

// Stylesheet aktivieren
function setActiveStyleSheet(title) {
  var i, a, main;
  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;
    }
  }
}


//-----------------------------------------------------------
// GetCookie
// Andreas Fischer, 1.5.2000
// This routine gets a cookie value
// use like this:
// GetCookie(name,dflt) ;
// where name is the name of the stored value (cookie Id)
//       dflt    a default value to be returned if no cookie exists

function GetCookie(name,dflt)
{
  var oval = dflt ;
  var arg  = name + "=";
  var argl = arg.length;
  var cool = document.cookie.length;
  var i    = 0;
  while(i < cool)
  {
    var offset = i + argl;
    if(document.cookie.substring(i, offset) == arg)
    {
      var ind = document.cookie.indexOf(";", offset);
      if(ind == -1)
      ind = document.cookie.length;
      oval = unescape(document.cookie.substring(offset, ind));
      break ;
    }
    else
    {
      i = document.cookie.indexOf(" ", i) + 1;
      if(i == 0)
      break;
    }
  }
  return oval;
}

//-----------------------------------------------------------
// SetCookie
// Andreas Fischer, 1.5.2000
// use like this:
// SetCookie(name, value [, retpd, path, domain, secure]);

function SetCookie(name, value) {
  var argv    = SetCookie.arguments;
  var argc    = SetCookie.arguments.length;
  var retp    = (argc > 1) ? argv[2] : null;

  if(retp == null)
  var expires = null ;
  else
  {
    var expires = new Date() ;
    now = expires.getTime() ;
    then = now + retp*86400000 ;
    expires.setTime(then) ;
  }
  var path    = (argc > 2) ? argv[3] : null;
  var domain  = (argc > 3) ? argv[4] : null;
  var secure  = (argc > 4) ? argv[5] : false;
  string = name + "=" + escape(value)
     + ((expires == null) ? "" : ("; expires=" + expires.toGMTString()))
     + ((path == null) ? "" : ("; path=" + path))
     + ((domain == null) ? "" : ("; domain=" + domain))
     + ((secure == true) ? "; secure" : "");
  document.cookie = string ;
}


// Empfehlung:
//
// Es wird zeitlich etwas weniger hoplrig, wenn das Stylesheet
// schon vom PHP beim Schreiben der Seite richtig gestellt wird.
// Die entsprechende Routine beim Öffnen der Seite könnte etwa
// so aussehen:

// Stylesheets:
// Das vom User bevorzugte ist im Cookie "mst"
// Es wird zuerst verwendet, danach alle andern
// als alternate Stylesheets

// $my_styles = array("Werkstatt"   => "infra/style.css",
//                   "Schneiderei" => "infra/style_sch.css") ;
// if($_COOKIE["mst"] != "") $act_style = $_COOKIE["mst"] ;
// else $act_style = key($my_styles) ;
// print "\n<link rel=\"stylesheet\" type=\"text/css\" href=\"" . $my_styles[$act_style] . "\" title=\"$act_style\" />" ;
// foreach($my_styles as $stt => $stf) {
//   if($stt != $act_style)
//     print "\n<link rel=\"alternate stylesheet\" type=\"text/css\" href=\"$stf\" title=\"$stt\" />" ;
// }

window.onload = function(e) {
  var title = GetCookie("mst",getPreferredStyleSheet()) ;
  // alert("gewünschtes Stylesheet: " + title) ;
  populateStyleSelectorBox() ;
  setActiveStyleSheet(title);
}
//  Diese Funktionen habe ich unter                  //
//  http://www.alistapart.com/stories/alternate/     //
//  gefunden, wo sie auch erklärt sind.              //
//  Sie liefern die nötige Infrastruktur,            //
//  um automatisch aus einer von mehreren            //
//  Darstellungsarten (Stylesheets) zu wählen.       //
//
//  Anwendung:                                       //
//  1. Skript im HEAD einbinden.                     //
//  2. VOR dem Skript mehrere LINKs zu den Style     //
//     Sheets einfügen. Alle mit Titeln versehen,    //
//     eines mit rel="stylesheet" und die übrigen    //
//     mit rel="alternate stylesheet"                //
//  3. Für manuelles Umschalten im MSIE muss eine    //
//     Möglichkeit bestehen, setActiveStyleSheet()   //
//     aufzurufen.                                   //

function setActiveStyleSheet(title) {
  var i, j, a, main, box ;
  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;
		if(box = document.getElementById("StyleSelectorBox")) {    // my addition: process
		  for(j=0 ; j<box.options.length ; j++) {                  // a style selector box
		    if(box.options[j].value == title) {
			  box.options[j].selected = true ;
			}
		  }
		}
	  }
    }
  }
}

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 populateStyleSelectorBox() {
  var i, a, box, stentry ;
  if(box = document.getElementById("StyleSelectorBox")) {
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
      if(a.getAttribute("rel").indexOf("style") != -1
         && a.getAttribute("title")
        ) {
        stentry = new Option(a.getAttribute("title"), a.getAttribute("title"), false, true);
        box.options[box.length] = stentry ;
      }
	}
  }
  return null;
}


