/*
$Id: styleswitcher.js
Original from from http://www.alistapart.com/articles/alternate/
Modified by Jochen Arndt

Function SwitchLanguage() by Jochen Arndt
*/

/* Get plain CSS file name (without path and extension)
*/
function getStyleName(path) {
  var name = path;
  var pos = path.lastIndexOf("/");
  if (pos != -1) {
    var len = path.length - 1;
    name = path.substr(pos+1, len);
  }
  pos = name.indexOf(".css");
  if (pos != -1) name = name.substr(0, pos);
  return name;
}

function readCookie() {
  if(navigator.cookieEnabled != false) {
    var nameEQ = "style=";
    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;
}

function createCookie(value,days) {
  if(navigator.cookieEnabled != false) {
    if (days) {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = "; expires="+date.toGMTString();
    }
    else expires = "";
    document.cookie = "style="+value+expires+"; path=/";
  }
}

/* Get name of active style sheet (must have title attribute)
    Checks first for an active alternative style sheet
    If none found, the name of an active global sheet or an empty string is returned
*/
function getActiveStyleSheet() {
  var i, a;
  var global = "";
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) {
      var name = getStyleName(a.getAttribute("href"));
      if(a.getAttribute("rel").indexOf("alt") != -1)
        return name;
      if (global == "")
        global = name;
      }
  }
  return global;
}

/* Select active style sheet if not the current one
*/
function setActiveStyleSheet(style) {
  var i, a;
  if (style && style != getActiveStyleSheet()) {
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
      if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
        a.disabled = true;
        if(getStyleName(a.getAttribute("href")) == style) {
          a.disabled = false;
/*          if (a.getAttribute("rel").indexOf("alt") != -1) file = a.getAttribute("href"); */
        }
      }
    }
/*    createCookie(style, 7); */
/* Old Konqueror versions does not switch style when changing disabled flag
   So we change the global sheet file
*/
/*
    if (file != "") {
      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")) {
          a.href = file;
      }
    }
*/
  }
}

/* Check if alternative sheet is stored in cookie
    and switch to if not the current one
*/
window.onload = function(e) {
  setActiveStyleSheet(readCookie());
}
/* Save currently selected style
    Necessary if style has been changed by browser menu (not supported by IE)
*/
window.onunload = function(e) {
  createCookie(getActiveStyleSheet(), 7);
}

/* Jump from current page to corresponding page in other language.
   Uses index page as destination by default.
   Get current page without path and extension from URL, prefix
   with new directory and append new postfix and extension.
*/
function SwitchLanguage() {
  var path = "./german/";
  var name = "";
  var name_ext = "_d";
  var ext = ".htm";
  var uri = document.URL;
  pos = uri.lastIndexOf("/");
  if (pos != -1) {
    name = uri.substr(pos+1);
    pos = name.indexOf(".");
    if (pos != -1) {
      ext = name.substr(pos);
      name = name.substr(0, pos);
      pos = name.lastIndexOf("_d");
      if (pos != -1) {
        name = name.substr(0, pos);
        name_ext = "";
      }
    }
    else {
      name = "index";
    }
    if (name == "jobs" || name == "anfahrt" || name == "design") {
      name = "index";
    }
  }
  if (uri.lastIndexOf("german/") != -1) {
    if (name.substr(0, 8) == "mailform") {
      path = "../english/";
    }
    else {
      path = "../";
    }
  }
  else if (uri.lastIndexOf("english/") != -1) {
      path = "../german/";
  }
  if (name == "") {
    name = "index";
  }
  if (name == "index") {
    name_ext = "";
  }
  document.location.href = path + name + name_ext + ext;
}

/* Open file in new window/tab like HTML a tag with target="_blank"
*/
function OpenBlank(ref) {
  NewWin = window.open(ref, "_blank");
  NewWin.focus();
}
