$(document).ready(function(){
    $('#videoContainer').videoNav();
    $('.videoSet a').fancybox({'type':'swf','width':500,'height':321});
});

// 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);
});


// 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);
    });
};

jQuery.fn.extend({
    videoNav: function(){
	var dst = this;							// save a reference to the container
	var currentPage = 1;						// what page of videos we are looking at;
	var totalPages = $('.videoSet',$(dst)).length;			// how many pages of thumbs have we got

	var changeSection = function() {
	    $('.videoSet:visible',$(dst)).fadeOut('slow',function(){	// fade out visible section
		$('#videos'+currentPage).fadeIn('slow');		// fade in the new section

		if(currentPage < totalPages) {				// re enable the next button if not on last page
		    $('#scrollSection_nav_nxt').removeClass('disabled');
		}

		if(currentPage > 1) {					// re enable the previous button if not on page 1
		    $('#scrollSection_nav_prv').removeClass('disabled');
		}
	    });
	}

	$('#scrollSection_nav_nxt').bind('click',function(){		// click event handler
	    if(!$(this).hasClass('disabled')) {				// only if it is not disabled
		$('#videoNav a').addClass('disabled');			// disable clicks while it's processed
		currentPage++;
		changeSection();
	    }
	});

	$('#scrollSection_nav_prv').bind('click',function(){		// click event handler
	    if(!$(this).hasClass('disabled')) {				// only if it is not disabled
		$('#videoNav a').addClass('disabled');			// disable clicks while it's processed
		currentPage--;
		changeSection();
	    }
	});
    }
});
