function visualMainMenu(){
	var target = "visualMainMenu";
	var visualMainMenu = "<ul style=\"padding-top: 20px;\">";
	var portfolio = getContentByContentPath(siteContent.portfolioPath);
	var portfolioContent = portfolio.subContent;
	for (var foo = 0; foo < portfolioContent.length; foo++) {
		visualMainMenu += "<li style=\"width: 20%; margin-left:5px; margin-right:-5px\"><a href=\"#/" + portfolio.urlLabel + "/" + portfolioContent[foo].urlLabel + "\"><img class=\"frame5\" src=\"images/" + portfolioContent[foo].urlLabel + "5frm.jpg\" /><h3></a><a href=\"#/" + portfolio.urlLabel + "/" + portfolioContent[foo].urlLabel + "\">" + portfolioContent[foo].label + "</h3></a></li>";
	}
	visualMainMenu += "</ul>";
	document.getElementById(target).innerHTML = visualMainMenu;
}

function metaMenu(target){
	var metaMenu = "";
	var meta = sitemapContent;
	for (var foo = 0; foo < meta.length; foo++) {
		if (meta[foo].meta == true) {
			metaMenu += "<a class=\"metaLink\" href=\"#/" + meta[foo].urlLabel + "\">" + meta[foo].label + "</a>";
		}
	}
	document.getElementById(target).innerHTML = metaMenu;
}

function mainMenu(target){
	var mainMenu = "\n";
	var portfolio = getContentByContentPath(siteContent.portfolioPath);
	var portfolioContent = portfolio.subContent;
	for (var foo = 0; foo < portfolioContent.length; foo++) {
		mainMenu += "<a href=\"#/" + portfolio.urlLabel + "/" + portfolioContent[foo].urlLabel + "\" onMouseOver=\"resetMainMenu(" + foo + ",true);\" onMouseOut=\"resetMainMenu();\"><img id=\"menuItem" + foo + "\" src=\"images/" + portfolioContent[foo].urlLabel + "0.gif\" /></a>";
	}
	document.getElementById(target).innerHTML = mainMenu;
}

function resetMainMenu($id, active){
	var portfolio = getContentByContentPath(siteContent.portfolioPath).subContent;
	for (var foo = 0; foo < portfolio.length; foo++) {
		var a = false;
		if (active && foo == $id) {
			a = true;
		}
		if (("1" + NEEDLE + foo) == currentMenuCid) {
			a = true;
		}
		changeMainMenuImage(foo, portfolio[foo].urlLabel, a);
	}
}

function changeMainMenuImage(id, image, active){
	document.getElementById("menuItem" + id).src = "images/" + image + ((active) ? 1 : 0) + ".gif";
}

function objectList(obj){
	var output = new Array();
	for (var i in obj) {
		if (typeof(obj[i]) == "object") {
			var get = objectList(obj[i]);
			for (var a = 0; a < get.length; a++) {
				if (Number(i).toString() == "NaN") {
					var val = "." + get[a];
					output.push(i + "" + val.split(".[").join("["));
				}
				else {
					output.push("[" + i + "]." + get[a]);
				}
			}
		}
		else {
			if (Number(i).toString() == "NaN") {
				output.push(i + " = " + obj[i]);
			}
			else {
				output.push("[" + i + "] = " + obj[i]);
			}
		}
	}
	return output;
}

function getLabelByContentId($cid){
	return getContentByContentId($cid).label;
}

function getUrlLabelByContentId($cid){
	return getContentByContentId($cid).urlLabel;
}

function getLocationUrlByContentId($cid){
	var content = $cid.split(NEEDLE);
	var url = "";
	var cid = content[0];
	for (var foo = 0; foo < content.length; foo++) {
		url += getUrlLabelByContentId(cid) + "/";
		cid += NEEDLE + content[foo + 1];
	}
	return url.substr(0, url.length - 1);
}

var pt = _gat._getTracker("UA-15288346-1");

function newContent($cid){
	if ($cid !== currentCid) {
		lastCid = currentCid;
		currentMenuCid = currentCid = $cid;
		var content = getContentByContentId($cid);
		document.title = getSiteTitleByContentId($cid);
		window.location = "#/" + getLocationUrlByContentId($cid);
		pt._trackPageview(getLocationUrlByContentId($cid));
		resetMainMenu(666, false);
		establishContent($cid);
	}
}
// try{}catch(err){}


function loadHtml(file, fnc){
	var myText = new XMLHttpRequest();
	myText.open("GET", file, true);
	myText.onreadystatechange = function(){
		if (this.readyState == 4) {
			fnc(this.responseText);
		}
	}
	myText.send(null);
}

function insertIntoContent($content){
	document.getElementById("content").innerHTML = $content;
	var fnc = getContentByContentId(currentCid).content.fnc;
	if (fnc) {
		this[fnc]();
	}
}

function showPortFolio($cid){
	var output = "";
	var portfolio = getContentByContentId($cid)
	portfolioContent = portfolio.subContent;
	var objectOverview = "";
	var objectOverview2 = "";
	
	
	var cnt1 = new Array();
	var cnt2 = new Array();
	
	for (var foo = 0; foo < portfolioContent.length; foo++) {
		if (portfolioContent[foo].content.artist == "1") {
			cnt2.push({data:portfolioContent[foo],id:foo});
		}
		else {
			cnt1.push({data:portfolioContent[foo],id:foo});
		}
	}
	
	for (var foo = 0; foo < cnt1.length; foo++) {
		var link = getLocationUrlByContentId($cid + NEEDLE + cnt1[foo].id);
		var title = cnt1[foo].data.label;
		
		var img = (cnt1[foo].data.content.image.length !== undefined) ? cnt1[foo].data.content.image[0].thumb : cnt1[foo].data.content.image.thumb;
		objectOverview += (foo % 3 == 0) ? "<div>" : "";
		objectOverview += "<li style=\"width: 220px;\"><a href=\"#/" + link + "\"><img class=\"frame5\" src=\"" + img + "\" /><h3 style=\"height:33px;\" class=\"thumbText\"></a><a href=\"#" + link + "\">" + title + "</h3></a></li>";
		objectOverview += ((foo + 1) % 3 == 0) ? "</div>" : "";
	}
	for (var foo = 0; foo < cnt2.length; foo++) {
		var link = getLocationUrlByContentId($cid + NEEDLE + cnt2[foo].id);
		var title = cnt2[foo].data.label;
		
		var img = (cnt2[foo].data.content.image.length !== undefined) ? cnt2[foo].data.content.image[0].thumb : cnt2[foo].data.content.image.thumb;
		objectOverview2 += (foo % 3 == 0) ? "<div>" : "";
		objectOverview2 += "<li style=\"width: 220px;\"><a href=\"#/" + link + "\"><img class=\"frame5\" src=\"" + img + "\" /><h3 style=\"height:33px;\" class=\"thumbText\"></a><a href=\"#" + link + "\">" + title + "</h3></a></li>";
		objectOverview2 += ((foo + 1) % 3 == 0) ? "</div>" : "";
	}
	//	for (var foo = 0; foo < portfolioContent.length; foo++) {
	//		var link = getLocationUrlByContentId($cid + NEEDLE + foo);
	//		var title = portfolioContent[foo].label;
	//		
	//		var img = (portfolioContent[foo].content.image.length !== undefined) ? portfolioContent[foo].content.image[0].thumb : portfolioContent[foo].content.image.thumb;
	//		objectOverview += (foo % 3 == 0) ? "<div>" : "";
	//		objectOverview += "<li style=\"width: 187px;\"><a href=\"#/" + link + "\"><img class=\"frame5\" src=\"" + img + "\" /><h3 style=\"height:33px;\" class=\"thumbText\"></a><a href=\"#" + link + "\">" + title + "</h3></a></li>";
	//		objectOverview += ((foo + 1) % 3 == 0) ? "</div>" : "";
	//	}
	
	
	
	objectOverview += (objectOverview.substr(-5) == "</li>") ? "</div>" : "";
	output += "<ul style=\"width:100%;\">";
	output += "<li style=\"width:300px; padding-right:20px;\">";
	output += "<h2>" + portfolio.label + "</h2>";
	output += "<p>" + portfolio.content.copy + "</p>";
	output += "</li>";
	output += "<li id=\"objectOverview2\">" + ((objectOverview2 !== "") ? "Von Jan Witte-Kropius<br><br>" : "") + objectOverview + "</li>";
	output += "</ul>";
	//
	objectOverview2 += (objectOverview.substr(-5) == "</li>") ? "</div>" : "";
	output += "<ul style=\"width:100%;\">";
	output += "<li style=\"width:300px; padding-right:20px;\">";
	output += "<h2></h2>";
	output += "<p></p>";
	output += "</li>";
	output += "<li id=\"objectOverview2\">" + ((objectOverview2 !== "") ? "Von Bernadette Maria Roolf<br><br>" : "") + objectOverview2 + "</li>";
	output += "</ul>";
	//
	document.getElementById("content").innerHTML = output;
}

function bigGalleryImage($img, $title){
	document.getElementById("bigGalleryImage").src = $img;
	document.getElementById("galleryImageText").innerHTML = $title;
}

function portfolioItem($cid){
	var item = getContentByContentId($cid);
	var portfolio = getContentByContentId($cid.split(NEEDLE)[0] + NEEDLE + $cid.split(NEEDLE)[1]).subContent;
	var currentCid = Number($cid.split(NEEDLE)[2]);
	var prevCid = $cid.split(NEEDLE)[0] + NEEDLE + $cid.split(NEEDLE)[1] + NEEDLE + ((currentCid == 0) ? portfolio.length - 1 : currentCid - 1);
	var nextCid = $cid.split(NEEDLE)[0] + NEEDLE + $cid.split(NEEDLE)[1] + NEEDLE + ((currentCid == portfolio.length - 1) ? 0 : currentCid + 1);
	
	var gallery = "<div><li style=\"width: 576px;\"><img id=\"bigGalleryImage\" class=\"frame5\" src=\"\" /><h3 class=\"galleryImageText\" id=\"galleryImageText\"></h3></li></div>";
	
	if (item.content.image.length !== undefined) {
		for (var foo = 0; foo < item.content.image.length; foo++) {
			var title = item.content.image[foo].headline;
			var big = item.content.image[foo].big;
			var thumb = item.content.image[foo].thumb;
			var link = "javascript:bigGalleryImage('" + big + "','" + title + "');";
			gallery += (foo % 3 == 0) ? "<div>" : "";
			gallery += "<li style=\"width: 187px; margin-right:6px;\"><a href=\"" + link + "\"><img class=\"frame5\" src=\"" + thumb + "\" /><h3 class=\"galleryImageText\"></a><a href=\"" + link + "\">" + title + "</h3></a></li>";
			gallery += ((foo + 1) % 3 == 0) ? "</div>" : "";
		}
	}
	var output = "";
	output += "<ul style=\"width:100%;\">";
	output += "<li style=\"width:370px; padding-right:20px;\">";
	output += "<h2>" + item.label + "</h2>";
	output += "<p>" + item.content.copy + "</p>";
	output += "<p class=\"textFooter\"><a style=\"float:left;\" href=\"javascript:newContent('" + prevCid + "');\">vorheriges Projekt</a><a style=\"float:right;\" href=\"javascript:newContent('" + nextCid + "');\">nächstes Projekt</a></p>";
	output += "</li>";
	output += "<li id=\"objectOverview\">" + gallery + "</li>";
	output += "</ul>";
	document.getElementById("content").innerHTML = output;
	if (item.content.image.length !== undefined) {
		bigGalleryImage(item.content.image[0].big, item.content.image[0].headline);
	}
	else {
		bigGalleryImage(item.content.image.big, item.content.image.headline);
	}
}

function establishContent($cid){
	var cid = getContentByContentId($cid);
	var content = cid.content;
	document.getElementById("content").innerHTML = cid.label;
	
	if (content !== undefined) {
		if (content.modul == "loadHtml" && content.file !== "" && content.file !== undefined) {
			loadHtml(content.file, insertIntoContent);
		}
		if (content.modul == "callFirstChild") {
			newContent($cid + NEEDLE + "0");
		}
		if (content.modul == "showPortFolio") {
			showPortFolio($cid);
		}
		
		if (content.modul == "portfolioItem") {
			portfolioItem($cid);
			currentMenuCid = $cid.split(NEEDLE)[0] + NEEDLE + $cid.split(NEEDLE)[1]
			resetMainMenu($cid.split(NEEDLE)[1], true);
		}
	}
}

function getSiteTitleByContentId($cid){
	var siteTitleNeedle = siteContent.siteTitleNeedle;
	var content = $cid.split(NEEDLE);
	var title = siteContent.siteTitle;
	var cid = content[0];
	for (var foo = 0; foo < content.length; foo++) {
		title += " " + siteTitleNeedle + " " + getLabelByContentId(cid);
		cid += NEEDLE + content[foo + 1];
	}
	return title;
}

function checkHash(){
	var cid = "";
	var notFound = true;
	var hash = window.location.hash;
	if (hash == "#/callLastContent") {
		newContent(lastCid);
	}
	else {
		if (hash !== currentHash) {
			cid = getContentIdByHash(hash);
			currentHash = hash;
			if (cid !== undefined) {
				newContent(cid);
			}
			else {
				newContent("0");
			}
		}
	}
}

function getContentByContentPath($name){
	return getContentByContentId(getContentIdByHash($name));
}

function getContentIdByHash($hash){
	var path = new Array();
	var tempHash = $hash.split("#").join("").split("/");
	for (var foo = 0; foo < tempHash.length; foo++) {
		var h = tempHash[foo];
		if (h !== "") {
			path.push((h).toLowerCase());
		}
	}
	var mapPath = sitemapContent;
	var lastPath = sitemapContent;
	for (var a = 0; a < path.length; a++) {
		for (var b = 0; b < mapPath.length; b++) {
			if (mapPath[b].urlLabel == path[a]) {
				lastPath = mapPath[b];
				mapPath = (mapPath[b].subContent && a < path.length - 1) ? mapPath[b].subContent : mapPath[b];
			}
		}
	}
	return (mapPath.cid == undefined) ? lastPath.cid : mapPath.cid;
}

function convertToURL($str){
	return escape($str.toLowerCase().split("ä").join("ae").split("ö").join("oe").split("ü").join("ue").split("ß").join("ss").split(" ").join("_").split("/").join("_").split("&").join("+").split("á").join("a").split("à").join("a").split("â").join("a"));
}

function addItemToSitemap($path, $cid){
	var items = $path.item;
	var itemCount = 0;
	var output = new Array();
	for (var foo = 0; foo < items.length; foo++) {
		var subContent = null;
		var cid = (($cid !== "") ? $cid + NEEDLE : "") + itemCount;
		var label = items[foo].label;
		var urlLabel = convertToURL(label);
		var content = items[foo].content;
		var meta = null;
		if (items[foo].item) {
			subContent = addItemToSitemap(items[foo], cid);
		}
		if (items[foo].meta == "true") {
			meta = true;
		}
		output.push({
			meta: meta,
			cid: cid,
			label: label,
			urlLabel: urlLabel,
			content: content,
			subContent: subContent
		});
		itemCount++;
	}
	return output;
}

var siteContent = new Object();
var sitemapContent;
var currentContent;
var currentCid = null;
var lastCid = null;
var currentHash = null;
var lastHash = null;
var NEEDLE = "|";

function getContentByContentId($cid){
	var output;
	var content = $cid.split(NEEDLE);
	var path = sitemapContent[content[0]];
	output = path;
	if (content.length > 1) {
		for (var foo = 1; foo < content.length; foo++) {
			if (path.subContent[content[foo]]) {
				path = path.subContent[content[foo]];
				output = path;
			}
		}
	}
	return output;
}

function setup($xmlFile){
	var xmlTree = new XML.ObjTree();
	xmlTree.parseHTTP($xmlFile, {}, init)
}

function init($xml){
	siteContent = $xml.content;
	sitemapContent = addItemToSitemap(siteContent.site, "");
	mainMenu("mainMenu");
	metaMenu("metaMenu");
	var hashInt = setInterval(checkHash, 100);
	
}

setup("xml/content.xml");

