var doScrollLeft = function() {
	if 	(theCurrentPosition < theStartingPosition) {
		theGallery.get('tween', {duration:1000,onComplete:function(e){
			//theCurrentPosition = theCurrentPosition+310;													  
		}}).start('left',theCurrentPosition+310);
		theCurrentPosition = theCurrentPosition+310;
		if (theGalleryHolder.getElements('span.right').hasClass('noscroll')) {
			theGalleryHolder.getElements('span.right').removeClass('noscroll');
		}		
		if (theCurrentPosition == theStartingPosition) {
			this.addClass('noscroll');
		}		
	}
}

var doOverLeft = function() {
	this.addClass('over');
	this.setStyle('cursor','pointer');
}

var doLeaveLeft = function() {
	this.removeClass('over');
	this.setStyle('cursor','auto');
}

var doScrollRight = function() {	
	if 	(theCurrentPosition > theEndingPosition+310) {
		theGallery.get('tween', {duration:1000,onComplete:function(e){
			//theCurrentPosition = theCurrentPosition-310;													  
		}}).start('left',theCurrentPosition-310);
		theCurrentPosition = theCurrentPosition-310;
		if (theGalleryHolder.getElements('span.left').hasClass('noscroll')) {
			theGalleryHolder.getElements('span.left').removeClass('noscroll');
		}
		if (theCurrentPosition == theEndingPosition+310) {
			this.addClass('noscroll');
		}
	}
}

var doOverRight = function() {
	this.addClass('over');
	this.setStyle('cursor','pointer');
}

var doLeaveRight = function() {
	this.removeClass('over');
	this.setStyle('cursor','auto');
}

var doScrollingGallery = function() {
	theGalleryHolder = $('scrollinggallery');
	theGallery = theGalleryHolder.getFirst('ul');
	counter = 0;
	theGallery.getChildren('li').each(function(el){
		counter+=1;
	});
	theGallery.setStyle('width',counter*310);
	theGalleryHolder.getElements('span.left').addEvents({
		'click': doScrollLeft,
		'mouseover': doOverLeft,
		'mouseleave': doLeaveLeft
	});
	theGalleryHolder.getElements('span.right').addEvents({
		'click': doScrollRight,
		'mouseover':doOverRight,
		'mouseleave':doLeaveRight
	});
	theStartingPosition = 80;
	theEndingPosition = theStartingPosition - (counter*310);
	theCurrentPosition = theStartingPosition;
	theGalleryHolder.getElements('span.left').addClass('noscroll');
	slideInterval = doScrollRight.periodical(5000, theGalleryHolder.getElements('span.right'));
	
	theGalleryHolder.addEvents({
		'mouseover' : function() {
			$clear(slideInterval);
		},
		'mouseleave' : function() {
			slideInterval = doScrollRight.periodical(5000, theGalleryHolder.getElements('span.right'));		
		}
	});
}

var reinstateSlide = function() {
	slideInterval = doScrollRight.periodical(5000, theGalleryHolder.getElements('span.right'));	
}



window.addEvent('domready', doScrollingGallery);