var panelManagerRegistry = [];

function PanelManager() {
  var that = this;

  this.panels = [];
  this.panelIndex = {};
  this.currentPanelIdx = null;
  this.propsActive = null;
  this.propsInactive = null;
  this.propsHoverActive = null;
  this.propsHoverInactive = null;
  this.closeCurrent = false;

  this.setPanelCSS = function(active, activeHover, inactive, inactiveHover) {
    this.propsActive = active;
    this.propsActiveHover = activeHover;
    this.propsInactive = inactive;
    this.propsInactiveHover = inactiveHover;
  }

  this.addPanel = function(title,content) {
    var t = {'title':title, 'content':content};
    if (this.panels.length==0) this.currentPanelIdx = 0;
    this.panelIndex[content] = this.panels.length;
    this.panels.push(t);
  }

  this.setCloseCurrent = function(cl) {
    this.closeCurrent = cl;
  }

  this.setDefault = function(pname) {
    var idx = this.panelIndex[pname];
    if (idx===undefined) {
      this.currentPanelIdx = -1;
    } else {
      this.currentPanelIdx = idx;
    }
  }

  this.isCurrent = function(pname) {
    var idx = this.panelIndex[pname];
    return (idx==this.currentPanelIdx);
  }

  this.showPanel = function(pname) {
    var idx = this.panelIndex[pname];
    if (this.currentPanelIdx!=idx) {
      jQuery('#'+pname).slideDown('medium');
      jQuery('#'+this.panels[idx].title+'_title').css(this.propsActive);
      if (this.currentPanelIdx >= 0) {
        jQuery('#'+this.panels[this.currentPanelIdx].content).slideUp('medium');
        jQuery('#'+this.panels[this.currentPanelIdx].title).css(this.propsInactive);
      }
      this.currentPanelIdx = idx;
      this.hoverPanel(pname,true);
    } else {
      if (this.closeCurrent) {
        if (this.currentPanelIdx >= 0) {
          jQuery('#'+this.panels[this.currentPanelIdx].content).slideUp('medium');
          jQuery('#'+this.panels[this.currentPanelIdx].title).css(this.propsInactive);
        }
        this.currentPanelIdx = -1;
        this.hoverPanel(pname,false);
      }
    }
  }

  this.hoverPanel = function(pname,hovering) {
    var idx = this.panelIndex[pname];
    if (this.currentPanelIdx!=idx) {
      if (hovering) {
        jQuery('#'+this.panels[idx].title).css(this.propsInactiveHover);
      } else {
        jQuery('#'+this.panels[idx].title).css(this.propsInactive);
      }
    } else {
      if (hovering) {
        jQuery('#'+this.panels[idx].title).css(this.propsActiveHover);
      } else {
        jQuery('#'+this.panels[idx].title).css(this.propsActive);
      }
    }
  }

  this.start = function() {
    for (var i=0; i<this.panels.length; i++) {
      var pname = this.panels[i].content;
      jQuery('#'+this.panels[i].title).click(generateObjMethodRef(this,'showPanel',[pname]));
      jQuery('#'+this.panels[i].title).hover(
        generateObjMethodRef(this,'hoverPanel',[pname,true]),
        generateObjMethodRef(this,'hoverPanel',[pname,false]));
    }
  }
}
