var activeThumb;
var secCounter;
var numberOfTeasers;

$(document).ready(function() {
	
	secCounter = 0;
	activeThumb = 0;
	numberOfTeasers = 0;
	
	$(document).find('.news-teaser-element').each(function(i) {
		
		title = '';
		img = '';
		video = '';
		
		$(this).find('h1').each(function() {
			title = $(this).html();
		});
		
		$(this).find('.image-link').each(function() {
			img = $(this).html();
		});
		
		$(this).find('.video-id').each(function() {
			video = 'http://img.youtube.com/vi/' + $(this).html() + '/2.jpg';
			video = video.replace(/:/g, '[dots]');
		})
		
		htmlCode = '<div class="thumb">';
		if (img != ''){
			htmlCode = htmlCode + '<img src="thumbr/t.php?src=../' + img + '&w=96&h=54&c=1" />';
		}
		if (video != ''){
			htmlCode = htmlCode + '<img src="thumbr/t.php?src=' + video + '&w=96&h=54&c=1" />';
		}
		htmlCode = htmlCode + '<p>' + title + '</p>';
		htmlCode = htmlCode + '</div>';
		
		$(document).find('.news-thumb-list').each(function(i) {
			$(this).html($(this).html() + htmlCode);
		});
		
		numberOfTeasers++;
		
	});
	
	$(document).find('.thumb', '.news-thumb-list').each(function(i) {
		
		
		
		$(this).click(function () {
			
			changeTeaser(i);
			
		});
		
		
		$(this).mouseover(function (){
			if (i != activeThumb){
				$(this).addClass('active-thumb');
			}
		});
			
		$(this).mouseout(function (){
			if (i != activeThumb){
				$(this).removeClass('active-thumb');
			}
		});
		
	});
	
	
	changeTeaser(activeThumb);
	
	//setInterval(function(){ everySecond(); }, 1000);
	
});

function everySecond(){
	
	if (secCounter < 5){
		secCounter = secCounter + 1;
	}else{
	
		if (activeThumb < numberOfTeasers-1){
			
			changeTeaser(activeThumb+1);
			
		}else{
			
			changeTeaser(0);
		}
		
	}
	
}
	
function changeTeaser(teaserNr){
	
	secCounter = 0;
	
	activeThumb = teaserNr;
	
	$(document).find('.thumb').each(function(i) {
		
		if (i == teaserNr){
			$(this).addClass('active-thumb');
		}else{
			$(this).removeClass('active-thumb');
		}
		
	});
	
	$(document).find('.news-teaser-element').each(function(i) {
		
		if (i == teaserNr){
			$(this).fadeIn(300);
		}else{
			$(this).fadeOut(300);
		}
		
	});
	
}
