var api_key = '063219437acd8b8cef45b7e0c46c6716';
var data;
var current;
var source_type;
var source_id;
var set_id;



function requestUserID(source_id) {
		var flickr_api_path = "http://api.flickr.com/services/rest/?format=json&api_key=" + api_key;
		flickr_api_path += "&jsoncallback=handleJSONUsernameResponse&method=flickr.people.findByUsername&username=" + source_id;
		bObjUser = new JSONscriptRequest(flickr_api_path); 
		bObjUser.buildScriptTag(); 
		bObjUser.addScriptTag();
}

function requestPhotoData(source_id) {
		var flickr_api_path = "http://api.flickr.com/services/rest/?format=json&api_key=" + api_key;
		flickr_api_path += "&jsoncallback=handleJSONUserResponse&method=flickr.people.getPublicPhotos&user_id=" + source_id;
		bObjPhotos = new JSONscriptRequest(flickr_api_path); 
		bObjPhotos.buildScriptTag(); 
		bObjPhotos.addScriptTag();
}
	
function handleJSONUsernameResponse(rsp) {
		if (rsp.stat.toLowerCase() == "fail") {
			alert("We were not able to find your Flickr account.  Sorry Please check your Username and try again.");
			return;
		}
		//alert(rsp.user.nsid);
		SeasonCards.cardItems.flickrID = rsp.user.nsid;
		requestPhotoData(SeasonCards.cardItems.flickrID);
		bObjUser.removeScriptTag();
	}

function handleJSONUserResponse(rsp) {
		if (rsp.stat.toLowerCase() == "fail") {
			alert("We were able to retrieve your account id but were not able to find any photos in your Flickr account.");
			return;
		}
		//alert(rsp.photos.photo.id);
		SeasonCards.cardItems.FlickrPhotoArray = rsp.photos.photo;
	    initPhotos();
		bObjPhotos.removeScriptTag();
	}

function getPhotoIndexById(id) {
		var i = this.data.length;
		while (i--) {
			if (this.data[i].id == id) return i;
		}
		return -1;
	}
	
function getPhotoId(i, photoArray) {
		return photoArray[i].id;
	}
	
function getPhotoTitle(i, photoArray) {
		return photoArray[i].title;
	}
	
function getPhotoUrl(i, photoArray) {
		var photo = SeasonCards.cardItems.FlickrPhotoArray[i];
		var server = photo.server;
		var id = photo.id;
		var secret = photo.secret;
		var farm = "farm"+photo.farm;
		var s = (this.image_size == "") ? "" : "_" + this.image_size;
		var url = "http://"+farm+".static.flickr.com/" + server + "/" + id + "_" + secret + ".jpg";
		return url;
	}
	
function getFlickrUrl(i, photoArray) {
		var photo = photoArray[i];
		var owner = this.set_owner == null ? photo.owner : this.set_owner;
		var id = photo.id;
		var url = "http://www.flickr.com/photos/" + owner + "/" + id;
		return url;
	}