var festivalID;
var xml;
var cursor = {x:0, y:0};
document.onmousemove=updateMousePosition;

function updateMousePosition(e) {
	e = e || window.event;
	if (e.pageX || e.pageY) {
		cursor.x = e.pageX;
		cursor.y = e.pageY;
	}
	else {
		cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
		cursor.y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
	}
}

function loadxml(filename) {
	var file = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
	file.open('GET', filename, false);
	file.send(null);
	return file.responseXML;
}

function list(tag,element) {return (element||document).getElementsByTagName(tag)}

function field(label, record, subrecord) {
	if (!label) {
		return (record.text)?record.text:record.textContent;
	}
	else if (subrecord) {
		var r = record.childNodes;
		for (var i=0;i<r.length;i++) if (r[i].nodeName == subrecord) return r[i].getAttribute(label);
	}
	else return record.getAttribute(label);
}

function showhide(state,anchor,content,x,y) {
	if (document.getElementById) {
		var ThisPopup=document.getElementById('popup');
		if (anchor) {
			var elem=document.getElementById(anchor);
			var tagname="";
			if (y==0) y=elem.offsetHeight;
			if (x==0) x=elem.offsetWidth; else x+=cursor.x;
			while (elem&&(typeof(elem)=="object")&&(typeof(elem.tagName)!="undefined")) {
				y+=elem.offsetTop;
//				x+=elem.offsetLeft;
				tagname=elem.tagName.toUpperCase();
				if (tagname=="BODY") elem=0;
				if (typeof(elem)=="object") if (typeof(elem.offsetParent)=="object") elem=elem.offsetParent;
			}
//			x+=cursor.x;
			ThisPopup.style.left=x+'px';
			ThisPopup.style.top=y+'px';
			ThisPopup.innerHTML=content;
			ThisPopup.style.backgroundColor='#ffcf5a';
			ThisPopup.style.borderStyle="ridge";
			ThisPopup.style.width="480px";
			ThisPopup.style.padding="5px";
		}
		ThisPopup.style.visibility=state;
	}
}

function timetocols(t1, t2) {
	var c1=12*parseInt(t1.substring(0,2)) + parseInt(t1.substring(3,5))/5;
	var c2=12*parseInt(t2.substring(0,2)) + parseInt(t2.substring(3,5))/5;
	return c2-c1;
}

function infoact(id) {
	var days = list('day',xml);
	var acts = list('act',xml), act, a;
	var locations = list('location',xml), location, l;
	var performances, performance, p;
	var description, d;
	var icon;
	var html = "", times, text;
	a = 0;
	while(act = acts[a++]) if (field('id', act) == id) {
		html= "<div style='font-family:verdana; font-size:10pt; margin-bottom:10px; text-align:justify;'>";
		html+= "<div style='font-size:15pt; font-weight:bold; clear:left;'>" + field('artist', act) + (field('title', act)?" - " + field('title', act):"") + "</div>";
		html+= "<p/>";
		if (field('source', act, 'image')) html+= "<a href='festivals/" + festivalID + "/images/acts/hires/" + field('source', act, 'image')+"'><img style='float:left; border:0px; margin-right:10px;' src='festivals/" + festivalID + "/images/acts/thumb/" + field('source', act, 'image') + "'/></a>";
		description = list('description', act);
		d = 0;
		while (text = description[d++]) html+= field(0, text);
		html+= "<p style='clear:left;'/>";
		html+= "<div style='float:right; padding-top:5px; padding-left:5px;'>"
		icon = field('source', act, 'icon');
		if (icon) html+= "<a href='http://"+field('website', act, 'icon')+"'><img style='border:0px;' src='festivals/" + festivalID + "/images/" + icon + "'/></a>";
		l=0;
		while(location = locations[l++]) if (field('id', location) == field('location', act, 'performance')) {
			if (field('index', location, 'position')) html+= "<img style='border:0px;' src='festivals/" + festivalID + "/images/locations/location" + field('index', location, 'position') + ".gif'/>";
		}
		html+= "</div>";
		performances = list('performance', act);
		performance = performances[0];
		if (performance) {
			d=0;
			while(day = days[d++]) {
				p = 0; times=0;
				while(performance = performances[p++]) if(field('date', performance) == field('id', day)) times++;
				if (times) {
					html+= "<b>" + field('name', day) + "</b><br/>";
					p = 0;
					while(performance = performances[p++]) if(field('date', performance) == field('id', day)) {
						html+= "&#160;&#160;&#160;" + (field('duration',act)?"aanvang ":"doorlopend ") + field('begin', performance) + (field('duration',act)?"":"-"+field('end',performance));
						l=0;
						while(location = locations[l++]) if (field('id', location) == field('location', performance)) {
							html+= ", " + field('name', location) + " [";
							description = list('description', location);
							dd = 0;
							while (text = description[dd++]) html+= field(0, text);
							html+= "]";
						}
						html+="<br/>";
					}
				}
			}
			html+=  "<b>entree: " + (field('price',act)?field('price',act):"gratis") + (field('duration',act)? " | " + field('duration',act):"") + "</b><br/>";
		}
		info = field('website', act, 'contact')
		if (info) html+= "<a style='text-decoration: none;color:#d2691e;' href='http://" + info + "'>" + info + "</a>";
		html+= "</div>";
		html+= "<p style='clear:right;'/>";
	}
	return html;
}

function infolocation(id) {
	var locations = list('location',xml), location, l;
	var description, d;
	var html = "", text;
	l = 0;
	while(location = locations[l++]) if (field('id', location) == id) {
		if (field('source', location, 'image')) html+= "<img style='border:0px; float:left;' src='festivals/" + festivalID + "/images/locations/" + field('source', location, 'image') + "'>";
		html+= "<b>" + field('name', location) + "</b><br>";
		description = list('description',location);
		d = 0;
		while (text = description[d++]) html+= field(0,text);
	}
	return html;
}

function lineup() {
	var acts = list('act',xml), act, a;
	var locations = list('location',xml), location, l;
	a=0;
	var html = header();
	html+= "<div style='font-size:20pt;'>Lineup " + field('name',xml, 'festival') + " " + field('city',xml, 'festival') + " " + field('year',xml, 'festival') + "</div>";
	html+= "<div style='clear:left; height:10px; margin-top:10px; margin-bottom:10px; background-color:#d2691e;'></div>";
	while (act = acts[a++]) {
		html+= infoact(field('id',act));
		html+= "<div style='clear:left; height:10px; margin-top:10px; margin-bottom:10px; background-color:#d2691e;'></div>";
	}
	html+= "<img style='border:0px;' src='festivals/" + festivalID + "/images/plattegrond.gif'/>"
	document.getElementById('franje').innerHTML = html;
}

function speelplan() {
	var act, location, performance;
	var days = list('day',xml);
	var acts = list('act',xml);
	var locations = list('location',xml);
	var html = header();
	html+= "<div style='font-size:20pt;'>Speelplan " + field('name',xml, 'festival') + " " + field('city',xml, 'festival') + " " + field('year',xml, 'festival') + "</div>";
	var d=0;
	while (day = days[d++]) {
		var date = field('id', day);
		html+= "<br/><br/><div style='font-size:20pt; font-weight:bold;'>" + field('name', day) + "</div><br/>";
		html+= "<table>";
		html+= "<tr><td></td>";
		for (c=12*parseInt(field('begin',day).substring(0,2)); c<12*parseInt(field('end',day).substring(0,2));c++) html+="<td><img src='festivals/" + festivalID + "/images/5min.gif'></td>";
		html+= "</tr>";
		var row, r, a, p, t, l = 0, line = 0;
		while (location = locations[l++]) {
			row = new Array;
			r=0;
			a=0;
			while (act = acts[a++]) {
				performances = list('performance',act);
				p=0;
				while (performance = performances[p++]) if ((field('date', performance)==date) && (field('location', performance)==field('id',location)))
					row[r++] = field('begin', performance) + "|" + field('end', performance) + "|" + field('artist', act) + "|" + field('id', act) + "|" + field('title', act);
			}
			row.sort();
			if (row.length) {
				// output time grid
				if (line%6==0) {
					html+="<tr><th><div style='text-align:right;'>tijd</div><div style='text-align:left;'>locatie</div></th>";
					for (c=parseInt(field('begin',day).substring(0,2)); c<parseInt(field('end',day).substring(0,2));c++) {
						html+= "<th class='fix' style='text-align:left; vertical-align:top; background-color:#ddd;' colspan='6'>"+c+"</th>";
						html+= "<th class='fix' style='text-align:left; vertical-align:top;' colspan='6'></th>";
					}
					html+= "</tr>";
				}
				// output performances
				id = "a"+d+"_"+l;
				html+= "<tr" + (line%2?"":" style='background-color:#ddd;'") + ">";
				html+= "<td id=\""+id+"\" class='hover' style='text-align:left; font-weight:bold; padding:5px; background-color:" + (line%2?"#ddd":"#bbb") + ";' onmouseover='showhide(\"visible\",\""+id+"\",infolocation(\"" + field('id', location) + "\"),0,-10);' onmouseout='showhide(\"hidden\");'>" + field('name', location) + "</td>";
				r=0; t=field('begin', day);
				for (r=0;r<row.length;r++) {
					timeslot = row[r].split('|');
					id = "a"+d+"_"+l+"_"+r;
					if (timetocols(t,timeslot[0])) html+= "<td class='fix' colspan='"+ timetocols(t, timeslot[0]) + "'" + (line%2?"":" style='background-color:#dfdfdf;'") + "></td>";
					html+= "<td id=\""+id+"\" class='hover' colspan='"+ timetocols(timeslot[0], timeslot[1]) + "' style='text-align:center; background-color:" + (line%2?"#fcb33a":"#dc931a") + ";' onmouseover='showhide(\"visible\",\""+id+"\",infoact(\"" + timeslot[3] + "\"),-120,0);' onmouseout='showhide(\"hidden\");'>" + timeslot[2] + " <i>" + timeslot[4] + "</i></td>";
					t = timeslot[1];
				}
				if (parseInt(t.substring(0,2)+t.substring(3,5))<100*parseInt(field('end',day).substring(0,2))) html+= "<td class='fix' colspan='"+ timetocols(t, "2430") + "'" + (line%2?"":" style='background-color:#dfdfdf;'") + "></td>";
				html+= "</tr>";
				line++;
			}
			delete row;
		}
		html+= "</table>";
	}
	document.getElementById('franje').innerHTML = html;
}

function affiche() {
	html = header();
	html+= '<img src="festivals/' + festivalID + '/images/affiche.jpg"/>';
	document.getElementById('franje').innerHTML = html;
}

function header() {
	return '<div style="font-size:10pt; font-family:Verdana, Geneva, sans-serif;"><a class="hover" onclick="affiche();">affiche</a> - <a class="hover" onclick="lineup();">lineup</a> - <a class="hover" onclick="speelplan();">speelplan</a> - <a href="http://' + field('hostfestivalsite', xml, 'festival') + '" target="new">' + field('hostfestival', xml, 'festival') + '</a></div><p/>';
}

function franje(id) {
	festivalID = id;
	city = id.substring(id.indexOf('_'));
	year = id.substring(0, id.indexOf('_'));
	xml = loadxml('festivals/' + id + '/festival.xml');
	affiche();
}