function Library(folder, filename) {
	this.name = "library class";	
	this.folder = folder;
	this.xml = sendRequest(folder+filename);
	this.paneState = null;
	this.contentState = null;
};


Library.prototype.setLibraryXML = function(req) {
	alert("hello!");	
	this.xml = req.responseXML;		
}

Library.prototype.removeChildren = function(el) {
	try {
		for(var i=0; i<el.childNodes.length; i++) {
			el.removeChild(el.childNodes[i]);
		}
	} catch(err) { }
}

Library.prototype.getGalleryIconsHTML = function(limit) {	
	Gs = this.xml.documentElement.getElementsByTagName("gallery");
	this.numberOfGalleries = Gs.length;
	var pane = document.getElementById("pane");

	try {
		pane.innerHTML = "";
	} catch(err) { }
	
	if(limit > Gs.length) limit = Gs.length;
	
	for(var i=0; i<limit; i++) {
			
		if(Gs[i].getAttribute("icon") == "1") {	
			var id = Gs[i].getAttribute("id");

			var nodes = Gs[i].childNodes;
		
			for(var j=0; j<nodes.length; j++) {
				switch(nodes[j].nodeName) {
					case "title":
						var title = nodes[j].firstChild.nodeValue;	
						break;
					case "size":
						var size = nodes[j].firstChild.nodeValue;	
						break;
					case "date":							
						var date = nodes[j].firstChild.nodeValue;
						break;
					default:
						break;	
				}
			}

			// Image
			var iconEl = Gs[i].getElementsByTagName("thumbpath");
			var iconPath = iconEl[0].firstChild.nodeValue;
			var img = document.createElement("img");
			img.setAttribute("src", "photo_resources/"+iconPath);
			img.setAttribute("width", "60px");
			img.setAttribute("height", "45px");			
			img.className = "tinythumb icon";

			// Anchor
			var a = document.createElement("a");
			a.setAttribute("href", "javascript:void(0)");

			a.onclick = (function (id) { return function (e) { library.displayGalleryThumbs(id); }; })(id);

			a.onmouseover = (function (id, title, date, size, iconPath) { return function (e) { library.displayPreview(e, id, title, date, size, iconPath); }; })(id, title, date, size, iconPath);

			a.onmouseout = (function (id) { return function (e) { library.removePreview(e, id); }; })(id);
			
			a.appendChild(img);
			pane.appendChild(a);
		}
	}

	
	if(limit < Gs.length) {
	
		var showMore = document.createElement("a");
		showMore.setAttribute("id", "showmore");
		showMore.setAttribute("href", "javascript:void(0)");
		showMore.onclick = this.unhideThumbs;
		showMore.appendChild(document.createTextNode("Show  me more..."));
		
		pane.appendChild(showMore);
		
		for(var i=limit; i<Gs.length; i++) {
			if(Gs[i].getAttribute("icon") == "1") {	
				var id = Gs[i].getAttribute("id");

				var nodes = Gs[i].childNodes;
			
				for(var j=0; j<nodes.length; j++) {
					switch(nodes[j].nodeName) {
						case "title":
							var title = nodes[j].firstChild.nodeValue;	
							break;
						case "size":
							var size = nodes[j].firstChild.nodeValue;	
							break;
						case "date":							
							var date = nodes[j].firstChild.nodeValue;
							break;
						default:
							break;	
					}
				}
				
				// Image
				var iconEl = Gs[i].getElementsByTagName("thumbpath");
				var iconPath = iconEl[0].firstChild.nodeValue;
				var img = document.createElement("img");
				img.setAttribute("src", "photo_resources/"+iconPath);
				img.setAttribute("width", "60px");
				img.setAttribute("height", "45px");			
				img.className = "tinythumb icon";
				

				// Anchor
				var a = document.createElement("a");
				a.setAttribute("href", "javascript:void(0)");

				a.onclick = (function (id) { return function (e) { library.displayGalleryThumbs(id); }; })(id);

				a.onmouseover = (function (id, title, date, size, iconPath) { return function (e) { library.displayPreview(e, id, title, date, size, iconPath); }; })(id, title, date, size, iconPath);

				a.onmouseout = (function (id) { return function (e) { library.removePreview(e, id); }; })(id);
			
				a.style.display = "none";
				a.appendChild(img);
				pane.appendChild(a);
			}
		}
	}
	
	this.paneState = "icons";
}


Library.prototype.unhideThumbs = function(e) {
	var pane = document.getElementById("pane");
	var showMore = document.getElementById("showmore");
	pane.removeChild(showMore);

	allAs = document.documentElement.getElementsByTagName("a");
	for(var i=0; i< allAs.length; i++) {
		if(allAs[i].style.display == "none") {
			allAs[i].style.display = "inline";
		}
	}
}



Library.prototype.getGalleryHead = function(xml, gid, pid, photoEl) {


	var galleryHead = document.createElement("div");
	galleryHead.className = "galleryhead";
	galleryHead.setAttribute("id", "galleryhead");

	var title = document.createElement("h1");	
	var date = document.createElement("span");
	title.className = "gallerytitle";
	date.className = "galleryinfo gallerydate";


	var nodes = xml.documentElement.childNodes

	for(var i=0; i<nodes.length; i++) {
		switch(nodes[i].nodeName) {
			case "title":						
				title.appendChild( document.createTextNode(nodes[i].firstChild.nodeValue));	
				break;
			case "date":	
				date.appendChild(
				document.createTextNode(nodes[i].firstChild.nodeValue));
				break;
			default:
				break;	
		}
	}


	var number = xml.documentElement.getElementsByTagName("photo").length;
	var numberOfPhotos = document.createElement("span");
	numberOfPhotos.className = "galleryinfo";
	numberOfPhotos.appendChild(document.createTextNode(" ("+number+" photos)"));




	
	var permalink = document.createElement("a");
	permalink.className = "permalink";
	if(pid != null) {
		permalink.setAttribute("href", "photos/"+gid+"/"+pid);
	} else {
		permalink.setAttribute("href", "photos/"+gid);
	}
	permalink.appendChild(document.createTextNode("Permalink"));	
	
	if(pid) {
		var photographer = document.createElement("span");
		photographer.className = "galleryinfo";
		var photoTitle = document.createElement("span");
		photoTitle.className = "galleryinfo";			
		var photoDate = document.createElement("span");
		photoDate.className = "galleryinfo";			
		
		
		var photoNodes = photoEl.childNodes;
		for(var i=0; i<photoNodes.length; i++) {
			switch(photoNodes[i].nodeName) {
				case "title":
					if(photoNodes[i].childNodes.length > 0) {
						photoTitle.appendChild( document.createTextNode(photoNodes[i].firstChild.nodeValue));	
					}
					break;
				case "photographer":						
					photographer.appendChild( document.createTextNode(" by "+photoNodes[i].firstChild.nodeValue));	
					break;
				case "datetime":	
					photoDate.appendChild( document.createTextNode( photoNodes[i].firstChild.nodeValue));
					break;
				default:
					break;	
			}
		}


		galleryHead.appendChild(title);
		galleryHead.appendChild(photoDate);
		galleryHead.appendChild(photographer);
		
		galleryHead.appendChild(permalink);


	} else {
	
		galleryHead.appendChild(title);
		galleryHead.appendChild(date);
		galleryHead.appendChild(numberOfPhotos);
		galleryHead.appendChild(permalink);

	}

	return galleryHead;

}

//Library.prototype.displayGalleryThumbs = function(gid) {

Library.prototype.displayGalleryThumbs = function(gid) {

	xml = sendRequest(this.folder+gid+".xml");

	var galleryHead = this.getGalleryHead(xml, gid, null);	
	var content = document.getElementById("content");
	try { content.innerHTML = ""; } catch(err) { }
	content.appendChild(galleryHead);	

	
	Ps = xml.documentElement.getElementsByTagName("photo");
	for(var i=0; i<Ps.length; i++) {	
		var pid = Ps[i].getAttribute("id");
		var islandscape = Ps[i].getElementsByTagName("islandscape")[0].firstChild.nodeValue;
		var div = document.createElement("div"); 
		var cla = "thumb";
		if(islandscape == 1) {
			cla = "thumb lthumb";
		}
		div.className = cla;
		
		var a = document.createElement("a");
		a.setAttribute("href", "javascript:void(0)");		
		a.className = cla;
		a.onclick = (function (gid, pid) { return function (e) { library.displayGalleryPicture(gid, pid); }; })(gid, pid);		
		
		var img = document.createElement("img");
		var ImgSrc = Ps[i].getElementsByTagName("thumbpath")[0].firstChild.nodeValue;
		img.setAttribute("src", "photo_resources/"+ImgSrc);
		img.className = cla;
		if(islandscape == 1) {
			img.setAttribute("width", "160px");
			img.setAttribute("height", "120px");
		} else {
			img.setAttribute("width", "120px");
			img.setAttribute("height", "160px");		
		}

		a.appendChild(img);		
		div.appendChild(a);		
		content.appendChild(div);
	}
	
	if(this.paneState != "icons") {
		this.getGalleryIconsHTML(1+this.numberOfGalleries);
	}
	scrollToTop();
}



Library.prototype.displayGalleryPicture = function(gid, pid) {

	xml = sendRequest(this.folder+gid+".xml");

	var pane = document.getElementById("pane");
	if(this.paneState == "icons") {
		try {
			pane.innerHTML = "";
		} catch(err) { }
	}	
	
	var photoImg = document.createElement("img");
	var prevPid = null;
	var nextPid = null;
	
	Ps = xml.documentElement.getElementsByTagName("photo");
	for(var i=0; i<Ps.length; i++) {	

		var islandscape = Ps[i].getElementsByTagName("islandscape")[0].firstChild.nodeValue;
		var thispid = Ps[i].getAttribute("id");

		if(thispid == pid) {
			var galleryHead = this.getGalleryHead(xml, gid, pid, Ps[i]);		
			if(i > 0) { 
				var prevPid = Ps[i-1].getAttribute("id"); 
			}
			if(i < Ps.length-1) { 
				var nextPid = Ps[i+1].getAttribute("id"); 
				photoImg.onclick = (function (gid, nextPid) { return function (e) { library.displayGalleryPicture(gid, nextPid); }; })(gid, nextPid);
			}		
			var photoSrc = Ps[i].getElementsByTagName("photopath")[0].firstChild.nodeValue;

			photoImg.setAttribute("src", "photo_resources/"+photoSrc);
			
			
			if(this.paneState == "thumbs") {
				break;
			}
		}
		
		if(this.paneState == "icons") {
		
			var div = document.createElement("div"); 
			div.className = "tinythumb";
			
			var a = document.createElement("a");
			a.setAttribute("href", "javascript:void(0)");
			if(islandscape == 1) {
				a.className = "tinylthumb";
			}
			a.onclick = (function (gid, thispid) { return function (e) { library.displayGalleryPicture(gid, thispid); }; })(gid, thispid);
			
			var img = document.createElement("img");
			var thumbSrc = Ps[i].getElementsByTagName("thumbpath")[0].firstChild.nodeValue;
			img.setAttribute("src", "photo_resources/"+thumbSrc);
			img.className = "tinythumb";
			
			if(islandscape == 1) {
				img.style.width = 60+"px";
				img.style.height = 45+"px";
			} else {
				img.style.width = 45+"px";
				img.style.height = 60+"px";
			}
			
			a.appendChild(img);		
			div.appendChild(a);
			
			pane.appendChild(div);
		}
	}
	this.paneState = "thumbs";

//	var galleryHead = this.getGalleryHead(xml, gid, pid);

	var gallerycontrol = document.createElement("div");
	gallerycontrol.className = "gallerycontrol";

	if(prevPid != null) {
		var prevA = document.createElement("a");
		prevA.setAttribute("href", "javascript:void(0)");
		prevA.onclick = (function (gid, prevPid) { return function (e) { library.displayGalleryPicture(gid, prevPid); }; })(gid, prevPid);
		prevA.appendChild(document.createTextNode("Previous"));
		gallerycontrol.appendChild(prevA);
		gallerycontrol.appendChild(document.createTextNode(" : "));
	}

	var backUp = document.createElement("a");
	backUp.setAttribute("href", "javascript:void(0)");
	backUp.onclick = (function (gid) { return function (e) { library.displayGalleryThumbs(gid); }; })(gid);
	backUp.appendChild(document.createTextNode("Back to gallery"));
	gallerycontrol.appendChild(backUp);

	if(nextPid != null) {
		var nextA = document.createElement("a");
		nextA.setAttribute("href", "javascript:void(0)");
		nextA.onclick = (function (gid, nextPid) { return function (e) { library.displayGalleryPicture(gid, nextPid); }; })(gid, nextPid);
		nextA.appendChild(document.createTextNode("Next"));
		gallerycontrol.appendChild(document.createTextNode(" : "));		
		gallerycontrol.appendChild(nextA);
	}	
	
	var photo = document.createElement("div");
	photo.className = "photo";
	photoImg.className = "photo";
	photo.appendChild(photoImg);

	var content = document.getElementById("content");
	try {
		content.innerHTML = "";
	} catch(err) { }


//	galleryHead.appendChild(gallerycontrol);
	content.appendChild(galleryHead);
	content.appendChild(gallerycontrol);
	
/*	
	var clear = document.createElement("div");
	clear.style.clear = "right";
	clear.style.height = 50+"px";	
	content.appendChild(clear);		
*/

	content.appendChild(photo);
	scrollToTop();
}



Library.prototype.displayInitialContent = function() {
	stats = this.xml.documentElement.getElementsByTagName("stats");
	stats = stats[0];

	var libStats = document.createElement("div");
	libStats.className = "stats";
	
	var basic = document.createElement("p");
	basic.className = "stats";
	basic.appendChild(document.createTextNode("There are "+stats.getAttribute("galleries")+" photo galleries, containing a total of "+stats.getAttribute("photos")+" photos, taken by "+stats.getAttribute("phs")+" photographers."));
	
	phsEl = stats.getElementsByTagName("photographer");
	var phsDiv = document.createElement("div");
	phsDiv.className = "statsPhs";

	for(var i=0; i<phsEl.length; i++) {
		var phDiv = document.createElement("div");
		phDiv.className = "statsPh";
		var phName = document.createElement("div");
		phName.className = "statsPhName";
		phName.appendChild(document.createTextNode( phsEl[i].getAttribute("name")));
		var phBar = document.createElement("div");
		phBar.className = "statsPhBar";		
		phBar.style.width = 55 * phsEl[i].getAttribute("fraction")+"%";
		phBar.appendChild(document.createTextNode( phsEl[i].getAttribute("total")));		

		phDiv.appendChild(phName);
		phDiv.appendChild(phBar);
		phsDiv.appendChild(phDiv);

		var br = document.createElement("br");
//		phsDiv.appendChild(br);		
		
	}


	libStats.appendChild(basic);
	libStats.appendChild(phsDiv);
	var content = document.getElementById("content");
	if(content.innerHTML != null)
		content.innerHTML = "";
	content.appendChild(libStats);
}


Library.prototype.displayPreview = function(e, gid, t, d, s, i) {

	if (!e) return; // var e = window.event;
	if (e.target) target = e.target;
	else if (e.srcElement) target = e.srcElement;
	if (target.nodeType == 3) // defeat Safari bug
		target = targ.parentNode;

	target.onmousemove = (function (gid) { return function (e) { library.positionPreview(e, gid); }; })(gid);

	var preview = document.createElement("div");
	preview.className = "preview";
	preview.setAttribute("id", gid);

	var title = document.createElement("h2");
	title.className = "previewItem previewTitle";
	title.appendChild(document.createTextNode(t));
	
	var date = document.createElement("span");
	date.className = "previewItem previewDate";
	date.appendChild(document.createTextNode(d));
	
	var size = document.createElement("span");
	size.className = "previewItem previewNumberOfPhotos";
	size.appendChild(document.createTextNode(" ("+s+" photos)"));
	
	var img = document.createElement("img");
	img.setAttribute("src", "photo_resources/"+i);
	img.setAttribute("width", "160px");
	img.setAttribute("height", "120px");			
	img.className = "previewItem previewImage";
	
	preview.appendChild(title);
	preview.appendChild(date);
	preview.appendChild(size);
	preview.appendChild(img);	

	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	// Do something with this information



	var offsetY = 200;
	var offsetX = 30;
	preview.style.top = (posy-offsetY)+"px";
	preview.style.left = (posx+offsetX)+"px";


	document.documentElement.appendChild(preview);

}

Library.prototype.positionPreview = function(e, gid) {
	if (!e) return; // var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;

	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	// Do something with this information



	var offsetY = 200;
	var offsetX = 30;
	preview = document.getElementById(gid);
	preview.style.top = (posy-offsetY)+"px";
	preview.style.left = (posx+offsetX)+"px";
	var debug = document.getElementById("debug");
	debug.innerHTML = posx+" "+posy;
}

Library.prototype.removePreview = function(e, gid) {
	if (!e) return; // var e = window.event;
	if (e.target) target = e.target;
	else if (e.srcElement) target = e.srcElement;
	if (target.nodeType == 3) { // defeat Safari bug
		target = targ.parentNode;
	}

	target.onmousemove = null;
	document.documentElement.removeChild(document.getElementById(gid));
}




