
/**
 * @author Dallas Clark <dallas@dallasjclark.com>
 * @description Creates banners using iframes with a controller
 */

var Banner = new Class({
	Implements: [Options, Events],
	
	options: {
		sub_elements: 'a',
		banner_images_container: '',
		banner_images_controller: '',
		banner_images: [],
		
		current_banner_index: 0,
		
		automatic_rotator: '',
		automatic_rotator_delay: 3
	},
	
	/**
	 *
	 */
	initialize: function(options){
		this.setOptions(options);
		
		this.setupBanners();
		this.hideAllBanners();
		if(this.options.banner_images.length > 0) {
			this.showBanner(this.options.current_banner_index);
		}
		this.setupBannerRotation();
	},
	
	/**
	 *
	 */
	setupBanners: function() {
		this.options.banner_images = $(this.options.banner_images_container).getElements(this.options.sub_elements);
	},
	
	/**
	 *
	 */
	hideAllBanners: function() {
		this.options.banner_images.each(function(banner_image) {
			banner_image.setStyle('display', 'none');
		});
	},
	
	/**
	 *
	 */
	showBanner: function(banner_id) {
		this.options.banner_images[banner_id].setStyle('display', '');
	},
	
	/**
	 *
	 */
	setupBannerRotation: function() {
		var rotate_banner = function() {
			this.rotateBanner();
		}.bind(this);
		rotate_banner(); // First Rotate
		this.options.automatic_rotator = rotate_banner.periodical(this.options.automatic_rotator_delay * 1000);
	},
	
	/**
	 *
	 */
	rotateBanner: function() {
		if(this.options.current_banner_index >= this.options.banner_images.length) {
			this.options.current_banner_index = 0;
		}
		this.hideAllBanners();
		this.showBanner(this.options.current_banner_index);
		this.options.current_banner_index = this.options.current_banner_index + 1;
	}
});
