// Banner Rotator for SDA
// By: David Rugendyke
// 2010

window.addEvent('domready', function() {
	
	// The main products menu
	var banners = new rotateBanners();

});


var rotateBanners = new Class({
									
        Implements: [Options, Events],
		
		// Default Options
        options: {
				banner_rotate_delay: 6000,
                banner_container: 'banners',
				banner_class: '.banner-image',
				banner_array: Array()
        },
		
		// The constructor method
        initialize: function(options){
			// Set the options
			this.setOptions(options);
	
			// Only run if the menu is present
			if($(this.options.banner_container)) {
				// Get all the banners elements into an array
             	this.get_banners();
				// Proceed if we have banners
				if(this.options.banner_array.length > 0) {
					// Hide all banners but the first one if we have banners		
					var count = 0;
					this.options.banner_array.each(function(banner) {
						if(count > 0) { banner.setStyle('display','none'); };
						count++;
					});
									
					// Now start rotating
					this.rotate_banners.periodical(this.options.banner_rotate_delay, this);
				}				
			}
        },
		
		// Get all the banners elements into an array
        get_banners: function() {
			this.options.banner_array = $(this.options.banner_container).getElements(this.options.banner_class);	 
		},
		
		// Rotates the banners
		rotate_banners: function() {
			
			var Ob = this.options;
			// No. of banners
			var total = this.options.banner_array.length;
			// Flag
			var check = false;
		
			// The current banner is visible
			this.options.banner_array.each(function(banner, index) {
				
					// The current showing banner
					if(banner.getStyle('display') == 'block' && check == false) {
						// Skip to the next one now
						var next = parseInt(index);
						next++;
			
						if(!Ob.banner_array[next]) { next = 0; }
						var next_banner = Ob.banner_array[next];
						// Show the next one
						banner.setStyle('display', 'none');
						next_banner.setStyle('display', 'block');
						// Exit the loop now
						check = true;
					}
			});
		
		}

		
});
