$(document).ready(function(){
	auto = window.setInterval(rotate, 4000);
	$(".btn").each(function(){
		$(this).mouseup(function(){
			if(switcher(this)) window.clearInterval(auto);
		});
	});	
});
function rotate()
{
	var current = $(".feature .imgs:visible").attr("id");
	var next = findNext('right',current);
	if (next)
	{
		$("#" + next).fadeIn('fast');
		$("#" + current).fadeOut('fast');
	}
}
function switcher(target)
{
	var str = $(target).attr('class');
	var ar = str.split(' ');
	var current = $(".feature .imgs:visible").attr("id");
	var next = findNext(ar[1],current);
	if (next)
	{
		$("#" + next).fadeIn('fast');
		$("#" + current).fadeOut('fast');
		return 1;
	}
	return 0;
}

//find out which is next
function findNext(side,current)
{
	
	var pattern = /f[0-9]+/;
	if (pattern.test(current))
	{
		var cn = parseInt(current.replace('f',''));
		if (side == 'right')
		{
			var next = cn + 1;
			if ($("#f" + next).length > 0)
			{
				return 'f' + next;
			}
			else
			{
				return 'f1';
			}
		}
		else if (side == 'left')
		{
			var next = cn - 1;
			if ($("#f" + next).length > 0)
			{
				return 'f' + next;
			}
			else
			{
				var i = 1;
				var notDone = true;
				while(notDone)
				{
					if ($("#f" + i).length > 0)
					{
						var save = i;
					}
					else
					{
						notDone = false;
					}
					i++;
				}
				return 'f' + save;
			}
		}
	}
}


