/// nearbyPhotosWidget.js
/// Written by: Dan Capshaw
/// URL: http://runtrails.com/mymountaintrail/nearby-photos-widget-for-wordpress/ 
/// Copyright 2010 Dan Capshaw
//
//    This file is part of the Nearby Photos Widget.
//
//    The Nearby Photos Widget 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.
//
//    The Nearby Photos Widget 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 The Nearby Photos Widget.  If not, see <http://www.gnu.org/licenses/>.

if (typeof Object.create !== 'function'){
	Object.create = function (o) {
		var F = function() {};
		F.prototype = o;
		return new F();
	};
}


var nearbyPhotosWidget = {

nearbyPhotos: function (latitude, longitude, photonumber, radius, start_date){
	var pname = "p#" + defaultPhotoswidget_id + "-lonlat";
	//jQuery(pname).html('<p id="'+ pname + '">Longitude: ' + longitude + ', Latitude: ' + latitude + '</p>');
	jQuery(pname).html('Longitude: ' + longitude + ', Latitude: ' + latitude);
	
	var idname = "#" + defaultPhotoswidget_id + "-div";
	//alert(idname);
	jQuery(idname).empty();
	
		params = new Array();
		params = {
			'api_key'	: defaultPhotosflickr_key,
			'method'	: 'flickr.photos.search',
			'format'	: 'json',
			'jsoncallback' : 'nearbyPhotosCallback',
			'machine_tag_mode'	: 'any',
			'has_geo'	: '1',
			'lat'		: latitude,
			'lon'		: longitude,
			'radius'	: radius,
			'sort'		: 'interestingness-desc',	
			'per_page'	: photonumber,
			'page'		: '1',
			'min_taken_date' : start_date,
			'safe_search' : '1',
			'extras'	: 	'date_taken, url_m, path_alias, url_sq'};
		//
		// call the API and decode the response
		//
		var url = "http://api.flickr.com/services/rest/?" + this.photoParamEncode(params);

		jQuery(document).ready(function(){
					try {
				    var elem = document.createElement("script");
				    elem.src = url;
				    document.body.appendChild(elem);
				}
				catch (e) {
				    alert('error in get filckr data: ' + e);
				}
			});
},  

photoParamEncode: function (params){	
	
	encoded_params = new Array();
			
	for (key in params){
	
		encoded_params += encodeURIComponent(key)+'='+ encodeURIComponent(params[key])+'&';
	}
	
	encoded_params = encoded_params.replace(/&$/,'');
	
	return encoded_params;
},

getLocalPhotos: function (photonumber, radius, start_date){

	var loki = LokiAPI();
	var that = this;
	loki.onSuccess = function(location) {
	
	that.nearbyPhotos(location.latitude, location.longitude, photonumber, radius, start_date);
}
	loki.onFailure = function(error) {

		switch(error){
		case 1: alert('error: Scanner not found');
			break;
		case 2: alert('Warning: Wi-Fi is not available on your system.  Using your ISP location as a backup.  The pictures you see may not be quite as close as they would have been if you had wi-fi in the area since this method is less precise for YOUR location.');
				
		loki.onSuccess = function(location) {
				that.nearbyPhotos(location.latitude, location.longitude, photonumber, radius, start_date);
		}
				loki.setKey(defaultPhotosloki_key);
				loki.requestIPLocation(true,loki.NO_STREET_ADDRESS_LOOKUP);
			break;
		case 3: alert('Warning: No Wi-Fi in range.  Using your ISP location as a backup.  The pictures you see may not be quite as close as they would have been if you had wi-fi in the area since this method is less precise for YOUR location.');
				loki.onSuccess = function(location) {
						that.nearbyPhotos(location.latitude, location.longitude, photonumber, radius, start_date);
				}
				loki.setKey(defaultPhotosloki_key);
				loki.requestIPLocation(true,loki.NO_STREET_ADDRESS_LOOKUP);
			break;
		case 4:  alert('Error: Invalid Application Key.  Please notify webmaster');
			break;
		case 5:  alert('Error:  Location server is unavailable');
			break;
		case 6: alert('Error:  Location cannot be determined');
			break;
		case 7: alert('Error:  Proxy is unauthorized.  Please notify webmaster');
			break;
		case 8: alert('Error:  File I/O error.  Please notify webmaster');
			break;
		case 9: alert('Error:  Invalid file format.  Please notify webmaster');
			break;
		case 10:  alert('Error 10.  Please notify webmaster');
			break;
		case 1000:  alert('Error: Plugin could not be installed.');
			break;
		case 1001: alert('Error: User denied location request');
			break;
		default:  alert('System error.  Please notify webmaster');
			break;
		}
	}

loki.setKey(defaultPhotosloki_key);
loki.requestLocation(true,loki.NO_STREET_ADDRESS_LOOKUP);
}
};

var nearbyPhotosCallback = function(data) {
	if (data.stat == 'fail') {
		alert('Unable to access the Flickr database.  \n\nFlickr message: '+ data.message);
	}
    if (data.photos.photo.length > 0){
    
	    for (i = 0; i < data.photos.photo.length; ++i) {
			var url=data.photos.photo[i].url_sq;
	        var title=data.photos.photo[i].title;
	        var owner=data.photos.photo[i].owner;
	        var id=data.photos.photo[i].id;
	        var photoReference= 'http://www.flickr.com/photos/'+owner + '/' + id + '/';
	        var idname = "#" + defaultPhotoswidget_id + "-div";
	    	jQuery(idname).append('<a target="_blank" href="' + photoReference + '"><img class="square_flickr" src="' + url + '" alt="'+ title + '" title="' + title + '" /></a>');
	        }
	    }
    else { alert("Unfortunately there are no photos available from Flickr in your area within the specified radius and dates");
    }
}
