var articleSlider = function (opts){

    var _this 			= this;
	this.moduletable	= $(opts.moduletable);

    if(this.moduletable.length === 0){
        return;
    }

	this.btns				= jQuery(opts.btns);

    this.speed 				= opts.speed;
	this.interval 			= opts.interval;
	this.wait 				= opts.wait;

    this.isAnimated 		= false;
	this.timer				= false;

    var isAutoScroll 		= this.isAutoScroll = opts.isAutoScroll || false;
	var isInfiniteScroll 	= this.isInfiniteScroll = (this.autoScroll === true) ? true : (opts.isInfiniteScroll || false);

	this.currentPage		= 0;
	this.nextPage			= this.currentPage+1;
    this.totalPages			= this.moduletable.children().length;
	
	this.moduletable.children(":first-child").siblings().hide();
	this.btns.children(":first-child").addClass('nav_active');

	
	$('a',this.btns).each(function(pos){ 
        $(this).bind('click',function(){ 
			if(_this.totalPages > 1 && !_this.isAnimated && _this.currentPage!=pos){
				_this.isAnimated = true;
           		_this.nextPage	= pos;
				_this.gotoNext();
			}
			_this.reLaunchAnimation();
			return false;
        }); 
    });
	
	
    if(this.totalPages > 1){
        this.autoScrollTimer = false;
        if(isAutoScroll){
            this.launchAutoScroll();
        }
    }
};


articleSlider.prototype = {
	reLaunchAnimation: function(){
		var _this = this;
		if(this.autoScrollTimer){
        	clearInterval(_this.autoScrollTimer);
        	this.autoScrollTimer = null;
		
			if(this.timer){
				this.clearTimer();
			}
			
			this.timer = setTimeout(function(){
				_this.launchAutoScroll();
				_this.clearTimer();
			},this.wait);
		}
	},
	clearTimer: function(){
		clearTimeout(this.timer);
		this.timer = false;
	},
	launchAutoScroll: function() {
		var _this = this;
		this.autoScrollTimer = setInterval(function(){		
				_this.isAnimated	= true;
           		_this.gotoNext();
           },this.interval);
	},
	gotoNext:function(){
	
		var _this = this;
		if(this.nextPage>=this.totalPages){
			this.nextPage=0;
		}
		
		jQuery('div.article:eq('+this.currentPage+')').fadeOut(_this.speed,function(){			
			_this.btns.children(".nav_active").removeClass('nav_active');
		});
		
		jQuery('div.article:eq('+this.nextPage+')').fadeIn(_this.speed,function(){
			_this.btns.children(":eq("+_this.nextPage+")").addClass('nav_active');
			_this.currentPage = _this.nextPage;
			_this.nextPage++;
			_this.isAnimated = false;
		});		
    }
};
