var gallery_images = new Array();
var previous_enabled = true;
var next_enabled = true;

function image(name, src, background, description)
{
	this.name = name;
	this.src = src;
	this.background = background;
	this.description = description;
	
	this.show = function() {
		$("#description h2").text(this.name);
		$("#description p").text(this.description);
		
		var self = this;
		
		$("#image-wrap img").fadeOut("fast", function() {
			$(this).attr("src", self.src);
			$(this).attr("alt", self.name);
			
			if(self.background == "white") {
				$("#image-wrap").addClass("white");
			}
			else {
				$("#image-wrap").removeClass("white");
			}
			
			$(this).fadeIn("fast");
		});
	}
}

function update_counter(index)
{
	$("#image-counter").text((index + 1) + "/" + gallery_images.length);
}

function update_prev_next(index)
{
	if($("#film-strip a").eq(index).prev().length > 0) {
		$("a#prev-link").fadeIn();
		$("a#prev-link").attr("href", $("#film-strip a").eq(index).prev().attr("href"));
		previous_enabled = true;
	}
	else {
		$("a#prev-link").fadeOut();
		previous_enabled = false;
	}
	
	if($("#film-strip a").eq(index).next().length > 0) {
		$("a#next-link").fadeIn();
		$("a#next-link").attr("href", $("#film-strip a").eq(index).next().attr("href"));
		next_enabled = true;
	}
	else {
		$("a#next-link").fadeOut();
		next_enabled = false;
	}
	
}

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$(document).ready(function() {
	$("#image-wrap").css("min-height", "500px");
	$("#image-wrap img").hide();
	$("#image-wrap img").load(function() {
		$("#image-wrap img").fadeIn("fast");
	});
	
	$("#film-strip a").click(function() {
		var index = $("#film-strip a").index(this);
		gallery_images[index].show();
		update_counter(index);
		update_prev_next(index);
		return false;
	});
	
	$(document).keyup(function(e) {
   		if (e.which == 188) {
   			// previous (<)
   			if(previous_enabled) $("a#prev-link").trigger("click");
   			return false;
   		}
   		else if (e.which == 190) {
	   		// next (>)
   			if(next_enabled) $("a#next-link").trigger("click");
   			return false;
   		}
   	});
	
	$("a#prev-link,a#next-link").click(function() {
		var link = $(this).attr("href")
		var index = $("#film-strip a").index($("#film-strip a[href='" + link + "']"));
		gallery_images[index].show();
		update_counter(index);
		update_prev_next(index);
		return false;
	});
});