var current_img = 0;
var last_index = 0;
var delay = 5;
var speed = 1200;
var timeout;

$(document).ready(function(){
	last_index = $('#header_left img.animate').length - 1;
	if (last_index > 0)
	{
		$('#header_left a.thumb').click(function(event){
			event.preventDefault();
			event.stopPropagation()
			clearTimeout(timeout);
			
			if ($('#header_left img.animate:eq('+nextIndex()+')').css('display') == 'block')
			{				
				$('#header_left img.animate:eq('+current_img+')').stop(true, true);
				$('#header_left img.animate:eq('+nextIndex()+')').stop(true, true);
				current_img = nextIndex();
			}
			
			var clicked_img;
			var tmp_index = 0;
			$('#header_left img.animate').each(function(){
				if ($(this).attr('src') == $(event.target).parent().attr('href'))
				{
					clicked_img = tmp_index;
				}
				
				tmp_index++;
			});
			
			$('#header_left img.animate:eq('+current_img+')').fadeOut(speed);
			$('#header_left img.animate:eq('+clicked_img+')').fadeIn(speed, function(){
				current_img = clicked_img;
				timeout = window.setTimeout("tweenit()", delay * 1000);
			});

		});
		
		$('#header_left img.animate:gt(0)').hide();
		timeout = window.setTimeout("tweenit()", delay * 1000);
	}
});

function tweenit()
{
	$('#header_left img.animate:eq('+current_img+')').fadeOut(speed);
	$('#header_left img.animate:eq('+nextIndex()+')').fadeIn(speed, function(){
		current_img = nextIndex();
		timeout = window.setTimeout("tweenit()", delay * 1000);
	});
}

function nextIndex()
{
	return current_img + 1 > last_index ? 0 : current_img + 1;
}
