var dropDown = {
  dropDownArray:[],
  isOldIe:false,
  initialize:function(){
    if(navigator.userAgent.indexOf("MSIE") >-1 && parseInt(navigator.appVersion) <= 6){
      dropDown.isOldIe = true;
    }
    var containers = yuiDom.getElementsByClassName("dropDownContainer");
    containers.forEach(function(div){
      var containers = [];
      var list = yuiDom.getElementsByClassName("dropDownList",null,div)[0];
      dropDown.dropDownArray.push(div);
      var trigger = yuiDom.getElementsByClassName("dropDownTrigger",null,div)[0];

      var dropDownConfig = {
        list:list,
        container:div,
        isTopNav:yuiDom.hasClass(div,"topNavMenuContainer"),
        stayOpen:yuiDom.hasClass(trigger,"stayOpen"),
        showOnHover:yuiDom.hasClass(trigger,"showOnHover"),
        hideTimeout:null,
        backgroundIframe:null,
        isShowing:false
      };

      trigger.dropDownConfig = dropDownConfig;
      list.dropDownConfig = dropDownConfig;
      div.dropDownConfig = dropDownConfig;

      containers.push(trigger);
      if(dropDownConfig.stayOpen){
        containers.push(div);
      }
      if(dropDownConfig.showOnHover){
        yuiEvent.addListener(trigger,"mouseover",dropDown.show);
        if(!dropDownConfig.stayOpen){
          yuiEvent.addListener(trigger,"mouseover",dropDown.setHide);
          yuiEvent.addListener(div,"mouseover",dropDown.clearHide);
          yuiEvent.addListener(div,"mouseout",dropDown.setHide);
        }
      }else{
        yuiEvent.addListener(trigger,"click",dropDown.show);
      }
      widgetCloser.addHandler(dropDown.hideThis,trigger,containers);
      if(dropDown.isOldIe){
        var iframe = document.createElement("iframe");
        iframe.frameBorder = "0";
        iframe.src = "/common/blank.jsp";
        yuiDom.addClass(iframe,"backgroundIframe");
        dropDownConfig.list.parentNode.insertBefore(iframe,dropDownConfig.list);
        dropDownConfig.backgroundIframe = iframe;
      }
    });
  },
  setHide:function(e){
    var self = this;
    if(!this.dropDownConfig.hideTimeout && this.dropDownConfig.isShowing){
      this.dropDownConfig.hideTimeout = setTimeout(function(){
        dropDown.hideThis.apply(self);
      },600);
    }
  },
  clearHide:function(){
    if(this.dropDownConfig.hideTimeout){
      clearTimeout(this.dropDownConfig.hideTimeout);
      this.dropDownConfig.hideTimeout = null;
    }
  },
  show:function(e){
    var self = this;
    dropDown.clearHide.apply(this);

    if(this.dropDownConfig.container && yuiDom.hasClass(this.dropDownConfig.container,"dropDownContainerOpen")){
      return;
    }
    if(!this.dropDownConfig.isShowing){
      this.dropDownConfig.isShowing = true;
      yuiDom.addClass(this.dropDownConfig.container,"dropDownContainerOpen");
      dropDown.hide(this.dropDownConfig.container);
      if(dropDown.isOldIe){
        var reduce = 2;
        if(this.dropDownConfig.isTopNav) { reduce = 17; }
        this.dropDownConfig.backgroundIframe.style.height = (this.dropDownConfig.list.clientHeight - reduce) +"px";
        this.dropDownConfig.backgroundIframe.style.width = (this.dropDownConfig.list.clientWidth -reduce) +"px";
      }
    }
    yuiEvent.preventDefault(e);
  },
  hideThis:function(){
    if(this.dropDownConfig.isShowing){
      this.dropDownConfig.isShowing = false;
      yuiDom.removeClass(this.dropDownConfig.container,"dropDownContainerOpen");
    }
  },
  hide:function(exceptContainer){
    dropDown.dropDownArray.forEach(function(container){
      if(!exceptContainer || exceptContainer != container ){
        dropDown.hideThis.apply(container);
      }
    });
  }
};
yuiEvent.onDOMReady(dropDown.initialize);

var widgetCloser = {
  initialized:false,
  closers:[],
  addHandler:function(method,context,testElements){
    if(!widgetCloser.initialized){
      yuiEvent.addListener(document.body,"click",widgetCloser.doClose);
      widgetCloser.initialized = true;
    }
    widgetCloser.closers.push({
      method:method,
      context:context,
      testElements:testElements
    });
  },
  doClose:function(e){
    var target = yuiEvent.getTarget(e);
    widgetCloser.closers.forEach(function(args){
      var isContained = false;
      for(var i=0;i<args.testElements.length;i++){
        if(yuiDom.isAncestor(args.testElements[i],target)){
          isContained = true;
          break;
        }
      }
      if(!isContained){
        args.method.apply(args.context,[e]);
      }
    });
  }
};