/*extern dojo, photoData, currentPhotoNum */

/**
 * Shows the photo dialog
 */
function showPhoto(photoNum)
{
	var d = document;
	
	// photo data
	var photo = photoData[photoNum];
	currentPhotoNum = photoNum;
	
	// create a new image element, with width/height
	// this seems to display better than reusing an existing element
	// by just replacing the src attribute
	var photoImg = new Image();
	photoImg.width = photo.width;
	photoImg.height = photo.height;
	photoImg.src = photo.src;
	
	try {
		
		// destroy an existing image
		var photoLink = d.getElementById('id_photo_dialog_link');
		photoLink.innerHTML = '';
		photoLink.appendChild(photoImg);
	
		// resize the frame
		var photoFrame = d.getElementById('id_photo_frame');
		photoFrame.style.width = photo.width + 'px';
	
		// caption
		d.getElementById('id_photo_dialog_caption').innerHTML = photo.caption;
	
		// copyright
		var photoCopyright = d.getElementById('id_photo_dialog_copyright');
	
		if (photo.copyright_link === '') {
			photoCopyright.innerHTML = photo.copyright;
		}
		else {
			photoCopyright.innerHTML = '';
			var copyrightLink = d.createElement('a');
			if (photo.copyright_link.indexOf('http://') === -1)
			{
		   		copyrightLink.href = 'http://' + photo.copyright_link;
			}
			else
			{
		   		copyrightLink.href = photo.copyright_link;
			}
			copyrightLink.setAttribute('target','_blank');
			copyrightLink.innerHTML = photo.copyright;
			photoCopyright.appendChild(copyrightLink);
		}
	
	
		// show/hide prev/next arrows
		if (photoNum > 0) {
			dojo.byId('id_prev_photo').style.display = '';
		}
		else {
			dojo.byId('id_prev_photo').style.display = 'none';
		}
		if (photoNum < photoData.length - 1) {
			d.getElementById('id_next_photo').style.display = '';
		}
		else {
			d.getElementById('id_next_photo').style.display = 'none';
		}
	
		// show the widget
		// prevent errors if the widget is not yet initialized
		var dialogWidget = dijit.byId('id_photo_dialog');
		dialogWidget.show();
	}
	catch (err) {
		var txt = "There was an error on this page.\n\n";
  		txt += "Error description: " + err.description + "\n\n";
  		txt += "Click OK to continue.\n\n";
  		//alert(txt);
	}
}

/**
 * Hides the photo dialog
 */
function hidePhoto()
{
	dojo.widget.byId('id_photo_dialog').hide();
}

/**
 * Shows the previous photo
 */
function showPreviousPhoto()
{
	if (currentPhotoNum > 0)
	{
		showPhoto(currentPhotoNum-1);
	}
}

/**
 * Shows the next photo
 */
function showNextPhoto()
{
	if (currentPhotoNum < photoData.length - 1)
	{
		showPhoto(currentPhotoNum+1);
	}
}
