$(document).ready(function(){
	$('button').filter('.button').each(function(){
		var text = $(this).text();

		var onClick = $(this).attr('onclick');
		var cls = $(this).attr('class');
		var id = $(this).attr('id');
		
		var buttonIdentifier = Math.floor(Math.random()*10001);

		$(this).replaceWith('<div class="button" button-id="'+buttonIdentifier+'"><div class="leftCorner"></div><div class="text">'+text+'</div><div class="rightCorner"></div></div>');

		$('div.button[button-id="'+buttonIdentifier+'"]').attr('class', cls);
		$('div.button[button-id="'+buttonIdentifier+'"]').attr('id', id);

		$('div.button').filter('[button-id="'+buttonIdentifier+'"]').click(function(){
			onClick();
		});
	});

	$('select#languageDropdown').change(function(){
		if($(this).val() != ''){
			window.location = '/'+$(this).val();
		}
	});

	var nextSlide = setInterval('nextSlide()', 5000);

	// Bind sumbit event
	$(".searchForm").bind("submit", function(event) {
		
		// Get search request
		var search = $(this).find('[name="zoekterm"]').val();
		var formAction = $(this).attr("action");

		// Redirect user
		document.location = formAction+"/"+search;
			return false;
	});
});

function nextSlide(){
		if($('#projectPhoto div.project').length == 0){
			clearInterval(nextSlide);
		}

		if($('#projectPhoto div.project').length == 1){
			// 1 foto, niet rotaten
			return;
		}

		var active = $('#projectPhoto .active');
		var next = $('#projectPhoto .nextactive');

		next.show();
		active.fadeOut('slow', function(){
			active.removeClass('active nextactive');
			active.addClass('inactive');

			next.removeClass('nextactive inactive');
			next.addClass('active');

			var nextNext = next.next('.inactive');

			if(nextNext.length == 0){
				var nextNext = $('#projectPhoto div.inactive').first();
			}
			
			nextNext.removeClass('inactive active');
			nextNext.addClass('nextactive');
			
			if($('.projectPhotoText').length > 0){
				$('.projectPhotoText').hide();
				$('#projectPhotoText'+next.attr('id')).show();
			}
		});
}

