/* Author: Simon Gilhooly, Atom14 Ltd; */

var uk = {
	co: {
		atom14: {
			flickrUserId: null,
			flickrApiKey: null,

			getFlickrPhotoSets: function () {
				// this function will retreive all photosets from the given Flickr account
				// call Flickr API specifying a callback function to process the results;
				if ((this.flickrUserId != null) && (this.flickrApiKey != null)) {
					var script = document.createElement("script");
					script.type = "text/javascript";
					script.src = "http://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=" + this.flickrApiKey + "&format=json&jsoncallback=atom14.processPhotoSets&user_id=" + this.flickrUserId;
					
					//add this newly created script element to the head section of our document, where it will immediately be executed
					document.getElementsByTagName("head")[0].appendChild(script);
				} else {
					alert ("Need to set flickrUserId and flickrApiKey properties before calling this method.");
				}
			},
			getFlickrPhotoSizes: function (photoId) {
				// this function will retrieve the various size options of the given photo
				if (this.flickrApiKey != null) {
					var script = document.createElement("script");
					script.type = "text/javascript";
					script.src = "http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=" + this.flickrApiKey + "&format=json&jsoncallback=atom14.processPhotoSizes&photo_id=" + photoId;
				
					document.getElementsByTagName("head")[0].appendChild(script);
				} else {
					alert ("Need to set flickrApiKey property before calling this method.")
				}
			},
			processPhotoSets: function (jsonData) {
				var outputHTML = "";
				
				if (jsonData.stat == "ok"){
				// if we got some good data - run through each photoset in turn
					for (var i = 0; i < jsonData.photosets.photoset.length; i++){
						var photoSetId = jsonData.photosets.photoset[i].id;
						var photoSetDesc = jsonData.photosets.photoset[i].description._content;
						var primaryPhotoId = jsonData.photosets.photoset[i].primary
						var photoSetURL = "http://www.flickr.com/photos/" + this.flickrUserId + "/sets/" + photoSetId + "/"
						var photoSetTitle = jsonData.photosets.photoset[i].title._content;
						var photoSetCount = jsonData.photosets.photoset[i].photos;
			
					// create a new div element for this photoset, add a class and id
						var photoSet = document.createElement("div");
						photoSet.setAttribute("class", "photoSet");
						photoSet.setAttribute("id", photoSetId);
						document.getElementById("photoGallery").appendChild(photoSet);
			
					// add the content of the photoset div
						outputHTML = "<h2>" + photoSetTitle + "</h2><a href='" + photoSetURL + "'>" + "<img id=" + primaryPhotoId + " alt='" + photoSetDesc + "' /></a><p>" + photoSetDesc + "</p><p> This <a href='http://www.flickr.com/photos/29678295@N07/'>Flickr</a> photoset contains <a href='" + photoSetURL + "'>" + photoSetCount + " photos</a>.</p><p class='clear'></p>";
						document.getElementById(photoSetId).innerHTML = outputHTML;
			
					// make a call to get the URL for the thumbnail version of primary photo of this photoset
						atom14.getFlickrPhotoSizes(primaryPhotoId);
					}
				} else {
					alert ("We didn't get our jsonData properly");
				}
			},
			processPhotoSizes: function(jsonData) {
				if (jsonData.stat == "ok"){
					for (var i = 0; i < jsonData.sizes.size.length; i++) {
						if (jsonData.sizes.size[i].label == "Thumbnail"){
							var photoSource = jsonData.sizes.size[i].source
							var photoId = photoSource.slice(photoSource.lastIndexOf("/") + 1, photoSource.indexOf("_"));
							document.getElementById(photoId).setAttribute("src", photoSource);
						}
					}
				} else {
					alert ("We didn't get our jsonData properly");
				}
			}
		}
	}
}


