/**
 * @author Ideatronic
 */
var scrollGallery = new Class({
	options:{
		galleryId:'scrollGallery',
		pagesId:'.scrollPages',
		galleryContId:'scrollGalleryContainer',
		scrollLeft:'scrollGalleryLf',
		scrollRight:'scrollGalleryRg',
		eventType:'click',
		periodical:5000,
		autoscroll:true
	},
	initialize:function(options){
		this.setOptions(options);
		var $this = this;
		this.gallery = $(this.options.galleryId);
		this.pages = $$(this.options.pagesId);
		this.galleryCont = $(this.options.galleryContId);
		this.scrollLeftCont = $(this.options.scrollLeft);
		this.scrollRightCont = $(this.options.scrollRight);
		
		
		this.scrollDirection = 0;								//0=right, 1=left
		this.currentPage = 0;
		this.periodical = this.options.periodical;
		this.autoscroll = this.options.autoscroll;
		this.currentWidth = 0;
		
		
		this.fullWidth = this.pages.length*(this.pages[0].getScrollSize().x);
		this.scrollWidth = this.pages[0].getScrollSize().x;
		this.galleryCont.setStyle('width',this.fullWidth);
		
		this.sg = new Fx.Scroll(this.options.galleryId,{link:'cancel',duration:1000});
		
		if(this.scrollLeftCont && this.scrollRightCont)
		{
			this.scrollLeftCont.addEvents({
					'click':function(){
						$this.scrollLeft()
					},
					'mouseenter':function(e){
						var ev = new Event(e);
						ev.stopPropagation();
						$this.stopAutoScroll();
					}
				});
			this.scrollRightCont.addEvents({
					'click':function(){
						$this.scrollRight()
					},
					'mouseenter':function(e){
						var ev = new Event(e);
						ev.stopPropagation();
						$this.stopAutoScroll();
					}
				});
		}
		
		
		if(this.autoscroll)
		{
			this.startAutoScroll();
			$(this.options.galleryId).addEvents({
				'mouseenter':function(e){
					var ev = new Event(e);
					ev.stopPropagation();
					$this.stopAutoScroll();
				},
				'mouseleave':function(e){
					var ev = new Event(e);
					ev.stopPropagation();
					$this.startAutoScroll();
				}
			});
		}		
	},
	scrollGallery:function(value){
		this.sg.start(value, 0);
	},
	scrollLeft:function(){
		if((this.currentWidth-this.scrollWidth)>=0)
		{
	      	this.scrollGallery((this.currentWidth-this.scrollWidth));
	      	this.currentWidth-=this.scrollWidth;
	      	return;
	    }
	},
	scrollRight: function(){
		if ((this.currentWidth+this.scrollWidth)<this.fullWidth) {
			this.scrollGallery((this.currentWidth+this.scrollWidth));
			this.currentWidth+=this.scrollWidth;
			return;
		}
	},
	autoScroll:function(){
		if((this.currentWidth-this.scrollWidth)>=0 && this.scrollDirection == 1)
		{
	      	this.scrollGallery((this.currentWidth-this.scrollWidth));
	      	this.currentWidth-=this.scrollWidth;
	      	return;
	    }
		if ((this.currentWidth+this.scrollWidth)<this.fullWidth && this.scrollDirection == 0) {
			this.scrollGallery((this.currentWidth+this.scrollWidth));
			this.currentWidth+=this.scrollWidth;
			return;
		}
		if((this.currentWidth+this.scrollWidth) >= this.fullWidth && this.scrollDirection == 0)
		{
			return this.scrollDirection = 1;
		}
		if(this.currentWidth == 0 && this.scrollDirection == 1)
		{
			return this.scrollDirection = 0;
		}
	},
	startAutoScroll:function(){
		var $this = this;
		$clear(this.timer);
		this.timer = (function(){
			$this.autoScroll();
		}).periodical(this.periodical);
		this.scrollInProgress = true;
	},
	stopAutoScroll:function(){
		$clear(this.timer);
		this.scrollInProgress = false;
	}
});
scrollGallery.implement(new Options);

