var list = new Array();

$(document).ready(function(){
	var navigation = "";
	
	$.getJSON("gallery-files/get.php", 
		function(json){
			if(json.length > 0){
				navigation = process_output(json);
			} else {
				navigation = "The request did not return results";
			}
			
//			// turn the list into an accordion
//			$("#nav").html(navigation).accordion({
//				header:'a.head'
//			});

			
		}
	);
	
});

function process_output(json){
	
	var sep = "/";
	var photodir = "photos";
	var thumbdir = "thumb";
	//json.reverse();
	$('#nav').empty();
	for (var i=0;i<json.length;i++){
		var year = json[i].name;
		//list[json[i].name] = json[i].images;
//		out += "<a class='head'>" + year + "</a>";
		
		//$('#nav').append("<a class='head'>" + year + "</a>");
		var out = "<a class='head'>" + year + "</a>";
		
		// process children
		children = json[i].children;
		children.reverse();
//		out += "<ul>";
		//$('#nav').append('<ul id="nav-'+ year +'">');
		out += '<ul id="nav-'+ year +'">';
		
		for (var j = 0; j<children.length;j++){
			var nameWithDate = children[j].name;
			var nameMinusDate = children[j].name.substring(9);
			
			list.push({
				"nameWithDate":nameWithDate,
				"nameMinusDate":nameMinusDate,
				"children":children[j].children
			})
			//$('#nav #nav-'+year).append('<li class="links"><a class="images_links">' + nameMinusDate + '</a></li>');

			out += '<li class="links"><a class="images_links">' + nameMinusDate + '</a></li>';
			
//			out += "<li class=\"links\">";
//			out += "<a class='images_links' href='#' onclick='return false;'>";
//			//out += numToMonth(children[i].name.substr(4,2), true)
//			out += " ";		
//			out += nameMinusDate
//			out += "</a>";
//			out += "</li>";
			

			
//			// process images for this child
//			if (children[j].children.length > 0){
//				var newout = "<ul title='"+ nameWithDate +"' id='"+ nameWithDate + "'>";
//				//$(thisul).hide();
//				$(children[j].children).each(function(index){
//					newout += "<li>";
//					//newout += '<a href="' + photodir + sep + year + sep + nameWithDate + sep + this.file + '">';
//					newout += '<img src="' + photodir + sep + year + sep + nameWithDate + sep + this.file + '" alt="Some alt text" border="0"/>';
//					//newout += '</a>';
//					newout += '</li>';
//					//this.file+"</li>";
////<li><a href="PathToFullSizeImage"><img src="PathToFullThumbnailImage" alt="Some alt text" width="144" height="96" border="0"/></a></li>
//
//				});
//
//			} 
		}
		//$('#nav').append('</ul>');
		out += '</ul>';
//		out += "</ul>";
		
		$('#nav').append(out);
	}
	
	$('a.images_links').click(function(e){
		e.preventDefault();
		createGallery($(this).html());
	});
	// turn the list into an accordion
	$('#nav').accordion({
		header:'a.head',
		autoheight:false
	});
//	return out;
}

function createGallery(name){
	if (list.length < 1)return;
	var obj = getCategoryDetails(name);
	if (!obj)return;
	
	// replace spaces with underscores
	var longName = obj.nameWithDate.replace(/ /g,"_");
	
	// check to see if this ul has already been created
	var el = $('#'+ longName);
	// if the selected gallery is already displayed.. no nothing
	if ($('#' + longName + ':visible').length == 1)return;
	

	
	// hide the currently displayed galleries
	$('#content div.image_gallery').hide();
	
	// if the selected gallery has already been created just show it
	if (el.length > 0){
		// show hidden gallery
		el.show();

	} else {
		// image location information
		var sep = "/";
		var photodir = "photos";
		var thumbdir = "thumb";
		var month = numToMonth(longName.substr(4,2), true);
		var year = longName.substr(0,4);
		
		// create the gallery
		$('#content').append('<ul id="' + longName + '" title="' + obj.nameMinusDate + ' (' + month + ' ' + year + ')">');
		for (var i=0,len=obj.children.length;i<len;i++){
			
			var out = '<li><a href="' + photodir + sep + year + sep + obj.nameWithDate + sep + obj.children[i].file + '">';
			out += '<img src="' + photodir + sep + year + sep + obj.nameWithDate + sep + thumbdir + sep + 'thumb_' + obj.children[i].file.substring(7) + '" border="0"/></a></li>';
//			$('#content #' + longName).append('<li>')
//			.append('<a href="' + photodir + sep + year + sep + obj.nameWithDate + sep + obj.children[i].file + '">')
//			.append('<img src="' + photodir + sep + year + sep + obj.nameWithDate + sep + obj.children[i].file + '" border="0"/>')
//			.append('</a></li>');
			
			$('#content #' + longName).append(out);
			//<li><a href="PathToFullSizeImage"><img src="PathToFullThumbnailImage" alt="Some alt text" width="144" height="96" border="0"/></a></li>
		}
		$('#content').append('</ul>');
		
		// turn it into a gallery view
		$('#content #' + longName).jqGalView({
			openTxt:'Click to Enlarge',
			closeTxt:'Click to Close'
		});
		
		// remove the directions
		$('#directions').remove();
	}
}

/*
 * Get the appropriate object from the list array
 * based upon the passed in name.
 */
function getCategoryDetails(name){
	for (var i=0,len=list.length;i<len;i++){
		if (name == list[i].nameMinusDate){
			return list[i];
		}
	}
	return null;
}

/*
 * Convert from 01-12 to the appropriate English Month
 */
function numToMonth(number, abbr){
	abbr = (abbr) ? true : false;
	number = parseFloat(number);
	switch(number){
		case 1: return (abbr) ? 'Jan':'January';break;
		case 2: return (abbr) ? 'Feb':'February';break;
		case 3: return (abbr) ? 'Mar':'March';break;
		case 4: return (abbr) ? 'Apr':'April';break;
		case 5: return (abbr) ? 'May':'May';break;
		case 6: return (abbr) ? 'Jun':'June';break;
		case 7: return (abbr) ? 'Jul':'July';break;
		case 8: return (abbr) ? 'Aug':'August';break;
		case 9: return (abbr) ? 'Sep':'September';break;
		case 10: return (abbr) ? 'Oct':'October';break;
		case 11: return (abbr) ? 'Nov':'November';break;
		case 12: return (abbr) ? 'Dec':'December';break;
		default:return "unknown";
	}
}
