var $j = jQuery.noConflict();

$j(document).ready(function() {
	
	/* Setup variables
	-----------------------------------------------------*/
	$slideshow = true;
	$count = 0;
	
	
	
	/* Variables
	-----------------------------------------------------*/
	$list = $j(".galleryImage");
	$total = $list.length;
	
	
	
	/* Initialize the gallery
	-----------------------------------------------------*/
	loadImage();
	
	
	
	/* Load image function
	-----------------------------------------------------*/
	function loadImage() {
		
		// Stop the timer
		timerStop();
		
		// Save the path
		var $path = $j($list[$count]).attr("path");

		// Create a new image
		var img = new Image();
		
		// Load the image
		$j(img).load(function() {
	
			// Hide the image
			$j(this).hide();

			// Add the image to the galleryContainer div
			$j("#imageRotator").append(this);
	
			$j(this).fadeIn('slow', function() {
	
				var $imgList = $j("#imageRotator").children("img");
				for (var $i=0; $i<$imgList.length-1; $i++) {
					$j($imgList[$i]).remove();	
				}
				
				// Start the timer
				timerStart();
			});
			
		// Set the new image path
		}).attr('src', $path);
	}
	
	
	
	/* Timers
	-----------------------------------------------------*/
	function timerStart() {
		
		$j("#imageRotator").everyTime(6000, "timer", function() {
														 
			if ($total > 1) {
				
				// Add to the count
				$count++;
				if ($count == $total) $count = 0;
	
				// Load the image
				loadImage();
			}
		});
	}
	
	function timerStop() {
		
		// Stop the timer
		$j("#imageRotator").stopTime("timer");
	}

	
});
