var promoBanners = Class.create(); promoBanners.prototype = { initialize: function( container_id, tag_name, class_name, expires ) { this.initialized = false; this.container = $(container_id); if( ! this.container ) { return false; } this.elements = $A( this.container.getElementsByTagName( tag_name ) ); if( ! this.elements ) { return false; } this.expires = ( expires ) ? parseInt(expires) : 30; this.class_name = class_name; this.interval = null; this.max_loop = null; this.current_loop = 0; this.max = this.elements.length - 1; this.cookie = container_id + '_last_index'; this.last_index = Cookie.get( this.cookie ); this._next(); for( var i=0; i<=this.max; i++ ) { if( i != this.last_index ) { Element.addClassName( this.elements[i], this.class_name ); } } this.initialized = true; return true; }, _next: function() { if( this.last_index == null ) { this.last_index = Math.round( Math.random() * this.max ); } else { this.last_index = parseInt(this.last_index); if( this.last_index == this.max ) { this.last_index = 0; } else { ++this.last_index; } } if( this.last_index < 0 || this.last_index > this.max ) { this.last_index = Math.round( Math.random() * this.max ); } Cookie.set( this.cookie, this.last_index, this.expires ); return true; }, _toggle: function() { Element.addClassName( this.elements[this.last_index], this.class_name ); this._next(); Element.removeClassName( this.elements[this.last_index], this.class_name ); if( this.max_loop ) { ++this.current_loop; if( this.current_loop >= this.max_loop ) { if( this.interval ) clearInterval(this.interval); } } return true; }, _play: function( time_interval, max_loop ) { if( !this.initialized ) { return false; } this.interval = setInterval( this._toggle.bind(this), time_interval*1000 ); if( parseInt(max_loop) ) { this.max_loop = (this.max+1)*max_loop; } return true; } }