$(document).ready(function()
{
	var delay = 3500; // milliseconds
	var current = 1;
	var old = 0;
	var width = 722;
	
	if($("div.rotator ul").children().length > 1)
	{
		// generate buttons
		$("div.rotator").append("<div class=\"rotator-menu\"></div>");
		for(var i = 0; i < $("div.rotator ul").children().length; i++)
		{
			$("div.rotator .rotator-menu").append("<a href=\"#\" class=\"b" + (i+1) + "\"><img src=\"img/b_rotatorMarker.gif\" /></a>");
		}
	
		
		var t = setTimeout(next,delay);
		$("div.rotator-menu a:nth-child("+ current +") img").attr("src", "img/b_rotatorMarker_current.gif");
	
	}
	
	$("div.rotator ul li:nth-child("+ current +")").attr("style", "left: 0px;");
	
	$("div.rotator-menu a").click(function()
	{
		clearTimeout(t);
		old = current;
		current = parseInt($(this).attr('class').slice(1));

		$("div.rotator-menu a:nth-child("+ old +") img").attr("src", "img/b_rotatorMarker.gif");
		$("div.rotator-menu a:nth-child("+ current +") img").attr("src", "img/b_rotatorMarker_current.gif");

		if (current > old) {animateLeft()}
		if (current < old) {animateRight()}
		return false;
	});

	
	
	function next()
	{
		old = current;
		current++;
		if (current == $("div.rotator ul").children().length + 1)
		{current = 1}
		
		$("div.rotator-menu a:nth-child("+ old +") img").attr("src", "img/b_rotatorMarker.gif");
		$("div.rotator-menu a:nth-child("+ current +") img").attr("src", "img/b_rotatorMarker_current.gif");
		
		animateLeft();
		t = setTimeout(next,delay);
	}

	function animateLeft()
	{
		$("div.rotator ul li:nth-child("+ old +")").fadeOut('slow');
		$("div.rotator ul li:nth-child("+ current +")").css("display", "none");
		$("div.rotator ul li:nth-child("+ current +")").css("left", "0px");
		$("div.rotator ul li:nth-child("+ current +")").fadeIn('slow');

		
	}
	
	function animateRight()
	{
		$("div.rotator ul li:nth-child("+ old +")").fadeOut('slow');
		$("div.rotator ul li:nth-child("+ current +")").css("left", "0px");
		$("div.rotator ul li:nth-child("+ current +")").fadeIn('slow');
		
	}
});
