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 localContainers = [];
      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"),
        clicked:false,
        trigger:trigger,
        hideTimeout:null,
        backgroundIframe:null,
        isShowing:false
      };

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

      localContainers.push(trigger);
      if(dropDownConfig.stayOpen){
        localContainers.push(div);
      }
      if(dropDownConfig.showOnHover){
        yuiEvent.addListener(trigger,"mouseover",dropDown.show);
        if(dropDownConfig.stayOpen){
          localContainers.forEach(function(el){
            yuiEvent.addListener(el,"click",dropDown.setClicked);
          });

        }
        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);
      }
      SW.domWidget.bodyClickHandler.add(dropDown.hideThis,localContainers,trigger);
      if(dropDown.isOldIe  && yuiDom.hasClass(dropDownConfig.list,"useIeHack") ){
        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){
    if(this.dropDownConfig.stayOpen && this.dropDownConfig.clicked){
      return;
    }
    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 && this.dropDownConfig.trigger){
      if(yuiDom.hasClass(this.dropDownConfig.trigger,"disabled") || 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 && yuiDom.hasClass(this.dropDownConfig.list,"useIeHack")){
        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;
      this.dropDownConfig.clicked = false;
      yuiDom.removeClass(this.dropDownConfig.container,"dropDownContainerOpen");
    }
  },
  hide:function(exceptContainer){
    dropDown.dropDownArray.forEach(function(container){
      if(!exceptContainer || exceptContainer != container ){
        dropDown.hideThis.apply(container);
      }
    });
  },
  setClicked:function(e){
    this.dropDownConfig.clicked = true;
  }
};
yuiEvent.onDOMReady(dropDown.initialize);
