/**
 * @author Ideatronic
 */
var scrollGallery = new Class({
    options:{
	galleryId:'scrollGallery',
	pagesId:'.scrollPages',
	galleryContId:'scrollGalleryContainer',
	scrollLeft:'scrollGalleryLf',
	scrollRight:'scrollGalleryRg',
	eventType:'click',
	periodical:5000,
	paginationClass:'',
	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.pagination = false;
		
	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.options.paginationClass!='')
	{
	    this.pagination = true;
	    this.paginationClass = this.options.paginationClass;
	    this.paginate();
	}
			
		
	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();
		}
	    });
	}
	this.scrollGallery(0);
    },
    scrollGallery:function(value){
	this.sg.start(value, 0);
    },
    scrollLeft:function(){
	if((this.currentWidth-this.scrollWidth)>=0 && (this.currentPage-1)>=0)
	{
	    this.scrollGallery((this.currentWidth-this.scrollWidth));
	    this.currentWidth-=this.scrollWidth;
	    this.currentPage--;
	    if(this.pagination)
		this.markPage(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++;
	    if(this.pagination)
		this.markPage(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;
    },
    openPage:function(pageNo)
    {
	var scrollWidth = pageNo*this.scrollWidth;
	this.scrollGallery(scrollWidth);
	this.currentPage = pageNo;
	
	this.markPage(this.currentPage);
    },
    markPage:function(pageNo)
    {
	for(var i=0; i<this.paginationTab.length; i++)
	{
	    this.paginationTab[i].removeClass('active');
	}
	this.paginationTab[pageNo].addClass('active');
    },
    paginate:function()
    {
	var paginer = $$(this.paginationClass)[0];
	var paginationParent = paginer.getParent();
	var cloneTab = new Array();
	for(var i=0; i<this.pages.length; i++)
	{
	    var $this = this;
	    cloneTab[i] = paginer.clone();
	    cloneTab[i].inject(paginationParent);
	    var tabs = cloneTab;
	    cloneTab[i].iter = i;
	    cloneTab[i].addEvent('click',function(){
		$this.openPage(this.iter);
		$this.stopAutoScroll();
	    })
	}
	paginer.dispose();
	this.paginationTab = cloneTab;
    }
});
scrollGallery.implement(new Options);

