﻿if (Object.isUndefined(Nmma)) { Nmma = { }; }
if (Object.isUndefined(Nmma.Shared)) { Nmma.Shared = { }; } 
if (Object.isUndefined(Nmma.Shared.Cabinet)) { Nmma.Shared.Cabinet = { }; } 

Nmma.Shared.Cabinet.List = Class.create({
  initialize: function(container, collapsed, options) {
    if ($(container)) {
			this.options = {};
			this.options.activeClassName = 'active';			
			this.options.collapsedClassName = 'collapsed';
      this.options.effect = typeof Effect != 'undefined';			
      this.options.effectDuration = 0.25;			
			this.options.expandedClassName = 'expanded';
			this.options.folderClassName = 'folder';
      if (options && !Object.isUndefined(options)) {
        if (!Object.isUndefined(options.activeClassName)) { this.options.activeClassName = options.activeClassName; }      
        if (!Object.isUndefined(options.collapsedClassName)) { this.options.collapsedClassName = options.collapsedClassName; }
        if (!Object.isUndefined(options.effect)) { this.options.effect = options.effect; }
        if (!Object.isUndefined(options.expandedClassName)) { this.options.expandedClassName = options.expandedClassName; }
        if (!Object.isUndefined(options.folderClassName)) { this.options.folderClassName = options.folderClassName; }
			}
			// DMA: Effect doesn't work well on IE?
      this.options.effect = false;			
    	var folders = $$('#' + container + ' .' + this.options.folderClassName + ' .' + this.options.folderClassName + '-header' + ' .' + this.options.folderClassName + '-title');    	
		  folders.each(function(folder) {
		    folder.addClassName(collapsed ? this.options.collapsedClassName : this.options.expandedClassName);
		    if (collapsed) {
		      folder.up().next().hide();
		    }
		    else {
          folder.up().next().show();		    
		    } 
			  Event.observe(folder, 'click', this.clickHandler.bind(this, folder));
		  }.bind(this));		  		  
    }  
  },
  clickHandler: function(folder) {
    folder.up().toggleClassName(this.options.activeClassName);          
    folder.toggleClassName(this.options.collapsedClassName);    
    folder.toggleClassName(this.options.expandedClassName);    
    if (this.options.effect) {
      new Effect.toggle(folder.up().next(), 'Slide', { 'duration': this.options.effectDuration });
    }
    else {
      folder.up().next().visible() ? folder.up().next().hide() : folder.up().next().show()
    }
  }  
});

Nmma.Shared.Cabinet.Accordion = Class.create({
  initialize: function(container, options) {
    if ($(container)) {
			this.options = {};
			this.options.activeFolderClassName = 'active';
      this.options.effect = typeof Effect != 'undefined';			
      this.options.effectDuration = 0.75;	
			this.options.folderClassName = 'folder';    
      if (options && !Object.isUndefined(options)) {
        if (!Object.isUndefined(options.activeFolderClassName)) { this.options.activeFolderClassName = options.activeFolderClassName; }
        if (!Object.isUndefined(options.effect)) { this.options.effect = options.effect; }
        if (!Object.isUndefined(options.folderClassName)) { this.options.folderClassName = options.folderClassName; }
			}			  		
			// DMA: Effect doesn't work well on IE?
      this.options.effect = false;			
    	var folders = $$('#' + container + ' .' + this.options.folderClassName + ' .' + this.options.folderClassName + '-header' + ' .' + this.options.folderClassName + '-title');
		  var first = true;
		  folders.each(function(folder) {
			  folder.height = folder.up().next().getHeight() + 24;
			  if (first) {
			    this.current = folder;
			    this.next = this.current;
          this.current.up().next().show();
          this.current.toggleClassName(this.options.activeFolderClassName);			    
			    first = false;
			  }
        else {
			    //folder.up().next().setStyle({ 'height': '0px', 'display': 'none' });
			    folder.up().next().hide();
        }			  
			  Event.observe(folder, 'click', this.clickHandler.bind(this, folder));		  
		  }.bind(this));		  		  
    }
  },
  clickHandler: function(folder) {
    if (!this.animating) {
	    this.isAnimating = true;    
		  if (this.current != folder) {
		    this.next = folder;
		    this.animate();
		  }
		  else {
	      this.isAnimating = false;    
		  }
		}
  },
	animate: function() {
    if (this.animating) return false;	
    this.current.toggleClassName(this.options.activeFolderClassName);
    this.next.toggleClassName(this.options.activeFolderClassName);
    this.next.up().next().show();
    if (this.options.effect) {
      var effects = new Array();
      //effects.push(new Effect.Scale(this.next.up().next(), 100, { 'sync': true, 'scaleMode': { 'originalHeight': this.next.height - this.next.getHeight() + 20 }, 'scaleX': false, 'scaleY': true, 'scaleContent': false, 'scaleFrom': 0 }));
      effects.push(new Effect.Scale(this.next.up().next(), 100, { 'sync': false, 'scaleMode': { 'originalHeight': this.next.height - this.next.getHeight()}, 'scaleX': false, 'scaleY': true, 'scaleContent': false, 'scaleFrom': 0 }));      
      effects.push(new Effect.Scale(this.current.up().next(), 0, { 'sync': false, 'scaleX': false, 'scaleY': true, 'scaleContent': false, 'scaleFrom': 100 }));
      new Effect.Parallel(effects, {
        'duration': this.options.effectDuration,
        'beforeStart': function() { }.bind(this),
        'afterFinish': function() {
          this.current.up().next().hide();
          this.current = this.next;
          this.isAnimating = false;
        }.bind(this)
      });  
    }
    else {
      this.current.up().next().hide();
      this.next.up().next().show();
      this.current = this.next;
      this.isAnimating = false;
    }  
  }
});    


Nmma.Shared.Cabinet.HorizontalTabs = Class.create({
  initialize: function(container, options) {
    if ($(container)) {
			this.options = {};
			this.options.activeFolderClassName = 'active';
      if (options && !Object.isUndefined(options)) {
        if (!Object.isUndefined(options.activeFolderClassName)) { this.options.activeFolderClassName = options.activeFolderClassName; }
			}			  		
		  var tables = $$('#' + container + ' > div > table tr table ');
		  this.table = $(container).down().down();
		  tables.each(function(table) {
		    for (var i = 0; i < table.rows[0].cells.length; i++) {
           Event.observe(table.rows[0].cells[i], 'click', this.clickHandler.bind(this, table.rows[0].cells[i]));				    		    
		    }
		 }.bind(this));
	   this.clickHandler(tables[tables.length - 1].rows[0].cells[0]);
    }  
  },
  clickHandler: function(tab) {
    if (this.currentTab != tab) {
      tab.toggleClassName(this.options.activeFolderClassName);
      tab.addClassName(this.options.activeFolderClassName);      
      if (this.currentTab) {
        this.currentTab.toggleClassName(this.options.activeFolderClassName);
        this.currentTab.removeClassName(this.options.activeFolderClassName);
      }
      if (this.currentPanel) {
        this.currentPanel.hide();
      }
      $(tab.id + '_').show();
      this.currentPanel = $(tab.id + '_');
      this.currentTab = tab;    
    
      if (tab.up(4).rowIndex + 1 != this.table.rows.length) {
        var tr = this.table.tBodies[0].replaceChild(this.table.rows[this.table.rows.length - 1], tab.up(4));
        this.table.tBodies[0].appendChild(tr);
      }
    }    
  }  
});    

