/*--------------------------------------------------------------------------*
 * 
 * Copyright (C) 2008 Brand Labs LLC
 * 
 * Sequential Banner
 * 
 * Version 1.0.0
 * 
 *--------------------------------------------------------------------------*/
var Banner = Class.create({
	/**
	 * 
	 * @param {Object} link
	 * @param {Object} imageSrc
	 */
	initialize: function(link, imageSrc) {
		this.link = link;
		this.imageSrc = imageSrc;
	},
	
	/**
	 * 
	 */
	getElement: function() {
		var anchorElement = null;
		var imageElement = null;
		
		anchorElement = new Element('a', {href: this.link});
		imageElement = new Element('img', {src: this.imageSrc});
		
		//Add the image under the anchor
		anchorElement.insert(imageElement);
				
		return anchorElement;
	}
});
	
var SequentialBanner = Class.create({
	/**
	 * 
	 * @param {Object} banners
	 * @param {Object} elementName
	 * @param {Object} delay
	 */
	initialize: function(banners, elementName, delay) {
		this.currentPosition = 0;
		this.element = null;
		
		this.banners = banners;
		this.delay = delay;
		
		Event.observe(window, 'load', this.load.bindAsEventListener(this, elementName));
		
		//Pre-load images
		this.banners.each(function(item){
			item.getElement();
		});
	},
	
	/**
	 * 
	 */
	load: function() {
		var args = $A(arguments);
		args.shift();
		
		//Get the element
		this.element = $(args.first());
		
		//Start the executer
		this.start();
	},
	
	/**
	 * 
	 */
	start: function() {
		if(this.pe != null) {
			this.stop();				
		}
		this.pe = new PeriodicalExecuter(this.execute.bindAsEventListener(this), this.delay);			
	},
	
	/**
	 * 
	 */
	stop: function() {
		if(this.pe != null) {
			this.pe.stop();
			this.pe = null;
		}
	},
	
	/**
	 * 
	 */
	execute: function() {
		this.currentPosition++;
		this.currentPosition = this.currentPosition % this.banners.size();
	
		this.updateElement();
	},
	
	/**
	 * 
	 */
	updateElement: function() {
		this.element.update(this.banners[this.currentPosition].getElement());
	}
});