/* 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 Accordion = Class.create();
Accordion.prototype = {

	initialize: function(id, defaultExpandedCount, te, tc, ta, ce, cc, EventType) {
		if(!$(id)) throw("Attempted to initalize accordion with id: "+ id + " which was not found.");
		this.accordion = $(id);
		this.homeTitleEn = 'Making Paper Fun - Domtar - Home';
		this.homeTitleFr = 'Making Paper Fun - Domtar - Home';
		this.options = {
			toggleElement: te,
			toggleClass: tc,
			toggleActive: ta,
			contentElement: ce,
			contentClass: cc
		}
		this.contents = this.accordion.select(this.options.contentElement+'.'+this.options.contentClass);
		
		this.isAnimating = false;
		this.maxHeight = 256;
		if (defaultExpandedCount == 0) {
			for(var i=0; i<this.contents.length; i++) {
				if(this.contents[i].previous().hasClassName(this.options.toggleActive)) {
					this.contents[i].previous().addClassName(this.options.toggleClass);
					defaultExpandedCount = i + 1;
				}
			}
		}
		this.current = defaultExpandedCount ? this.contents[defaultExpandedCount-1] : this.contents[0];
		this.toExpand = null;
		//this.checkMaxHeight();
		this.initialHide();
		this.attachInitialMaxHeight();

		var clickHandler =  this.clickHandler.bindAsEventListener(this);
		if (EventType == "mouseover") {
			this.accordion.observe('mousemove', clickHandler);
			this.accordion.observe('mouseover', clickHandler);
		} else {
			this.accordion.observe(EventType, clickHandler);
		}
		this.initialShow();
	},

	expand: function(el) {
		this.toExpand = el.next('.' + this.options.contentClass);
		if(this.current != this.toExpand) {
			this.toExpand.show();
			this.animate();
		} else if (this.toExpand == this.contents[0] && document.title == this.homeTitleEn && !this.current.previous().hasClassName(this.options.toggleActive)) {
			this.current = this.contents[2];
			this.toExpand.show();
			this.animate();
		}
	},

	checkMaxHeight: function() {
		for(var i=0; i<this.contents.length; i++) {
			if(this.contents[i].getHeight() > this.maxHeight) {
				this.maxHeight = this.contents[i].getHeight();
			}
		}
	},

	attachInitialMaxHeight: function() {
		if (document.title != this.homeTitleEn) {
			this.current.previous().addClassName(this.options.toggleActive);
			if(this.current.getHeight() != this.maxHeight) {
				this.current.setStyle({height: this.maxHeight+"px"});
			}
		}
	},
	
	clickHandler: function(e) {
		if (!this.isAnimating) {
			var el = e.element();
			while (el && el.hasClassName && !el.hasClassName(this.options.toggleClass)) {
				el=el.parentNode;
			}
			if(el && el.hasClassName && el.hasClassName(this.options.toggleClass) && el.next() != null) {
				if (el.next().hasClassName(this.options.contentClass)) {
					this.expand(el);
				}
			}
		}
	},

	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({height: 0});
			//}
		}
	},
	
	initialShow: function(){
		for(var i=0; i<this.contents.length; i++){
			if(this.contents[i] == this.current) {
				if (document.title != this.homeTitleEn) {
					this.contents[i].show();
				}
					//this.contents[i].previous().animate();
					//this.animate();
					/*
					if (el.next().hasClassName(this.options.contentClass)) {
						this.expand(this.current.previous('.' + this.options.toggleActive));
					} else {
						
					}
					*/
			}
		}
		//this.current = this.contents[0];
	},

	animate: function() {
		var effects = new Array();
		var options = {
			sync: true,
			scaleFrom: 0,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			scaleMode: {
				originalHeight: this.maxHeight,
				originalWidth: this.accordion.getWidth()
			},
			scaleX: false,
			scaleY: true
		};

		effects.push(new Effect.Scale(this.toExpand, 100, options));

		options = {
			sync: true,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			scaleX: false,
			scaleY: true
		};

		effects.push(new Effect.Scale(this.current, 0, options));

		var myDuration = 0.6;

		new Effect.Parallel(effects, {
			duration: myDuration,
			fps: 35,
			queue: {
				position: 'end',
				scope: 'accordion'
			},
			beforeStart: function() {
				this.isAnimating = true;
				this.current.previous('.' + this.options.toggleClass).removeClassName(this.options.toggleActive);
				this.toExpand.previous('.' + this.options.toggleClass).addClassName(this.options.toggleActive);
			}.bind(this),
			afterFinish: function() {
				this.current.hide();
				this.toExpand.setStyle({ height: this.maxHeight+"px" });
				this.current = this.toExpand;
				this.isAnimating = false;
			}.bind(this)
		});
	}

};