// JavaScript Document


var showDuration = 6000;
var animSpeed = 1000;

var viewPortWidth = 714;
var count = 0;
var reelWidth = 0;
var current = 0;
var currentposition = 0;
var clicked = false;
var imagerefs = [];
var textrefs = [];
var textposrefs = new Object();
var numtext = 0;
var currenttext = -1;


//this is a recursive function that handles the regular changing of the image in view.
function initialize(){
	
	//alert(current);
	
	
	if(!clicked){
		if(current == count-1){
			currentposition = 0;
		}else{
			currentposition = (viewPortWidth*(current+1))*-1;
		}
		
		current ++;
		current = current % count;

		$("#img-reel").animate({left: currentposition},animSpeed);
		updateItemSelected();
	}else{
		clicked = false;
	}
	
	setTimeout("initialize()",animSpeed+showDuration);

}



function gotoScreen(num){
	
	current = num;
	currentposition = (current*viewPortWidth)*-1;
	current = current % count;
	
	clicked = true;
	$("#img-reel").animate({left: currentposition},animSpeed)
	
	updateItemSelected();
	
}




function updateItemSelected(){
	
	var i = 0;
	$('#rotatorlinks ul:first li').each(function(){
		
		if(i == current){
			$(this).addClass("selected");
		}else{
			$(this).removeClass("selected");
		}
		i++;
	});
	
	
	var imgURL = $(imagerefs[current]).parent().attr('href');
	$('#rotator-link').attr('href',imgURL);
	$('.rotator-link').each(function(){
		$(this).attr('href',imgURL);
		
	});
	
}



$(document).ready(function() {
	
	
	$('#img-reel img').each(function(){
		imagerefs[count] = this;
		count++;
		$('#rotatorlinks').append('<ul></ul>');
		$('#rotatorlinks ul:first').append('<li><a href="#" rel="' + (count-1) + '"><span>' + count + '</span></a></li>');
		
	});
	
	$('#rotatorlinks ul:first li').each(function(){
		$(this).click(function(){
			var goto = $(this).find('a:first').attr('rel');
			gotoScreen(goto);
		});
	});
	
	
	
	reelWidth = viewPortWidth*count;
	$("#img-reel").width(reelWidth);
	$('#scrolling-imgs').css("overflow","hidden");
	
	updateItemSelected();
	setTimeout("initialize()",showDuration);
	
	
});

