window.onload = function () {
	setLinks();
	loadPage("Home");
	$("title").onclick = loadPage;
};

function setLinks() {
	var links = $$(".locallink");
	for (var i = 0; i < links.length; i++) {
		links[i].onclick = loadPage;
		links[i].onmouseover = onHover;
		links[i].onmouseout = offHover;
	}
}

function loadPage(page) {
	if (page != "Home") {
		page = this.getAttribute("value");
		if (page == "") {
			page = this.textContent;
		}
	}
	
	if ($("title").firstDescendant() != null) {
		$("title").firstDescendant().remove();
	}
	
	var h2 = document.createElement("h2");
	h2.innerHTML = page;
	$("title").appendChild(h2);
	$("title").setAttribute("value", page);
	
	new Ajax.Request (
		"images.php",
		{
			method: "get",
			onSuccess: ajaxSuccess,
			parameters:
			{
				page: page
			}
		}
	);
}

function ajaxSuccess(ajax) {
	$("imagearea").innerHTML = ajax.responseText;
	setUp();
}

function onHover() {
	this.addClassName("hover");
}

function offHover() {
	this.removeClassName("hover");
}

function setUp() {
	if ($$(".image img").length != 0) {
		var images = $$(".image img");
		for (var i = 0; i < images.length; i++){
			images[i].onclick = enlarge;
		}
	} else if ($$(".menu").length != 0) {
		var menus = $$(".menu");
		for (var i = 0; i < menus.length; i++) {
			menus[i].onclick = loadPage;
		}
	}
}

function enlarge() {
	var page = this.getAttribute("value");
	if (page == "") {
		page = this.textContent;
	}
	var image = this.getAttribute("alt");
	new Ajax.Request (
		"images.php",
		{
			method: "get",
			onSuccess: ajaxSuccessEnlarge,
			parameters:
			{
				page: page,
				image: image
			}
		}
	);
}

function ajaxSuccessEnlarge(ajax) {
	$("imagearea").innerHTML = ajax.responseText;
	$("enlargedimage").onclick = loadPage;
}

