﻿YAHOO.namespace("Hollow.Widget");

(function() {
  var SELECTED_CLASS_NAME = "Selected";
  var PAST_CLASS_NAME = "Past";
  var FUTURE_CLASS_NAME = "Future";
  var BUTTON_CLASS_NAMES = {NEXT: "Next", PREVIOUS: "Previous", SHOW: "Show", POSTVIEW: "Postview", PREVIEW: "Preview"};
  var OPTION_CLASS_NAME = "Option";
  var ICON_CLASS_NAME = "Icon";
  var LABEL_CLASS_NAME = "Label";
  
  YAHOO.Hollow.Widget.Accordion = function(container, sections, cfg){
    var cfg = (null == cfg) ? {} : cfg;
    this.sections = sections;
    this.container = YAHOO.util.Dom.get(container);
    this.currentIndex = (cfg.currentIndex) ? cfg.currentIndex : 0;
    this.sectionChanged = new YAHOO.util.CustomEvent("sectionChanged", this);
  };

  YAHOO.Hollow.Widget.Accordion.prototype = {
    
    show: function(newIndex){
      if (newIndex == this.currentIndex) {
        return this.currentIndex;
      }
      else if (null == newIndex || newIndex == NaN || newIndex < 0 || newIndex >= this.sections.length) {
        throw("Section Index Out Of Range, max:" + (this.sections.length - 1) + ", min:0, given:" + newIndex);
      }
      this.transition(newIndex, this.currentIndex);
      this.currentIndex = newIndex;
      this.sectionChanged.fire(this.currentIndex);
    },
    
  	next: function(){
  	  var nextIndex = this.currentIndex + 1;
      return this.show((nextIndex >= this.sections.length) ? this.sections.length - 1 : nextIndex);	
    },
  
    previous: function(){
  	  var previousIndex = this.currentIndex - 1;
      return this.show((previousIndex < 0) ? 0 : previousIndex);
    },
    
    expand: function(){
      
    },
    
    collapseToPast: function(){
      
    },
    
    collapseToFuture: function(){
      
    },
    
    pastToFuture: function(){
      
    },
    
    futureToPast: function(){
      
    },
    
    transition: function(newIndex, oldIndex){
      for(var i=0;i<this.sections.length;i++){
        if(i < newIndex){
          YAHOO.util.Dom.removeClass(this.sections[i], SELECTED_CLASS_NAME);
          YAHOO.util.Dom.removeClass(this.sections[i], FUTURE_CLASS_NAME);
          YAHOO.util.Dom.addClass(this.sections[i], PAST_CLASS_NAME);
        } else if( i == newIndex){
          YAHOO.util.Dom.addClass(this.sections[i], SELECTED_CLASS_NAME);
          YAHOO.util.Dom.removeClass(this.sections[i], FUTURE_CLASS_NAME);
          YAHOO.util.Dom.removeClass(this.sections[i], PAST_CLASS_NAME);
        } else if (i > newIndex){
          YAHOO.util.Dom.removeClass(this.sections[i], SELECTED_CLASS_NAME);
          YAHOO.util.Dom.addClass(this.sections[i], FUTURE_CLASS_NAME);
          YAHOO.util.Dom.removeClass(this.sections[i], PAST_CLASS_NAME);
        }
      }
      return newIndex;
    }
  
  };
  
  YAHOO.Hollow.Widget.AccordionController = function(container, show){
    this.show = show;
    this.container = YAHOO.util.Dom.get(container);
    this.buttons = {};
    this.buttons = {
      next: YAHOO.util.Dom.getElementsByClassName(BUTTON_CLASS_NAMES.NEXT, null, this.container),
      previous: YAHOO.util.Dom.getElementsByClassName(BUTTON_CLASS_NAMES.PREVIOUS, null, this.container),
      previews: YAHOO.util.Dom.getElementsByClassName(BUTTON_CLASS_NAMES.PREVIEW, null, this.container),
      postviews: YAHOO.util.Dom.getElementsByClassName(BUTTON_CLASS_NAMES.POSTVIEW, null, this.container)
    }
    
    this.sections = [];
    for(var i=0;i<this.show.sections.length; i++){
      this.sections[i] = {
        container: this.show.sections[i],
        postview: YAHOO.util.Dom.getElementsByClassName(BUTTON_CLASS_NAMES.POSTVIEW, null, this.show.sections[i])[0],
        preview: YAHOO.util.Dom.getElementsByClassName(BUTTON_CLASS_NAMES.PREVIEW, null, this.show.sections[i])[0],
        options: YAHOO.util.Dom.getElementsByClassName(OPTION_CLASS_NAME, null, this.show.sections[i])
      };
      if(this.sections[i].preview)
        YAHOO.util.Event.addListener(this.sections[i].preview, "click", this.handleShow, i, this);
      if(this.sections[i].postview)
        YAHOO.util.Event.addListener(this.sections[i].postview, "click", this.handleShow, i, this);
      
      for(var j=0;j<this.sections[i].options.length;j++){
        YAHOO.util.Event.addListener(this.sections[i].options[j], "click", this.handleSelectionClick, i, this);
      }
    }
    YAHOO.util.Event.addListener(this.buttons.next, "click", this.handleNext, this, true);

    for(var i=0;i<this.sections.length;i++){
      this.updatePostViewForSection(this.sections[i]);
    }
  };

  YAHOO.Hollow.Widget.AccordionController.prototype = {
    
    handleShow: function(ev, newIndex){
      this.show.show(newIndex);
    },
    
    handleNext: function(ev){
      this.show.next();
    },
    
    handlePrevious: function(ev){
      this.show.previous();
    },
    
    handleSelectionClick: function(ev, section){
      this.updatePostViewForSection(this.sections[section]);
    },
    
    updatePostViewForSection: function(section){
      if(section.postview){
        var postIcon = YAHOO.util.Dom.getElementsByClassName(ICON_CLASS_NAME, null, section.postview)[0];
        var postLabel = YAHOO.util.Dom.getElementsByClassName(LABEL_CLASS_NAME, null, section.postview)[0];
        for(var i=0;i<section.options.length;i++){
          var selectionInput = YAHOO.util.Dom.getElementBy(function(el){return el.type == "radio"}, "INPUT", section.options[i]);
          if(selectionInput.checked){
            var selectedIcon = YAHOO.util.Dom.getElementsByClassName(ICON_CLASS_NAME, null, section.options[i])[0];
            var selectedLabel = YAHOO.util.Dom.getElementsByClassName(LABEL_CLASS_NAME, null, section.options[i])[0];

            YAHOO.util.Dom.setStyle(postIcon, "background-image", YAHOO.util.Dom.getStyle(selectedIcon, "background-image"));
            postLabel.innerHTML = selectedLabel.innerHTML;
            break;
          }
        }
      }
    }
  }

  YAHOO.Hollow.Widget.AccordionFactory = {
  
    buildAccordionFromList: function(listContainer, cfg){
      var cfg = (null == cfg) ? {} : cfg;
      var container = YAHOO.util.Dom.get(listContainer);
      var sections = YAHOO.util.Dom.getChildrenBy(container, function(el){return el.tagName == "LI"});
      if (!sections || sections.length <= 0) {
        throw ("No list items in: " + listContainer);
      }
      var currentIndex = -1;
      for (var i = 0; i < sections.length; i++) {
        if (YAHOO.util.Dom.hasClass(sections[i], SELECTED_CLASS_NAME)) {
          currentIndex = i;
          break;
        }
      }
      if (currentIndex < 0) {
        currentIndex = 0;
        YAHOO.util.Dom.addClass(sections[0], SELECTED_CLASS_NAME);
      }
      cfg.currentIndex = currentIndex;
      return new YAHOO.Hollow.Widget.Accordion(container, sections, cfg);
    },
  
    buildAccordionByClassName: function(container, className){
      return false;
    },
    
    buildAccordion: function(sections){
      return false;
    }
  };  
})();