function prepareFlickrPoem(xmlPoem, stanzaId, status) {
	
	//List of prepositions, conjunctions etc...
	replaceWords = new Array('', 'the', 'an', 'a', 'for', 'and', 'nor', 'but', 'or', 'yet', 'so', 'after', 'although', 'as', 'if', 'though', 'because', 'before', 'even', 'if', 'only', 'in', 'now', 'once', 'rather', 'since', 'that', 'than', 'though', 'till', 'unless', 'until', 'when', 'whenever', 'where', 'whereas', 'wherever', 'while', 'also', 'both', 'not', 'either', 'neither ', 'whether', 'at', 'on', 'however', 'by', 'my', 'some', 'over', 'with', 'i', 'he', 'she', 'it', 'they', 'you', 'we', 'they', 'them', 'theirs', 'themselves', 'us', 'ours', 'ourselves', 'him', 'her', 'it', 'his', 'hers', 'its', 'himself', 'herself', 'itself', 'your', 'yourself', 'me', 'myself', 'of', 'thy', 'is');
	//replaceWords = new Array();
	//Number words replaced
	var replaceCount = 0;
	
	//Running total of words
	var totalLineWords = 0;
	var subtotalWords = 0;
	
	//Create poem holder
	var poemHolder = document.createElement("div");
	poemHolder.id = 'poemHolder';
	
	//Create stanza holder
	var newStanzaHolder = document.createElement("div");
	newStanzaHolder.id = 'stanza'+stanzaId;
	newStanzaHolder.className = "stanza";
	poemHolder.appendChild(newStanzaHolder);
	
	//Append poem holder
	var digitalPoem = document.getElementById("digipoem");
	digitalPoem.appendChild(poemHolder);

	if (xmlPoem.getElementsByTagName("author")[0]) {
		var author = xmlPoem.getElementsByTagName("author")[0].childNodes[0].nodeValue;
	}
	else {
		var author = "Anonymous";
	}
	
	if (xmlPoem.getElementsByTagName("title")[0]) {
		var title = xmlPoem.getElementsByTagName("title")[0].childNodes[0].nodeValue;
	}
	else {
		var title = "Untitled";
	}
	
	//Progress: 1-Start, 2-End, 4-NoStanza
	if (status == 1){
		var poemHeading = document.getElementById("poemheading");

		if (document.getElementById('poemtitle')) {
			var oldtitleHolder = document.getElementById('poemtitle');
			poemHeading.removeChild(oldtitleHolder);
		}
		
		var titleHolder = document.createElement('span');
		titleHolder.setAttribute("id", "poemtitle");
		var titleNode = document.createTextNode(title + " - " + author);
		titleHolder.appendChild(titleNode);
		poemHeading.appendChild(titleHolder);
	}

	//Get stanza(s)
	if (status == 4){
		stanzaContent = xmlPoem;
		var stanzaArray = xmlPoem.getElementsByTagName("poem");
	}
	else {
		var stanzaArray=xmlPoem.getElementsByTagName("stanza");
		stanzaContent=stanzaArray[stanzaId];
		if (stanzaContent == null){
			stanzaContent.removeChild(stanzaArray[stanzaId]);
		}
	}
	
	var stanzalinearray=stanzaContent.getElementsByTagName("line");
	var totalLines = stanzalinearray.length;
	
	//For each line  
	for (z=0;z<totalLines;z++) {
		
		var idLine = ('line'+z);
		
		try {
			lineContent=stanzalinearray[z].childNodes[0].nodeValue;
		}
		catch(err) {
			lineContent = null;
		}
				
		if (lineContent != null) {
		
			while (lineContent.charAt(0) == ' ') {
				lineContent = lineContent.substring(1);
			}
			while (lineContent.charAt(lineContent.length - 1) == ' ') {
				lineContent = lineContent.substring(0, lineContent.length - 1);
			}
		
			var newLineHolder = document.createElement("p");
			newLineHolder.setAttribute("id", idLine);
			newStanzaHolder.appendChild(newLineHolder);
			
			var line = lineContent.replace(/[\.,;!#\$\/:\?'\(\)\[\]_\-\\]/g,'');

			var splitline=line.split(" ");
			var totalLineWords = splitline.length;
			
			//For each word
			for (y=0;y<totalLineWords;y++) {
			
				var idWord = ('stanza'+stanzaId+'line'+z+'word'+y);
						
				var word = splitline[y];
				wordLower=word.toLowerCase();
			
				if (wordLower != '') {
					var newWordHolder = document.createElement("span");
					newWordHolder.setAttribute("id", idWord);
					newWordHolder.style.width = '200px';
					newWordHolder.className = ("wordholder");
				}
				$(newWordHolder).draggable({containment: "parent"});
				
				var wordNode = document.createTextNode(word);
				var tagWordNode = document.createTextNode(word);
				var tagWord = document.createElement('span');
				$(tagWord).draggable({containment: 'parent'});
				
				tagWord.className = ("tagword");
				tagWord.appendChild(tagWordNode);
					
				for (i=0; i<replaceWords.length; i++) {
					if (wordLower==replaceWords[i]) {
						newWordHolder.appendChild(wordNode);
						newWordHolder.style.width = 'auto';
						newLineHolder.appendChild(newWordHolder);
						word='replaced';
					}
				}
				
				if (word != 'replaced') {
					
					//Create image link
					var imageLink = document.createElement("a");
					imageLink.className = "imagelink";
					imageLink.href = "#";
					imageLink.id = idWord + 'imagelink';
					imageLink.title = word;
					$(imageLink).click(imageBlink);

					//$(imageLink).lightBox();
					//$(imageLink).hover(imageHover, imageOut);
					//$(image).mouseout(ImageOnMouseOut);
					
					var nav = document.createElement("span");
					$(nav).hover(imageHover, imageOut);
					nav.id = idWord + 'nav';
					nav.className = "nav";
					nav.style.display = 'none';
					//$(nav).click(navClick);
					
					
					//~ var navNext = document.createElement("a");
					//~ navNext.href = "#";
					//~ navNext.className = "navnext";
					//~ var navNextImg = new Image();
					//~ navNextImg.src = 'http://www.dudeyjon.com/digipoem/images/arrow-r.png';
					//~ navNext.appendChild(navNextImg);
					//~ $(navNext).click(imageNext);
					//~ nav.appendChild(navNext);

					var navFlickr = document.createElement("a");
					navFlickr.href = imageLink.href;
					navFlickr.className = 'navflickr';
					//~ var navFlickrImg = new Image();
					//~ navFlickrImg.src = 'http://www.dudeyjon.com/digipoem/images/flickr.png';
					//~ navFlickr.target = "_blank";
					//~ //$(navFlickr).click(imageFlickr);
					//~ navFlickr.appendChild(navFlickrImg);
					nav.appendChild(navFlickr);
					
				//nav.appendChild(makeNav('#', 'navnext', 'http://www.dudeyjon.com/digipoem/images/arrow-r.png', 'imageNext'));
					//~ nav.appendChild(makeNav('#', 'navflickr', 'http://www.dudeyjon.com/digipoem/images/flickr.png', 'navClick'));
				//	var itemLink = new makeNav('#', 'navlarge', 'http://www.dudeyjon.com/digipoem/images/arrow-r.png', 'navClick');
				//	nav.appendChild(itemLink);
					//~ nav.appendChild(makeNav('#', 'navprev', 'http://www.dudeyjon.com/digipoem/images/arrow-l.png', 'imagePrev'));
					
					
					
					
					
					
					
				var navPrev = makeNav('#', 'navprev', 'http://www.dudeyjon.com/digipoem/images/arrow-l.png');
				$(navPrev).click(imagePrev);
				nav.appendChild(navPrev);
					
			
				var navLarge = makeNav('#', 'navlarge', 'http://www.dudeyjon.com/digipoem/images/large.jpg');
				$(navLarge).click(navResize);
				nav.appendChild(navLarge);
				
				var navHome = makeNav('#', 'navhome', 'http://www.dudeyjon.com/digipoem/images/flickr.png');
				$(navHome).click(imageHome);
				nav.appendChild(navHome);
					//$(navFlickr).click(imageFlickr);
					
				
				var navNext = makeNav('#', 'navnext', 'http://www.dudeyjon.com/digipoem/images/arrow-r.png');
				$(navNext).click(imageNext);
				nav.appendChild(navNext);

					
					
					
					
					
					

					//~ var navPrev = document.createElement("a");
					//~ navPrev.href = "#";
					//~ navPrev.className = "navprev";
					//~ var navPrevImg = new Image();
					//~ navPrevImg.src = 'http://www.dudeyjon.com/digipoem/images/arrow-l.png';
					//~ navPrev.appendChild(navPrevImg);
					//~ $(navPrev).click(imagePrev);
					//~ nav.appendChild(navPrev);
					 
					imageLink.appendChild(nav);
					
					var loadingImage = makeLoadingImage(idWord);
					imageLink.appendChild(loadingImage);
					
					newWordHolder.appendChild(imageLink);
					newWordHolder.className = 'wordimgholder';
					
					newWordHolder.appendChild(tagWord);
					
					newLineHolder.appendChild(newWordHolder);
					
				
					
					var requestRecieved = false;
					
					//~ for (i=0; i<wordArray.length; i++) {	
						//~ if ((idWord+0)==wordArray[i]) {
							//~ requestRecieved = true;
							//~ populatePoem(0, 50, idWord, i, word);
						//~ }
					//~ }
					
					
					
					//~ if (requestRecieved == false) {
					new requestFlickr(word, idWord, idLine); 
					//}
					
					
					
				}
			}
		}
	}
	
	stanzaNav(stanzaId, stanzaArray);
	
}




function makeNav (itemLoc, itemId, itemImg, itemCallback) {
						
	var itemLink = document.createElement("a");
	itemLink.href = itemLoc;
	itemLink.id = itemId;
	itemLink.className = 'navitem';
	//~ $(itemLink).click(itemCallback);
						
	var itemImage = new Image();
	itemImage.src = itemImg;
						
	itemLink.appendChild(itemImage);
	
	return itemLink;
						
}







function requestFlickr(word, idWord) {

	//Flickr Request variables
	var hostname = 'http://api.flickr.com/';
	var perpage = 10;
	var path = 'http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=4b061797cb4461989b0de3ae153c1e96&tags="'+encodeURIComponent(word)+'"&sort=relevance&tag_mode=all&per_page='+perpage+'&page=1';
	var url = 'http://www.dudeyjon.com/digipoem/restapiproxy.php?csurl='+encodeURIComponent(path);
	var method = "GET";
	var asyncRequest = "true";
	//var callback = responseFlickr;
	
	//Make new request
	//new xmlRequest(url, method, asyncRequest, callback);
	
	$.getJSON(path+"&format=json&jsoncallback=?", jsonFlickr);



	function jsonFlickr(data) {
		
		if (!data) {
			return false;
		}
		//Get individual photo urls
		if (data.photos) {
			
			var total = data.photos.total;
			var subtotal = data.photos.photo.length;
			
			if (total == 0) {
				//new makeImage(farmid, serverid, id, secret, title, userid, i, subtotal, total);
				//Clear loading image
				var loadingId = '#loadingimg' + idWord;
				if ($(loadingId)) {
					var loadingImage = $(loadingId);
					loadingImage.attr('src', 'images/noimage.gif');
					loadingImage.attr('alt', "Unknown");
					loadingImage.attr('title', "Flickr has no images tagged with the word '" + word + "'");
				}
			}
			else if (subtotal > 0) {
				for (i=0;i<1;i++) {
					
					//new makeImage(farmid, serverid, id, secret, title, userid, i, subtotal, total);
					//photo
					//var image = data.photos.photo[i];
					//image.msg = 'Sometext';

					var image = new makeImage(data.photos, word, idWord, 0);
					
					//fimg.makeImage(0);

				}
			}
		}
	
	}

}
	
function makeImage (photos, word, idWord, photoNum) {
	
	if (!photos.photo[photoNum]) {
		return false;
	}
	
	// Get the photo
	var photo =  photos.photo[photoNum];
	
	// Make an image
	var image = new Image;
	
	// Get placeholder link
	var imageLink = document.getElementById(idWord + 'imagelink');
	//$(imageLink).find('.navflickr')[0].href = "http://www.flickr.com/photos/" + photo.owner + "/" + photo.id;
	imageLink.href = "http://www.flickr.com/photos/" + photo.owner + "/" + photo.id;
	$(imageLink).hover(imageHover, imageOut);
	imageLink.appendChild(image);

	// Custom properties
	image.word = word;
	image.photos = photos;
	image.idWord = idWord;
	image.photoNum = photoNum;
	image.fkup = false;
	image.photoSize = 'm';
	
	//$(image).click(ImageOnMouseClick);
	$(image).hide();
	$(image).load(displayImage);
	
	// Standard properties
	image.id = idWord + 'image';
	image.className = 'flickrimg';
	image.src = "http://farm"+photo.farm+".static.flickr.com/"+photo.server+"/"+photo.id+"_"+photo.secret+"_m"+".jpg";

}

function displayImage(image) {
	
	var idWord  = this.idWord;
	var image  = document.getElementById(idWord + 'image');
	var imageWidth = $(image).width();
	
	var holder = image.parentNode.parentNode;
	$(holder).width(imageWidth);
	
	$(image).fadeIn('slow', function () {
		document.getElementById('loadingimg' + idWord).style.display = 'none';
	});
	
}

function loading (image) {
	
	var idWord  = image.idWord;	
	var image  = document.getElementById(idWord + 'image');
	var imageWidth = $(image).width();
	
	var holder = image.parentNode.parentNode;
	$(holder).width(imageWidth);
	
	document.getElementById('loadingimg' + idWord).style.display = 'block';
	image.style.display = 'none';
	
	
}

function advanceImage(image, photoNum, photoSize) {
	
	if (!image.photos.photo[photoNum]) {
		
		return false;
	}
	
	loading(image);
	
	$(image).load(displayImage);
	
	// Get the photo
	var photo =  image.photos.photo[photoNum];

	image.id = image.idWord + 'image';
	image.photoNum = photoNum;
	image.src = "http://farm"+photo.farm+".static.flickr.com/"+photo.server+"/"+photo.id+"_"+photo.secret+photoSize+".jpg";
	image.parentNode.href = "http://farm"+photo.farm+".static.flickr.com/"+photo.server+"/"+photo.id+"_"+photo.secret+".jpg";

}
	

//~ function loadingImage (loadId, showLoad) {
	//~ if (showLoad) {
		//~ jQuery('#'+loadId).fadeOut('slow');		
	//~ }
	//~ else {
		//~ jQuery('#'+loadId).fadeOut('slow');
	//~ }
//~ }
	













	
function populatePoem (photoNum, photoTotal, idWord, requestCount, word) {
	
	//Remove loading image
	var loadingId = 'loadingimg' + idWord;
	try {
		var loadingImage = document.getElementById(loadingId);
		loadingImage.parentNode.removeChild(loadingImage);
	}
	catch(err){
	}
	
	//Append navigation and images
	try {
		var newWordHolder = $('#'+idWord);
		newWordHolder.prepend(imageLinkArray[requestCount]);
		imageLinkArray[requestCount].childNodes[0].onmouseout();
		
	}
	catch (err) {
		
	}
	
}
function imageBlink() {
	return false;
}

	
function imageHome() {
	var navItem = this;
	var imageLink = navItem.parentNode.parentNode;
	window.open(imageLink.href);
}

function imageHover () {
	
	var imageLink = this;
	var imageNav = $(imageLink).find('.nav');
	$(imageNav).fadeIn('slow');
}

function imageOut() {

	var imageLink = this;
	var imageNav = $(imageLink).find('.nav');
	$(imageNav).hide();
	//$(image).attr('src', medImageSrc);
	//image.className = ('imgout');
}

function imageMagnify() {
	
	image.style.width = 'auto';
	image.style.height= 'auto';
	image.parentNode.parentNode.style.height = 'auto';
}

function navResize() {
	
	var navItem = this;
	var navImage = $(navItem).find('img')[0];
	var imageLink = navItem.parentNode.parentNode;
	var image = $(imageLink).find('.flickrimg')[0];

	if (image.photoSize == 'm') {
		image.photoSize = 'l';
		$(navImage).attr('src', 'http://www.dudeyjon.com/digipoem/images/med.jpg');
		advanceImage(image, image.photoNum, '');
	}
	else {
		image.photoSize = 'm';
		$(navImage).attr('src', 'http://www.dudeyjon.com/digipoem/images/large.jpg');
		advanceImage(image, image.photoNum, "_m");
	}

	image.style.width = 'auto';
	image.style.height= 'auto';
	image.parentNode.parentNode.style.height = 'auto';
	
	return false;
}


function navSmall() {
	
	
}


function imageNext() {

	var next = this;
	var imageLink = next.parentNode.parentNode;
	var image = $(imageLink).find('.flickrimg')[0];
	var photoNum = image.photoNum;
	var size = '_m';
	advanceImage(image, photoNum + 1, size);
			
	return false;
}


function imagePrev () {
	
	var prev = this;
	var imageLink = prev.parentNode.parentNode;
	var image = $(imageLink).find('.flickrimg')[0];
	var photoNum = image.photoNum;
	var size = '_m';
	advanceImage(image, photoNum - 1, size);

	return false;
	
}


//On image mouse over show medium image		
function ImageOnMouseClick() {
	
	return false;	
	
	var image = this;
	var photoNum = this.photoNum;
	
	if (!image.photos.photo[photoNum]) {
		return false;
	}
	
	//$(image).fadeOut('slow', function () {
	//image.className = 'loadingimg';
	var size = '_m';
	advanceImage(image, photoNum + 1, size);
			
	
					
}




















//http://www.dudeyjon.com/digipoem/scripts/prepareYte.js

function prepareYtePoem(xmlPoem) {
	
	var linearray=xmlPoem.getElementsByTagName("line");
	var ytePoem = document.getElementById("ytepoem");
		
	var yteContent = document.createElement("div");
	yteContent.setAttribute("id", "yteContent");
	ytePoem.appendChild(yteContent);
		
	for (e=0;e<linearray.length;e++) {
		
		try {
			line=xmlPoem.getElementsByTagName("line")[e].childNodes[0].nodeValue;
		}
		catch(err) {
			line = null;
		}
			
		if (line != null ) {
			var newLine = document.createElement("p");
			var newLineTerms = document.createElement("div");
			var lineNode = document.createTextNode(line);

			var linePlus=line.replace(/\s+/g,"+");
			newLineTerms.setAttribute("id", 'lineterm'+e);
			newLine.appendChild(lineNode);
			idLine = newLineTerms.getAttribute("id");
				
			originalPoem.appendChild(newLine);
			yteContent.appendChild(newLineTerms);
		
			requestYte(linePlus, idLine);
		}
	}
}

function requestYte (line, idLine) {

	var hostname = 'http://api.search.yahoo.com/';
	var path = 'ContentAnalysisService/V1/termExtraction?appid=tHT5qsXV34FNwpel1Di0GCA5qdaqojOPQAxKZViFp5QL.TXeehD8Uu1LQ1JeF6s-&context=' + line;
	var url = 'http://www.dudeyjon.com/digipoem/restapiproxy.php?hostname=' + encodeURIComponent(hostname) + '&api_path=' + encodeURIComponent(path);
	var method = "POST";
	var asyncRequest = "true";
	var callback = loadYteXML;
	var xmlDoc = xmlRequest(url, method, asyncRequest, callback);

	function loadYteXML(request)
	{	
		// IE Code
		if (window.ActiveXObject)
		{
			var response = request.responseXML;
			xmlDocYte=new ActiveXObject("Microsoft.XMLDOM");
			xmlDocYte.async=false;
			xmlDocYte.load(response);
			getTerm(xmlDocYte);
		}
	
		// Firefox Code
		else if (document.implementation&&document.implementation.createDocument)
		{
			var response = request.responseXML;
			var xmlDocYte = document.createElement("div");
			xmlDocYte.innerHTML = response;
			getTerm(response);
		}
	
		// Error Code
		else
		{
			alert('Your browser cannot handle this script');
		}
	}
	
	function getTerm(xmlDocYte)
	{
		var results=xmlDocYte.getElementsByTagName("Result");
		for (i=0;i<results.length;i++)
		{
			var terms=xmlDocYte.getElementsByTagName("Result")[i].childNodes[0];
			
			var ytePoem = document.getElementById(idLine);
			var newTerm = document.createElement("p");
			var termNode = document.createTextNode(terms.nodeValue);
			newTerm.appendChild(termNode);
			
			ytePoem.appendChild(newTerm);
		}
	}

	function populateTerms(term)
	{
		var newTerm = document.createElement("p");
		var newLine = document.getElementById(idLine);
		var termNode = document.createTextNode(term);
		newTerm.appendChild(termNode);
	}


}
