function getInArray(a,v) {
	for (var x=0; x<a.length; x++) {
		if (a[x][0]==v) return a[x][1];
	}
	return false;
}

function getInTimeline(i) {
	for (var x=0; x<timeline.length; x++) {
		for (var y=0; y<timeline[x].length;y++) {
			if (timeline[x][y]==i) return {group:x,position:y};
		}
	}
	return false;
}

function VTMPage(){
	var q = window.location.search;
	var g = q.substring(1, q.length).split("&");
	var s = [];
	for (var x=0; x<g.length; x++) {
		s.push(g[x].split("="));
	}
	this.currentpage = getInArray(s,"id");
	if (this.currentpage == false) {
		//this.currentpage = timeline[0][0]; // start
		this.currentpage = 1;
	}
	this.group = getInTimeline(this.currentpage);
}

window.onload = function(){
	/*var l = document.links;
	var length = l.length;
	for (var x=0; x<length; x++) {
		var i = l[x].href;
		i = i.substring(i.lastIndexOf("/")+1,i.length);
		l[x].href = l[x].href + "?id=" + i.split(".")[0];
	}*/
	var p = new VTMPage();
	if (p.group != false) {
		var progress = document.getElementById('progress');
		if (progress) {
			progress.innerHTML = "s "+(p.group.position+1)+" av "+(timeline[p.group.group].length-1);
		}
		var navigation = document.getElementById('navigation');
		if (navigation) {
			var buttons = navigation.getElementsByTagName('a');
			var fallback = null;
			if (i = timeline[p.group.group][p.group.position-1]) {
				fallback = i;	
			}
			if (i = timeline[p.group.group][p.group.position+1]) {
				var c = document.getElementById('content');
				var pa = document.createElement('p');
				if (typeof i == "string") {
					buttons[0].title = i;
					buttons[0].onclick=function(){
						this.className = this.className + " last";
						tip(this.title);
						return false;
					}
					//pa.innerHTML = i;
				} else {
					buttons[0].href = location.pathname+"?id="+i;
					var a = document.createElement('a');
					a.appendChild(document.createTextNode('Neste >>'));
					a.href = buttons[0].href;
					a.className="next";
					pa.appendChild(a);
				}
				c.appendChild(pa);
				if (!fallback) {
					fallback = i;
				}
			}
			if (fallback) {
				var deletebutton = document.getElementById('deletebutton');
				if (deletebutton) deletebutton.href += "&fallback="+fallback;
			}
			buttons[1].onclick=function(){history.go(-1); return false};
		}
		var current = document.getElementById('current');
		if (current) {
			current.href += "?id="+timeline[p.group.group][0];
		}
	}

	if (window.opener) {
		/*var frame = document.getElementById('frame');
		window.status = frame.offsetHeight;
		var h = frame.offsetHeight;
		var w = frame.offsetWidth;
		window.resizeTo(w+20,h+20);*/
	}
	
	// Simple workaround -- By Tor 28/4 2008
	
	
	/*function getText (o) { 
		return (document.all) ? o.innerText : o.textContent;
	}
	
	var list = document.getElementById("list");
	
	if (list) {
		var links = list.getElementsByTagName("a");
		var mpb 	= null;
		var pi 		= null;
		var parent1 = null;
		var parent2 = null;
		for (var x=0; x<links.length; x++) {
			var l = links[x];
			if(getText(l) == "Praktisk informasjon") {
				var pi = l;
			}
			if(getText(l) == "Manpower Bemanning") {
				var mpb = l;
			}
		}
		
		if (mpb && pi) {
			var parent = pi.parentNode;
			mpb.parentNode.appendChild(pi);
			parent.parentNode.removeChild(parent);
			var a = document.createElement("a");
			a.href="#";
			a.appendChild(document.createTextNode("Manpower Bemanning"));
			a.onclick=function(){
				var disp = mpb.style.display;
				mpb.style.display = pi.style.display = disp == "none" ? "block" : "none";
				return false;
			}
			pi.innerHTML = "&bull; " + pi.innerHTML;
			mpb.parentNode.insertBefore(a, mpb);
			mpb.innerHTML = "&bull; Fagområder";
			mpb.style.display = pi.style.display = "none";
			mpb.className = pi.className = "sub";
			//mpb.style.fontWeight = pi.style.fontWeight = "normal";
			//mpb.style.borderTop = pi.style.borderTop = "1px solid #D06206";
		}
		
		
	}*/
	
	
	
	/*var id = timeline[p.group.group][p.group.position];
	var left = document.getElementById("left");
	var l = left.getElementsByTagName("a");
	var pi = false;
	var pic = false;
	for (var x=0; x<l.length; x++) {
		var t = l[x].innerHTML;
		console.log(l[x].parentNode.className);
		if(t.indexOf("Praktisk informasjon") > -1) {
			pi = true;
			var pid = l[x].parentNode;
			var p = pid.cloneNode(true);
			if (p.className=="current") {
				left.className = "preview";	
			} else {
				pid.style.display="none";
				//console.log(p.style.display);
			}
		}
	}
	
	
	if (id=="4") {
		if (pi) left.className = "preview";
		var d = document.createElement("div");
		var a = document.createElement("a");
		a.href = "#";
		a.onclick = function() {
			left.className="";
			p.style.display="none";
			this.style.display="none";
			return false;
		}
		a.appendChild(document.createTextNode("Fagområder"));
		d.appendChild(a);
		left.appendChild(d);
		left.appendChild(p);
		p.style.display="block";
	}*/
	// End of simple workaround
}

function getOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}

function getOffsetTop (el) {
	var ol=el.offsetTop;
	while ((el=el.offsetParent) != null) { ol += el.offsetTop; }
	return ol;
}

function showtestimonial(dropdown) {
	var p = document.getElementById('poptestimonial');
	if (dropdown.value=="") {
		p.style.display="none";	
		return false;
	}
	p.style.background="url(images/"+dropdown.value+")";
	p.style.top = "220px";
	p.style.left = "250px";
	p.style.display="block";
}

function hidetestimonial () {
	var p = document.getElementById('poptestimonial');
	p.style.display="none";
	document.getElementById('testimonial').selectedIndex=0;
}

function tip(t) {
	var a = document.getElementById('alert');
	if (!a) a = document.createElement('div');
	a.id="alert";
	a.innerHTML = t;
	a.style.display='block'; 
}
function description(o,c) {
	o.className=c;
}

