/**
 * Functions for Song Contest 2009 web site
 *
 * @author Jesse Barros <webmaster@ksbe.edu>
 * @copyright (C) 2009 Kamehameha Schools. All rights reserved.
 */

function SponsorFader() {
	// Default speeds to use, in milliseconds.
	this.displayInterval = 5000;
	this.fadeTime = 750;

	// Set an object reference.
	var that = this;
	
	// jQuery object representing the fading sponsor slot.
	var slot = $('#sponsor3');
	
	// jQuery object representing the set of sponsors.
	var sponsors = slot.find('.sponsor');
	
	// Which index is currently being displayed.
	var index = 0;
	
	/**
	 * Initializes the sponsor fader.
	 */
	var initialize = function () {
		// Hide all sponsor logos except for the first one.
		sponsors.slice(1).hide();
		
		// Set the initial timer.
		window.setTimeout(that.nextSponsor, that.displayInterval);
	};
	
	/**
	 * Increments the sponsor fader, fading out the current sponsor and thereby
	 * displaying the next sponsor in the set.  If the last sponsor is currently
	 * visible, this will cause the first sponsor to appear.
	 */
	this.nextSponsor = function () {
		// How many sponsors are available for display?
		var totalSponsors = sponsors.length;
		
		// Which sponsor is next?  (It's 0 if this is the last sponsor.)
		var nextIndex;
		if (index === totalSponsors - 1) {
			nextIndex = 0;
		} else {
			nextIndex = index + 1;
		}
		
		// Get a reference to the current and next sponsor.
		var current = sponsors.eq(index);
		var next = sponsors.eq(nextIndex);
		
		// Set the z-index for the next sponsor (which is currently hidden)
		// higher than the current sponsor.
		next.css('z-index', '1');
		
		// Fade in the next sponsor.
		next.fadeIn(that.fadeTime, function () {
			// next is now the current sponsor, while current is the previous.
			
			// Reset the z-index.
			next.css('z-index', '0');
			
			// Hide the sponsor that was just hidden.
			current.hide();
			
			// Set the index of the current sponsor.
			index = nextIndex;
			
			// Set the next timeout.
			window.setTimeout(that.nextSponsor, that.displayInterval);
		});
	};
	
	// Run initialization routines.
	initialize();
	
}

/**
 * Updates the winners list to display the appropriate winner panel.
 *
 * @param id The ID of the panel and choice to display.
 */
function updateWinnerList(id) {
	// jQuery list of chooser links.
	var chooser_links = $('#winner-list .chooser li');
	
	// Assign the current and next winner panels.
	var current = '#' + chooser_links.filter('.active').attr('id') + '-panel';
	var next = '#' + id + '-panel';

	// Remove the active highlight from all menu items.
	chooser_links.not('#' + id).removeClass('active');
	
	// Reapply the active highlight to the desired menu item.
	$('#' + id).addClass('active');
	
	$(current).fadeOut('medium', function() {
		$(next).fadeIn('medium');
	});
}

$(function () {

	// Begin fading sponsors.
	var sf = new SponsorFader();
	
	// Initialize the winnner list.
	$('#winner-list .chooser li').click(function() {
		updateWinnerList($(this).attr('id'));
	});

});

