/*
	Share Our Strength scripts (utilizing jQuery 1.2.6)
	Spring 2009 homepage
	Developed by Saforian
*/


// Rotating banner settings
var banner_speed = 6000;				// Milliseconds between auto-rotations


// Other global variables
var banner_timer = null;				// Rotation timer ID
var banner_animating = false;			// Flag if rotating (to avoid overlapping)
var swapTextBoxes = [];					// Store original values for cleared text fields

var $j = jQuery.noConflict();

/* --- Initialize page --- */
$j(document).ready(function(){

	// Homepage: Top banner switching
	initHomepageBanners();

});
// Homepage: Top banner switching
function initHomepageBanners(){

	if (!document.getElementById("cyclebox")) { return; }


	// Inject 1-2-3 cycle controls list
	var cyclecode = '<ul id="banner-controls">';
	$j("#cyclebox > .cyclediv").each(function(i){
		cyclecode += '<li><a href="#' + $j(this).attr("id") + '">' + (i+1) + '</a></li>';
	});
	cyclecode += '</ul>';

	$j("#cyclebox").append(cyclecode);

	$j("#banner-controls a:first").addClass("active");
	$j("#banner-controls li:last").addClass("last");


	// Click event
	$j("#banner-controls a").click(function(){

		// Skip if number already showing
		if ($j(this).hasClass("active")) { return false; }

		// Hide visible banner
		$j(".cyclediv:visible").fadeOut("normal");
		$j("#banner-controls a.active").removeClass("active");

		// Show new banner
		$j(this.hash).fadeIn("slow");
		$j(this).addClass("active");

		return false;
	});


	// Pause auto-rotating on controls or link hover
	$j(".cyclediv a, #banner-controls").hover(function(){
		bannerStopRotation();
	}, function(){
		bannerStartRotation();
	});


	// Start rotating on page load
	bannerStartRotation();
}


// Start rotation (begin timer)
function bannerStartRotation(){
	banner_timer = window.setInterval('bannerRotate()', banner_speed);
}

// Stop rotation (cancel timer)
function bannerStopRotation(){
	window.clearInterval(banner_timer);
	banner_timer = null;
}

// Rotation function
function bannerRotate() {

	// Find next banner
	var $jnext = $j("#banner-controls a.active").parent("li").next().children("a");

	// Or loop to #1 if at end
	if ($jnext.size() == 0) {
		$jnext = $j("#banner-controls > li:first > a");
	}

	// Click the number link
	$jnext.click();
}
