$(document).ready(function(){
			$("#text_box").hide();
			$("#prev, #next").hide();
			loadProjects();
			$("#projlist li a").live('click', function() {
				projID = ($(this).attr('id'));
				imgID = 'first';
				loadImage(projID, imgID);
			});
			$("#photo_box #nav a").live('click', function(event) {
					event.preventDefault();
			});				
			$("#nav_box a").live('click', function(event) {
					event.preventDefault();
					$("#footer").hide();
					href = $(this).attr("href");
					$("#nav_box a").removeClass("texton");
					$(this).addClass("texton");
					//handle text links
					if ($(this).hasClass("textlink")) {
						$("#text_box").show();
						$("#photo_box").hide();
						$("#text_box").load(href);
						$("#projlist").slideUp();
					};
					//handle photography
					if (href == "#photos") {
						$("#projlist").slideToggle();
					}
			});		
			// Set nav behavior
			$("#photo_box #nav a").click(function(event) {
					if($(this).hasClass('outside')) {
						window.location = $(this).attr('href');
					}
					else {
						event.preventDefault();
						img = $(this).attr("href");
						proj = $(".projectcode").first().attr("id");
						//alert(projID + " " + img + " pos is " + pos);
						if(!img || img == "") {
							return false;
						}
						else {
						loadImage(proj, img);
						};
					};
			});		
			/*$("#photo_box #nav a").hover(
				  function () {
				  	if ($(this).attr('href') != "") {
						$(this).addClass("texton");
						$(this).removeClass("grey");
					};
				  },
				  function () {
  				  	if ($(this).attr('href') != "") {
						$(this).removeClass("texton");
						$(this).addClass("grey");
					};
				  }
			);*/
		});

		function loadProjects() {
			$("#projlist").hide();
			$.get("photolist.xml",function(xml){
				$("#projlist").append("<ul class=\"nobullets\"></ul>");
				$("project",xml).each(function(i) {
					navtxt = "<li>";
					navtxt += "<a id=\"" + $(this).attr("id") + "\" href=\"#" + $(this).attr("name") + "\">";
					navtxt += $(this).attr("name");
					navtxt += "</a>";
					navtxt += "</li>";
					$("#projlist ul").append(navtxt);
				});
			});
		}
		
		function loadImage(projID, imgID) {
			$("#text_box").hide();
			$("#photo_box").show();
			$("#prev, #next").show();
			$(".projectcode").first().attr('id', projID);
			$.get("photolist.xml",function(xml){
				total = $("project[id='" + projID + "']",xml).find("photo").length;
				// Find first image
				if (imgID == "first") {
					imgID = $("project[id='" + projID + "']",xml).find("photo").first().attr("id");
				};
				/* alert("total is " + total + ", projID is " + projID + ", imgID is " + imgID); */
				//Set path
				path = $("project[id='" + projID + "']",xml).attr("path");
				// Find single Image in Specified Project
				newimg = $("project[id='" + projID + "']",xml).find("photo[id='" + imgID + "']").first();
				/* alert(newimg.attr("id")); */
				pos = newimg.index() + 1;
				nextid = newimg.next().attr("id");
				previd = newimg.prev().attr("id");
				id = newimg.attr("id");
				src = "graphics/" + path + "/" + newimg.attr("src");
				orient = newimg.attr("orient");
				if(newimg.find("title").text()) {
					tit = newimg.find("title").text();
				}
				else {
					tit = "";
				};
				if(tit.length < 1) {
					tit = "&nbsp;";
				};
/*				alert(tit + " is title");
			alert(id + " is id, " + src + " is src, " + title + " is title" + previd + " is previd, " + nextid + " is nextid, " + pos + "is pos"); */
				//Load Image
				if (orient == "p") {
					size = "width=\"400px\" height=\"600px\"";
				}
				else if (orient == "l") {
					size = "width=\"600px\" height=\"400px\"";
				}
				else {
					size = "";
				};
				phototxt = "<img id=\"" + id + "\" src=\"" + src + "\" " + size + ">";
				$("#photo_box #image").html(phototxt);
				$("#photo_box #title").html(tit);
				//Set count
				$("#photo_box #nav #count").html(pos + "/" + total + " ");
				//Change nav links
				if(nextid) {
					$("#photo_box #nav #next a").attr("href", nextid);
					$("#photo_box #nav #next a").removeClass('textoff');
					$("#photo_box #nav #next a").addClass('navon'); 
				}
				else {
					$("#photo_box #nav #next a").attr("href", "");
					$("#photo_box #nav #next a").addClass('textoff');
					$("#photo_box #nav #next a").removeClass('navon');
				};
				if(previd) {
					$("#photo_box #nav #prev a").attr("href", previd);
					$("#photo_box #nav #prev a").removeClass('textoff');
					$("#photo_box #nav #prev a").addClass('navon');
				}
				else {
					$("#photo_box #nav #prev a").attr("href", "");
					$("#photo_box #nav #prev a").addClass('textoff');
					$("#photo_box #nav #prev a").removeClass('navon');
				};		  	    
			}, 'xml');
		};
