var last_hash   = '';

function enlargePhoto( id ) {
    if( id == 'top' ) {
        return false; // nothing to do!
    }
    
    pContainer  = $('#enlargedPhotoContainer')[0];
    enlarged    = $('#enlargedPhoto')[0];
    comments    = $('#commentsContainer')[0];
    img         = $('#photo_'+id)[0];
    pcaption    = $('#caption_'+id)[0];
    
    // find the id of this photo, therefore finding the previous and next photos
    for( i=0; i < photo_ids.length; i++ ) {
        if( photo_ids[i] == id ) {
            if( i == 0 ) {
                i = photo_ids.length;
            }
            break;
        }
    }
    // prepare the ids
    id_prev = photo_ids[(i - 1) % photo_ids.length];
    id_next = photo_ids[(i + 1) % photo_ids.length];
    
    // fille up the div for enlarged photo with the HTML code
    comments.innerHTML = $('#comments_'+id)[0].innerHTML;
	enlarged.innerHTML = '<table width="800"><tr><td class="photo-albums-photo-current-caption-name">'+img.alt+' - '+pcaption.innerHTML+'</td><td class="photo-albums-photo-current-caption-nextpreviousclose"><a href="#" onclick="return enlargePhoto('+id_prev+')">&laquo; Previous</a>&nbsp;&nbsp;<a href="#" onclick="return enlargePhoto('+id_next+')">Next &raquo;</a> | <a href="#" onclick="return closePhoto()">Close window</a></td></tr></table><br /><img src="'+img.src+'" alt="'+img.alt+'" title="'+img.alt+'" style="margin-top:-10px; cursor: pointer;" onclick="return enlargePhoto('+id_next+')" />';
	
	// turn on the lights!
    pContainer.style.display    = 'block';
    
    // move the viewport to the very top, where the div will show up
    window.location.hash = '#';
    
    // plug the photo id into the URL, thus giving each photo a unique URL
    last_hash = window.location.hash = '#'+id;
    
    return false; // kill the click event
}

function closePhoto() {
    pContainer                  = $('#enlargedPhotoContainer')[0];
    pContainer.style.display    = 'none';
    last_hash = window.location.hash = '#top';
    
    return false; // kill the click event
}

function watchLocationHash() {
    if( last_hash != window.location.hash ) {
        last_hash   = window.location.hash;
        
        if( window.location.hash.length > 1 ) {
            enlargePhoto(window.location.hash.substring(1));
        } else {
            closePhoto();
        }
    }
    setTimeout('watchLocationHash()',500);
}

