/*

mrmattspangler.com photo module

*/


if(tumblr_api_read){
				
	var photos = tumblr_api_read['posts'];
	var photos_total = photos.length; /*tumblr_api_read["posts-total"];*/
	var photos_index = 0;
	
}

if(photos_total > 0 && homepage_flag && photos[0]["photo-url-500"] != null){
	
	var photo_module_cont = document.getElementById("photosmodule");
	
	if(photo_module_cont){
		var photo_module = document.createElement('div');
		photo_module.className = "module postitem posttype-photo";
		photo_module.style.background = 'url(' + photos[0]["photo-url-500"] + ') top left no-repeat';
		
		//quick way of adding nav
		photo_module.innerHTML = '<div class="sectionnav"><h3 class="sectionnav_lbl">Memorable Stuff.</h3><div class="sectionnav_navlinks"><a href="javascript://" onclick="photo_module_prev()" class="nav_prev" id="photomodule_prev"><span>prev</span></a><a href="javascript://" class="nav_next" id="photomodule_next" onclick="photo_module_next()"><span>next</span></a></div></div>';
		
		var photo_list = document.createElement('ul');
		photo_list.className = "photolist";
		
		for(i = 0; i < photos_total; i++){
			
			photo_list.appendChild(document.createElement('li'));
			
			

			
			if(i == 0){
				photo_list.childNodes[i].className = "photoitem";
			} else {
				photo_list.childNodes[i].className = "photoitem hidden";
			}
			
			var tempstring = '';
			tempstring += '<a href="' + photos[i]["url"] + '" class="shim"><img src="' + photos[i]["photo-url-500"] + '" /></a>'; //placeholder for the image
			
			if(photos[i]["photo-caption"]){
				tempstring += '<div class="postcontent">' + photos[i]["photo-caption"] + '</div>';
			}
			
			if(photos[i]["tags"]){
				tempstring += '<div class="postfooter"><ul class="linklist"><li class="listlabel">Tags. </li>';
				
				for(j = 0; j < photos[i]["tags"].length; j++){
					tempstring += '<li class="linkitem"><a href="http://mrmattspangler.com/tagged/' + photos[i]["tags"][j] + '">' + photos[i]["tags"][j] + '</a>. </li>';
				}
				
				tempstring += '</ul></div>';
				
				
			}
			
			photo_list.childNodes[i].innerHTML += tempstring;
		}						
		
		photo_module.appendChild(photo_list);
		
		photo_module_cont.appendChild(photo_module);
		
	}
	
	var photo_module_prev_arrow = document.getElementById('photomodule_prev');
	var photo_module_next_arrow = document.getElementById('photomodule_next');
	photo_module_nav(0);
}

function photo_module_next(){
	if((photos_index + 1) < photos_total){
		newindex = photos_index + 1;
	} else {
		newindex = 0;
	}
	
	photo_module_show(photos_index, newindex);
}

function photo_module_prev(){
	if((photos_index - 1) >= 0){
		newindex = photos_index - 1;
	} else {
		newindex = photos_total - 1;
	}
	
	photo_module_show(photos_index, newindex);
}

function photo_module_show(index, newindex){
	if(index != newindex){
		
		photo_module.style.background = 'url(' + photos[newindex]["photo-url-500"] + ') top left no-repeat';
		
		for(c = 0; c < photos_total; c++){
			if(c == newindex){
				photo_list.childNodes[c].className = "photoitem";
			} else {
				photo_list.childNodes[c].className = "photoitem hidden";
			}
		}
	
		photos_index = newindex;
		photo_module_nav(index);
	
	}
}

function photo_module_nav(index){
	
	//since we're looping the photos, we can have prev/next as long as there are more than 2
	if(photos_total > 1){
		photo_module_prev_arrow.className = "nav_prev";	
		photo_module_next_arrow.className = "nav_next";
	}
}