
var currentEvent;
var numberOfEvents;

$(document).ready(function() {
	
	currentEvent = 0;
	numberOfEvents = 0;
	
	$(document).find('.events-home').each(function(i) {
		$(this).find('.image_container').each(function(i) {
			
			if (i == 0){
				$(this).animate({
					left: '48px'
				}, 0);
			}
			
			numberOfEvents++;
		});
	});
	
	$(document).find('.next-button').each(function(i) {
		$(this).click(function() {
			nextEvent();
		});
	});
	
	$(document).find('.previous-button').each(function(i) {
		$(this).click(function() {
			previousEvent();
		});
	});
	
	
	prefillNewsletterEmailField();
	
});

function nextEvent(){
	
	var nextEvent = currentEvent +1;
	
	if (nextEvent > numberOfEvents-1){
		nextEvent = 0;
	}
	
	
	$(document).find('.events-home').each(function(i) {
		$(this).find('.image_container').each(function(i) {
		
			if (nextEvent == i){
				
				$(this).animate({
					left: '400px'
				}, 0, function() {
					$(this).animate({
						left: '48px'
					}, 500, "swing");
				});
				
			}else if (currentEvent == i){
				$(this).animate({
					left: '-300px'
				}, 500, "swing");
			}
		});
	});
	
	currentEvent = nextEvent;
	
}


function previousEvent(){
	
	var nextEvent = currentEvent -1;
	
	if (nextEvent < 0){
		nextEvent = numberOfEvents-1;
	}
	
	$(document).find('.events-home').each(function(i) {
		$(this).find('.image_container').each(function(i) {
		
			if (nextEvent == i){
				
				$(this).animate({
					left: '-300px'
				}, 0, function() {
					$(this).animate({
						left: '48px'
					}, 500, "swing");
				});
				
			}else if (currentEvent == i){
				$(this).animate({
					left: '400px'
				}, 500, "swing");
			}
		});
	});
	
	currentEvent = nextEvent;
}

function prefillNewsletterEmailField(){
    $('#ctrl_1').val( $.trim( get_url_param('email')).replace('%40', '@'));
    
}

function get_url_param( p )
{
  p = p.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+p+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

