

var items;
var currentTop = 0;
var blockAnim = false;

var active = 0;

// newsy
$(document).ready(function() {
	var items = $('#index-news').find('.index-news-item').length;
	var itemWidth = $('.index-news-item').width() + parseInt($('.index-news-item').css('margin-left'));
	var newsWidth = items * itemWidth;

	$('#index-news ul').css('width', newsWidth);
	
	$('#prev-news').click(function() {
		if(active > 0 ) {
			active = active - 1;
			$('#index-news ul').animate({ 'left' : -active * itemWidth });
		}
	});
	
	$('#next-news').click(function() {
		if(active < items - 3 ) {
			active = active + 1;
			$('#index-news ul').animate({ 'left' : -active * itemWidth });
		}
	});
});

// funkcja zmieniająca obrazek do innego
function slideTo(num) {
	if (num != currentTop && !blockAnim) {
		blockAnim = true;
		
		$('#slider-link-'+num).addClass('slider-link-active');
		$('#slider-link-'+currentTop).removeClass('slider-link-active');
		
		var onTop = $('#slider-'+currentTop);
		var newTop = $('#slider-'+num);
		
		currentTop = num;
		newTop.css('z-index',10);
		
		onTop.fadeOut('slow', function(){
			newTop.css('z-index', 20);
			onTop.css('z-index', 5).show();
			
			blockAnim = false;
		});
		return true;
	}
	return false;
}

// przygotowanie pętli
var sliderInterval;

function prepareNewLoop() {
	if (sliderInterval) {
		clearInterval(sliderInterval);
	}
	
	sliderInterval = setInterval(function(){
			slideTo((currentTop+1) % items);
		}, 5000);
}

// uruchomienie całości
$(document).ready(function() {
	
	items = $('#slider div').length;
	$('#slider-link-0').addClass('slider-link-active');
	
	// uruchomienie zmiany czasowej
	prepareNewLoop();
	
	// dodanie akcji dla linków
	for(var i=0; i<items; i++) {
		with ({num: i}) {
			$('#slider-link-'+num).click(function(){
				if (slideTo(num)) {
					// przesunięcie pętli, jeśli wykonano
					clearInterval(sliderInterval);
					setTimeout(function() {
						prepareNewLoop();
					}, 2000);
				}
			});
		}
	}
});

// exams diplomas
$(document).ready(function() {
	$('#exams').css('z-index', '2');
	$('#diplos').css('z-index', '1');
	
	$('#exams h3').click(function() {
		$('#exams').css('z-index', '2');
		$('#diplos').css('z-index', '1');
	});
	
	$('#diplos h3').click(function() {
		$('#diplos').css('z-index', '2');
		$('#exams').css('z-index', '1');
	});
});

