/*
    This file is part of JonDesign's SmoothGallery v2.0.

    JonDesign's SmoothGallery is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    JonDesign's SmoothGallery is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with JonDesign's SmoothGallery; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Main Developer: Jonathan Schemoul (JonDesign: http://www.jondesign.net/)
	Many thanks to:
	- Tomocchino for his great help and flickr class
*/

var FlickrGallerySet = gallerySet.extend({
	initialize: function(element, options) {
		this.setOptions({
			username: false,
			user_id: false,
			api_key: '',
			defaultTransition: "fade",
			mode: 'sets',
			textGallerySelector: "{0}'s Flickr sets"
		}, options);
		this.gallerySet = [];
		this.options.textGallerySelector = formatString(
												this.options.textGallerySelector,
												this.options.username
											);
		Flickr.api_key = this.options.api_key;
		if ((!this.options.user_id) && this.options.username)
			this.options.user_id = Flickr.getUserId(this.options.username)
		if (this.options.mode=='tags')
			this.checkOutTagsData();
		else
			this.checkOutData();
		this.options.manualSetData = this.gallerySet;
		this.parent(element, this.options);
	},
	checkOutTagsData: function() {
		response = Flickr.getPopularTags({
			user_id: this.options.user_id
		});
		tags = response.who.tags.tag;
		tags.each(function(tag) {
			galleryDict = {
				title: tag._content,
				elements: []
			}
			photos = Flickr.searchPhotos({
				user_id: this.options.user_id,
				tags: tag._content
			}).photos.photo;
			currentArrayPlace = 0;
			photos.each(function(photo){
				sizes = Flickr.getPhotoSizes(photo.id);
				galleryDict.elements.extend([{
					image: sizes.Medium,
					number: currentArrayPlace,
					transition: this.options.defaultTransition,
					title: photo.title,
					description: '',
					thumbnail: sizes.Thumbnail
				}]);
				currentArrayPlace++;
			}, this);
            this.gallerySet.extend([galleryDict]);
        }, this);
	},
	checkOutData: function() {
		response = Flickr.getPhotosets({
			user_id: this.options.user_id
		});
		photosets = response.photosets.photoset;
		photosets.each(function(photoset) {
			galleryDict = {
				title: photoset.title._content,
				elements: []
			}
			photos = Flickr.getPhotosFromSet({
				photoset_id: photoset.id
			}).photoset.photo;
			currentArrayPlace = 0;
			photos.each(function(photo){
				sizes = Flickr.getPhotoSizes(photo.id);
				galleryDict.elements.extend([{
					image: sizes.Medium,
					number: currentArrayPlace,
					transition: this.options.defaultTransition,
					title: photo.title,
					description: '',
					thumbnail: sizes.Thumbnail
				}]);
				currentArrayPlace++;
			}, this);
            this.gallerySet.extend([galleryDict]);
        }, this);
	}
});


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Follows: Flickr (class)
 * MooTools Based Flickr JSON Library
 * Inspired by Tomocchino's work
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

var Flickr = new Abstract({
    request: function(method, params){
		var myXHR = new XHR({
			async: false,
            method: 'get'
        });
        var defaults = { format: 'json', method: method, nojsoncallback: '1', api_key: this.api_key };
        var request = $H({});
        $H($extend(defaults, params || {})).each(function(value, key){
            request.set(key, value)
        });
        
        myXHR.send("proxy.php?yws_path=" + encodeURIComponent("services/rest/?" + Object.toQueryString(request.obj)));
		return Json.evaluate(myXHR.response.text);
    },
    
    getPhotosets: function(params){
        return this.request('flickr.photosets.getList', params);
    },
	
	getPopularTags: function(params){
        return this.request('flickr.tags.getListUserPopular', params);
    },
	
	searchPhotos: function(params){
        return this.request('flickr.photos.search', params);
    },
	
	getPhotosFromSet: function(params){
        return this.request('flickr.photosets.getPhotos', params);
    },
	
	getPhotoSizes: function(photoId){
        sizes = this.request('flickr.photos.getSizes', {photo_id: photoId}).sizes.size;
		sizesDict = {};
		sizes.each(function(size){
			sizesDict[size.label] = size.source;
		}, this);
		return sizesDict;
    },
	
	getUserId: function(user){
        return this.request('flickr.people.findByUsername', {username: user}).user.nsid;
    }
});
