(function() {

var G = window.Galleria;
if (typeof G == 'undefined') {
    return;
}

var F = G.Picasa = function(user,album,home_id) {
    if (!user) {
        G.raise('No API key found');
    }
	this.callback = function(){};
	this.user = user;
        this.album = album;
	this.options = {
		max: 40,
                home_id: home_id
		//use_original: false,
		//use_original: true,
		//sort: 'interestingness-desc'
	}
}

F.prototype = {
	search: function(str) {
		this._set(arguments);
		return this._find({
			text: str
		});
	},
	getTags: function(str) {
		this._set(arguments);
		return this._find({
			tags: str
		});
	},
	getUser: function(username) {
		var args = arguments;
		return this._call({
			method: 'flickr.urls.lookupUser',
			url: 'flickr.com/photos/'+username
		}, function(data) {
			this._set(args);
			this._find({
				user_id: data.user.id,
				method: 'flickr.people.getPublicPhotos'
			});
		});
	},
	getSet: function(photoset_id) {
		this._set(arguments);
		return this._find({
			photoset_id: photoset_id,
			method: 'flickr.photosets.getPhotos'
		});
	},
	getGallery: function(gallery_id, thumbpicasa) {
		this._set(arguments);
		return this._find({
			gallery_id: gallery_id,
			method: 'getGallery',
                        thumb_picasa: thumbpicasa
		});
	},
	setOptions: function(options) {
		jQuery.extend(this.options, options);
		return this;
	},
	_set: function(args) {
		args = Array.prototype.slice.call(args);
		this.callback = args[2] || args[1];
		if (typeof args[1] == 'object') {
			this.setOptions(args[1]);
		}
		return this;
	},
	_call: function(params, callback) {
		var url = 'http://picasaweb.google.com/data/feed/api/user/' + this.user + '/albumid/' + this.album+'?';
		var scope = this;
		params = jQuery.extend({
			format : 'json',
			callback : '?'

		}, params);
		jQuery.each(params, function(key, value) {
			url += '&'+ key + '=' +value;
		});
		jQuery.getJSON(url, function(data) {


			if (data.encoding) {
				callback.call(scope, data);
			} else {
                                 //Gestione errore
				//G.raise(data.code.toString() + ' ' + data.stat + ': ' + data.message);
			}
		});
		return scope;
	},
	_find: function(params) {
            //alert(this.options.data_type)
		params = jQuery.extend({
	            kind: 'photo',
                    alt: 'json-in-script',
		    //extras: 'original_format, o_dims, url_t, url_m, url_l',
                    access: 'public',
		    sort: this.options.sort,
			user_id: this.options.user_id
		}, params);

		return this._call(params, function(data) {
			var obj = { length: 0 };


                        if (params.gallery_id) {
							//alert(this.options.home_id+' '+params.gallery_id)
							if (this.options.home_id == this.album) {
								$('#' + params.gallery_id).html( data.feed.subtitle.$t)
							}
							else {
								$('#' + params.gallery_id).html('<h1>' + data.feed.title.$t + '</h1>' + data.feed.subtitle.$t)
							}
						}
			var photos = data.feed.entry ? data.feed.entry : data.photoset.photo;
			var len = Math.min(this.options.max, photos.length);
			for (var i=0; i<len; i++) {
				var photo = photos[i].content.src;
				var splitted = photo.split('/');

	
var url = splitted[0] + '/' + splitted[1] + '/' + splitted[2] + '/' + splitted[3] + '/' + splitted[4] + '/' + splitted[5] + '/' + splitted[6] + '/s0/' + splitted[7];
				
				var item = {
					thumb: photos[i].media$group.media$thumbnail[0].url,
					image: url,
					title: photos[i].summary.$t
				};
				Array.prototype.push.call(obj, item);
			}
			this.callback.call(this, obj);
		});
	}
}


// Static
F.getFeed = function(type, params) {

}

})();
