$(document).ready(function() {
	
	setInterval(changeBg, 6000);
	
	bgs = new Array('slide_1.jpg', 'slide_2.jpg', 'slide_3.jpg', 'slide_4.jpg', 'slide_5.jpg', 'slide_6.jpg');
	
	var i = 1;
	
	for ( n=1; n<bgs.length; n++ ) {
		var div = "<div id='slide"+n+"'></div>";
		var html = $('#slide_preload').html()+div;
		$('#slide_preload').html(html);
		$('#slide'+n).css('background-image', 'url("images/'+bgs[n]+'")');
	}
	
	function changeBg()  {
		
		var url = 'url("images/'+bgs[i]+'")';
		
		$('#slideshow').animate({
			opacity: 0
		}, 1000);
		
		setTimeout(bg, 1000);
		
		function bg() {
			
			$('#slideshow').css('background-image', url).animate({
				opacity: 1
			}, 1000);
			
		}
		
		if ( i >= (bgs.length-1) ) {
			i = 0;
		} else {
			i = i + 1;
		}
	}
	
	// Check if the error div exists in the contact page
	if ( $("p.error").length > 0 ) {
		
		$("p.error").animate({ opacity:"0" }, { duration:1 }).animate({ opacity:"0" }, { duration:399 }).animate({ opacity:"1" }, { duration:1000 });
		setTimeout('$("p.error").show()', 400);
		
	}
	
});