var first_time = true;

$(document).ready(function() {
	
	get_random_bg();
	window.setInterval(get_random_bg, 5000);
	
});

function get_random_bg()
{
	// unique random number to workaround IE cache issue
	var r = new Date().getTime();
	
	$.get('includes/display_pano.php?r=' + r, function(data){
		
		//alert(data);
		
		var info = eval("("+data+")");
		var show = "";
		
		// check to see which pano is going to show up next
		if(($("#display_image_two").css("display") == "none") || first_time)
			show = 'two';
		else
			show = 'one';
		
		// setup hidden pic
		$("#display_image_"+show).attr('src', info[0]);
		
		// wait for the image to load into the DOM (no red X in IE)
		$("#display_image_"+show).load(function(){
												
			// setup blurb
			$("#display_blurb_"+show).html(info[2]);
			
			// setup menu section
			$("#menu_"+show).css('background', 'url('+info[1]+') top left no-repeat');
			
			// flip the two panos
			if(show == 'two')
			{	
				// PANO TWO
				$("#display_image_two").fadeIn("slow");
				$("#display_blurb_two").fadeIn("slow");
				$("#menu_two").fadeIn("slow", function(){
													   
					// if first time around, show the first pano so the next loops work
					if(first_time)
					{
						$("#display_image_one").show();
						first_time = false;
					}

				});
			}
			else
			{
				// PANO ONE
				$("#display_image_two").fadeOut("slow");
				$("#display_blurb_two").fadeOut("slow");
				$("#menu_two").fadeOut("slow");
			}
		});
	});
}
