/**
 *	This script is free to use. It has been created by http://www.aplusmedia.de and
 *	can be downloaded on http://www.esteak.net.
 *	License: GNU GPL 2.0: http://creativecommons.org/licenses/GPL/2.0/
 *	Example on: http://blog.aplusmedia.de/moo-gallery/flickr.php
 *
 * 	Uses FlickrAPI script by Neil Jenkins - http://www.nmjenkins.com
 */
var JSGallery_flickr = JSGallery.extend({
	/**
	 * Extension to JSGallery class. It is able to display flickr images as gallery.
	 * 
	 * @param {String} APIKey your flickr API key
	 * @param {Object} flickrOptions a flickr API option object
	 * 					Example: {method: 'flickr.photos.search', text: 'dresden hdr', per_page: 10}
	 * 					More information: http://www.flickr.com/services/api/
	 * @param {HTMLElement} bigImageContainer same like in JSGallery
	 * @param {HTMLElement} pageContainer same like in JSGallery
	 * @param {Integer} imagesPerPage how many images to display on one page
	 * @param {Object} options all JSGallery options
	 */
	initialize: function(APIKey, flickrOptions, bigImageContainer, pageContainer, imagesPerPage, options) {
		this.currentPageNumber = 0;
		this.loadedImages = 0;
		this.blockKeys = false;
		this.thumbs = [];
		this.imagesPerPage = imagesPerPage;
		this.setOptions(options);
		this.bigImage = bigImageContainer;
		this.pageContainer = pageContainer.empty();
		FlickrAPI.initialise(APIKey);
		FlickrAPI.get(flickrOptions, this.flickrCallback.bind(this));
		this.createControls();
		if($defined(this.options.loadingImage)) {
			new Asset.image('image/loading.gif');
		}
	},
	/**
	 * Internal callback for flickr data.
	 * @param {Object} data the JSON object
	 */
	flickrCallback: function(data) {
		//add the images
		data.photos.photo.each(function(photo) {
			var path = 'http://farm1.static.flickr.com/' + photo.server + '/' + photo.id + '_' + photo.secret;
			this.addImage(path + '_s.jpg', path + '.jpg');
		}, this);
		//init preloading
		this.loadNextImage();
		//select first image
		if(this.options.initialIndex != -1) {
			this.selectByIndex(this.options.initialIndex);
		} else {
			this.gotoPage(0);
		}
	}
});
