/*
//*******
//
//	filename: carousel.js
//	author: Zack Brown
//	date: 6th August 2010
//
//*******
*/

//declare Carousel class
var Carousel = Class.create();

//declate the Carousel prototype
Carousel.prototype = {

	//periodical executer
	pe: null,

	//wrapper function for init
	initialize: function(options)
	{
		//gather list element
		this.list = $(options.list);
		
		//check element is valid
		if(this.list != undefined)
		{
			//gather input element
			this.total_images = this.list.childElements().length;
			
			//current image index
			this.index = 0;
			
			//rotation delay
			this.delay = 1;
			
			//gather child elements
			var children = this.list.childElements().without(this.list.childElements().first());
			
			//loop through array of images
			children.each(function(element)
			{
				//remove image
				element.setStyle({display: "none"});
			});
			
			//create a periodical executer
			this.pe = new PeriodicalExecuter(this.rotate.bind(this), 10);
			
			//check elements exists
			if($("carousel_next") != undefined && $("carousel_previous") != undefined)
			{
				//observe slide next and previous click events
				Event.observe($("carousel_next"), "click", this.slide_next.bind(this));
				Event.observe($("carousel_previous"), "click", this.slide_previous.bind(this));
			}
		}
	},
	
	//rotate images
	rotate:function()
	{
		//gather child elements
		var children = this.list.childElements();
		
		//fade out current image
		children[this.index].fade({ duration: this.delay});
		
		//increment index
		this.index = (this.index < (this.total_images - 1) ? (this.index + 1) : 0);
		
		//fade in new image
		children[this.index].appear({ duration: this.delay});
	},
	
	//rotate to next slide
	slide_next: function()
	{
		//stop periodical executer
		this.pe.stop();
		
		//gather child elements
		var children = this.list.childElements();
		
		//fade out current image
		children[this.index].fade({ duration: this.delay});
		
		//increment index
		this.index = (this.index < (this.total_images - 1) ? (this.index + 1) : 0);
		
		//fade in new image
		children[this.index].appear({ duration: this.delay});
		
		//create a periodical executer
		this.pe = new PeriodicalExecuter(this.rotate.bind(this), 10);
	},
	
	//rotate to previous slide
	slide_previous: function()
	{
		//stop periodical executer
		this.pe.stop();
		
		//gather child elements
		var children = this.list.childElements();
		
		//fade out current image
		children[this.index].fade({ duration: this.delay});
		
		//decrement index
		this.index = (this.index > 0 ? (this.index - 1) : (this.total_images - 1));
		
		//fade in new image
		children[this.index].appear({ duration: this.delay});
		
		//create a periodical executer
		this.pe = new PeriodicalExecuter(this.rotate.bind(this), 10);
	}
};

//observe window load events
Event.observe(window, "load", function()
{
	//create a new carousel
	var carousel = new Carousel({list: "carousel_list", image: "page_carousel_image"});
	
});
