/* Simple Accordion Script 
 * Requires Prototype and Script.aculo.us Libraries
 * By: Brian Crescimanno <brian.crescimanno@gmail.com>
 * http://briancrescimanno.com
 * This work is licensed under the Creative Commons Attribution-Share Alike 3.0
 * http://creativecommons.org/licenses/by-sa/3.0/us/
 */

if (typeof Effect == 'undefined')
  throw("You must have the script.aculo.us library to use this accordion");

var HorizontalAccordion = Class.create({

    initialize: function(id, defaultExpandedCount) {
        if(!$(id)) throw("Attempted to initalize accordion with id: "+ id + " which was not found.");
        this.accordion = $(id);
        this.options = {
            toggleClass: "horizontal-accordion-toggle",
            toggleActive: "horizontal-accordion-toggle-active",
            contentClass: "horizontal-accordion-content"
        }
        this.contents = this.accordion.select('div.'+this.options.contentClass);
        this.toggles = this.accordion.select('div.'+this.options.toggleClass);
        this.isAnimating = false;
        this.maxWidth = 0;
        this.currToggleHeight = 0;
        this.current = defaultExpandedCount ? this.contents[defaultExpandedCount-1] : this.contents[0];
        this.toExpand = null;

        this.checkMaxWidth();
        this.checkCurrToggleHeight();
        this.initialHide();
        this.attachInitialMaxWidth();
        this.attachInitialToggleHeight();

        var clickHandler =  this.clickHandler.bindAsEventListener(this);
        this.accordion.observe('click', clickHandler);
        
    },

    expand: function(el) {
      this.toExpand = el.next('div.'+this.options.contentClass);
      if(this.current != this.toExpand){
						this.toExpand.show();
            this.animate();
        }
    },

    checkMaxWidth: function() {
        for(var i=0; i<this.contents.length; i++) {
            if(this.contents[i].getWidth() > this.maxWidth) {
                this.maxWidth = this.contents[i].getWidth();
            }
        }
    },

    attachInitialMaxWidth: function() {
			this.current.previous('div.'+this.options.toggleClass).addClassName(this.options.toggleActive);
      if(this.current.getWidth() != this.maxWidth) this.current.setStyle({width: this.maxWidth+"px"});
    },

    checkCurrToggleHeight: function() {
    		var reference = (this.toExpand != null ? this.toExpand : this.current);
    		var fullHeight = reference.getHeight();
    		var sampleToggle = this.toggles.first();
    		var toggleTopPad = parseInt(sampleToggle.getStyle('padding-top'));
    		var toggleBotPad = parseInt(sampleToggle.getStyle('padding-bottom'));
    		
    		this.currToggleHeight = fullHeight - toggleTopPad - toggleBotPad;
    		//console.info('currToggleHeight: '+this.currToggleHeight);
    },

    attachInitialToggleHeight: function() {
    	for(var i=0; i<this.toggles.length; i++) {
       	if(this.toggles[i].getHeight() != this.currToggleHeight) this.toggles[i].setStyle({minHeight: this.currToggleHeight+"px"});
      }
    },

    clickHandler: function(e) {
      var el = e.element();
      //console.info("accordion clickHandler"); console.info(el);
      if(el.hasClassName(this.options.toggleClass) && !this.isAnimating) {
        this.expand(el);
        this.accordion.fire("horizontal_accordion:expanded"); // Fire custom 'expanded' event to handle IE's inefficiencies.
      }
    },

    initialHide: function(){
        for(var i=0; i<this.contents.length; i++){
            if(this.contents[i] != this.current) {
                this.contents[i].hide();
                this.contents[i].setStyle({width: 0});
            }
        }
    },

    animate: function() {
        var effects = new Array();
        var options = {
            sync: true,
            scaleFrom: 0,
            scaleContent: false,
            transition: Effect.Transitions.sinoidal,
            scaleMode: {
                originalHeight: this.accordion.getHeight(),
                originalWidth: this.maxWidth
            },
            scaleX: true,
            scaleY: false
        };

        effects.push(new Effect.Scale(this.toExpand, 100, options));

				// Added by Shastic to provide height resizing for the toggle elements
				this.checkCurrToggleHeight(); // Set this.currToggleHeight
				
				options = {
						sync: true,
						style: 'min-height:'+this.currToggleHeight+'px;',
            scaleContent: false,
            transition: Effect.Transitions.sinoidal,
            scaleX: false,
            scaleY: true
				};
				
				for(var i=0; i<this.toggles.length; i++) { // Resize each toggle element.
	        effects.push(new Effect.Morph(this.toggles[i], options));
        }
				// End of addition

        options = {
            sync: true,
            scaleContent: false,
            transition: Effect.Transitions.sinoidal,
            scaleX: true,
            scaleY: false
        };

        effects.push(new Effect.Scale(this.current, 0, options));

        var myDuration = 0.75;
				
        new Effect.Parallel(effects, {
            duration: myDuration,
            fps: 35,
            queue: {
                position: 'end',
                scope: 'accordion'
            },
            beforeStart: function() {
                this.isAnimating = true;
                this.current.previous('div.'+this.options.toggleClass).removeClassName(this.options.toggleActive);
                this.toExpand.previous('div.'+this.options.toggleClass).addClassName(this.options.toggleActive);
            }.bind(this),
            afterFinish: function() {
                this.current.hide();
                this.toExpand.setStyle({ width: this.maxWidth+"px" });
                this.current = this.toExpand;
                this.isAnimating = false;
            }.bind(this)
        });
    }

});

//document.observe("dom:loaded", function(){
//    accordion = new Accordion("test-accordion", 2);
//})