/* DECLARE THE NAMESPACE */
var cash = { emea : {} };

cash.emea = {
  /* *************************** */
  /* ********  OBJECTS  ******** */
  /* *************************** */

  /*
   * .obj     ->  TopNavigationMenu
   * .purpose ->  Top Navigation using multi level drop down menu
   * .params  ->  @navId = nav container
   * .notes   ->  top items need CSS class 'mainTab'; nested menus need CSS class "submenu";
   *
   */
  TopNavigationMenu : {
    init: function(navId){
      this.id = navId;
      this.setup();
    },
    setup : function(){
      /* Initialize overall menu functionality; add IE6 Drop Down iFrame fix */
      this.items = $("#" + this.id).find("li");
      var len = this.items.length;

      for ( var i = 0; i < len; i++) {
        var firstUl = $(this.items[i]).find("ul:first");
        if ($(firstUl).hasClass('submenu')) {
          var offsetCss = ($(this.items[i].parentNode).hasClass('submenu')) ? ' offset' : '';
          var children = $(firstUl).children();

          //getting height this way because height of second level submenu is not accurately retrieved; dependency on defined height for each item of the submenu (e.g., 26px)
          var iHeight = children.length * 26;
          var iframe = new cash.emea.iFrameDropDownFix({
            'attr': {
              'src' : "javascript:''",
              'scrolling' : 'no',
              'frameborder' : '0',
              'height' : iHeight,
              'width' : $(firstUl).css('width')
            },
            'css' : 'iframeHidden ' + offsetCss
          });

          $(firstUl).before(iframe);
        }

        //attach Mouseover|Mouseout events to each menu item
        this.items[i].onmouseover = function(x) {
          return function() {
            $(this).addClass("hover");
            $(this).find("ul:first").css("display", "block");
            $(this).find(".iframeHidden:first").css("display", "block");
          }
        }(i);

        this.items[i].onmouseout = function(x) {
          return function() {
            $(this).removeClass("hover");
            $(this).find("ul:first").css("display", "none");
            $(this).find(".iframeHidden:first").css("display", "none");
          }
        }(i);
      }
    }
  },
  /* *************************** */
  /* ******** FUNCTIONS ******** */
  /* *************************** */
  /*
   * .func    ->  iFrameDropDownFix
   * .purpose ->  iFrame over nested menus fix for IE6 problem SELECT element
   *
   */
  iFrameDropDownFix : function (obj){
    this.iframe = document.createElement('iframe');
    this.cssClass = obj['css'];
    this.attr = obj['attr'];
    if (this.attr) {
      for (a in this.attr) {
        this.iframe.setAttribute(a,this.attr[a]);
      }
    }
    this.iframe.className = this.cssClass;
    return this.iframe;
  },

  /*
  * .func    ->  showHideToolTip
  * .params  ->  @elementSet = array of DOM elements to apply listener to
  * .purpose ->  toggle display for hidden tooltip divs
  *
  */
  showHideToolTip : function(elementSet) {
    $(elementSet).hover(
      function(){$(this).siblings(".hidden").css("display","block");},
      function(){$(this).siblings(".hidden").css("display","none");}
    );

  }
}

/* BUCKET FOR ALL DOM ONLOAD DEPENDENT FUNCTIONS */
$(document).ready(function(){
  /* Initialize Top Navigation */
 cash.emea.TopNavigationMenu.init("nav");
});

