/*
	Xstrata plc
	Global navigation menu javascript functions

	Author: David Roessli (david dot roessli at cybmed dot com)
	Created on 2006-06-09
	Version: 1
	Revision: 0

	History:

		200606091508: genesis

	Terms
	You may adapt and learn from the elements of this CSS to use in your own
	projects, but you may not copy the design (the unique combination of images,
	colors, sizes, typography, and positioning) since this is copyright of
	Xstrata plc and may not be reproduced.

*/
function displaySubNav(name) {

	new Ajax.Updater(name, "/ajax/ajax_nav_submenu.php", {
		asynchronous: true,
		method: "get",
		parameters: "sub="+name,
		onLoading: function(request) { },
		onComplete: function(request) { new Effect.SlideDown(name); },
		onFailure: function(request) { }
	}); 
	return false;
}


Element.showIf = function() {
  for(var i = 0; i < arguments.length; i++ ) {
    var element = $(arguments[i])
    if(element) Element.show(element)
  }
}

Element.hideIf = function() {
  for(var i = 0; i < arguments.length; i++ ) {
    var element = $(arguments[i])
    if(element) Element.hide(element)
  }
}

/* ================================ */
/* = General Drawer-related tasks = */
/* ================================ */

var Drawer = {
	
  hideAll:function() {

      Element.hideIf('lnav_0');
      Element.hideIf('lnav_1');
      Element.hideIf('lnav_2');
      Element.hideIf('lnav_3');
      Element.hideIf('lnav_4');
      Element.hideIf('lnav_5');
      Element.hideIf('lnav_6');
      Element.hideIf('lnav_7');
      Element.hideIf('lnav_8');
  },
	
  slideUp: function(on, lnav) {
    if(this.inhibit) return

   if(on) {
      new Effect.SlideUp(lnav);
      setTimeout('Drawer.hideAll()', 400)
   }
  },
	
  toggle: function(on, current, lnav) {
    if(this.inhibit) return

   if(on) {
//		alert($(current).style.display);
/*
		Leave them open..

	  if (current != 'lnav_1') Element.hideIf('lnav_1');
      if (current != 'lnav_2') Element.hideIf('lnav_2');
      if (current != 'lnav_3') Element.hideIf('lnav_3');
      if (current != 'lnav_4') Element.hideIf('lnav_4');
      if (current != 'lnav_5') Element.hideIf('lnav_5');
      if (current != 'lnav_6') Element.hideIf('lnav_6');
*/
//		alert($(current).style.display);
      if ($(current).style.display == 'none') {
//	    Element.showIf(current);
		foo = displaySubNav(current);
//		new Effect.SlideDown(current);
//		if($(lnav).style.display == 'none') new Effect.SlideDown(lnav)
	  }
	  else {
      	new Effect.SlideUp(current);
//		Element.hideIf(current);
//		Drawer.slideUp(true, lnav);
//       setTimeout('Drawer.hideAll()', 400)
	  }
    }
  }
}

/* =============================== */
/* = General Hover-related tasks = */
/* =============================== */

var Hover = {
  EXIT_DELAY  : 2400,
  HOVER_BG_ON : 'url(../img/hover-gradient.gif) left repeat-y',
  HOVER_BG_OFF: 'none',
  
  lastTimer   : null,
  lastCommand : null,
  inhibit     : false,
  
  clearCurrent: function() {
    if(!this.lastTimer) return
    clearTimeout(this.lastTimer)
    eval(this.lastCommand)
    this.lastTimer = this.lastCommand = null
  },
  
  endWith: function(command) {
    if(this.inhibit) return
    this.lastCommand = command
    this.lastTimer = setTimeout(command, this.EXIT_DELAY)
  },
  
  toggle: function(on, container, nubbin) {
    if(this.inhibit) return
	
    if(on) {
//      if($(container))
//        $(container).style.background = this.HOVER_BG_ON
      Element.showIf(nubbin)
    } else {
//      if($(container))
//        $(container).style.background = this.HOVER_BG_OFF
      Element.hideIf(nubbin)
    }
  }
}
/* ================================================= */
/* = Tasks relating to individual navigation items = */
/* ================================================= */

var lists = {
  ERROR_MSG: "There was an error processing this request.\n" +
             "Please wait a minute and try again.",

  hover: {
    begin: function(id) {
      Hover.clearCurrent()
//      Hover.toggle(true, 'show_item_' + id, 'nubbin_item_' + id)
      Hover.toggle(true, 'show_item_' + id, 'nav_' + id)
//      Hover.toggle(true, 'show_item_' + id, id)
    },
    
    end: function(id, delay) {
      if(delay)
        Hover.endWith('lists.hover.end(' + id + ')')
      else
//        Hover.toggle(false, 'show_item_' + id, 'nubbin_item_' + id)
        Hover.toggle(false, 'show_item_' + id, 'nav_' + id)
//        Hover.toggle(false, 'show_item_' + id,  id)
    }
  },

  zzz_placeholder: false // so we don't have to remember to add a comma
}
