$(document).ready(function(){
	var speed = 1000;
	var timeout = 5000;
	var animate = true;
	var currentSlide = 1;
	var slides = $('#building-nav ul li').size();
	
	//hide nav select states
	$('#building-nav .thumbselect').hide();
	//move navigations href to prevent screen jumping
	$('#building-nav a').each(function(){
		$(this).addClass($(this).attr('href'));
		$(this).attr('href', 'javascript:void(0);');
	});
	
	// Show the first Slide
	showSlide(currentSlide);

	//Handle Direct Navigation
	$('#building-nav ul li a').click(function(){
		var parts = $(this).parent().attr('id').split('-');
		var slideNum = parts[1];
		animate = false;
		showSlide(slideNum, true);
	});
	
	function nextSlide(){
		if(animate) currentSlide++;
		if(currentSlide > slides) currentSlide = 1;
		showSlide(currentSlide);
	}
	
	function showSlide(num, click){
		currentSlide = num;
		var sel = '#slide-' + num + ' a';
		if(animate || click){
			// 1. Show Picture
			$('.building-image').fadeOut(speed);
			$($(sel).attr('class')).fadeIn(speed);
			// 2. Move Star
			moveStar($(sel).attr('rel'));
			// 3. Navigation Select State
			$('img.thumbselect').fadeOut(speed);
			$(sel + ' img.thumbselect').fadeIn(speed, function(){
				//animation loop
				setTimeout(function(){
					nextSlide();
				}, timeout);
			});
		}
		else {
			animate = !animate;
		}
	}
	
	function moveStar(coords){
		var coords = coords.split('-');
		//adjusting for the size of the star image
		var xpos = (coords[0] - 76) + 'px';
		var ypos = (coords[1] - 73) + 'px';
		$('#buildings-star').animate({left: xpos, top: ypos }, speed);
	}	
});