/* --------------- G L O B A L  H E L P E R S --------------------------------------------------- */

function $(id) {
      return document.getElementById(id);
}

function GetCookie(name) {
   var cookie = document.cookie;
   var prefix = name + "=";
   var begin = cookie.indexOf("; "+ prefix);

   if (begin == -1) {
      begin = cookie.indexOf(prefix);if (begin != 0) return null;} else {
      begin += 2;
   }

   var end = cookie.indexOf(";", begin);

   if (end == -1) end = cookie.length;

   return unescape(cookie.substring(begin + prefix.length, end));
}

function SetCookie ( name, value, expires, path, domain, secure ) {
   var exp_date = new Date ( expires );
   var cur_cookie =  name + "="+ escape ( value ) +
                     ( (expires) ? "; expires="+ exp_date.toGMTString() : "") +
                     ( (path) ? "; path="+ path : "") +
                     ( (domain) ? "; domain="+ domain : "") +
                     ( (secure) ? "; secure": "");

   document.cookie = cur_cookie;
}

function getPage() {
   var query = window.location.search;
   var args = query.split('&');
   var page = args[0].split('=');
   return page[1];
}


/* ---------------- C O N S T A N T S ------------------------------------------------------ */

var DEBUG         = false;
var MENU_ARROW    = '/cms/images/arrow.gif';
var SUBMENU_ARROW = '/cms/images/arrow_sub.gif';


/* --------------- C O N S T R U C T O R --------------------------------------------------- */

function FSDZ_Menu(menu_id, submenu_id) {
   var self = this;

   /* Properties */
   this.id       = menu_id;
   this.subId    = submenu_id;
   this.menu     = null;
   this.submenu  = null;
   this.expanded = false;
   this.links    = [];

   /* Methods */
   this.init          = FSDZ_Menu__init;
   this.expand        = FSDZ_Menu__expand;
   this.collapse      = FSDZ_Menu__collapse;
   this.menuOver      = FSDZ_Menu__menuOver;
   this.menuOut       = FSDZ_Menu__menuOut;
   this.setCurrent    = FSDZ_Menu__setCurrent;
   this.storeCurrent  = FSDZ_Menu__storeCurrent;

   /* Helper methods */
   this.getMenu       = FSDZ_Menu__getMenu;
   this.isSubMenuLink = FSDZ_Menu__isSubMenuLink;
   this.exists        = FSDZ_Menu__exists;

   /* Debugging Utility */
   this.debugArea;
}

function FSDZ_Menu__init() {
   var self = this;
	if (DEBUG) {
	   this.debugArea       = document.createElement('TEXTAREA');
	   this.debugArea.cols  = 60;
	   this.debugArea.rows  = 30;
	   this.debugArea.value = '';
	   this.debugArea.style.position = 'absolute';
	   document.body.appendChild(this.debugArea);
	}

   /* Initialize menu components */
   this.menu     = $(this.id);
   this.submenu  = $(this.subId);

   if (!this.exists(this.menu)) return;

   this.links    = this.menu.getElementsByTagName('a');

   /* Keep the current menu item in a cookie */
   this.storeCurrent();

   /* Attach event handler for menu items */
   for (var idx=0; idx<this.links.length; idx++) {
      var cur = this.links[idx];
      cur.onmouseover = this.menuOver;
      cur.onmouseout  = this.menuOut;
   }

   /* Attach handler to submenu root */
   if (this.exists(this.submenu)) {
      this.submenu.onclick = function(e) {
         if (self.getMenu().style.display == 'block') {
            self.collapse();
            return false;
         }
         if (self.getMenu().style.display == 'none') {
            self.expand();
            return false;
         }
      }
   }

   /* Set current item obtained through cookie */
   var current = GetCookie('cur');
   if (current != 'undefined') {
      this.setCurrent(current);
   } else {
      this.collapse();
   }
}

function FSDZ_Menu__storeCurrent(e) {
   SetCookie("cur", getPage());
}

function FSDZ_Menu__setCurrent(cur) {
   var cur_elem = $(cur);
   if (!this.exists(cur_elem)) return;

   if (this.isSubMenuLink(cur_elem)) {
      this.expand();
   } else {
      this.collapse();
   }
   /* Put current item in black */
   cur_elem.style.color = "black";
}

function FSDZ_Menu__menuOver(e) {
   /* Ignore the mouseover event if the menu/page is not fully loaded yet */
   if (!self.menu) return;

   e = e || window.event;

   var target = e.target || e.srcElement;
   var li = target.parentNode;

   if (self.menu.isSubMenuLink(target)) {
      li.style.listStyleImage = "url(" + SUBMENU_ARROW + ")";
   } else {
      li.style.listStyleImage = "url(" + MENU_ARROW + ")";
   }

   return false;
}

function FSDZ_Menu__menuOut(e) {
   e = e || window.event;

   var target = e.target || e.srcElement;
   var li     = target.parentNode;

   li.style.listStyleImage = "none";

   return false;
}

function FSDZ_Menu__collapse() {
   var submenu = this.getMenu();
   if (this.exists(submenu) && submenu.style.display == 'none') return;
   if (this.exists(submenu)) {
      submenu.style.display = 'none';
   }
}

function FSDZ_Menu__expand() {
   var submenu = this.getMenu();
   if (this.exists(submenu) && submenu.style.display == 'block') return;
   if (this.exists(submenu)) {
      submenu.style.display = 'block';
   }
}

function FSDZ_Menu__getMenu() {
   return $('faessler').parentNode.parentNode.parentNode;
}

function FSDZ_Menu__exists(elem) {
   return (typeof(elem) != 'undefined' && elem != null) ? true : false;
}

function FSDZ_Menu__isSubMenuLink(elem) {
   if (!this.exists(elem)) return false;
   return (elem.className == 'submenu_link') ? true : false;
}

