/* Simple Slider Script
 * Requires Prototype and Script.aculo.us Libraries
 * By: Bjoern Hoffmann
 */

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

var Slider = Class.create({

    initialize: function(id) {
        if(!$(id)) throw("Attempted to initalize slider with id: "+ id + " which was not found.");
        this.slider = $(id);
        this.options = {
            toggleClass: "slider-toggle",
            toggleActive: "slider-toggle-active",
            contentClass: "slider-content"
        }
        this.contents = this.slider.select('div.'+this.options.contentClass);
        this.isAnimating = false;
        this.toExpand = null;

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

    expand: function(el) {
        this.toExpand = el.next('div.'+this.options.contentClass);

        if(el.hasClassName(this.options.toggleActive)) {
        	 this.animate('up');
        } else {
        	this.animate('down');
        }
    },

    clickHandler: function(e) {
        var el = e.element();
        if(el.hasClassName(this.options.toggleClass) && !this.isAnimating) {
            this.expand(el);
        }
    },

    animate: function(state) {

        var myDuration = 1.00;

        if(state == 'up') {
        	Effect.SlideUp(this.toExpand, {
	            duration: myDuration,

	            beforeStart: function() {
	                this.isAnimating = true;
	                this.toExpand.previous('div.'+this.options.toggleClass).removeClassName(this.options.toggleActive);
	            }.bind(this),
	            afterFinish: function() {
	                this.isAnimating = false;
	            }.bind(this)
	        });
        } else {
	        Effect.SlideDown(this.toExpand, {
	            duration: myDuration,

	            beforeStart: function() {
	                this.isAnimating = true;
	                this.toExpand.previous('div.'+this.options.toggleClass).addClassName(this.options.toggleActive);
	            }.bind(this),
	            afterFinish: function() {
	                this.isAnimating = false;
	            }.bind(this)
	        });
        }
    }

});
