var initialOffset = 0;
var currentOffset=0;
var showTabs = 5;


$(document).ready(
function(){
	$('body').addClass('jsenabled');
	$('#seriesholder').append('<a href="#" class="sipkavlevo"><img src="img/navigation-next.gif" alt="Next" /></a>');
	$('#seriesholder').append('<a href="#" class="disabled sipkavpravo"><img src="img/navigation-previous.gif" alt="Previous" /></a>');
	$('#seriesholder-bottom').append('<a href="#" class="sipkavlevo"><img src="img/navigation-next.gif" alt="Next" /></a>');
	$('#seriesholder-bottom').append('<a href="#" class="disabled sipkavpravo"><img src="img/navigation-previous.gif" alt="Previous" /></a>');
	
	$('a.sipkavlevo').click(
		function(){
			scrollMLeft();
			return false;
		}
	
	);
	$('a.sipkavpravo').click(
		function(){
			scrollMRight();
			return false;
		}
	
	);
	
	if(initialOffset < 0){
		currentOffset = initialOffset;
		tabs = $('#seriesholder ul li').get().length;
		$('#seriesholder ul').css('left',currentOffset*163);
		$('#seriesholder-bottom ul').css('left',currentOffset*163);
		
	
	
		if(initialOffset == 0){	
			$('a.sipkavpravo').not('.disabled').addClass('disabled');
		}else{
			$('a.sipkavpravo').filter('.disabled').removeClass('disabled');
		}

		if(initialOffset == -6){
			$('a.sipkavlevo').not('.disabled').addClass('disabled');
		}else{
			$('a.sipkavlevo.disabled').removeClass('disabled');
		}
	}
}

);


var scrollMLeft = function(){
	scrollM(-3);
}
var scrollMRight = function(){
	scrollM(3);
}

var scrollM = function(offset){
	var offset = getNextOffset(offset);
	$('#seriesholder ul').animate(	{left:offset },'slow');
	$('#seriesholder-bottom ul').animate(	{left: offset},'slow');
}

var getNextOffset = function(direction){
	tabs = $('#seriesholder ul li').get().length;
	var _direction = direction;
	if(currentOffset >= 0 && direction > 0){
		_direction = 0;
	}

	if(currentOffset-showTabs+tabs <=0  && direction< 0){
		_direction = 0;
	}
		
	if(currentOffset+_direction >= 0){	
		$('a.sipkavpravo').not('.disabled').addClass('disabled');
	}else{
		$('a.sipkavpravo').filter('.disabled').removeClass('disabled');
	}
	

	if(currentOffset+_direction-showTabs+tabs <=0){
		$('a.sipkavlevo').not('.disabled').addClass('disabled');
	}else{
		$('a.sipkavlevo.disabled').removeClass('disabled');
	}

	
	newOffset = (currentOffset+_direction)*163;
	currentOffset +=_direction;
	return newOffset;
}