var currentSection = 1;							// what section are we currently looking at

$(document).ready(function(){
    $('#scrollBox_holder_theMaggies').scrollJudgesSection();
    $('#footer_judges').judgesBottomNav();
});

// use window.load rather than document.ready so that we ensure the images are loaded before the animation starts
$(window).load(function(){
    // start the animation after a short delay
    setTimeout("animate()",1000);

    $('#judges_photo').animate({ "top": "-430px" }, 1500, "swing");
	
});


// animate the logo sign
function animate() {
    var spd = 1500;

    // show the coming soon logo
    $('#comingSoon_coverVoting').fadeIn('slow');

    // show the main logo and scroll
    $('#theMaggies_logo').animate({ "top": "0" }, spd, "swing");
    $('#scroll').animate({ "top": "90px" }, spd, "swing", function(){
	// add some glitter to the scroll
	$('#stage-front').addSparkleGroup(501,116,4,220,50);

	// move the first section across
	$('#pollResults_holder').animate({ "left": "0" }, spd, "swing",function(){
	    // show the nav
	    $('#pollResults_nav').fadeIn('slow');
	});
    });
};

jQuery.fn.extend({
    // horizontally scrollable sections
    scrollJudgesSection: function() {

	var dst = this;								// save a reference to the target
	var totalSections = parseInt($(this).width() / 1000);			// how many sections are there (sections always being 1000 wide)
	
	var spd = 1500;								// how long we take to animate between sections (in ms)

	// create the next handler

	var handler_nxt = function(){
	    if(!$('#scrollSection_nav_nxt').hasClass('disabled')) {
		$('#scrollSection_nav_nxt').addClass('disabled');		// disable while scrolling

		currentSection++;						// increment the current section

		$('#judges_photo img').attr('src','/images/judges_0.png');	// blank the photo
		$('.judges_footerElement').removeClass('selected');		// unselect all footer photos

		// enable the previous button
		$('#scrollSection_nav_prv').removeClass('disabled');

		// animate the scrolling section to the left
		$(dst).animate({ "left": "-=1000px" }, spd, "swing", function() {
		    $('#judges_photo img').attr('src','/images/judges_'+currentSection+'.png');	// update the photo
		    $('#footer_judges_'+currentSection).addClass('selected');	// update the footer

		    // if we are not on the last section, re-enable the next button
		    if(currentSection <= totalSections) {
			$('#scrollSection_nav_nxt').removeClass('disabled');
		    }
		});
	    }

	    return false;							// don't use the anchor as a link
	}

	var handler_prv = function(){
	    if(!$('#scrollSection_nav_prv').hasClass('disabled')) {
		$('#scrollSection_nav_prv').addClass('disabled');		// disable while scrolling

		currentSection--;						// decrement the current section

		$('#judges_photo img').attr('src','/images/judges_0.png');	// blank the photo
		$('.judges_footerElement').removeClass('selected');		// unselect all footer photos

		// enable the next button
		$('#scrollSection_nav_nxt').removeClass('disabled');

		// animate the scrolling section to the left
		$(dst).animate({ "left": "+=1000px" }, spd, "swing", function() {
		    $('#judges_photo img').attr('src','/images/judges_'+currentSection+'.png');	// update the photo
		    $('#footer_judges_'+currentSection).addClass('selected');	// update the footer

		    // if we are not on the first section, re-enable the previous button
		    if(currentSection > 1) {
			$('#scrollSection_nav_prv').removeClass('disabled');
		    }
		});
	    }

	    return false;							// don't use the anchor as a link
	}

	$('#scrollSection_nav_prv').bind('click',handler_prv);
	$('#scrollSection_nav_nxt').bind('click',handler_nxt);
    },

    judgesBottomNav:function() {
	
	$('a',$(this)).bind('click',function(){
	    if(!$(this).parent().hasClass('selected')) {
		$('#scrollSection_nav_nxt').addClass('disabled');		// disable buttons while scrolling
		$('#scrollSection_nav_prv').addClass('disabled');

		$('#judges_photo img').attr('src','/images/judges_0.png');	// blank the photo
		$('.judges_footerElement').removeClass('selected');		// unselect all footer photos

		var id = parseInt($(this).parent().attr('id').substr(-1));	// target id to scroll to
		var target = (id - 1) * -1000;					// x position to scroll to
		currentSection = id;						// what section are we currently looking at
		
		// scroll to the right person
		$('#scrollBox_holder_theMaggies').animate({ "left": target + 'px'}, 1500, "swing", function(){

		    $('#judges_photo img').attr('src','/images/judges_'+id+'.png');	// update the photo
		    $('#footer_judges_'+id).addClass('selected');		// update the footer

		    if(id != 1) {							// not the first bio, enable the previous button
			$('#scrollSection_nav_prv').removeClass('disabled');
		    }

		    if(id != 9) {							// not the last bio, enable the next button
			$('#scrollSection_nav_nxt').removeClass('disabled');
		    }
		});
	    }

	   return false;
	});
    }
});