/*
	Share Our Strength scripts (utilizing jQuery 1.2.6)
	Spring 2009 homepage
	Developed by Saforian
*/


// Rotating banner settings
var banner_speed = 9000;				// 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


/* --- Initialize page --- */
$j(document).ready(function(){

	// CSS3 support
	$j(".columns>.col:last-child").addClass("last-child");
	$j("#tabbed-news-community-box .inside p:first-child").addClass("first");

	// Clear input boxes
	initSwapTextBoxes();

	// Homepage: Top banner switching
	initHomepageBanners();

	// Homepage: News and Community tabbed box
	$j("#tabbed-news-community-box #tabs a").click(function(){

		// Hide old tab
		$j("#tabbed-news-community-box .inside").hide();
		$j("#tabbed-news-community-box #tabs a").removeClass("on");

		// Show new tab
		$j(this.hash).show();
		$j(this).addClass("on");

		return false;
	});
});



// Clears the value of prepopulated form fields as long as they have a class "swaptextbox"
function initSwapTextBoxes(){
	// Store the default value for each box
	$j('input[type=text][value].swaptextbox').each(function() {
		swapTextBoxes[$j(this).attr('id')] = $j(this).attr('value');
	});

	// Add focus and blur events to set or clear the value
	$j('input[type=text][value].swaptextbox').bind('focus', function() {
		if($j(this).val() == swapTextBoxes[$j(this).attr('id')]) {
			$j(this).val('');
		}
	});

	$j('input[type=text][value].swaptextbox').bind('blur', function() {
		if($j(this).val() == '') {
			$j(this).val(swapTextBoxes[$j(this).attr('id')]);
		}
	});
}



// 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();
}


