$(document).ready(function(){
	loadGallery();
	timer1 = setTimeout("advanceSlideRight()", 10000);
});

var counter = 0;
var newGalleryLength = 0;
var clicked = 0;
var timer1;
var timer2;

// Hide form elments on page load
function loadGallery(){
	
	var galleryLength = $(".middleGallery").length;
	newGalleryLength = (galleryLength - 1);
	
	$(".middleGallery").hide();
	$("#copy_0.middleGallery ").show();
	
	$("#moveRight").click(function(){
		clicked = 1;
		advanceSlideRight();
	});
	
	$("#moveLeft").click(function(){
		clicked = 1;
		advanceSlideLeft();
	});
	
	$("ul.galleryNum li").click(function(){
		clicked = 1;
		counter = $(this).attr("id").charAt("5");
		shift(counter);
	});
}

function advanceSlideRight(){
	if(counter>=0 && counter<newGalleryLength){
		counter++;
		shift(counter);
	}
	else if(counter==newGalleryLength)
	{
		counter=0;
		shift(counter);
	}
}

function advanceSlideLeft(){
	if(counter>0 && counter<=newGalleryLength)
	{
		counter--;
		shift(counter);
	}
	else if(counter==0)
	{
		counter=newGalleryLength;
		shift(counter);
	}
}

function shift(num){
	if(clicked == 0)
	{
		timer2 = setTimeout("advanceSlideRight()", 6500);
	}
	if(clicked == 1)
	{
		clearTimeout(timer1);
		clearTimeout(timer2);
	}
	var show = "#copy_"+num;
	var link = "#link_"+num;
	$(".galleryNum li").removeClass("on");
	$(link).addClass("on");
	$(".middleGallery").hide();
	$(show).fadeIn(1500);
}
