/// nearbyPhotos.js
/// Written by Dan Capshaw


////////////////////////////////////////////////////  Square Photos           
function nearbyPhotos (latitude, longitude){
	
	jQuery("#local_photos_title").html("<h3>Photos Near You</h3>");
	
	jQuery("#nearby_photos").html("<div></div>");
	
		params = new Array();
		
		params = {
			'api_key'	: '9bc4f99565b964aad18a7fa4749f9441',
			'method'	: 'flickr.photos.search',
			'format'	: 'json',
			'jsoncallback' : 'nearbyPhotosCallback',
			'machine_tag_mode'	: 'any',
			'has_geo'	: '1',
			'lat'		: latitude,
			'lon'		: longitude,
			'radius'	: '32',
			'sort'		: 'interestingness-desc',	
			'per_page'	: '13',
			'page'		: '1',
			'extras'	: 	'date_taken, url_m, path_alias, url_sq'};
		
		encoded_params = new Array();
		
		
		for (key in params){
		
			encoded_params += encodeURIComponent(key)+'='+ encodeURIComponent(params[key])+'&';
		}
		
		encoded_params = encoded_params.replace(/&$/,'');
					
		//
		// call the API and decode the response
		//
		
		var url = "http://api.flickr.com/services/rest/?" + encoded_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);
				}
			});
		    
		
}      
function dansPhotos(){
	params = new Array();
	jQuery("#banner_photos").html("<div></div>");
	jQuery("#dans_photos_title").html("<h3>My Photos</h3>");
	
	params = {
		'api_key'	: '9bc4f99565b964aad18a7fa4749f9441',
		'method'	: 'flickr.photos.search',
		'format'	: 'json',
		'jsoncallback': 'dansPhotosCallback',
		'machine_tag_mode'	: 'any',
		'user_id'	: '22987924@N06',
		'sort'		: 'interestingness-desc',	
		'per_page'	: '13',
		'page'		: '1',
		'extras'	: 	'date_taken, url_m, path_alias, url_sq'};
	
	encoded_params = new Array();
	
	
	for (key in params){
	
		encoded_params += encodeURIComponent(key)+'='+ encodeURIComponent(params[key])+'&';
	}
	
	encoded_params = encoded_params.replace(/&$/,'');
				
	//
	// call the API and decode the response
	//
	
	var url = "http://api.flickr.com/services/rest/?" + encoded_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);
			}
		});
	    
	
}      

function nearbyPhotosCallback(data) {
    if (data.photos.photo.length > 0){
    //	jQuery("#local_photos").html("<h3>Nearby Photos</h3>");
    //	jQuery("#banner_photos").html("<div></div>");
    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 + '/';
    	jQuery("#nearby_photos").append('<a href="' + photoReference + '"><img class="square_flickr" src="' + url + '" alt="'+ title + '" title="' + title + '" /></a>');
        }
    }
    else { alert("Sorry, no photos available from Flickr.");
    }
       } 

function dansPhotosCallback(data) {
    if (data.photos.photo.length > 0){
    //	jQuery("#local_photos").html("<h3>Nearby Photos</h3>");
    //	jQuery("#banner_photos").html("<div></div>");
    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 + '/';
    	jQuery("#banner_photos").append('<a href="' + photoReference + '"><img class="square_flickr" src="' + url + '" alt="'+ title + '" title="' + title + '" /></a>');
        }
    }
    else { alert("Sorry, no photos available from Flickr.");
    }
       } 

function load()
{
	var loki = LokiAPI();
	loki.onSuccess = function(location) {
	alert("You appear to be located at:  Longitude = " +location.longitude + ", Latitude = " + location.latitude + ".  Brought to you by Loki.com.");
	dansPhotos();
	nearbyPhotos(location.latitude, location.longitude);
}
	loki.onFailure = function(error) {

		switch(error){
		case 1: alert('error: Scanner not found');
			break;
		case 2: alert('error: Wi-Fi not available.  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) {
			alert("You appear to be located near:  Longitude = " +location.longitude + ", Latitude = " + location.latitude + ".  Brought to you by Loki.com.");
			dansPhotos();
			nearbyPhotos(location.latitude, location.longitude);
		}
				loki.setKey('runtrails.com/mymountaintrail');
				loki.requestIPLocation(true,loki.NO_STREET_ADDRESS_LOOKUP);
			break;
		case 3: alert('error: 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) {
					alert("You appear to be located near:  Longitude = " +location.longitude + ", Latitude = " + location.latitude + ".  Brought to you by Loki.com.");
					dansPhotos();
					nearbyPhotos(location.latitude, location.longitude);
				}
				loki.setKey('runtrails.com/mymountaintrail');
				loki.requestIPLocation(true,loki.NO_STREET_ADDRESS_LOOKUP);
			break;
		case 4:  alert('error: Invalid Application Key.  Please notify webmaster');
			break;
		case 5:  altert('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('runtrails.com/mymountaintrail');
loki.requestLocation(true,loki.NO_STREET_ADDRESS_LOOKUP);
}

function getLocalPhotos(){
		jQuery("#local_photos_title").html("<p>This may take a moment</p>");
    	jQuery("#banner_photos").html("<div></div>");
    	load();
    	}
