// JavaScript Document

function setActive( that ) {
	$('#thumb-scroller a img.active').removeClass('active');
	$(that).find('img').addClass('active');
}

function setPreview( that ) {
	$('#preview-container img').attr('src', $(that).attr('href'));
	$('#preview-container .title').text( $(that).find('img').attr('alt') );
}

$(document).ready( function() {
	var currImg = 0;
	var maxImg = $('#thumb-scroller img').length;
	var iv;
	
	$('#thumb-scroller').css('width', 143*maxImg);
	
	$('#thumb-scroller a:first img').addClass('active');
	
	$('#preview .prev a').click( function() {
		var currElement;
		if(currImg>0) { 
			currImg--; 
			currElement = $('#thumb-scroller a img.active').parent().prev();
		} else { 
			currImg = maxImg-1; 
			currElement = $('#thumb-scroller').children()[maxImg-1];
		}
		setActive( currElement );
		setPreview( currElement );

		return false;
	});

	$('#preview .next a').click( function() {
		var currElement;
		if(currImg<(maxImg-1)) { 
			currImg++; 
			currElement = $('#thumb-scroller a img.active').parent().next();
		} else { 
			currImg = 0; 
			currElement = $('#thumb-scroller').children()[0];
		}
		setActive( currElement );
		setPreview( currElement );

		return false;
	});
	
	$('#thumbs .prev a').click( function() {
		if (parseInt($('#thumb-scroller').css('left'))<0) {
			$('#thumb-scroller').animate({left: '+=142px'}, { duration: "slow" });
		} else {
			$('#thumb-scroller').animate({left: '+=70px'}, { duration: "slow" })
								.animate({left: '-=70px'}, { duration: "slow" });
		}
		return false;
	});

	$('#thumbs .next a').click( function() {
		if (parseInt($('#thumb-scroller').css('left'))>-142*($('#thumb-scroller').find('a').length-4)) {
			$('#thumb-scroller').animate({left: '-=142px'}, { duration: "slow" });
		} else {
			$('#thumb-scroller').animate({left: '-=70px'}, { duration: "slow" })
								.animate({left: '+=70px'}, { duration: "slow" });
		}
		return false;
	});
	
	$('#thumb-scroller a').click( function() {
		setActive(this);
		setPreview(this);
		
		// hányadik az aktív elem?
		$('#thumb-scroller img').each( function(i) { if ($(this).hasClass('active')) currImg = i; });
		
		return false;
	});
	
	$('#vetites').toggle(
		function() { 
			$(this).addClass('active');
			$('#preview .next a').click();
			iv = setInterval( function() { $('#preview .next a').click() }, 4000 );
		},
		function() { 
			$(this).removeClass('active'); 
			clearInterval(iv);
		}
	);
});
