var images = new Array();
var current_image = 0;
var image_rotator_timer = 0;
var speed = 1000;
var delay = 8000;
var first = true;

function set_buttons() {
	$("div.buttons ul li").removeClass("on");
	$("div.buttons ul li").each(function(){
		if (current_image==parseInt($(this).text())) {
			$(this).addClass("on");
		}
	});	
}

function rotate_images(skip_fade) {
	image = $("#image_rotator_holder div#main-image img");
	
	if (image.attr("src")) {
		if (first) {
			image.attr("src", images[current_image]).fadeIn(3000).fadeTo(500, 100);
		} else {
			if(skip_fade) {
				image.attr("src", images[current_image]).fadeIn(3000).fadeTo(500, 100);
				set_buttons();
			} else {
				image.fadeOut(2000 , function() {
					image.attr("src", images[current_image]).fadeIn(3000).fadeTo(500, 100);
					set_buttons();
				});
			}
		
		}
		
		//console.log(current_image);
		if (current_image==(images.length-1)) current_image = 0;
		else current_image++;
	
		image_rotator_timer = setTimeout("rotate_images();", delay);
		first = false;
	}
}

$(document).ready(function() {
	
	$.get('../rotate.htm', function(html) {
		$("#image_rotator_holder").html(html);
		
		$("#image_rotator_holder ul li img").each(function(){
			images.push($(this).attr("src")); 
			//console.log($(this).attr("src"));
		});
		rotate_images();
	});
	
	$("div.buttons ul li").click(function(){
		//alert($(this).text());
		current_image=parseInt($(this).text())-1;
		clearTimeout(image_rotator_timer);
		rotate_images(true);
	});
	
});
