var tagScroller = new Class ( {
	
	Implements: [Options, Events],
	
	options: {'tagHolder' : null, 'period': 1500},
	tags: null,
	currentTagIndex: 0,
	myFx: null,
	
	initialize: function(options) {
    	this.setOptions(options); 
    	if (this.options.tagHolder == null) {
    		return;
    	}
    	this.tags = $$(this.options.tagHolder.getElements(".tag"));
    	if (this.tags.length < 2) {
    		return;
    	}
    	this.setupFirstRun();
    	this.myFx = new Fx.Scroll(this.options.tagHolder);
    	this.toNextTag.periodical(this.options.period, this);
  	},
  	
  	setupFirstRun: function() {
  		if (this.tags.length == 2) {
  			return;
  		}
  		for(x = 2; x < this.tags.length; x++) {
  			this.tags[x].dispose();
  		}
  	},
  	
  	toNextTag: function() {	
  		var scroller = this;
  		this.myFx.toBottom().chain(
  			function() {
  				 //get rid of top item 	and reposition at the new top
  				scroller.currentTagIndex++;
  				if (scroller.currentTagIndex > scroller.tags.length - 1) {
  					scroller.currentTagIndex = 0;
  				}
  				scroller.options.tagHolder.getElements(".tag")[0].dispose();
  				scroller.options.tagHolder.scrollTo(0,0);
  				scroller.setupNextTag();
  			}
  		);
  		
  	},
  	
  	setupNextTag: function() {
  		var nextTagIndex = this.currentTagIndex + 1;
  		if (nextTagIndex > this.tags.length - 1) {
  			nextTagIndex = 0;
  		}
  		var nextTag = this.tags[nextTagIndex].clone();
  		//IE is gay
  		nextTag.getElement('img').removeProperties('width', 'height');
  		nextTag.inject(this.options.tagHolder);
  		
  	}
	
});

window.addEvent('domready', function() {
	$$(".tags").each(function(item) {
		if (typeof(_tag_period) != 'undefined') {
			new tagScroller({'tagHolder': item, 'period': _tag_period});
		} else {
			new tagScroller({'tagHolder': item});
		}
	});
});

