/**
 * Class for news (FE)
 *
 * @package News
 * @category Module
 * @author Ondrej Frinta
 * @copyright e-invent s.r.o.
 */	
 
function News() {

	/** @var {object} */
	var self = this;
	
	/* nastaveni musi souhlasit s TPL a CSS*/
	var container = $('#newsContainer');
	var slider = $('#newsSlider');
	var itemWidth = 305;
	var newsTimerDuration = 10000;
	var newsSlideDuration = 550;	
	var totalItems = 0;	
	var actualItem = 0;		
	
	var canAnimate = true;
	var timer = null;	


	
	/**
	 * Class init
	 *
	 * @return {void}
	 */
	self.init = function() {
		totalItems = slider.children('.newsItem').size();
		
		if(totalItems<2) return;
		
		$('.boxNews .newsControl a, .boxNews .newsControl .actualCount').show();
		$('.boxNews .newsControl .actualCount').text("1/"+totalItems);
		$('.boxNews .newsControl .archive').css('background-position', '0 0');
		
		slider.width(totalItems*itemWidth);
		slider.css('position', 'relative');
		
		$('.boxNews .btnPrev').click(function() {
			clearTimeout(timer); timer = null;							
			self.changeImage(actualItem-1);
			return false;
		});	
		
		$('.boxNews .btnNext').click(function() {
			clearTimeout(timer); timer = null;			
			self.changeImage(actualItem+1);	
			return false;
		});		
		
		if(newsTimerDuration>0) {
			timer=setTimeout("newsTimer()", newsTimerDuration);
		}		
	}
	
	/**
	* change image
	*/
	self.changeImage = function(indexNew) {	

		if(!canAnimate) return false;	
		canAnimate = false;
		
		if (indexNew < 0) indexNew = totalItems-1;
		if (indexNew >= totalItems) indexNew = 0;

		var leftOffset = -itemWidth*indexNew;		
		slider.animate({left: leftOffset}, 	newsSlideDuration, function() {canAnimate = true;});
		actualItem = indexNew;
		$('.boxNews .newsControl .actualCount').text((actualItem+1)+"/"+totalItems);		
		if(timer==null && newsTimerDuration>0) {
			timer=setTimeout("newsTimer()", newsTimerDuration);
		}
	}
	
	/**
	* timer handler
	*/
	self.timerHandler = function() {	
		self.changeImage(actualItem+1);		
		timer=setTimeout("newsTimer()", newsTimerDuration);
	}
	
}

var news;
var newsTimer = function()  {news.timerHandler();}

$(document).ready(function() {
	
	news = new News();	
	news.init();
	
});
