$(document).ready(function() {
		var enlargeTrigger = $("a.enlarge-cover"); // Class name of the link that triggers the enlarged cover
		var imageUrl = enlargeTrigger.attr("href"); // gets the href value of the trigger element
		var img = new Image();
		var bkTitle = $("#bkstore-bkDetails h1").text();
		
		$("<div id='coverPopup-container'><div id='popupTitle'></div><div id='coverPopup'><div id='popupImg'></div><a href='#' class='closePopup'><img src='/images/popupBox-close.gif' /></a></div></div>").appendTo("body").hide();
		$(enlargeTrigger).click(function() {
			$("#popupImg").append("<div id='popupPreload'><img src='/images/spinner.gif' /></div>");
			$(img) // Create image and display it
					.load(function() { //after image has loaded
					    $("#popupPreload").remove();
						$(this).hide(); //hide the image
						$("#popupTitle").append("<h1>"+bkTitle+"</h1>")
						$("#popupImg").append(this); // attach new img to the div
						
						$(this).show();
						$("#coverPopup-container").fadeIn();
						
						// Find width of image, set Popup width, and center it on the page
						var imgWidth = $(img).width(); // get and store img width
						var popupWidth = $("#coverPopup-container").width();
						var popupHeight = $("#coverPopup-container").height();
						var screenWidth = $(document.window).width();
						var screenHeight = $(document.window).height();
						
						$("<div id='popupScreen'></div>").appendTo("body").hide();
						$("#popupScreen").css({width: screenWidth, height: screenHeight, opacity:.5});
						$("#popupScreen").fadeIn();
						
						$("#coverPopup-container").css({left: (screenWidth/2) - (popupWidth/2), top: 50 + "px", width: imgWidth + 50 + 'px' }); // set the popup width and position
					    
					})
					
					.error(function() {
						// Notification of an error loading the image
					})
					
					.attr("src", imageUrl); // Set img src
				
			return false; // Disable click through
		
		return false;
		});
		
		$(".closePopup").click(function() {
			$("#coverPopup-container")
				.fadeOut('', function() { // fadeout the Popup box
					$(img).remove(); // remove the Img in the Callback
					$("#popupTitle h1").remove(); // remove H1 tag
					//$("#popupScreen").remove();
				});
			$("#popupScreen")
				.fadeOut('', function() {
				$(this).remove();
			});
			return false;
		});
		
	});
