$(document).ready(
	function()
	{
		var currentIndex = 1;
		var moveDistance = 960;
		var numPages;
		
		var autoTimer;
		var movable = true;
		
		$('#rightBtn').addClass('normal');
		$('#leftBtn').addClass('disabled');

		function init()
		{
			numPages = $('#contentHolder').children().length;
			
			$('#rightBtn').addClass('normal');
			$('#rightBtn').removeClass('disabled');
			
			$('#leftBtn').addClass('normal');
			$('#leftBtn').removeClass('disabled');
			
			autoTimer = setInterval(moveImage, 10000);
		
			if(navigator.appVersion.indexOf("MSIE 6") != -1)
				if(true)
					moveDistance = 1000;
		}
				
		function moveRight()
		{
			if(movable)
			{
				lock();
				
				if(currentIndex < numPages)
				{
					currentIndex++;	
					$('#contentHolder').animate( { 'marginLeft': "-=" + moveDistance + "px" }, { queue: true, duration:1000, complete: function() { unlock(); } } );
				}
							
				else
				{	
					// remove all of the children except the last one and append them at the end
					for(var i = 0; i < numPages - 1; i++)
					{
						var lastChild = $('#contentHolder').children()[0];
						$('#contentHolder').children()[0].remove;
						$('#contentHolder').append(lastChild);
					}
					
					// reset the margin position
					$('#contentHolder').css('marginLeft', '0px');
					
					// move to the first page and then remove the last child and append it at the end
					$('#contentHolder').animate( { 'marginLeft': "-=" + moveDistance + "px" }, { queue: true, duration:1000, 
						complete: function() {
							var lastChild = $('#contentHolder').children()[0];
							$('#contentHolder').children()[0].remove;
							$('#contentHolder').css('marginLeft', '0px');
							$('#contentHolder').append(lastChild);
							
							unlock();
				    	} } );
					
					currentIndex = 1;
				}
				
				moveTimer = setTimeout(unlock, 1000);
			}
		}
				
		function moveLeft()
		{
			if(movable)
			{
				lock();
				
				if(currentIndex > 1)
				{
					currentIndex--;
					$('#contentHolder').animate( { 'marginLeft': "+=" + moveDistance + "px" }, { queue: true, duration:1000, complete: function() { unlock(); } } );
				}
				else
				{
					// remove all of the children except the first one and append them at the end
					for(var i = (numPages - 1); i > 0; i--)
					{
						var lastChild = $('#contentHolder').children()[(numPages - 1)];
						$('#contentHolder').children()[(numPages - 1)].remove;
						$('#contentHolder').prepend(lastChild);
					}
					
					// reset the margin position
					$('#contentHolder').css('marginLeft', -((numPages - 1) * moveDistance) + 'px');
					
					// move to the last page and then remove the first child and place it at the beginning
					$('#contentHolder').animate( { 'marginLeft': "+=" + moveDistance + "px" }, { queue: true, duration:1000, 
						complete: function() {
							var lastChild = $('#contentHolder').children()[(numPages - 1)];
							$('#contentHolder').children()[(numPages - 1)].remove;
							$('#contentHolder').css('marginLeft', -((numPages - 1) * moveDistance) + 'px');
							$('#contentHolder').prepend(lastChild);
							
							unlock();
				    	} } );
					
					currentIndex = numPages;
				}
			}
		}
		
		function lock() { movable = false; }
		function unlock() { movable = true; }
		function moveImage() { moveRight();	}
		
		$('#rightBtn').click(function() { clearInterval(autoTimer); moveRight(); });
		$('#leftBtn').click(function() { clearInterval(autoTimer); moveLeft(); });
		
		$("#leftBtn").hover(
				function() { $('#leftBtn').addClass('hover'); },
				function() { $('#leftBtn').removeClass('hover'); }
		);
		
		$("#rightBtn").hover(
				function() { $('#rightBtn').addClass('hover');	},
				function() { $('#rightBtn').removeClass('hover'); }
		);
		
		init();
	}
);