// This code is created and copyrighted by ::atd, Arne Schuber! atd.schubert(ät)googlemail.com


function Gallery() {
	this.domRoot = document.getElementById("galleryMain");
	this.canvas = document.getElementById("galleryCanvas");
	this.output = document.getElementById("galphoto");
	this.title = document.getElementById("galleryTitle");
	this.link = document.getElementById("galleryLink");
	this.src = this.output.getAttribute("src");
	this.folders = new Array();
	
	this.loadPhoto = function(src) {
		var height;
		this.src = src;
		if (window.innerHeight-420<20) {height=20;} else {height=window.innerHeight-420;}
		rg = src+"&maxHeight="+(window.innerHeight-420)+"&maxWidth=600";
		this.output.setAttribute("src", src+"&maxWidth=580"+"&maxHeight="+height);
		this.output.parentNode.setAttribute("href", src);
	};
	this.addFolder = function(title) {
		var obj = new Folder(this, title);
		this.folders.push(obj);
		this.folders[title] = obj;
		return obj;
	};
	this.reloadPhoto = function() {
		if(this.src) {
			this.loadPhoto(this.src);
		}
	};
	this.onResize = function() {
		this.canvas.setAttribute("style","height:"+(window.innerHeight-420)+"px;");
	}
}

function Folder(gallery, title) {
	this.title = title;
	this.gallery = gallery;
	
	this.firstPhoto = false;
	this.lastPhoto = false;
	
	this.photofield = document.createElement("div");
	this.photofield.obj = this;
	this.photofield.setAttribute("class", "photofield");
	this.photofield.setAttribute("style", "display: none;");
	
	this.gallery.domRoot.appendChild(this.photofield);

	this.show = function() {
		var i;
		
		for(i=0; i<this.gallery.folders.length; i++) {
			this.gallery.folders[i].hide();
		}
		this.photofield.setAttribute("style", "");
if(this.title.substr(2,1)=="-") {
		this.gallery.title.lastChild.nodeValue = this.title.substr(3);
} else {
		this.gallery.title.lastChild.nodeValue = this.title;
}
		if(this.firstPhoto) {
			this.firstPhoto.show();
		}
	};
	this.hide = function() {
		this.photofield.setAttribute("style", "display: none;");
	};
	
	this.deselect = function () {
		var obj = this.firstPhoto;
		while(obj) {
		obj.folderPic.setAttribute("class", "");
		obj = obj.next;
		}
	}
	
	this.addPhoto = function(src) {
		var obj;
		obj = new Photo(this, src);
		if(this.lastPhoto) {
			this.lastPhoto.next = obj;
			obj.prev = this.lastPhoto;
			this.lastPhoto = obj;
		} else {
			this.lastPhoto = obj;
			this.firstPhoto = obj;
		}
		return obj;
	}
	
	
}

function Photo(folder, src) {
	this.src = src;
	this.folder = folder;
	this.gallery = this.folder.gallery;
	
	this.prev = false;
	this.next = false;
	
	this.folderPic = document.createElement("img");
	this.folderPic.obj = this;
	this.folderPic.setAttribute("src", this.src+"&maxHeight=60"); //&maxWidth=45
	this.folderPic.setAttribute("onclick", "this.obj.show();");
	
	this.folder.photofield.appendChild(this.folderPic)
	
	this.show = function() {
		this.gallery.loadPhoto(this.src);
		this.gallery.output.obj = this;
		this.folder.deselect();
		this.folderPic.setAttribute("class", "active");
	}
}



