var $j = jQuery.noConflict();

$j(document).ready(function() {
	
	/* Setup variables
	-----------------------------------------------------*/
	$slideshow = true;
	$count = 0;
	$loadAmount = 10;
	$random = true;
	
	
	
	/* Variables
	-----------------------------------------------------*/
	$win_w = $j(this).width() - 200;
	$win_h = $j(this).height() - $j("#footer").height() - 20;
	if ($j("#galleryContainer").hasClass("gallery"))
	$win_h = $win_h - 60;
	$list = $j(".galleryImage");
	$total = $list.length;
	$current_artist = "";
	$current_event = "";
	
	
	
	/* Initialize the gallery
	-----------------------------------------------------*/
	if ($j("#galleryContainer").hasClass("gallery")){
	
		$j(".artistStatement").hide();
		
		jQuery.get("http://www.visitcenter.org/index.php/ajax/gallery_images", {program: $j("#galleryContainer").attr("event"), award: $j("#galleryContainer").attr("award"), year: $j("#galleryContainer").attr("year"), artist: $j("#galleryContainer").attr("artist"), limit: $loadAmount, offset: "0"}, function(data, status) {

			$j("#galleryImages").append(data);
			$list = $j(".galleryImage");
			$total = $list.length;
			if ($random) {
				getRandomImage();
			} else {
				loadImage();
			}
			
			if ($total==$loadAmount)
			loadImages_loop();
		});
	} else {
		if ($random) {
			getRandomImage();
		} else {
			loadImage();
		}
	}
	
	
	$j(window).resize(function() {
		$j(".scalableImage").each(function(index) {
			scaleImage($j(this));
		
		});
	});
	
	function loadImages_loop() {
		
		jQuery.get("http://www.visitcenter.org/index.php/ajax/gallery_images", {program: $j("#galleryContainer").attr("event"), award: $j("#galleryContainer").attr("award"), year: $j("#galleryContainer").attr("year"), artist: $j("#galleryContainer").attr("artist"), limit: $loadAmount, offset: $total}, function(data, status) {
	
			$j("#galleryImages").append(data);
			$list = $j(".galleryImage");
			
			if ($list.length != $total) {
				
				$total = $list.length;
				
				// Set the current
				$j("#currentImage").children("p").remove();
				$j("#currentImage").append("<p>"+($count+1)+"<span class='brownHash'> // </span>"+$total+"</p>");
				
				// Loop for more images
				loadImages_loop();
			}
		});
	}
	
	function getRandomImage() {
		$ran = Math.floor (Math.random() * $total); 
		
		if ($ran == $count) {
			$count = Number($ran-1);
			if ($count < 0) $count = Number($total-1);
			loadImage();
		} else {
			$count = $ran;
			loadImage();
		}
	}
	
	function scaleImage($img) {
		$win_w = $j(this).width() - 200;
		$win_h = $j(this).height() - $j("#footer").height() - 20;
		if ($j("#galleryContainer").hasClass("gallery"))
		$win_h = $win_h - 60;
		
		$scale = 1;
		$xScale = $win_w/$img.attr("w");
		$yScale = $win_h/$img.attr("h");
		$new_w = "";
		$new_h = "";
		
		if ($xScale < $yScale) {
			$scale = $xScale;
			$new_w = $img.attr("w") * $scale;
			$new_h = $img.attr("h") * $scale;
			
			if ($new_w < 780) {
				$new_w = $new_w+20;
				$win_w = $win_w+20;
				$scale  = $new_w/$img.attr("w");
				$new_h = $img.attr("h") * $scale;
			}
			
			$img.css("height", "auto");	
			$img.css("width", String($new_w+"px"));	
		} else {
			$scale = $yScale;
			$new_w = $img.attr("w") * $scale;
			$new_h = $img.attr("h") * $scale;
			/*
			if ($new_w < 780) {
				$new_w = 780;
				$scale  = 780/$img.attr("w");
				$new_h = $img.attr("h") * $scale;
			}
			*/
			$img.css("width", "auto");	
			$img.css("height", String($new_h+"px"));		
		}

		$j("#galleryContainer").css("width", String($new_w+"px"));
		$j("#galleryContainer").css("height", String($new_h+"px"));
		
		/*$j("#galleryBackground").css("width", String($win_w+"px"));
		$j("#galleryBackground").css("height", String($win_h+"px"));*/
	}
	
	
	
	/* Load image function
	-----------------------------------------------------*/
	function loadImage() {
		
		// Stop the timer
		if ($slideshow==true) timerStop();
		
		if ($j("#galleryContainer").hasClass("gallery")){
			
			// Set the current
			$j("#currentImage").children("p").remove();
			$j("#currentImage").append("<p>"+($count+1)+"<span class='brownHash'> // </span>"+$total+"</p>");
			
			if ($current_artist != String($j($list[$count]).attr("artist")) || $current_event != String($j($list[$count]).attr("event"))) {
				
				// Save the current artist
				$current_artist = String($j($list[$count]).attr("artist"));
				$current_event = String($j($list[$count]).attr("event"));

				jQuery.get("http://www.visitcenter.org/index.php/ajax/artist_info", { name: $current_artist, project: $current_event, time: "2pm" }, function(data, status) {
					
					$j(".artistBio").remove();
					$j("#memberInfo").prepend(data);
					
					// Remove the old credit
					$j("#imageTitle").empty();
					$j("#eventTitle").empty();
					$j("#awards").empty();
					
					// Add the new credit
					if ($j($list[$count]).attr("title")) {
						$str = "<p>"+$j($list[$count]).attr("title")+"<p>";
						$j("#imageTitle").append($str);
					}
					
					// Add the new credit
					if ($j($list[$count]).attr("project")) {
						$str = $j($list[$count]).attr("project");
						$proj = $str.split(":::::");
						
						for (var i=0; i<$proj.length; i++) {
							
							if ($proj[i] != "Review Santa Fe" && $proj[i] != "Review LA") {
								$str = "<p>"+$proj[i]+"<p>";
								$j("#eventTitle").append($str);
							}
						}
					}
					
					// Add the new credit
					if ($j($list[$count]).attr("award")) {
						$str = "<h5>"+$j($list[$count]).attr("award")+"<h5>";
						$j("#awards").append($str);
					}
					
					// Show the artist statement if there is one
					if ($j("#artistStatement").length != 0) {
						$j(".artistStatement").show();
						$j("#colorbox").hide();
					}
	
				});
			}

			
		} else {
		
			// Remove the old credit
			$j("#homePhotoCredit").children("p").remove();
			
			// Add the new credit
			if ($j($list[$count]).attr("credit_link")) {
				$str = "<p><span class='blueHash'>//&nbsp;&nbsp;</span><a href='"+$j($list[$count]).attr("credit_link")+"' target='"+$j($list[$count]).attr("link_target")+"'>"+$j($list[$count]).attr("credit")+"</a></p>";
			} else {
				$str = "<p><span class='blueHash'>//&nbsp;&nbsp;</span>"+$j($list[$count]).attr("credit")+"<p>";
			}
			$j("#homePhotoCredit").append($str);
		
		}
		
		// Save the path
		var $path = $j($list[$count]).attr("path");
		
		// Fade out old images
		$j("#galleryContainer").children("img").fadeOut('slow', function() {
			$j(this).remove();
		});

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

			// Add the image to the galleryContainer div
			$j("#galleryContainer").append(this);
			
			$j(this).removeAttr("width")
			   .removeAttr("height")
			   .css({ width: "", height: "" });
				
				// Save the width & height
				$w = $j(this).width();
				$h = $j(this).height();
				
				$j(this).attr("w", $w);
				$j(this).attr("h", $h);
				scaleImage($j(this));
	
			$j(this).fadeIn('slow', function() {
				
				// Start the timer
				if ($slideshow==true) timerStart();
			});
			
		// Set the new image path
		}).attr('src', $path);
	}
	
	
	
	/* Timers
	-----------------------------------------------------*/
	function timerStart() {
		
		$j(this).everyTime(6000, "timer", function() {
			
			if ($total > 1 && $j("#cboxLoadedContent").children(".artistPopup").length == 0) {
	
				// Load the image
				if ($random) {
					getRandomImage();
				} else {
					// Add to the count
					$count++;
					if ($count == $total) $count = 0;
					loadImage();
				}
			}
		});
	}
	
	function timerStop() {
		
		// Stop the timer
		$j(this).stopTime("timer");
	}
	
	
	
	/* Mouse Functions
	-----------------------------------------------------*/
	$j('.arrowPrev').click(function(e) {
		
		e.preventDefault();
		
		// Set the count
		$count--;
		if ($count < 0) $count = ($total-1);
		
		// Load the image
		//if ($random) {
			//getRandomImage();
		//} else {
			loadImage();
		//}
	});
	
	$j('.arrowNext').click(function(e) {
		
		e.preventDefault();
		
		// Set the count
		$count++;
		if ($count == $total) $count = 0;
		
		// Load the image
		//if ($random) {
			//getRandomImage();
		//} else {
			loadImage();
		//}
	});
	
	$j('.pauseButton').click(function(e) {
		
		e.preventDefault();
		
		if ($slideshow == false) {
			$j('.pauseButton').text("PAUSE");
			$slideshow = true;
			timerStart();
		} else {
			$j('.pauseButton').text("PLAY");
			$slideshow = false;
			timerStop();
		}
	});
	
});
