/**
 * @author Profitroom
 */
 
 var scrollGallery = new Class({
    options:{
		galleryId:'scrollGallery',
		pagesId:'.scrollPages',
		galleryContId:'scrollGalleryContainer',
		scrollLeft:'scrollGalleryLf',
		scrollRight:'scrollGalleryRg',
		eventType:'click',
		periodical:5000,
		galleryOrientation:'horizontal',
		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.galleryOrientation = this.options.galleryOrientation;
		this.createBlinks(this.pages);	
		this.scrollDirection = 0;								//0=right, 1=left
		this.currentPage = 0;
		this.autoscroll = this.options.autoscroll;
		this.currentWidth = 0;
		
		if (this.galleryOrientation=='horizontal') 
		{
			this.fullWidth = this.pages.length * (this.pages[0].getScrollSize().x);
			this.scrollWidth = this.pages[0].getScrollSize().x;
			this.galleryCont.setStyle('width', this.fullWidth);
		}
		else if(this.galleryOrientation == 'vertical')
		{
			this.fullWidth = this.pages.length * (this.pages[0].getScrollSize().y);
			this.scrollWidth = this.pages[0].getScrollSize().y;
			this.galleryCont.setStyle('height', this.fullHeight);
		}
		
		this.currentWidth = this.scrollWidth*11;
		this.currentPage = 10;
			
		this.sg = new Fx.Scroll(this.options.galleryId,{
		    link:'cancel',
		    duration:1000
		});
		
		this.galleryCont.addEvent('mousewheel', function(event) {
			var ev = new Event(event);
			var orient = ev.wheel;
			ev.stop();		
			if(orient<0)
				this.scrollRight()
			else	
				this.scrollLeft()
		}.bind(this))
		
		if(this.scrollLeftCont && this.scrollRightCont)
		{
		    this.scrollLeftCont.addEvents({
				'click':function(){
				    $this.scrollLeft()
				}.bind(this),
				'mouseenter':function(e){
				    var ev = new Event(e);
				    ev.stopPropagation();
				    this.stopAutoScroll();
				}.bind(this)
			    })
			this.scrollRightCont.addEvents({
				'click':function(){
				    $this.scrollRight()
				}.bind(this),
				'mouseenter':function(e){
				    var ev = new Event(e);
				    ev.stopPropagation();
				    this.stopAutoScroll();
				}.bind(this)
		    });
		}
		
		if(this.autoscroll)
		{
		    this.startAutoScroll();
		    $(this.options.galleryId).addEvents({
			'mouseenter':function(e){
			    var ev = new Event(e);
			    ev.stopPropagation();
			    this.stopAutoScroll();
			}.bind(this),
			'mouseleave':function(e){
			    var ev = new Event(e);
			    ev.stopPropagation();
			    this.startAutoScroll();
			}.bind(this)
		    });
		}
		this.scrollGallery(0);
    },
    scrollGallery:function(value){
		if(this.galleryOrientation == 'horizontal')
			this.sg.start(value-(11*this.scrollWidth), 0);
		else
			this.sg.start(0,value-(11*this.scrollWidth));
	},
	scrollLeft:function(){
	if((this.currentWidth-this.scrollWidth)>=(this.scrollWidth*11) && (this.currentPage-1)>=0)
		{
		    this.scrollGallery((this.currentWidth-this.scrollWidth));
		    this.currentWidth-=this.scrollWidth;
		    this.currentPage--;
		    return ;
		}
    },
    scrollRight: function(){
		if ((this.currentWidth+this.scrollWidth)<=this.fullWidth && (this.currentPage+1)<this.pages.length) {
		    this.scrollGallery((this.currentWidth+this.scrollWidth));
		    this.currentWidth+=this.scrollWidth;
		    this.currentPage++;
		    return;
		}
    },
    autoScroll:function(){
		if((((this.currentWidth+this.scrollWidth) >= this.fullWidth) || (this.currentPage == this.pages.length-1)) && this.scrollDirection == 0)
		{
		    return this.scrollDirection = 1;
		}
		if((this.currentWidth == 0||this.currentPage ==0) && this.scrollDirection == 1)
		{
		    return this.scrollDirection = 0;
		}
		if(this.scrollDirection == 1)
		    this.scrollLeft();
		if (this.scrollDirection == 0)
		    this.scrollRight();
	    },
	    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;
    },
	
	createBlinks: function(elements)
	{
		elements.each(function(el){
			el.setStyle('opacity',0.2);
			el.addEvents({
				'mouseenter':function(){
					$(this).fade(1);
				},
				'mouseleave':function(){
					$(this).fade(0.2)
				}
			});
		})
	}
});
scrollGallery.implement(new Options);
