/* Flickr photo search */

FLICKRKEY = "ad8a7202fdb53ac4dfec8235d8fbe185";

DATAHOLDER = "";

function buildModal(iSrc, iId) {
	//Grab and clear modal
	var modalContainer = document.getElementById("imageModal");
	modalContainer.innerHTML = "";
	//Create image element
	var modalImage = document.createElement("img");
	modalImage.src = iSrc;
	modalImage.id = iId;
	modalImage.style.width = "380px";
	//Create copy and past box
	var modalCP = document.createElement("input");
	modalCP.type = "text";
	modalCP.value = iSrc;
	modalCP.style.width = "340px";
	modalCP.style.marginTop = "8px";
	//append image and link
	modalContainer.appendChild(modalImage);
	modalContainer.appendChild(modalCP);
	//Check if the modal has already been initialized
	if ($("#imageModal").dialog({ width: 420, modal: true })) {
		$("#imageModal").dialog('open');
	} else {
		$("#imageModal").dialog({ width: 420, modal: true });
	}
}

function createFlickerURL() {
	//constructs the flickr request URL
	var URL = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=" 
			  + FLICKRKEY + 
			  "&text=" 
			  + document.getElementById("flickrInput").value + 
			  "&per_page="
			  + 60 +
			  "&format=json";
	return encodeURI(URL);
}

function getSearchResults() {
	//create the api request
    var flickrRequestURL = createFlickerURL();
    // grab and clear container content...
	var flickr_holder = document.getElementById('flikrHolder');	
	flickr_holder.innerHTML = "";
	//display the spinner
	$('#spinner').toggle();
    // do the api request
    var script = document.createElement('script');
    script.src = flickrRequestURL;
    script.type = 'text/javascript';
    var head = document.getElementsByTagName('head').item(0);
    head.appendChild(script);
    return false;
}

/* * If a sucessful API response is received, display search data. If not, display an error. */ 
 function jsonFlickrApi(rsp) { 
	// Saves the data in a global variable for later use
	DATAHOLDER = rsp;
        //test to see if the api request worked
		if (rsp.stat != "ok"){
				alert(rsp.message);
			return;
		}
        //if we got a response, loop through the returned json object and pluck out the image links
		for (var i=0; i<rsp.photos.photo.length; i++){
			var photo = rsp.photos.photo[i];
			//create image tag and src link for the thumbnails
			var innerGoodStuff = document.createElement('img');
			innerGoodStuff.src = "http://farm" + photo.farm + ".static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + ".jpg";
			innerGoodStuff.id = photo.id + "_s";
			
			var clickerlink = document.createElement('a');
			clickerlink.href="javascript:void(buildModal('" + innerGoodStuff.src + "', '" + innerGoodStuff.id + "'));";
			//Add modal links to DOM then append Images inside
			var flickr_holder = document.getElementById("flikrHolder");
			flickr_holder.appendChild(clickerlink);
			clickerlink.appendChild(innerGoodStuff);
			
		}
	$('#spinner').toggle();
	//set image sizes to 45%
	$("img").css("width", "45%");
	
}
