
var HomeSlideShow = new Class({
	Implements: [Events, Options],
	options : {
		'autoplay' : true,
		'delay' : 10000
	},
	
	initialize:function(options){
		this.setOptions(options);
		
		if ($chk($$('div.polymorph a.previous')[0])) {
			$$('div.polymorph a.previous')[0].addEvent('click', function(e) {
				this.stopAutoPlay();
				this.showPrev();
				e.stop();
			}.bindWithEvent(this));
		}
		if ($chk($$('div.polymorph a.next')[0])) {
			$$('div.polymorph a.next')[0].addEvent('click', function(e) {
				this.stopAutoPlay();
				this.showNext();
				e.stop();
			}.bindWithEvent(this));
		}
		this.timer = null;
		if (this.options.autoplay) {
			this.startAutoPlay();
		}
		if ($chk($$('div.actions_home div.slideshow a')[0]))
			$$('div.actions_home div.slideshow a')[0].addEvent('click', function(e){
				this.showNext();
				this.startAutoPlay();
				e.stop();
			}.bindWithEvent(this));
	},
	
	showPrev : function(noScroll){
		var prev = null;
		for (var i=0;i<DragBoxes.order.length;i++) {
			if (DragBoxes.current == DragBoxes.order[i]) {
				if (i != 0) {
					prev = DragBoxes.elements[DragBoxes.order[i-1].substr(7)];
				}
				else {
					prev = DragBoxes.elements[DragBoxes.order[DragBoxes.order.length-1].substr(7)];
				}
				break;
			}
		}
		prev.playBox(noScroll);
	},
	
	showNext : function(noScroll){
		var next = null;
		for (var i=0;i<DragBoxes.order.length;i++) {
			if (DragBoxes.current == DragBoxes.order[i]) {
				if (i != (DragBoxes.order.length-1)) {
					next = DragBoxes.elements[DragBoxes.order[i+1].substr(7)];
				}
				else {
					next = DragBoxes.elements[DragBoxes.order[0].substr(7)];
				}
				break;
			}
		}
		next.playBox(noScroll);
	},
	
	stopAutoPlay : function(){
		$clear(this.timer);
		this.timer = null;
		$('highlight').removeEvents('mouseenter');
	},
	
	startAutoPlay : function(){
		if (this.timer != null)
			return;
		this.timer = this.showNext.periodical(this.options.delay, this, [true]);
		$('highlight').addEvent('mouseenter', function(){
			this.stopAutoPlay();
		}.bind(this));
	}
	
});
