var slideInRecent = new Class({
	options: {
		displayContainerDiv: 'displaytopics',
		recentContainerDiv: 'recentitems',
		itemclassname: 'recentitem',
		itemsourceclassname: 'recentitem-source',
		delaytime: 10000,
		slideduration: 900
	},
	initialize: function(options) {
		this.setOptions(options);
		this.allTopicsId = $(this.options.recentContainerDiv);
		this.displaydiv = $(this.options.displayContainerDiv);
		this.alltopics = $$('#'+this.options.recentContainerDiv+' .'+this.options.itemsourceclassname);
		this.sliders = {};
		this.currentlyshowing = {};
		this.shown = {};
		this.showshow = true;
		//this.last
		var i = 0;
		//load any pre loaded discussions:
		$$('#'+this.options.recentContainerDiv+' .showing').each(function(el, ind) {
			this.currentlyshowing[el.id] = el;
		}, this);
		//start animation:
		this.itemcontroller.delay(this.options.delaytime, this);
		//this.timer = this.itemcontroller.periodical(this.options.delaytime, this);
	},
	itemcontroller: function() {
		if(this.showshow)
		{
			//animate discussions:
			var nextslide = this.getnextitem();
			if(nextslide)
			{
				this.slideinitem(nextslide);
				this.slideoutitem(this.getlastitem());
			}
			//wait and fire this function again.
			this.itemcontroller.delay((this.options.delaytime + $random(-3000, 3000) ), this); //fire the next event after delaying, plus or minus 3 seconds.
		}
		else {
			$clear(this.timer);
		}
	},
	slideinitem: function(el) {
		//make display div.
		var existing = $('d'+el.id);
		if($defined(existing))
		{
			existing.getParent().remove(); //remove bottom one.
		} 
		this.eld = new Element('div', {'id': 'd'+el.id, 'class': this.options.itemclassname }).injectTop(this.options.displayContainerDiv);
		this.eld.setHTML(el.innerHTML);
		
		//if(!$defined(this.sliders[el.id]))
		//{
			//if the slider effect doesnt exist yet create it.
		this.sliders[el.id] = new Fx.Slide(this.eld, {duration: this.options.slideduration, transition: Fx.Transitions.linear});
		//}
		this.sliders[el.id].hide();
		this.sliders[el.id].slideIn();
		this.currentlyshowing[el.id] = el;
		
	},
	slideoutitem: function(el) {
		if($defined(el.id) && !$defined(this.sliders[el.id]))
		{
			var oldwidth= ($('d'+el.id).getSize().size.x+'px');
			this.sliders[el.id] = new Fx.Slide("d"+el.id, {duration: this.options.slideduration, transition: Fx.Transitions.linear});
			$('d'+el.id).getParent().setStyle('width', oldwidth );
		}
		if($defined(el.id) && $defined(this.sliders[el.id]))
		{
			this.sliders[el.id].slideOut().chain( function() { 
				delete this.sliders[el.id];
			}.bind(this));
			
			delete this.currentlyshowing[el.id];
			this.shown[el.id] = el;
			
		}
	},
	getnextitem: function() {
		var nextitem = false;
		this.alltopics.each(function(el) { 
			if(!(this.currentlyshowing[el.id]) && !(this.shown[el.id]) && !nextitem )
			{
				//if it is not currently being shown, hasnt been shown, and one hasnt been selected already...
				nextitem = el;
				return el;
			}
		}, this);
		if(!nextitem) {
			//didnt find any.
			//reset the list and call this function again.
			this.shown = {};
			this.showshow = false; //turn off animation - 
			//return this.getnextitem();
			return false;
		}
		return nextitem;
	},
	getlastitem: function() {
		var last = false;
		$each(this.currentlyshowing, function(el, index){
			if(!last) {
				last = el;
				delete this.currentlyshowing.index;
			}
		 }, this);
		 if(!last)
		 {
		 	return false;
		 }
		return last;
	}
	

});

slideInRecent.implement(new Options, new Events);

