var showSlideShow="Start Slideshow";
var hideSlideShow="Stop Slideshow";

var img2Loading=true;
var slideShowRunning=false;
var ssDescription="";

function activateGalleryLinks(url) {
	activateThumbnails(url);
	activateFullsize(url);
	activateNext(url);
	activatePrev(url);
	activateFirst(url);
	activateLast(url);
	activateToggleSlideshow(url);
	//alert("Links activated.");
}

function scrollToThumbnail(id, direction) {
	var divWidth=$("#thumbnails").width();
	var scrollLeft=document.getElementById("thumbnails").scrollLeft;
	var offsetLeft=document.getElementById(id).offsetLeft-document.getElementById("thumbnails").offsetLeft;
	var width=$("#"+id).attr("width");
	//alert(id);
	if(offsetLeft>scrollLeft+divWidth-width) scrollLeft=offsetLeft;
	if(offsetLeft<scrollLeft) scrollLeft=offsetLeft-divWidth+width;
	document.getElementById("thumbnails").scrollLeft=scrollLeft;
}

function activateThumbnails(url) {
	$(".thumbnail").click(function() {
		var id=$(this).attr("id");
		sendGalleryCommand("show", id, url, false, updateGallery);
	});
}

function activateFullsize(url) {
	$(".fullSize").click(function() {
		sendGalleryCommand("next", 0, url, true, updateGallery);
	});
}

function activateNext(url) {
	$(".next").click(function() {
		sendGalleryCommand("next", 0, url, true, updateGallery);
		return false;
	});
}

function activatePrev(url) {
	$(".prev").click(function() {
		sendGalleryCommand("prev", 0, url, true, updateGallery);
		return false;
	});
}

function activateLast(url) {
	$(".last").click(function() {
		sendGalleryCommand("last", 0, url, true, updateGallery);
		return false;
	});
}

function activateFirst(url) {
	$(".first").click(function() {
		sendGalleryCommand("first", 0, url, true, updateGallery);
		return false;
	});
}

function activateToggleSlideshow(url) {
	$(".toggleSlideShow").html(showSlideShow).click(function() {
		if($(this).html()==showSlideShow) {
			$(".galleryView").fadeOut("slow", function() {
				if(!slideShowRunning) {
					slideShowRunning=true;
					doSlideShow(url, loadImg2);
				}
				$(".slideshow").fadeIn("slow", function() {
					$(".toggleSlideShow").html(hideSlideShow);	
				});
			});
		} else {
			$(".slideshow").fadeOut("slow", function() {
				$(".galleryView").fadeIn("slow", function() {
					slideShowRunning=false;
					$(".toggleSlideShow").html(showSlideShow);
				});
			});
		}
		return false;
	});
}

function updateGallery(cmd, msg) {
	var direction;
	//alert(msg['imageURL']);
	if(cmd=="next"||cmd=="last") direction=1;
	else direction=-1;
	$(".fullSize").attr({src: msg['imageURL']});
	$(".description").html(msg['description']);
	if(scroll) scrollToThumbnail(msg['id'], direction);
	$(".selectedThumbnail").removeClass("selectedThumbnail");
	$("#"+msg['id']).addClass("selectedThumbnail");
	if(msg['priority']) {
		$(".priority").val(msg['priority']);
		$(".caption").val(msg['caption']);
		$(".recordName").val(msg['name']);
		$(".imgName").html(msg['imgName']);
	}
}

function fadeToImg1(url) {
	$(".img1").fadeIn("slow");
	$(".img2").fadeOut("slow", function() {
		doSlideShow(url, loadImg2);
	});
}

function fadeToImg2(url) {
	$(".img1").fadeOut("slow");
	$(".img2").fadeIn("slow", function() {
		doSlideShow(url, loadImg1);
	});
}

function updateSlideshow(cmd, msg, url) {
	if(img2Loading) {
		$(".img2 img").attr({src: msg['imageURL']});
		$(".img2 .description").html(msg['description']);
		$(".img2").fadeIn("slow", function() {
			setTimeout("doSlideShow('"+url+"');", 5000);
		});
		
	} else {
		$(".img1 img").attr({src: msg['imageURL']});
		$(".img1 .description").html(msg['description']);
		$(".img2").fadeOut("slow", function() {
			setTimeout("doSlideShow('"+url+"');", 5000);
		});
	}
	img2Loading=!img2Loading;
}

function doSlideShow(url, callback) {
	if(slideShowRunning) {
		sendGalleryCommand("next", 0, url, true, callback);
	}	
}

function loadImg1(cmd, msg, url) {
	$(".img1 img").attr({src: msg['imageURL']});
	$(".img1 .description").html(msg['description']);
	setTimeout("fadeToImg1('"+url+"');", 3000);		
}

function loadImg2(cmd, msg, url) {
	$(".img2 img").attr({src: msg['imageURL']});
	$(".img2 .description").html(msg['description']);
	setTimeout("fadeToImg2('"+url+"');", 3000);		
}


function sendGalleryCommand(cmd, id, galleryURL, scroll, cb) {
	$.ajax({
		type: "POST",
		url: galleryURL,
		data: { command: cmd, imgID: id},
		dataType: 'json',
		success: function(msg) {
			cb(cmd, msg, galleryURL);
		}
	});
}