/* --- Rotating partner logos --- */

var rotation_speed = 4400;		// Milliseconds between auto-rotations

var rotation_timer = null;		// Rotation timer ID
var rotation_manuallystopped = false;	// Is timer manually stopped
var rotation_animating = false;	// Flag if rotating (to avoid overlapping)

// Partner logo auto-rotation
function startrotation(){
	$("#rotater-pause").removeClass("active"); // Unset paused image

	rotation_timer = window.setInterval('rotatepartners()', rotation_speed);
}
function stoprotation(){
	$("#rotater-pause").addClass("active"); // Set paused image

	window.clearInterval(rotation_timer);
	rotation_timer = null;
}
function rotatepartners() {
	$("#rotater-next").click();
}

// Page initilization
$(document).ready(function(){

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

	// Firefox opacity fix
	if ($.browser.mozilla && parseInt(navigator.appVersion) == 5) {
		$("body").css("opacity", "0.9999");
	}

	// Add class for scripted interface
	$("#allpartners").addClass("scripted");

	// Pick randomize first item
	var numitems = $("#allpartners>.block").size();
	var randomstart = Math.floor(Math.random()*numitems);
	$("#allpartners>.block:not(:eq(" + randomstart + "))").hide();

	// Insert rotater triggers
	$("#allpartners").append('<div style="visibility:hidden;" id="rotater"><a href="#" id="rotater-back">Back</a><a href="#" id="rotater-pause">Pause</a><a href="#" id="rotater-next">Next</a></div>');

	// Start auto-rotate
	startrotation();

	// Rotator actions
	$("#rotater-back").click(function(){

		// Get current and next item
		var $current = $("#allpartners>.block:visible");
		var $next = $current.prev(".block");

		// Loop back to first
		if ($next.size() == 0) {
			$next = $("#allpartners>.block:last");
		}

		// Fade to next item
		if (!rotation_animating) {
			rotation_animating = true;
			$current.fadeOut(300, function(){
				$next.fadeIn(400, function(){
					rotation_animating = false;

					// IE7 fading bug
					if ($.browser.msie) { this.style.removeAttribute("filter"); }
				});
			});
		}

		return false;
	});

	$("#rotater-next").click(function(){

		// Get current and next item
		var $current = $("#allpartners>.block:visible");
		var $next = $current.next(".block");

		// Loop back to first
		if ($next.size() == 0) {
			$next = $("#allpartners>.block:first");
		}

		// Fade to next item
		if (!rotation_animating) {
			rotation_animating = true;
			$current.fadeOut(200, function(){
				$next.fadeIn(400, function(){
					rotation_animating = false;
				});
			});
		}

		return false;
	});

	$("#rotater-pause").click(function(){

		if (rotation_timer == null) {

			// Start timer
			rotation_manuallystopped = false;
			startrotation();

		} else {

			// Stop timer
			rotation_manuallystopped = true;
			stoprotation();

		}

		return false;
	});

	// Also pause on item hover
	$("#allpartners>.block").hover(function(){

		stoprotation();

	}, function(){
		// Only restart if not manually stopped
		if (!rotation_manuallystopped) {
			startrotation();
		}
	});

});
