var PhotoGallery = {};

PhotoGallery.imagePath = "/assets/images/seadragon/img/" ;
PhotoGallery.viewer = null;

PhotoGallery.hide = function() {
	$.closeDOMWindow() ;
	if (PhotoGallery.viewer.isOpen()) {
		PhotoGallery.viewer.close() ;
	}
};

PhotoGallery.show = function (baseUrl, imageName) {
	if (PhotoGallery.viewer.isOpen()) {
		PhotoGallery.viewer.close() ;
	}
	PhotoGallery.viewer.openDzi (baseUrl + "/zoom/" + imageName + "/dzi.xml");
	$.openDOMWindow({
		loader:1,
		loaderImagePath: '/assets/images/common/loading.gif',
		loaderHeight : 50,
		loaderWidth : 50,
		width : 600,
		height : 480,
		borderSize: '10',
		borderColor: '#555',
		overlayOpacity : 5,
		positionType : 'centered',
		windowPadding : 0,
		windowSourceID : 'div.gallery-zoom-container'
	});
};

$(document).ready (function() {
	// Check to see if the gallery exists on the page first before trying to instantiate the SeaDragon viewer
	if ($("div.gallery-zoom-container")[0]) {
		$("article.photo-gallery img.main-image").click(function() {
			PhotoGallery.show($(this).attr("baseUrl"), $(this).attr("imageName"));
		});

		$("div.home-promo-a img.main-image").click(function() {
				PhotoGallery.show($(this).attr("baseUrl"), $(this).attr("imageName"));
		});

		$("#rightsidebar img.main-image").click(function() {
				PhotoGallery.show($(this).attr("baseUrl"), $(this).attr("imageName"));
		});

		$("#leftsidebar img.main-image").click(function() {
				PhotoGallery.show($(this).attr("baseUrl"), $(this).attr("imageName"));
		});

		/* Configure the Seadragon zoom viewer */
		Seadragon.Config.imagePath = PhotoGallery.imagePath;
		Seadragon.Config.proxyUrl = "/zoom/proxy.cmd?url=";

		PhotoGallery.viewer = new Seadragon.Viewer($("div.gallery-zoom-container")[0]);

		/* Remove the "full screen" control */
		var navControl = PhotoGallery.viewer.getNavControl();
		navControl.removeChild(navControl.lastChild);

		/* Create the close button */
		var closeButton = new Seadragon.Button("Close",
			PhotoGallery.imagePath + "/close_rest.png",
			PhotoGallery.imagePath + "/close_grouphover.png",
			PhotoGallery.imagePath + "/close_hover.png",
			PhotoGallery.imagePath + "/close_pressed.png", null,
			null, function() {
				PhotoGallery.hide();
			}, null, null);
		navControl.appendChild(closeButton.elmt);
	}
});

