var imagesPositioned = false;
var captionOpen = true;
$(window).load(function(){
	var thumbs = $("a.galleryThumb");
	var totalImages = thumbs.length;
	

	// creat the large image for each small image
	$("img.smallimage").each(
	
		// initialize the images
		function(e) {
			var img = $('<img src="'+$(this).attr("src")+'" border="0" />').appendTo("#galleryImageContainer");
			
			// if it's a video image, make it clickable to load the video
			var link = $(this).parent();
			if(link.attr("href").substring(link.attr("href").length-3) == "php") {
				img.click(
						  
					// load the video into the video container
					function() {
				
						var URL=link.attr("href").value;
						// window.location = "./100Grand.php" ;
						window.location = link.attr("href");
						

						// if wer are internet explorer, just open it in a new window
						if($.browser.msie) {
							var videoWindow = window.open(videoContainer.attr("videoSrc"), "video_window", 'left=100, top=100, width=640, height=380, toolbar=0, status=0, location=0, directories=0, menubar=0, scrollbars=0, resizable=0');
							videoWindow.focus();
						} else {
							
							// stop the current one if it's there
							if(document.embeds[0]) {
								document.embeds[0].Stop();
							}
							

						}
						
					}
				);
			} else {
			// otherise, have the click toggle the caption
				img.click(
					// load the video into the video container
					function() {
						$("#captionControl").click();
					}
				);
			}			
			
						
			
			// position all way off to the right
			img.css({top: "0px", left: "1000px"});

		}
	);


	// tell the gallery images what to do when clicked
	$("a.galleryThumb").click(
		function(e) {
			// dont' actually go to the image
			e.preventDefault();

			
			// condense the window because there's no controller
			$("#galleryWindow").css("height", "360px");


			// stop the current one if it's there
			if(document.embeds[0]) {
				document.embeds[0].Stop();
			}

			// try to clear the video if it's in there
			var videoContainer = $("#galleryVideoContainer");
			videoContainer.css("visibility", "hidden");
			videoContainer.attr("videoSrc", $(this).attr("href"));
			videoContainer.html("");


			// find the target image
			var targetImage = $("#galleryImageContainer img[src*="+$(this).attr("href").substring(0, $(this).attr("href").length-3)+"]");
			positionImages();
			
			// scroll to the top if we aren't there
			var scrollPosition = $(window).scrollTop();
			if(scrollPosition > 0) {
				$.scrollTo(0, scrollPosition, { onAfter: function() { slideToImage(targetImage); } });
			} else {
				slideToImage(targetImage);
			}
			

			// get any caption info
			var newCaption = $("#" + $(this).attr("id") + "_caption").html();
			setCaption(newCaption);
		}
	);

			
			
	
	
	// add the close functionality to the caption controls
	$("#captionControl").click(
		function(e) {
			if($("#captionContent").css("display") == "none") {
				$("#captionContent").slideDown("fast");
				$("#showCaption").css("display", "none");
				$("#hideCaption").css("display", "block");
				captionOpen = true;
			} else {
				$("#captionContent").slideUp("fast");
				$("#showCaption").css("display", "block");
				$("#hideCaption").css("display", "none");
				captionOpen = false;
			}
		}
	);



	// simulate a click on a link if we click on a name
	$("a.galleryLink").click(
		function(e) {
			// dont' actually go to the image
			e.preventDefault();

			$("a.galleryThumb[id*="+$(this).attr("href")+"]").click();
		}
	);
	
	
	
	$("#captionContent").fadeTo(0, 0.80);
	$("#captionControl").fadeTo(0, 0.80);
	


	// "click" the first link
	$("a.galleryThumb").eq(0).click();
	positionImages();
	
});


function setCaption(newCaption) {
	if(newCaption != null) {
		$("#captionContent").html(newCaption);
		$("#captionBox").css("display", "block");
		$("#captionControl").css("display", "block");
		
		// if we are currently showing the caption, show the hide box
		if(captionOpen) {
			$("#showCaption").css("display", "none");
			$("#hideCaption").css("display", "block");
			$("#captionContent").slideDown("fast");
		} else {
			$("#showCaption").css("display", "block");
			$("#hideCaption").css("display", "none");
		}				
	} else {
		$("#captionContent").slideUp("slow",
			function(e) {
				$(this).html(null);
			}
		);
		$("#captionControl").css("display", "none");
	}
}

function slideToImage(targetImage) {
	// find out where to go to and slide the images
	var container = $("#galleryImageContainer");
	var cx = parseInt(container.css("left"));
	var dx = -parseInt(targetImage.css("left"));
	container.animate({left: dx}, Math.sqrt(Math.abs(dx-cx))*10);
}


function positionImages() {

	// if we've loaded all of the images and have not positioned them
	if($("#galleryImageContainer img").length == $("img.smallimage").length) {
				
		// place all of the images in order of the small ones
		$("img.smallimage").each(
			function() {
				var lastImage;
				var thisImage = $("#galleryImageContainer img[src*=" + $(this).attr("src") + "]");
				var i = $("img.smallimage").index(this);

				lastImage = $("#galleryImageContainer img[src*=" + $("img.smallimage").eq(i-1).attr("src") + "]");

				if(i > 0 && (lastImage.width() > 0)) {
					thisImage.css({position: "absolute", top: "0px", left: parseInt(lastImage.css("left")) + lastImage.width()});
				} else {
					thisImage.css({position: "absolute", top: "0px", left: "0px"});
			}
			}
		);
		
		imagesPositioned = true;
	}
}

