//Javascript

var currentPanel= 0
var autoPlay = true
var timePassed = 0
var timeToChange = 3

function autoAdvance(){
	if(window.timePassed==window.timeToChange){
		window.timePassed=0;
		if(window.currentPanel==window.totalPanels){
			currentPanel=0;
			}	
		if(autoPlay==true){
			$('.marquee_nav a.marquee_nav_item:nth-child('+(window.currentPanel+1)+')').trigger('click');
			}
		}else{
		window.timePassed+=1;
		}
		/*debug*/$('.timePassed').html('timePassed='+window.autoPlay);
		/*debug*/$('.autoPlay').html('autoPlay='+window.autoPlay);
	}


$(document).ready(function(){
	
	/*debug*/$('.autoPlay').html('autoPlay='+window.autoPlay);
	/*debug*/$('.timePassed').html('timePassed='+window.timePassed);
	/*debug*/$('.timeToChange').html('timeToChange='+window.timeToChange);
	/*debug*/$('.currentPanel').html('currentPanel='+window.currentPanel);
	
	setInterval(autoAdvance,1000)
	$('.marquee_container').hover(
	function(){
		window.autoPlay=false;
		$(this).removeClass('autoplay');
		},
	function(){
		window.autoPlay=true;
		window.timePassed=0;
		$(this).addClass('autoplay');
		}
	);
	
	// Generate Navigation links based on no. of photos
	$('.marquee_panels .marquee_panel').each(function(index){
		$('.marquee_nav').append('<a class="marquee_nav_item" ></a>');
		window.totalPanels=index+1;
		/*debug*/$('.totalPanels').html('totalPanels='+window.totalPanels);
		
	});
	
	// Generate Photo Lineup - no. of photos time width is total width
	$('img.marquee_panel_photo').each(function(index){
		var photoWidth = $('.marquee_container').width();
		var photoPosition = index * photoWidth;
		$('.marquee_photos').append('<img class="marquee_photo" style="left: '+photoPosition+'" src="'+$(this).attr('src')+'" alt="'+$(this).attr('alt')+'" width="700" height="530" />');
		$('.marquee_photos').css('width', photoPosition+photoWidth);
	});

	// Set up Navigation Links
	$('.marquee_nav a.marquee_nav_item').click(function(){
		
		// Set the navigation state
		$('.marquee_nav a.marquee_nav_item').removeClass('selected');
		$(this).addClass('selected');
		
		var navClicked = $(this).index();
		var marqueeWidth = $('.marquee_container').width();
		var distanceToMove = marqueeWidth*(-1);
		var newPhotoPosition = navClicked*distanceToMove + 'px';
		var newCaption = $('.marquee_panel_caption').get(navClicked);
		window.currentPanel= navClicked+1;
		/*debug*/$('.currentPanel').html('currentPanel='+window.currentPanel);
		
		// Animate the photos and caption
		$('.marquee_photos').animate({left: newPhotoPosition}, 800);
		$('.marquee_caption').animate({top: '473px'}, 500, function(){
			var newHTML = $(newCaption).html();
			$('.marquee_caption_content').html(newHTML);
			setCaption();
		});
	});
	
	// Preload all images, then initialize marquee
	$('.marquee_panels img').imgpreload(function(){
		initializeMarquee();
	});

});

function initializeMarquee(){
	$('.marquee_caption_content').html(
		$('.marquee_panels .marquee_panel:first .marquee_panel_caption').html()
	);
	$('.marquee_nav a.marquee_nav_item:first').addClass('selected');
	$('.marquee_photos').fadeIn(1500);
	setCaption();
}
/*container height-new caption height - padding is new Caption height*/
/*no movement line 4 changed top: newCaptionTop to 1*/
function setCaption(){
	var captionHeight = $('.marquee_caption').height();
	var marqueeHeight = $('.marquee_container').height();
	var newCaptionHeight = 473;
	$('.marquee_caption').delay(100).animate({top: newCaptionHeight}, 500);
}



