if (Garmin == undefined) var Garmin = {};
if (Garmin.Foto == undefined) Garmin.Foto = {};


/** PanoramioPhotoAPI extends AbstractPhotoAPI to wrap Panoramio photo API.
 * @class Garmin.Photo.PanoramioPhotoAPI
 *
 * @constructor 
 * @param {map} GMap2 instance
 */
Garmin.Foto.PanoramioPhotoAPI = Class.create();
Garmin.Foto.PanoramioPhotoAPI.prototype = Object.extend(new Garmin.Foto.AbstractPhotoAPI(),	{

	initialize: function(map, infoWindowTemplate) {        
		this._init(map, infoWindowTemplate);
		this.lastQueryBounds = null;
 	},

	/** Construct the URL to call for the Paroramio API request 
	 * @param {String} photoSize can be original, medium (500, default value), small(240), thumbnail(small), square, mini_square
	 */
	constructQueryURL: function(reset, photoSize) {
	 	var _size = photoSize || "small";
	 	var _reset = reset || true;
	 	var mapBounds = (reset || !this.lastQueryBounds) ? this.caclulateQueryBounds() : this.lastQueryBounds;
		this.lastQueryBounds = mapBounds;
	 	var order = $F("panoramioQueryOrder");
	 	var set = $F("panoramioQuerySet");
	 	//var user = $F("panoramioUserID");
	 	var URL = "http://www.panoramio.com/map/get_panoramas.php?" + 
			"order=" + order + "&" + 	//popularity or upload_date
			"set=" + set + "&" +		//public or full
			"from=" + this.getStartPhoto() + "&" +
			"to=" + (this.getEndPhoto()+1) + "&" +
			"minx=" + mapBounds.getSouthWest().lng() + "&" +
			"miny=" + mapBounds.getSouthWest().lat() + "&" +
			"maxx=" + mapBounds.getNorthEast().lng() + "&" +
			"maxy=" + mapBounds.getNorthEast().lat() + "&" +
	 		//(user ? "user=" + user + "&" : "") +
			"callback=loader.storePhotos&" + 
			"size=" + _size;
	 	return encodeURI(URL); 
	},
	
	/** Construct Photo instances from JSON query response.
	 * @param {Object} json Photo-service-specific JSON query results.
	 */ 
	bindPhotos: function(json) {
		if (this.getStartPhoto()>=json.count) { //reset?
			this.startPhoto = 0; //Math.max(json.count - this.getPhotosPerPage(), 0);
		}
		var photos = [];
		for (var i = 0; i < json.photos.length; i++) {
			var id = json.photos[i].photo_id;
			var photo = this.map.loader.getStagedPhotos()[id]; //look in staged photos 1st
			if (!photo) {
				var photo = new Garmin.Foto.PanoramioPhoto(json.photos[i]);
			}
			photos.push(photo);
		}
		this.totalPhotos = json.count;
		return photos;	
	}
	
});

