/*
//*******
//
//	filename: accordion.js
//	author: Zack Brown
//	date: 6th December 2010
//
//*******
*/

//declare accordion class
var Accordion = Class.create();

//declare accordion prototype
Accordion.prototype = {

	//accordion wrappers
	accordion_wrappers: new Array(),

	//init function
	initialize: function()
	{
		//gather all accordion wrappers
		this.accordion_wrappers = $$(".column_accordion_wrapper");
		
		//check array of accordion wrappers is valid
		if(this.accordion_wrappers != undefined && this.accordion_wrappers.length > 0)
		{
			//loop index
			var index = 1;
			
			//loop through array of accordion wrappers
			this.accordion_wrappers.each(function(accordion_wrapper)
			{
				//check accordion element children exist
				if(accordion_wrapper.childElements().length > 0)
				{
					//gather accordion element
					var accordion = accordion_wrapper.childElements().last();
					
					//check accordion element is valid
					if(accordion != undefined && accordion.className == "column_accordion")
					{
						//set accordion open flag
						accordion.open = false;
						
						//set accordion animating flag
						accordion.animating = false;
						
						//set accordion id
						accordion.id = "accordion_" + index;
						
						//observe mouse over events
						Event.observe(accordion_wrapper, "mouseover", function()
						{
							//show accordion
							this.show(accordion);
							
						}.bind(this));
					}
				}
				
				//increment loop index
				++index;
				
			}.bind(this));
			
			//create periodical exectuter
			var periodical_executer = new PeriodicalExecuter(this.hideAll.bind(this), 5);
		}
		
		//gather accordion handles
		var handles = $$(".accordion_handle");
		
		//check array of accordion handles is valid
		if(handles != undefined && handles.length > 0)
		{
			//loop index
			index = 1;
			
			//loop through array of accordion handles
			handles.each(function(handle)
			{
				//gather accordion element
				var accordion_column = $("accordion_" + index);
				
				//check accordion element is valid
				if(accordion_column != undefined && accordion_column.className == "column_accordion")
				{
					//observe mouse over events
					Event.observe(handle, "mouseover", function()
					{
						//show accordion
						this.show(accordion_column);
						
					}.bind(this));
				}
				
				//increment loop index
				++index;
				
			}.bind(this));
		}
	},
	
	//show accordion
	show: function(accordion)
	{
		//check accordion is valid
		if(accordion != undefined)
		{
			//check accordion open and animating flag
			if(accordion.open == false && accordion.animating == false)
			{
				//show accordion
				Effect.BlindDown(accordion, {	duration: 0.25,
												beforeStart: function()
												{
													//set accordion open flag
													accordion.open = true;
													
													//set accordion animating flag
													accordion.animating = true;
													
													//hide all accordiond
													this.hideAll(accordion);
													
												}.bind(this),
												afterFinish: function()
												{
													//set accordion animating flag
													accordion.animating = false;
													
												}});
			}
		}
	},
	
	//hide all accordions
	hideAll: function(accordion)
	{
		//check accordion is valid
		if(accordion != null && accordion != undefined)
		{
			//gather all accordions except for newly opened accordion
			var accordions = $$(".column_accordion").without(accordion);
		}
		else
		{
			//gather all accordions
			var accordions = $$(".column_accordion");
		}
		
		//check array of accordions is valid
		if(accordions != undefined && accordions.length > 0)
		{
			//loop through array of accordions
			accordions.each(function(accordion)
			{
				//hide accordion
				this.hide(accordion);
				
			}.bind(this));
		}
	},
	
	//hide accordion
	hide: function(accordion)
	{
		//check accordion is valid
		if(accordion != undefined)
		{
			//check accordion open and animating flag
			if(accordion.open == true && accordion.animating == false)
			{
				//hide accordion
				Effect.BlindUp(accordion, {	duration: 0.25,
											beforeStart: function()
											{
												//set accordion animating flag
												accordion.animating = true;
											},
											afterFinish: function()
											{
												//set accordion open flag
												accordion.open = false;
												
												//set accordion animating flag
												accordion.animating = false;
												
											}});
			}
		}
	}

};

//observe window load events
Event.observe(window, "load", function()
{
	//create a new accordion
	var accordion = new Accordion();
	
});
