// JavaScript Document
var timeout;
var started = false;

function init() {
	trace("init");
	$('#nieuwstickerimages img').hide();
	$('#nieuwstickercontent div.active').show();
	
	$('#nieuwstickercontent div').hover(function() {
	    animate($('#nieuwstickercontent div.active'), $(this));
		pauseTicker();
	}, function() {
		resumeTicker();
	});
	
	$('#nieuwstickercontent div').each (function() {
		$(this).hover(function() {
			$('#nieuwstickercontent div').removeClass('active');
			$(this).addClass('active');
		});
	});
	tick();
	started = true;
}

function pauseTicker() {
	clearInterval(timeout);
}

function resumeTicker() {
	if (!timeout || started) {
		timeout = setInterval(tick,4000);	
	}
}

function tick() {
	trace("tick");
	var next;
	var current = $('#nieuwstickercontent div.active');
	if (current.next().length != 0) {
		next = current.next().addClass('active');
		current.removeClass('active');
	} else {
		next = $('#nieuwstickercontent div:first').addClass('active');
		current.removeClass('active');
	}
	
	animate(current,next);
}

function animate(current,next) {
	current = $('#'+current.attr('img'));	
	next = $('#'+next.attr('img'));
	
	current.css('z-index',100);
	next.css('z-index',200);
	
	current.fadeOut(500).animate({marginTop: '100'}, { queue:false, duration:500 });
	next.fadeIn(500).animate({marginTop: '0'}, { queue:false, duration:500 });	
	
	current.removeClass('active');
	next.addClass('active');
}

function trace(s) {
//	console.log(s);
}

$(document).ready(init);