/**
 * Class for news (backend)
 *
 * @package News
 * @category Module
 * @author ofrinta
 * @copyright e-invent s.r.o.
 */
function News() {

	/** @var */
	var self = this;
	var MAX_BOX_ITEMS = 4;
	var TIMEOUT = 6500;
	var timer = null;
	var active = 0;
	var total = 0;
	var holder = null;
	var bullHolder = null;
	var itemWidth = 0;
	
	/**
	 * Class init
	 *
	 * @return {void}
	 */
	self.init = function() {				
		
		total = $('.boxNews .item').size();
		total = (total < MAX_BOX_ITEMS) ? total : MAX_BOX_ITEMS; 
		
		itemWidth = $('.boxNews .content').width();

		//create buttons
		$('.boxNews .btnHolder').append('<div class="bullets" />');
		bullHolder = $('.boxNews .btnHolder .bullets');
		for (var i = 0; i < MAX_BOX_ITEMS; i++) {				
			if (total - i > 0) {
				bullHolder.append('<a href="#'+(i+1)+'" title="#'+(i+1)+'" class="bull"><span>'+(i+1)+'</span></a>');
			} else {
				bullHolder.append('<span class="bull"><!--disabled--></span>');					
			}
		}
		bullHolder.children("a:first").addClass("active");

		if(total>1) {
			$('.boxNews .content').wrapInner('<div class="contentWrap" />');
			holder = $('.boxNews .contentWrap');
			$(holder).width(itemWidth*total);
			
			/* init click function */			
			bullHolder.children("a").click(function(){																							
				var index = bullHolder.children("a").index(this);
				if(index == active)  return false;

				var offset = -itemWidth*(index); 
				bullHolder.children("a").removeClass("active");
				$(this).addClass("active");
				active = index;
				

				$(holder).animate({'left' : offset+'px'}, 350);

				clearTimeout(timer);
				timer = setTimeout("news.timerHandler()", TIMEOUT);				
				
				return false;
			});	
	
			timer = setTimeout("news.timerHandler()", TIMEOUT);				
		}
	}
	
	self.timerHandler = function() {
		var index = active+1;
		
		if (index >= total) index=0;

		var offset = -itemWidth*(index); 
		bullHolder.children("a").removeClass("active");
		$(bullHolder.children("a").get(index)).addClass("active");
		active = index;			

		$(holder).animate({'left' : offset+'px'}, 350);		
		
		timer = setTimeout("news.timerHandler()", TIMEOUT);
	}
}

var news = new News();
$(document).ready( function() {															
	news.init();
});
