var $j = jQuery.noConflict();
// Idexes for visible and buffer positions
var nextBufferIdx;
var nextIdx;
var testimonialIdx;
var prevIdx;
var prevBufferIdx;
var testiArr = new Array();

function testiListCtl(action)
{
	// Disable buttons while running
	// Enable buttons
	$j("#next_testimonial").unbind('click');
	$j("#prev_testimonial").unbind('click');

	if(action=="prev")
	{
		// Animations
		$j("#testimonial"+nextBufferIdx).show("normal");
		$j("#testimonial"+nextIdx).fadeTo("normal",1);
		$j("#testimonial"+testimonialIdx).fadeTo("normal",.3);
		$j("#testimonial"+prevIdx).hide("normal");
		
		// Increment Pointers  Note: they aren't necessarily sequential.
		prevBufferIdx = prevIdx;
		prevIdx = testimonialIdx;
		testimonialIdx = nextIdx;
		nextIdx = nextBufferIdx;
		nextBufferIdx = $j("#testimonial"+nextBufferIdx).attr("next");
		
		// If exists already in list
		if($j("#testimonial"+nextBufferIdx).length)
		{
			// pop LI and push it on bottom
			moveLi = $j("#testimonial"+nextBufferIdx);
			$j("#testimonial"+nextBufferIdx).remove();
			$j("#testimonial_list").append(moveLi);
			
			$j("#next_testimonial").click(function(){
				testiListCtl("next");
			});
			$j("#prev_testimonial").click(function(){
				testiListCtl("prev");
			});
		}
		else
		{
			getTestimonial(true,nextBufferIdx,"append","hide", true);
		}
		
	}
	else if(action=="next")
	{
		// Animations
		$j("#testimonial"+prevBufferIdx).show("normal");
		$j("#testimonial"+prevIdx).fadeTo("normal",1);
		$j("#testimonial"+testimonialIdx).fadeTo("normal",.3);
		$j("#testimonial"+nextIdx).hide("normal");
		
		// Decrement Pointers  Note: they aren't necessarily sequential.
		nextBufferIdx = nextIdx;
		nextIdx = testimonialIdx;
		testimonialIdx = prevIdx;
		prevIdx = prevBufferIdx;
		prevBufferIdx = $j("#testimonial"+prevBufferIdx).attr("prev");
		
		// If exists already in list
		if($j("#testimonial"+prevBufferIdx).length)
		{
			// pop LI and push it on top
			moveLi = $j("#testimonial"+prevBufferIdx);
			$j("#testimonial"+prevBufferIdx).remove();
			$j("ul#testimonial_list").prepend(moveLi);

			$j("#next_testimonial").click(function(){
				testiListCtl("next");
			});
			$j("#prev_testimonial").click(function(){
				testiListCtl("prev");
			});
		}
		else
		{
			getTestimonial(true,prevBufferIdx,"prepend","hide", true);
		}
	}
	// Action to initialize display
	else if(action=="init")
	{
		nextBufferIdx = 5;
		nextIdx = 4;
		testimonialIdx = 3;
		prevIdx = 2;
		prevBufferIdx = 1;
		
		getTestimonial(false,nextBufferIdx,"prepend","hide");
		getTestimonial(false,nextIdx,"prepend","show");
		getTestimonial(false,testimonialIdx,"prepend","show");
		getTestimonial(false,prevIdx,"prepend","show");
		getTestimonial(false,prevBufferIdx,"prepend","hide", true);
		
		$j("#testimonial"+testimonialIdx).fadeTo("fast",1);
	}
}

// Ajax funtion to get information.
function getTestimonial(async,id,position,displaymode,enableButtonsIn)
{
	var enableButtons = enableButtonsIn || false;
	$j.ajax({
		async: async,
		type: "POST",
		url: "/ajax/getTestimonial/"+id,
		datatype: "xml",
		success: function(xml){
			displayTestimonial($j(xml).find("PK_TestimonialNum").text(),
							   $j(xml).find("FK_PrevTestimonial").text(),
							   $j(xml).find("FK_NextTestimonial").text(),
							   $j(xml).find("TestimonialText").text(),
							   $j(xml).find("TestimonialSig").text(),
							   position,
							   displaymode
								);
			if(enableButtons)
			{
				// Enable buttons
				$j("#next_testimonial").click(function(){
					testiListCtl("next");
				});
				$j("#prev_testimonial").click(function(){
					testiListCtl("prev");
				});
			}	
		}
	});
}

// Appends list items to list.
function displayTestimonial(id,prev,next,text,sig,position,displaymode)
{
	// Build list item from Ajax data
	var newLi = $j("<li id=\"testimonial"+id+"\" class=\"testi_li\" testimonial_id=\""+id+"\" next=\""+next+"\" prev=\""+prev+"\">"+
			"<div class=\"testimonial_text\">"+text+"</div>"+
			"<div class=\"testimonial_sig\">"+sig+"</div>"+
			"</li>");

	// Put at top of list
	if(position=="prepend")
	{
		$j("#testimonial_list").prepend(newLi);
		// All items get opacity of .3 for fading animations
		$j("#testimonial"+id).fadeTo(0,.3);
		if(displaymode=="hide")
		{
			$j("#testimonial"+id).hide();
		}
		else if (displaymode=="show")
		{
			$j("#testimonial"+id).show(); 
		}
	}
	// Put at bottom of list
	else
	{
		$j("#testimonial_list").append(newLi);
		// All items get opacity of .3 for fading animations
		$j("#testimonial"+id).fadeTo(0,.3);
		if(displaymode=="hide")
		{
			$j("#testimonial"+id).hide();
		}
		else if (displaymode=="show")
		{
			$j("#testimonial"+id).show();
		}
	}

}

// On page load
$j(document).ready(function(){			
	testiListCtl("init");
});