// Set up our UXB namespace
if(typeof(UXB)=="undefined"){UXB={};}

/*Accordion Class - "holds the sections"
-----------------------------------------------------*/
UXB.Accordion = Class.create();
Object.extend(UXB.Accordion.prototype, {
	
	sections: null,
	container: null,
	currentSection: null,
	firstOpenSection: 0,
	transitionSpeed: '.6',
	
	initialize: function(container, options) {
		this.sections = [];
		this.container = container;
		
		// Initialize our options
		if(options != null && typeof(options) != 'undefined') {
			if(typeof(options.firstOpenSection) != "undefined") {
				this.firstOpenSection = options.firstOpenSection;
			}
			if(typeof(options.transitionSpeed) != "undefined") {
				this.transitionSpeed = options.transitionSpeed;
			}
		}
	},
	
	addSection: function(newSection) {
		this.sections.push(newSection);
	},
	
	switchSections: function(trigger) {
		if(trigger == this.currentSection) {
			window.location = this.currentSection.handle.href;
			return;
		}
		this.previousSection = this.currentSection;
		this.currentSection = trigger;
		this.currentSection.openSection();
		if(this.firstOpenSection == -1)
		  this.firstOpenSection = 1;
		else
		  this.previousSection.closeSection();
		
	}
	
});



/*Section Class - "the individual sections"
-----------------------------------------------------*/
UXB.Section = Class.create();
Object.extend(UXB.Section.prototype, {
	
	contentElement: null,
	handle: null,
	accordion: null,
	isOpen: null,
	isTransitioning: null,
	
	initialize: function(contentElement, handle, accordion, options) {
		this.contentElement = contentElement;
		this.handle = handle;
		this.accordion = accordion;
		this.isOpen = true;
		this.isTransitioning = false;
		
		// Initialize our options
		if(options != null && typeof(options) != 'undefined') {
			if(typeof(options.sectionCount) != "undefined") {
				this.sectionCount = options.sectionCount;
			}
		}
		
		// If this sectionCount equals Accordion's firstOpenSection, open it
		if(this.sectionCount == this.accordion.firstOpenSection) {
			this.openSection();
			this.accordion.currentSection = this;
		} else {
			this.closeSection();
		}
		
		// Set our on click handler
		var fireTrigger = function(event) {
			Event.stop(event);
			this.trigger();
		}
		Event.observe(this.handle, 'click', fireTrigger.bind(this), false);
	},
	
	trigger: function() {
		this.accordion.switchSections(this);
	},
	
	openSection: function() {
		if(this.isOpen || this.isTransitioning) {
			return;
		}
		this.isTransitioning = true;
		var afterFinish = function() {
			this.isOpen = true;
			this.isTransitioning = false;
		}.bind(this);
		this.switchHandleState('open');
		Effect.BlindDown(this.contentElement, {duration: this.accordion.transitionSpeed, afterFinish: afterFinish});
	},
	
	closeSection: function() {
		if(this.isTransitioning) {
			var closeDelay = this.closeSection.bind(this);
			setTimeout(closeDelay, this.accordion.transitionSpeed * 1000);
		}
		if(!this.isOpen) {
			return;
		}
		this.isTransitioning = true;
		var afterFinish = function() {
			this.isOpen = false;
			this.isTransitioning = false;
			this.switchHandleState('close');
		}.bind(this);
		Effect.BlindUp(this.contentElement, {duration: this.accordion.transitionSpeed, afterFinish: afterFinish});
	},
	
	switchHandleState: function(action) {
		if(action == 'open') {
			this.handle.setStyle({'backgroundPosition': "0 100%"});
		} else {
			this.handle.setStyle({'backgroundPosition': "0 0"});
		}
	}
	
});


/*The "Behavior"
-----------------------------------------------------*/
var Accordion = Behavior.create({
  initialize: function() {
		var container = this.element;
    var accordion = new UXB.Accordion(container, {firstOpenSection: '3'});
		var d = 0;
		$$('#' + accordion.container.id + ' li.section').each(function(li) {		
			var contentElement = li.down('div.body');
			var handle = li.down('a.handle');
			var section = new UXB.Section(contentElement, handle, accordion, {sectionCount: d});
			accordion.addSection(section);
			d++;
		});
  }
});