var photo_store = new Array();
var photo_store_index = 0;
 
/**
 * Construct the URL for a photo.
 *
 * @param photo a photo object as returned by the JSON Flickr response
 */
function flickr_photo_url(photo) {
    return "http://farm" + photo.farm + ".static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + "_m.jpg";
}
 
/**
 * Construct the URL for a photo page (the HTML page corresponding to the photo).
 *
 * @param photo a photo object as returned by the JSON Flickr response
 */
function flickr_photo_page_url(photo) {
    return "http://www.flickr.com/photos/" + photo.owner + "/" + photo.id;
}
 
/**
 * Flickr JSON callback function, called when the Flickr response arrived.
 *
 * @param response the response object as returned by the Flickr API
 */
function jsonFlickrApi(response) {
    var photos = response.photos;
    for (i = 0; i < photos.photo.length; i++) {
	p = {};
	p.url = flickr_photo_url(photos.photo[i]);
	p.page_url = flickr_photo_page_url(photos.photo[i]);
	p.title = photos.photo[i].title;
 
	photo_store[photo_store.length] = p;
    }
}
 
/**
 * Load another image.
 * 
 * @param targetImage the target image (id)
 * @param targetLink the link corresponding for the target image (id)
 * @param statusHolder the status holder (id)
 */
function load_image(targetImage, targetLink, statusHolder) {
    if (photo_store.length > 0) {
	if (photo_store_index < photo_store.length) {
	    var selected_photo = photo_store[photo_store_index++];
	    photo_store = photo_store.without(selected_photo);
 
	    $(targetImage).src = selected_photo.url;
/*	    $(targetImage).title = selected_photo.title;
	    $(targetImage).style.display = "inline";
 */
	    if ($(targetLink)) {
		$(targetLink).href = selected_photo.page_url;
	    }
 
	    if ($(statusHolder)) {
		$(statusHolder).innerHTML = selected_photo.title + " (" + photo_store_index + "/" + photo_store.length + ")";
	    }
	}
	else {
	    photo_store_index = 0;
	}
    }
}