var cPath = (window.getRelativeWebRoot ? window.getRelativeWebRoot() : '') + 'js/';

var cExtension = (window.location.pathname.match (/\.page$/) ? '.page' : '.html');

function setHeight(){
  var rows = dojo.query(".Row");
  if(rows != null && rows.length > 0){
    for(var i = 0; i<rows.length; i++){
      var columns = rows[i].childNodes;
      if(columns != null && columns.length>0){
        var height = 0;
        for(var j=0; j<columns.length; j++){

          if(dojo.hasClass(columns[j], "Column"))
            newHeight = dojo.contentBox(columns[j]).h;
          if(newHeight>height)
            height = newHeight;
        }
        if(dojo.isIE)height = height+1;
        for(var k = 0; k<columns.length; k++){
          if (columns[k].childNodes.length>0) {
            //console.debug (columns[k]);
            dojo.style(columns[k], "borderColor", "#7898D1");
          }
          dojo.style(columns[k], "height", height+5);
        }
      }
    }
  }
}


function dojoXHR(){

  dojo.xhrGet({
    url:'MenuEntries' + cExtension,
      handleAs: "text",

      load: function(response, ioArgs) {
        var node = document.createElement('div');
        node.innerHTML = response;
        initMainMenu (node.firstChild);
      var div = document.getElementById('mainMenu');
      div.parentNode.replaceChild(node.firstChild, div);
//			setTimeout (initMainMenu, 100);
    },

    error: function(response, ioArgs) {
      console.error("HTTP status code: ", ioArgs.xhr.status);
    }
  });


}

function initMainMenu (menu) {
  menu.delayClose = 400;
  menu.delayOpen = 200;
  menu.delayFirstOpen = 400;
  menu.isOpen = false;
  menu.timerClose = null;
  menu.timerOpen = null;
  menu.items = dojo.query("li > span > a", menu);

  menu.closeAll = function() {
    menu.items.every(
      function(a) {if (a.closeSubMenu) a.closeSubMenu(); return true;}
    );
    menu.isOpen = false;
  }

  var fn_mouseover = function() {
      clearTimeout (menu.timerClose);
      menu.timerOpen = setTimeout (dojo.hitch (this, this.openSubMenu), menu.isOpen ? menu.delayOpen : menu.delayFirstOpen);
    };
  var fn_mouseout = function() {
      clearTimeout (menu.timerOpen);
      menu.timerClose = setTimeout (dojo.hitch(menu, menu.closeAll), menu.delayClose);
    }
  var fn_openSubMenu = function() {
      if (this.ul) {
        this.ul.className = this.ul.className.replace (/ ?shown/, '') + ' shown';
        menu.isOpen = true;
      }
      var current = this;
      menu.items.every(
        function(a) {if (a != current && a.closeSubMenu) a.closeSubMenu(); return true;}
      );
    }
  var fn_closeSubMenu = function() {
      if (this.ul) this.ul.className = this.ul.className.replace (/ ?shown/, '');
    }

  var cCurrentMPID = 'MPID' + window.pagePath[0];
  menu.items.every(function(a) {
    if (a.id == cCurrentMPID) {
      li = a.parentNode;
      while (li && li.nodeName != 'LI') li = li.parentNode;
      if (li) li.className += ' current';
    }

    a.ul = a.nextSibling;
    while (a.ul && a.ul.nodeName != 'UL') a.ul = a.ul.nextSibling;

    a.onmouseover = fn_mouseover;
    a.onmouseout = fn_mouseout;
    a.openSubMenu = fn_openSubMenu;
    a.closeSubMenu = fn_closeSubMenu;
    return true;
  });

  dojo.query("li > a", menu).every(
    function(a) {
      a.onmouseover = function() {
        clearTimeout (menu.timerClose);
      }
      a.onmouseout = fn_mouseout;
      return true;
    }
  )
}

var browserName=navigator.appName;
var browserVer=parseInt(navigator.appVersion);
var version="";

var rot13map;

function rot13init()
{
  var map = new Array();
  var s   = "abcdefghijklmnopqrstuvwxyz";

  for (i=0; i<s.length; i++)
    map[s.charAt(i)]			= s.charAt((i+13)%26);
  for (i=0; i<s.length; i++)
    map[s.charAt(i).toUpperCase()]	= s.charAt((i+13)%26).toUpperCase();
  return map;
}

function rot13(a)
{
  if (!rot13map)
    rot13map=rot13init();
  s = "";
  for (i=0; i<a.length; i++)
    {
      var b = a.charAt(i);
      s	+= (b>='A' && b<='Z' || b>='a' && b<='z' ? rot13map[b] : b);
    }
  return s;
}

function unCryptMailto(s){
   var Ergebnis;
   s = rot13(s) + '';
   if (CONFIG['AntiSpamMail']['At']) {
     var regAusdruck = new RegExp("(.*)" + CONFIG['AntiSpamMail']['At'] + "(.*)", "g")
     Ergebnis = regAusdruck.exec(s);
     if (Ergebnis && Ergebnis.length > 1) location.href="mailto:" + Ergebnis[1] + "@" + Ergebnis[2];
   }
}
