String.prototype.trim = function() {
	return this.replace(/(^\s+|\s+$)/,'');
}

function nl2br ( text ) {
	return text.replace(/\n/g, "</p><p>\n");
}

function Calendar ( year, month , language ) {
	switch ( language ) {
			case "nl": 
				this.months = new Array ( "" , "Januari" , "Februari" , "Maart" , "April" , "Mei" , "Juni" , "Juli" , "Augustus" , "September" , "Oktober" , "November" , "December" ); 
				this.days = new Array ( "M" , "D" , "W" , "D" , "V" , "Z" , "Z" );
				this.related = "Gerelateerde artikelen";
				break;
			case "dk": 
				this.months = new Array ( "" , "Januar" , "Februar" , "Marts" , "April" , "Maj" , "Juni" , "Juli" , "August" , "September" , "Oktober" , "November" , "December" ); 
				this.days = new Array ( "M" , "T" , "O" , "T" , "F" , "L" , "S" );
				this.related = "bel&aelig;gtet artikler";
				break;
			default: 
				this.months =  new Array ( "" , "January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December" ); 
				this.days = new Array ( "M" , "T" , "W" , "T" , "F" , "S" );
				this.related = "Related articles";
				break;
	}
	this.year = year;
	this.month = month;
	this.width = 0;
	this.cdiv = "";
	this.filterCountry = "";
	this.filterCircuit = 0;
	this.events = new Array ( );
	this.circuits = new Array ( );

	this.loadEvents = function ( ) {
		this.events = new Array ( );
		var xmlDoc,x;
		var randomnumber=Math.floor(Math.random()*11111);
		var url = "/events.php?r="+randomnumber;
		if ( this.filterCircuit != 0 )
			url += "&ci=" + this.filterCircuit;
		if ( this.filterCountry != "" )
			url += "&c=" + this.filterCountry;
		url += "&y=" + this.year;
		url += "&m=" + this.month;
		if (window.XMLHttpRequest) {
			xmlDoc=new window.XMLHttpRequest();
			try {
				xmlDoc.open("GET",url,false);
				xmlDoc.send();
			} catch ( e ) { alert ( e ) }
			x = xmlDoc.responseXML;
		} else if (ActiveXObject("Microsoft.XMLDOM")) {
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async=false;
			xmlDoc.load(url);
			x = this.xmlDoc;
		} else {
			alert("Can not load calendar");
		}
		
		if ( x != null ) {
			var events = x.getElementsByTagName("event");
			for ( i = 0; i < events.length; i++ ) {
				eventX = new Event ( );
				for ( j = 0; j < events[i].childNodes.length; j++ ) {
					if ( events[i].childNodes.item(j).nodeName != "" && events[i].childNodes.item(j).childNodes[0] != null && events[i].childNodes.item(j).childNodes[0].nodeValue != null && events[i].childNodes.item(j).childNodes[0].nodeValue.trim() != "" ) {
						if ( events[i].childNodes.item(j).nodeName == "id" )
							eventX.setId ( events[i].childNodes.item(j).childNodes[0].nodeValue );
						if ( events[i].childNodes.item(j).nodeName == "name" )
							eventX.setName ( events[i].childNodes.item(j).childNodes[0].nodeValue );
						if ( events[i].childNodes.item(j).nodeName == "description" )
							eventX.setDescription ( events[i].childNodes.item(j).childNodes[0].nodeValue );
						if ( events[i].childNodes.item(j).nodeName == "circuit" )
							eventX.setCircuit ( events[i].childNodes.item(j).childNodes[0].nodeValue );
						if ( events[i].childNodes.item(j).nodeName == "year" )
							eventX.setYear ( events[i].childNodes.item(j).childNodes[0].nodeValue );
						if ( events[i].childNodes.item(j).nodeName == "month" )
							eventX.setMonth ( events[i].childNodes.item(j).childNodes[0].nodeValue );
						if ( events[i].childNodes.item(j).nodeName == "day" )
							eventX.setDay ( events[i].childNodes.item(j).childNodes[0].nodeValue );
					}
					if ( events[i].childNodes.item(j).nodeName == "articles" ) {
						for ( k = 0; k < events[i].childNodes.item(j).childNodes.length; k++ ) {
							var x = events[i].childNodes.item(j).childNodes.item(k).childNodes;
							var u = "";
							var t = "";
							for ( l = 0; l < x.length; l++ ) {
								if ( x.item(l).nodeName != "" && x.item(l).childNodes[0] != null ) {
									if ( x.item(l).nodeName == "url" )
										u =  x.item(l).childNodes[0].nodeValue;
									else if ( x.item(l).nodeName == "title" )
										t =  x.item(l).childNodes[0].nodeValue;
								}
							}
							if ( t.trim ( ) != "" && u.trim ( ) != "" )
								eventX.addArticle ( t , u );
						}
					}
					if ( events[i].childNodes.item(j).nodeName == "images" ) {
						for ( k = 0; k < events[i].childNodes.item(j).childNodes.length; k++ ) {
							var x = events[i].childNodes.item(j).childNodes.item(k).childNodes;
							var us = "";
							var um = "";
							var ub = "";
							var t = "";
							for ( l = 0; l < x.length; l++ ) {
								if ( x.item(l).nodeName != "" && x.item(l).childNodes[0] != null ) {
									if ( x.item(l).nodeName == "urlSmall" )
										us =  x.item(l).childNodes[0].nodeValue;
									else if ( x.item(l).nodeName == "urlMedium" )
										um =  x.item(l).childNodes[0].nodeValue;
									else if ( x.item(l).nodeName == "urlBig" )
										ub =  x.item(l).childNodes[0].nodeValue;
									else if ( x.item(l).nodeName == "title" )
										t =  x.item(l).childNodes[0].nodeValue;
								}
							}
							if ( t.trim ( ) != "" && us.trim ( ) != "" )
								eventX.addImage ( t , us , um , ub );
						}
					}
				}
				if ( eventX.getYear ( ) == this.year && eventX.getMonth ( ) == this.month )
					this.events[this.events.length] = eventX;
			}
		}
		this.printEvents ( );
	}
	
	this.addEvent = function ( e ) {
		x = this.events.length;
		b = true;
		for ( i = 0; i < x; i++ ) {
			if ( this.events[i].getId ( ) == e.getId ( ) ) {
				b = false;
				break;
			}
		}
		if ( b )
			this.events[this.events.length] = e;
	}
	
	this.getYear = function ( ) {
		return this.year;
	}
	
	this.getMonth = function ( ) {
		return this.months[this.month];
	}
	
	this.getDaysInMonth = function ( ) {
		var m = new Array ( 0,31,28,31,30,31,30,31,31,30,31,30,31 );
		var x = 0;
		if ( this.month == 2 && 
			( this.year % 4 ) == 0 && 
			( ( this.year % 100 ) != 0 || ( this.year % 400 ) == 0 ) 
			)
				x = 1;
		return m[this.month] + x;
	}
	
	this.getFirstDayOfMonth = function ( ) {
		var d = new Date();
		d.setFullYear(this.year);
		d.setMonth ( (this.month-1) );
		d.setDate ( 1 );
		var x = d.getDay ( );
		x -= 1;
		if ( x == -1 )
			x = 6;
		return x;
	}
	
	this.printCalendar = function ( x , cdiv ) {
		this.width = x;
		this.cdiv = cdiv;
		document.write ( "<div id=\"calendar" + this.cdiv + "\" class=\"calendar\" style=\"width: " + x + "px;\"></div><div id=\"events\"></div>" );
		this.loadEvents ( );
		this.printInnerHTML ( );
	}
	
	this.printInnerHTML = function ( ) {
		document.getElementById ( "calendar" + this.cdiv).innerHTML = this.getTitleBar ( ) + this.getDaysBar ( ) + this.getDaysTable ( ) + "<div style=\"clear: both\"></div>";
	}
	
	this.getRowHeight = function ( ) {
		return Math.round ( this.width / 7 );
	}
	
	this.getColWidth = function ( ) {
		return Math.round ( this.width / 7 );
	}
	
	this.prevMonth = function ( ) {
		this.changeMonth ( -1 );
	}
	
	this.nextMonth = function ( ) {
		this.changeMonth ( 1 );
	}
	
	this.changeMonth = function ( i ) {
		this.month += i;
		while ( this.month > 12 ) {
			this.month -= 12;
			this.year++;
		}
		while ( this.month < 1 ) {
			this.month += 12;
			this.year--;
		}
		this.loadEvents ( );
		this.printInnerHTML ( );
	}
	
	this.getTitleBar = function ( ) {
		return "<a href=\"#\" class=\"monthNav\" style=\"width: " + this.getColWidth ( ) + "px; height: " + this.getRowHeight ( ) + "px; line-height: " + this.getRowHeight ( ) + "px\" onclick=\"c.prevMonth(); return false;\">&lt;</a><a class=\"calendarTitle\" href=\"#\" onclick=\"" + this.cdiv + ".resetDayFilter();return false;\" style=\"width: " + ( this.getColWidth ( ) * 5 ) + "px; height: " + this.getRowHeight ( ) + "px; line-height: " + this.getRowHeight ( ) + "px\">" + this.getMonth ( ) + " " + this.getYear ( ) + "</a><a href=\"#\" class=\"monthNav\" style=\"width: " + this.getColWidth ( ) + "px; height: " + this.getRowHeight ( ) + "px; line-height: " + this.getRowHeight ( ) + "px\" onclick=\"c.nextMonth(); return false;\">&gt;</a>";
	}
	
	this.getDaysBar = function  ( ) {
		var s = "<div class=\"dayBar\" style=\"height: " + this.getRowHeight ( ) + "px;\">";
		s += "	<ul>";
		for ( i = 0; i < 7; i++ ) {
			s += "		<li style=\"height: " + this.getRowHeight ( ) + "px; width: " + this.getColWidth ( ) + "px; line-height: " + this.getRowHeight ( ) + "px\">" + this.days[i] + "</li>";
		}
		s += "	</ul>";
		s += "</div>";
		return s;
	}
	
	this.getDaysTable = function ( ) {
		var x = this.getDaysInMonth ( );
		var y = this.getFirstDayOfMonth ( );
		var z = x+y;
		if ( z == 28 )
			z = 0;
		else if ( z < 35 )
			z = 35-x-y;
		else if ( z < 42 )
			z = 42-x-y;
		while ( z >= 7 ) {
			z -= 7;
		}
		var s = "<div class=\"daysTable\" style=\"width: " + this.width + "px; height: " + ( this.getRowHeight ( ) * 5 ) + "px;\">";
		for ( var i = 0; i < y; i++ ) {
			s += "<div class=\"emptyDay\" style=\"height: " + this.getRowHeight ( ) + "px; width: " + this.getColWidth ( ) + "px; line-height: " + this.getRowHeight ( ) + "px\"></div>";
		}
		for ( var j = 1; j <= x; j++ ) {
			if ( this.getEventsOnDay ( j ).length > 0 )
				s += "<a href=\"#\" onClick=\"" + this.cdiv + ".printDayEvents("+j+");return false;\" class=\"day\" style=\"height: " + this.getRowHeight ( ) + "px; width: " + this.getColWidth ( ) + "px; line-height: " + this.getRowHeight ( ) + "px\">" + j + "</a>";
			else 
				s += "<div class=\"dayEmpty\" style=\"height: " + this.getRowHeight ( ) + "px; width: " + this.getColWidth ( ) + "px; line-height: " + this.getRowHeight ( ) + "px\">" + j + "</div>";
		}
		for ( var k = 0; k < z; k++ ) {
			s += "<div class=\"emptyDay\" style=\"height: " + this.getRowHeight ( ) + "px; width: " + this.getColWidth ( ) + "px; line-height: " + this.getRowHeight ( ) + "px\"></div>";
		}
		s += "</div>";
		return s;
	}
	
	this.getEventsOnDay = function ( day ) {
		var events = new Array ( );
		for ( var i = 0; i < this.events.length; i++ ) {
			if ( this.events[i].getDay ( ) == day )
				events[events.length] = this.events[i];
		}
		return events;
	}
	
	this.printEvents = function ( ) {
		s = "<ul class=\"eventList\">";
		for ( var i = 0; i < this.events.length; i++ ) {
			s += "<li><a href=\"#information\" onclick=\"" + this.cdiv + ".printEvent(" + this.events[i].getId ( ) + "); return true;\">" + this.events[i].getName ( ) + "</a></li>";
		}
		s += "</ul>";
		document.getElementById('events').innerHTML = s;
	}
	
	this.printDayEvents = function ( day ) {
		s = "<ul class=\"eventList\">";
		events2 = this.getEventsOnDay ( day );
		for ( var i = 0; i < events2.length; i++ ) {
			s += "<li><a href=\"#information\" onclick=\"" + this.cdiv + ".printEvent(" + events2[i].getId ( ) + "); return true;\">" + events2[i].getName ( ) + "</a></li>";
		}
		s += "</ul>";
		document.getElementById('events').innerHTML = s;
	}
	
	this.setCircuitFilter = function ( circuit ) {
		this.filterCircuit = circuit;
		this.loadEvents ( );
		this.printInnerHTML ( );
	}
	
	this.setCountryFilter = function ( country ) {
		this.filterCountry = country;
		this.filterCircuit = 0;
		this.loadEvents ( );
		this.printInnerHTML ( );
	}
	
	this.resetDayFilter = function ( ) {
		this.printEvents ( );
	}
	
	this.resetCircuitFilter = function ( ) {
		this.filterCircuit = 0;
		this.loadEvents ( );
		this.printInnerHTML ( );
	}
	
	this.printEvent = function ( id ) {
		x = this.events.length;
		e = null;
		for ( i = 0; i < x; i++ ) {
			if ( this.events[i].getId ( ) == id ) {
				e = this.events[i];
				break;
			}
		}
		if ( e != null ) {
			var c = this.getCircuit ( e.getCircuit ( ) )
			var t = "<h3>" + e.getDay ( ) + "-" + e.getMonth ( ) + "-" + e.getYear ( ) + " " + e.getName ( ) + "</h3>";
			if ( c != null )
				t += "<address>" + c.getName ( ) + " - " + c.getAddress ( ) + "</address>";
			t += "<p>" + nl2br ( e.getDescription ( ) ) + "</p>";
			var p = "";
			var x = e.getImages ( ).length;
			for ( i = 0; i < x; i++ ) {
				if ( e.getImages()[i][0] != "" )
					p += "<div class=\"eventPhoto\"><img src=\"" + e.getImages()[i][2] + "\" alt=\"" + e.getImages()[i][0] + "\" /><div class=\"eventPhotoTitle\">" + e.getImages()[i][0] + "</div></div>";
				else
					p += "<div class=\"eventPhoto\"><img src=\"" + e.getImages()[i][2] + "\" /></div>";
			}
			
			var a = "";
			var x2 = e.getArticles ( ).length;
			for ( i = 0; i < x2; i++ ) {
				a += "<li><a href=\"" + e.getArticles ( )[i][1] + "\">" + e.getArticles ( )[i][0] + "</a></li>";
			}
			
			if ( a != "" ) {
				t += "<p style=\"margin-top: 20px;\">" + this.related + ":</p><ul class=\"related\">";
				t += a;
				t += "</p>";
			}
			
			if ( p != "" )
				document.getElementById("eventInfo").innerHTML = "<a name=\"information\"></a><div id=\"information\"><div id=\"eventPhotos\">" + p + "</div>" + t + "</div>";
			else
				document.getElementById("eventInfo").innerHTML = "<a name=\"information\"></a><div id=\"information\">" + t + "</div>";
		}
	}
	
	this.addCircuit = function ( id , name , address ) {
		if ( this.getCircuit ( id ) == null )
			this.circuits[this.circuits.length] = new Circuit ( id , name , address );
	}
	
	this.getCircuit = function ( id ) {
		x = this.circuits.length;
		for ( i = 0; i < x; i++ ) {
			if ( this.circuits[i].getId ( ) == id ) {
				return this.circuits[i];
				break;
			}
		}
		return null;
	}
}
