/* copied from http://www.ajaxschmiede.de/javascript/fenstergrose-und-scrollposition-in-javascript-auslesen/
 * and modified
 */
function getInnerSize() {
  var myWidth = 640, myHeight = 480;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return([myWidth, myHeight]);
}

/*
 * An URI datatype.  Based upon examples in RFC3986.
 *
 * TODO %-escaping
 * TODO split apart authority
 * TODO split apart query_string (on demand, anyway)
 *
 * @(#) $Id$
 *
 * copied from http://js-uri.googlecode.com/files/js-uri-0.1.zip
 * and modified
 *
 */

// Constructor for the URI object.  Parse a string into its components.
function URI(str) {
  if (!str) str = "";
  // Based on the regex in RFC2396 Appendix B.
  var parser = /^(?:([^:\/?\#]+):)?(?:\/\/([^\/?\#]*))?([^?\#]*)(?:\?([^\#]*))?(?:\#(.*))?/;
  var result = str.match(parser);
  this.scheme    = result[1] || null;
  this.authority = result[2] || null;
  this.path      = result[3] || null;
  this.query     = result[4] || null;
  this.fragment  = result[5] || null;
  return(false);
}

// Restore the URI to it's stringy glory.
URI.prototype.toString = function() {
  var str = "";
  if (this.scheme) {
    str += this.scheme + ":";
  }
  if (this.authority) {
    str += "//" + this.authority;
  }
  if (this.path) {
    str += this.path;
  }
  if (this.query) {
    str += "?" + this.query;
  }
  if (this.fragment) {
    str += "#" + this.fragment;
  }
  return(str);
};

URI.prototype.canonizePath = function() {
  if (this.path) {
    var aPath = this.path.split('/');
    var aPathCanon = new Array();
    for (var k in aPath) {
      if ((aPath[k] == '.') || (aPath[k] == '')) {
        continue;
      }
      if (aPath[k] == '..') {
        if (aPathCanon.length > 0) {
          aPathCanon.pop();
        }
      } else {
        aPathCanon.push(aPath[k]);
      }
    }
    this.path = '/' + aPathCanon.join('/');
  }
  return(this.path);
}

/* copied from http://www.11tmr.com/11tmr.nsf/D6Plinks/MWHE-695L9Z
   and modified */
function getURLParam(strParamName) {
  var objURI = new URI(location.href);
  var strReturn = '';
  if (objURI.query != null) {
    var strQueryString = objURI.query;
    if (strQueryString.length > 0){
      var aQueryString = strQueryString.split('&');
      for (var iParam = 0; iParam < aQueryString.length; iParam++ ) {
        if (aQueryString[iParam].indexOf(strParamName + '=') > -1) {
          var aParam = aQueryString[iParam].split('=');
          strReturn = aParam[1];
          break;
        }
      }
    }
  }
  return(unescape(strReturn));
}

/* Time-String in übliches Format umwandeln */
function FormTime(TimeStr) {
  var time = new Date(TimeStr);

  var Tag = time.getDate();
  if (Tag < 10) Tag = "0" + Tag;

  var Monat = time.getMonth() + 1;
  if (Monat < 10) Monat = "0" + Monat;

  var Stunde = time.getHours();
  if (Stunde < 10) Stunde = "0" + Stunde;

  var Minute = time.getMinutes();
  if (Minute < 10) Minute = "0" + Minute;

  var Sekunde = time.getSeconds();
  if (Sekunde < 10) Sekunde = "0" + Sekunde;

  var Jahr = time.getFullYear();

  return Tag + "." + Monat + "." + Jahr + ", " + Stunde + "." + Minute + "." + Sekunde;
}

/* Speicherdatum des Dokuments als Text liefern */
function getModified(mod) {
  if (mod != "") var lastmod = FormTime(mod);
  else var lastmod = FormTime(document.lastModified);

  return("Letzte Änderung: " + lastmod + " - ");
}

/* MailTo in Tag schreiben */
function setMailTo(user, domain, target)  {
  var text = "";
  var mailTo = user + "@" + domain;
  var node = document.getElementById(target);
  if (node.firstChild != null) {
    text = node.firstChild.nodeValue;
    node.removeChild(node.firstChild);
  }
  if (text == "") {
    text = mailTo;
  }
  var A = document.createElement("a");
  var AText = document.createTextNode(text);
  A.href = "mailto:" + mailTo;
  A.appendChild(AText);
  node.appendChild(A);
  return(false);
}

/* Speicherdatum des Dokuments in Tag schreiben */
function setModified(id, mod, user, domain) {
  var mailTo = user + "@" + domain;
  var node = document.getElementById(id);
  var nodeText = document.createTextNode(getModified(mod));
  var A = document.createElement("a");
  var AText = document.createTextNode(mailTo);
  A.href = "mailto:" + mailTo;
  A.appendChild(AText);
  node.appendChild(nodeText);
  node.appendChild(A);
  return(false);
}

function addURLToParam(pName) {
  var objURI = new URI(location.href);
  var aaParams = new Array();
  var strQuery = '';
  if (objURI.query != null) {
    strQuery = objURI.query;
  }
  var aParams = strQuery.split('&');
  for (var iParam = 0; iParam < aParams.length; iParam++ ) {
    var aParam = aParams[iParam].split('=');
    if ((aParam[0].length > 0) &&
        (typeof(aParam[1]) != 'undefined')) {
      aaParams[aParam[0]] = aParam[1];
    }
  }
  objURI.query = null;
  aaParams[escape(pName)] = escape(objURI.toString());
  var i = 0;
  for (param in aaParams) {
    aParams[i] = param + '=' + aaParams[param];
    i++;
  }
  return(aParams.join('&'));
}

/* neue Funktionen seit 29.03.2009 */
function initMainMenu() {
  var items = document.getElementById('mainmenue').getElementsByTagName('li');
  for(var i=0; i<items.length; i++) {
    items[i].className = 'menuClosed';
    items[i].firstChild.className = null;
  }
  return(false);
}

function openMenuItem(obj) {
  if (obj != null) {
    if (obj.id == 'mainmenue') {
      return(false);
    }
    obj.className = null;
    openMenuItem(obj.parentNode);
  }
  return(false);
}

function initMenuItem(obj) {
  initMainMenu();
  obj.className = 'current';
  openMenuItem(obj.parentNode);
  return(false);
}

/* Parst den Cookiestring und liefert ein Array mit den einzelnen Cookies oder
   null zurueck. */
function getCookieObj() {
  if (!document.cookie || document.cookie == "") return null;
  var cookies = document.cookie.split(";");
  var objCookies = new Object();
  for (var i = 0; i < cookies.length; i++) {
    var cookie = cookies[i].substring(cookies[i].search(/\S/),
      cookies[i].length).split("=");
    if (cookie[1]) objCookies[cookie[0]] = unescape(cookie[1]);
    else objCookies[cookie[0]] = "";
  }
  return objCookies;
}

/* Liefert den Wert eines Cookies oder null. */
function getCookie(name) {
  var objCookies = new getCookieObj();
  var value = objCookies[name];
  if (value || value == "") return value;
  return null;
}

function getNaviCookie() {
  var aNaviCookie = new Array('', '', '');
  var strNaviCookie = getCookie('sccNaviKeks');
  if (strNaviCookie && strNaviCookie.length) {
    aNaviCookie = strNaviCookie.split('&');
  }
  return(aNaviCookie);
}

function setNaviCookie(strTopID, strLeftID, strAnchor) {
  strDomain = '; domain=' + location.hostname.match(/(\w|-)*\.(\w|-)*$/g);
  document.cookie = 'sccNaviKeks=' + strTopID + '&' + strLeftID + '&' + strAnchor.replace(/^#/, '') + strDomain + '; path=/';
}

function setMenuItem(strMainID) {
  var aNaviCookie = new getNaviCookie();
  if (aNaviCookie[0] == strMainID) {
    setNaviCookie('', '', '');
    if (aNaviCookie[1].length) {
      var objMenuItem = document.getElementById(aNaviCookie[1]);
      if (objMenuItem) {
        var objURI = new URI(objMenuItem.href);
        objURI.fragment = aNaviCookie[2];
        parent.content.location.replace(objURI.toString());
        initMenuItem(objMenuItem);
      } else if (aNaviCookie[1] == '_index') {
        var objURI = new URI('./');
        objURI.fragment = aNaviCookie[2];
        parent.content.location.replace(objURI.toString());
      }
    }
  } else {
    parent.content.location.replace('./');
  }
  return(false);
}

function getMySet() {
  var strTopID;
  var strLeftID;
  if (location.protocol == 'file:') {
    return(false);
  }
  if (arguments.length == 3) {
    strTopID = arguments[1];
    strLeftID = arguments[2];
  } else {
    strTopID = arguments[0];
    strLeftID = arguments[1];
  }
  if ((strTopID == 'home') && (strLeftID == '_index')) {
    strDomain = '; domain=' + location.hostname.match(/(\w|-)*\.(\w|-)*$/g);
    document.cookie = 'kaschbadil=gimbolaska' + strDomain + '; path=/';
  }
  if ((top.frames.length == 0) || (parent.location.hostname != location.hostname)) {
    // not in a set or in a alien set
    setNaviCookie(strTopID, strLeftID, location.hash);
    top.location.replace('/');
  } else {
    var objTopItem = parent.topmenue.document.getElementById(strTopID);
    if (objTopItem) {
      parent.topmenue.initMenuItem(objTopItem);
      var strTopItem = objTopItem.href;
      if (strTopItem == parent.leftmenue.location.href) {
        var objLeftItem = parent.leftmenue.document.getElementById(strLeftID);
        if (objLeftItem) {
          parent.leftmenue.initMenuItem(objLeftItem);
        }
      } else {
        setNaviCookie(strTopID, strLeftID, location.hash);
        parent.leftmenue.location.replace(strTopItem);
      }
    }
  }
  return(false);
}
