addOnload(startDesc);
addOnload(startList);

function addOnload(newFunction) {
	var oldOnload = window.onload;
	
	if (typeof oldOnload == "function") {
		window.onload = function() {
			if (oldOnload) {
				oldOnload();
			}
			newFunction();
			
		}
		
	}
	else {
		window.onload = newFunction;
		
	}
	
}






function startList() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("menu");
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
  }
  node.onmouseout=function() {
  this.className=this.className.replace(" over", "");
   }
   }
  }
 }
}




function showLocalDate(timestamp)
{

  var mmToMonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
  var dt = new Date(timestamp * 1000);
  var mm = mmToMonth[dt.getMonth()];
  return mm + " " + dt.getDate() + ", " + dt.getFullYear();
} 

function addTarget(article) {
	
	if (article.childNodes != undefined) {
	
		var articleChildren = article.getElementsByTagName('*');
		
		for (var i=0; i<articleChildren.length; i++) {
		
			if (articleChildren[i].tagName == 'A') {
			
				articleChildren[i].setAttribute('target','_blank');
				
			}
			
		}
		
	} else {
	
		return;
		
	}
}


function jsonFlickrApi(rsp) {
 if (rsp.stat != "ok"){
  // If this executes, something broke!
  return;
 }
    
 //this loop runs through every item and creates HTML 
 for (var i=0; i < rsp.photos.photo.length; i++) {
  photo = rsp.photos.photo[ i ];
    
    //Set Variables
	var t_url = "http://farm" + photo.farm + 
	".static.flickr.com/" + photo.server + "/" + 
	photo.id + "_" + photo.secret + ".jpg";
	
	var p_url = "http://www.flickr.com/photos/" + 
	photo.owner + "/" + photo.id;
	
	var p_id = photo.id;
	
	var p_header = document.createElement('p');
	p_header.setAttribute('id','p_header' + p_id);
	p_header.setAttribute('class','p_header');
	p_header.setAttribute('className','p_header');
	
	var title = document.createTextNode(photo.title);
	
	var p_title = document.createElement('span');
	p_title.setAttribute('class','photoheader');
	p_title.setAttribute('className','photoheader');
	
	var p_link = document.createElement('a');
	p_link.setAttribute('href', p_url);
	p_link.setAttribute('target', '_blank');
	
	var p_link2 = document.createElement('a');
	p_link2.setAttribute('href', p_url);
	p_link2.setAttribute('target', '_blank');
	
	var p_img = document.createElement('img');
	p_img.setAttribute('src', t_url);
	
	var article = document.createElement('div');
  	article.setAttribute('class', 'article hidepiece');
  	article.setAttribute('className', 'article hidepiece');
  	article.setAttribute('id', p_id);
  
 	var header = document.createElement('h2');
 	
 	/*var p_desc = document.createElement('p');
 	p_desc.setAttribute('class', 'photodesc');*/
	
	//Create nodes for appending to document
	 p_title.appendChild(title);
	
	article.appendChild(p_header);
	
	p_header.appendChild(p_link);
	
	p_link.appendChild(p_title);
	
	article.appendChild(p_link2);
	
	p_link2.appendChild(p_img);
	
//	article.appendChild(p_desc);
	
	document.getElementById('images').appendChild(article);
		
	} //end of loop

}

function startDesc() {

	setTimeout(startComments, 15);

	var allDivs = document.getElementsByTagName('div');
	
	for (var i=0; i<allDivs.length; i++) {
		if (allDivs[i].className == "article hidepiece") {
			var thisDiv = allDivs[i];
			addScript(thisDiv);
		}
	}
	
}

function addScript(thisDiv) {

	var photo_id = thisDiv.id;
	
	var script_link = "http://api.flickr.com/services/rest/?format=json&jsoncallback=flickrInfo&method=flickr.photos.getInfo&api_key=06f8045e3e80b258180ff3f9bdafdb38&photo_id=" + photo_id;
	
//	var script_link2 = "http://api.flickr.com/services/rest/?format=json&jsoncallback=flickrComments&method=flickr.photos.comments.getList&api_key=06f8045e3e80b258180ff3f9bdafdb38&photo_id=" + photo_id;
	
	var flickrInfoScript = document.createElement('script');
 	flickrInfoScript.setAttribute('src', script_link);
 	flickrInfoScript.setAttribute("type","text/javascript");
 	flickrInfoScript.setAttribute('id', 'tempFlickr');
 	
//	var flickrInfoScript2 = document.createElement('script');
//	flickrInfoScript2.setAttribute('src', script_link2);
// 	flickrInfoScript2.setAttribute("type","text/javascript");
//	flickrInfoScript2.setAttribute('id', 'script' + photo_id);
	
	document.getElementsByTagName("head")[0].appendChild(flickrInfoScript);
//	document.getElementsByTagName("head")[0].appendChild(flickrInfoScript2);
	
}


function flickrInfo(rsp) {

	if (rsp.stat != "ok"){
 			// If this executes, something broke!
  			return;
 	}
 	

		var photoDesc = rsp.photo.description._content;
		
		var photo_id = rsp.photo.id;
		
		//Add date to post
		var photo_date = showLocalDate(rsp.photo.dateuploaded);
		
		var s_date = document.createElement('span');
		s_date.setAttribute('class','headerDate');
		s_date.setAttribute('className','headerDate');
		
		s_date.innerHTML = photo_date;
		
		var addDateHere = document.getElementById('p_header' + photo_id);
		
		addDateHere.appendChild(s_date);
		
		var commentsNum = rsp.photo.comments._content;
		
		var divToAdd = document.getElementById(photo_id);
	
		var newTextP = document.createElement('p');
		newTextP.setAttribute('class', 'photodesc');
		newTextP.setAttribute('className', 'photodesc');
		
		newTextP.innerHTML = photoDesc;
		
		divToAdd.appendChild(newTextP);
		
		if (commentsNum==1) {
		
			var commentsTxt = document.createElement('p');
			commentsTxt.setAttribute('class', 'photodesc');
			commentsTxt.setAttribute('className', 'photodesc');
			commentsTxt.innerHTML = "This photo has <a href=\"http://www.flickr.com/photos/49838260@N00/" + photo_id + "\" target=\"_blank\">" + commentsNum + "</a> comment. <a href=\"#\" onclick=\"Effect.BlindDown('comments" + photo_id + "'); return false;\" class=\"viewalllink\">[view]</a>";
		
			divToAdd.appendChild(commentsTxt);
			
			var commentsDiv = document.createElement('div');
			commentsDiv.setAttribute('id', 'comments' + photo_id);
			commentsDiv.setAttribute('class', 'commentsdiv');
			commentsDiv.setAttribute('className', 'commentsdiv');
			commentsDiv.setAttribute('style', 'display:none;');
			commentsDiv.setAttribute('cssText', 'display:none;');
			commentsDiv.style.cssText = 'display:none';
			
			divToAdd.appendChild(commentsDiv);
		}
		
		if (commentsNum>1) {
		
			var commentsTxt = document.createElement('p');
			commentsTxt.setAttribute('class', 'photodesc');
			commentsTxt.setAttribute('className', 'photodesc');
			commentsTxt.innerHTML = "This photo has <a href=\"http://www.flickr.com/photos/49838260@N00/" + photo_id + "\" target=\"_blank\">" + commentsNum + "</a> comments. <a href=\"#\" onclick=\"Effect.BlindDown('comments" + photo_id + "'); return false;\" class=\"viewalllink\">[view]</a>";
		
			divToAdd.appendChild(commentsTxt);
			
			var commentsDiv = document.createElement('div');
			commentsDiv.setAttribute('id', 'comments' + photo_id);
			commentsDiv.setAttribute('class', 'commentsdiv');
			commentsDiv.setAttribute('className', 'commentsdiv');
			commentsDiv.setAttribute('style', 'display:none;');
			commentsDiv.setAttribute('cssText', 'display:none;');
			commentsDiv.style.cssText = 'display:none';
			
			divToAdd.appendChild(commentsDiv);
		}
		
		var flickrScript = document.getElementById('tempFlickr');
		
		document.getElementsByTagName("head")[0].removeChild(flickrScript);
	
		addTarget(newTextP);
		
}



function startComments() {

	var allDivs = document.getElementsByTagName('div');
	
	for (var i=0; i<allDivs.length; i++) {
		if (allDivs[i].className == "article hidepiece") {
			var thisDiv = allDivs[i];
			addComments(thisDiv);
		}
	}
	
}

function addComments(thisDiv) {

	var photo_id = thisDiv.id;
	
	var script_link2 = "http://api.flickr.com/services/rest/?format=json&jsoncallback=flickrComments&method=flickr.photos.comments.getList&api_key=06f8045e3e80b258180ff3f9bdafdb38&photo_id=" + photo_id;
	
 	
	var flickrInfoScript2 = document.createElement('script');
	flickrInfoScript2.setAttribute('src', script_link2);
 	flickrInfoScript2.setAttribute("type","text/javascript");
	flickrInfoScript2.setAttribute('id', 'script' + photo_id);
	
	document.getElementsByTagName("head")[0].appendChild(flickrInfoScript2);
	
}


function flickrComments(rsp) {

	if (rsp.stat != "ok"){
 			// If this executes, something broke!
  			return;
 	}
 	
 	if (rsp.comments.comment != undefined){
 	
		var commentsLength = rsp.comments.comment.length;
		
		var photo_id = rsp.comments.photo_id;
		
		if (document.getElementById("comments" + photo_id) != null) {
	
			var divToAdd = document.getElementById("comments" + photo_id);	
		
			for (var i=0; i<commentsLength; i++){
				
		
				var comment = rsp.comments.comment[i];
				var commentid = comment.id;
				var author = comment.authorname;
				var authorid = comment.author;
				var authorLink = "http://www.flickr.com/photos/" + authorid + "/";
				var postDate = showLocalDate(comment.datecreate);
				var commentLink = comment.permalink;
				var commentContent = comment._content;
				
				var commentDiv = document.createElement('div');
				commentDiv.setAttribute('id', commentid);
				commentDiv.setAttribute('class', 'commentdiv');
				commentDiv.setAttribute('className', 'commentdiv');
				
				var commentAuthor = document.createElement('p');
				commentAuthor.innerHTML = "On " + postDate + " <a href=\"" + authorLink + "\" target=\"_blank\">" + author + "</a> wrote:";
				
				var commentText = document.createElement('p');
				commentText.setAttribute('class', 'commentText');
				commentText.setAttribute('className', 'commentText');
				commentText.innerHTML = commentContent + "<br /><a href=\"" + commentLink + "\" class=\"commentpermalink\" target=\"_blank\">(permalink)</a>";
				
				commentDiv.appendChild(commentAuthor);
				commentDiv.appendChild(commentText);
				divToAdd.appendChild(commentDiv);
				
				addTarget(commentText);
					
			}			
			
			var hideall = document.createElement('p');
			hideall.innerHTML = "<br /><a href=\"#\" onclick=\"Effect.BlindUp('comments" + photo_id + "'); return false;\" class=\"viewalllink\">[hide]</a>";
		
			divToAdd.appendChild(hideall);
			
			var flickrScript2 = document.getElementById('script' + photo_id);
		
			document.getElementsByTagName("head")[0].removeChild(flickrScript2);
				
		} 
		
	
	} else {
	
		var photo_id = rsp.comments.photo_id;
	
		var flickrScript2 = document.getElementById('script' + photo_id);
		
		document.getElementsByTagName("head")[0].removeChild(flickrScript2);
		
		return;
		
	}
	


 }
 
