
var showDuration = 5000;
var animSpeed = 1000;

var viewPortWidth = 730;
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;
var animate = true;


//this is a recursive function that handles the regular changing of the image in view.
function initialize(){
	
	//alert(current);
	
	if(animate){
		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++;
	});
	
	$('#rotator .ctatext div:first').html(textrefs[current]);
	
	
	var imgURL = $(imagerefs[current]).parent().attr('href');
	$('#rotator-link').attr('href',imgURL);
	$('.rotator-link').each(function(){
		$(this).attr('href',imgURL);
		
	});
	
}



$(document).ready(function() {
	
	
	$('#rotatorlinks').hide();
	
	$('#rotator').mouseenter(function(){
		$('#rotatorlinks').show();
		 animate = false;
	});
	
	$('#rotator').mouseleave(function(){
		$('#rotatorlinks').hide();
		animate = true;
	});
	
	$('#img-reel img').each(function(){
		imagerefs[count] = this;
		textrefs[count] = '<h4>'+$(this).attr('alt')+'</h4>'+'<p>'+$(this).attr('longdesc')+'</p>';
		count++;
		
		if(count == 1){
			$('#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(e){
			e.preventDefault();
			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);
	
	
});

