$(document).ready(function(){
	
	$.getScript('js/gallery-sets.js');
	
	$(".popovergallery").click(function () {
		
		var currentKey = 0;
		var setName = $(this).attr('href');
		var galleryArray = eval(setName);
		var arrayLength = galleryArray.length;
		
		$('<div id="coverpage"></div>').appendTo('body');
		$('<div id="popup-window"><div id="show-img"></div> <div id="show-txt"></div> <a href="#" id="close"><img src="images/close.gif" alt="" /></a> <a href="#" id="previous"><img src="images/prevlabel.gif" alt="" /></a> <a href="#" id="next"><img src="images/nextlabel.gif" alt="" /></a> </div>').appendTo('body');
		
		$("#show-img").html('<img src="' + galleryArray[currentKey][0] + '" alt="" />').hide();
		$("#show-txt").html(galleryArray[currentKey][1]).hide();
		
		$("#popup-window").fadeIn('medium', function() {
			
			$("#show-img").show();
			$("#show-txt").show();
			$("#previous").show();
			$("#next").show();
			$("#close").show();
			
		});
		
		
		
		
		$("a#next").click(function () {
  		
  		if (currentKey == (arrayLength - 1)) {
  			currentKey = 0;
  		} else {
  			currentKey = currentKey + 1;
  		}
  		
  		$("#show-img").html('<img src="' + galleryArray[currentKey][0] + '" alt="" />').hide();
  		$("#show-txt").html(galleryArray[currentKey][1]).hide();
  		
  		$("#popup-window").fadeIn('slow', function() {
				
				$("#show-img").show();
				$("#show-txt").show();
				$("#previous").show();
				$("#next").show();
				$("#close").show();
				
			});
  		
  	});
		
		
		
		$("a#previous").click(function () {
  		
  		if (currentKey == 0) {
  			currentKey = (arrayLength - 1);
  		} else {
  			currentKey = currentKey - 1;
  		}
  		
  		$("#show-img").html('<img src="' + galleryArray[currentKey][0] + '" alt="" />').hide();
  		$("#show-txt").html(galleryArray[currentKey][1]).hide();
  		
  		$("#popup-window").fadeIn('slow', function() {
				
				$("#show-img").show();
				$("#show-txt").show();
				$("#previous").show();
				$("#next").show();
				$("#close").show();
				
			});
  		
  	});
  	
  	
  	$("a#close").click(function () {
  		$("#popup-window").remove();
  		$("#loading").remove();
  	  $("#coverpage").fadeOut("fast", function() {
  	  	$("#coverpage").remove();
  	  });
  	});
		
		
  	return false;
  	
  });
  
});