// $Id: DEV-dhtml_menu.js,v 1.1.2.2 2007/11/28 21:03:57 brmassa Exp $

dhtmlMenu = {};

/**
 * Grabs the cookie data.
 */
jQuery(document).ready(function(){
var loc=jQuery(document).attr('location')+'';
if(true || loc.indexOf('#')==-1){
	//console.log(jQuery("div.contentlist_body"));
	jQuery("div.contentlist_body li a").each(function(i){
		//console.log(this);
		if (this.href == document.location) {
			jQuery(this).addClass('current');
          
          jQuery(this).parents()
                        .each(function (i) {
                        if(this.tagName == 'LI' && jQuery(this).attr('class')!='leaf'){
                              jQuery(this).addClass("expanded");
                              var div = jQuery(this).attr('id').replace(/menu-/,'#');
                              jQuery(div).show();
                              }
                         })
		}
	});}});
dhtmlMenu.getCookie = function(name) {
  var search = name + "=";
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search);
    if (offset != -1) {
      offset += search.length;
      var end = document.cookie.indexOf(";", offset);
      if (end == -1) {
        end = document.cookie.length;
      }
      returnvalue = unescape(document.cookie.substring(offset, end));
    }
  }
  return returnvalue;
};

/**
 *  Changes the state of a submenu from open to close.
 */
dhtmlMenu.switchMenu = function(submenu, parent) {
  if(jQuery(parent).is(".expanded")) {
    if (true) {
      jQuery(submenu).slideUp("fast", dhtmlMenu.saveMenuState);
    }
    else {
      jQuery(submenu).css("display", "none");
    }
    jQuery(parent).removeClass("expanded").addClass("collapsed");
  }
  else {
    dhtmlMenu.hideAllChildren(parent.parentNode);
    if (true) {
      jQuery(submenu).slideDown("fast", dhtmlMenu.saveMenuState);
    }
    else {
      jQuery(submenu).css("display", "block");
    }
    jQuery(parent).removeClass("collapsed").addClass("expanded");
  }
  dhtmlMenu.saveMenuState();
};

/**
 * Saves the states of the menus.
 */
dhtmlMenu.saveMenuState = function() {
  var blocks = "";
  jQuery("div.submenu").each(function(i) {
    if (this.style.display != "none") {
      if (blocks != "") {
        blocks += ",";
      }
      blocks += this.id;
    }
  });
  document.cookie = "dhtml_menu=" + blocks + ";path=/";
};

/**
 * Start everything: Attaches the online users autoupdate behaviour
 * to the block content.
 */
jQuery(function() {
  var cookievalue = dhtmlMenu.getCookie("dhtml_menu");
  if (cookievalue != "") {
    var cookieList = cookievalue.split(",");
    for (var i = 0; i < cookieList.length; i++) {
      jQuery("#"+ cookieList[i]).show();
      jQuery("#menu-" + cookieList[i]).addClass("expanded");
    }
  }
  jQuery("ul.dhtml_menu li[@class!=leaf] > a").each(function() {
    if (jQuery(this).parent().children("div.submenu").length > 0) {
      jQuery(this).css({paddingLeft: "2em", marginLeft: "-2em", zIndex: 2}).click(function(e) {
        id = jQuery(this).parents()[0].id.replace("menu-", "");
        dhtmlMenu.switchMenu(jQuery("#"+ id)[0], jQuery(this).parents()[0]);
        return false;
      })
      .dblclick(function(e) {
        window.location = this.href;
      });
    }
  });
  jQuery(window).unload(dhtmlMenu.saveMenuState);
});

/**
* Hide all submenus
*/

dhtmlMenu.hideAllChildren = function(parent) {
  for (var i=0; i<parent.childNodes.length; ++i) {
    if (jQuery(parent.childNodes[i]).is(".expanded")){
      jQuery(parent.childNodes[i].getElementsByTagName("div")[0]).css("display","none");
      jQuery(parent.childNodes[i]).removeClass("expanded").addClass("collapsed");
    }
  }
};