//GLOBAL VARIABLES
numberOfImages = 0;
currentImage = 0;

//ROLLOVER SCRIPT
function rolloverButtons(imgID, imgSrc){
	if(document.getElementById(imgID).src != imgSrc){
		document.getElementById(imgID).src = imgSrc;
	}
}

//GIF FUNCTION
function animatedGif(){
	//IF DIV EXISTS
	if($('.animatedGif').length){
		//RUN ANIMATION
		gifInterval = setInterval(gifAnimation, 4000);
	}
}

//ANIMATION FUNCTION
function gifAnimation(){
	//VARIABLES
	numberOfImages = parseInt($('.animatedGif').attr('alt').substring($('.animatedGif').attr('alt').length - 1));
	currentImage = parseInt($('.animatedGif').attr('src').substring($('.animatedGif').attr('src').length - 5, $('.animatedGif').attr('src').length - 4));

	//RESET COUNTER
	if(currentImage == numberOfImages){
		currentImage = 0;
	}
	
	//ANIMATE IMAGES
	$('.animatedGif:first').clone().insertBefore($('.animatedGif:first'));
	$('.animatedGif:first').css({'position':'absolute', 'left':$('.animatedGif:first').offset().left + 'px', 'top':$('.animatedGif:first').offset().top + 'px', 'opacity':0, 'z-index':2});
	$('.animatedGif:first').attr({'src':$('.animatedGif').attr('src').substring(0, $('.animatedGif').attr('src').length - 5) + (currentImage + 1) + $('.animatedGif').attr('src').substring($('.animatedGif').attr('src').length - 4), 'alt':$('.animatedGif').attr('alt').substring(0, $('.animatedGif').attr('alt').length - 6) + (currentImage + 1) + ' of ' + numberOfImages});
	$('.animatedGif:first').animate({opacity:1}, 1000, function(){
		$('.animatedGif:first').css({'position':'', 'left':'', 'top':'', 'z-index':1});
		$('.animatedGif:last').remove();
	});
}

//RUN ON PAGE LOAD FUNCTIONS
$(document).ready(function(){
	//ANIMATED GIF
	animatedGif();
});
