function EventCalendar() {
	this.id								= null;

	this.year							= null;
	this.month 						= null;
	this.day							= null;
	this.eventsfile				= null;
	
	this.div							= null;
	this.daysTable				= null;
	this.details					= null;
	
	this.eventDays				= [];	
	
	this.type							= null;
}

// constants
{
CalendarTYPE_EDIT			= 2;
CalendarTYPE_SMALL		= 1;
CalendarTYPE_LARGE		= 0;

DAYSOFTHEWEEK					= ['S', 
												 'M', 
												 'T', 
												 'W', 
												 'T', 
												 'F', 
												 'S'];
DAYSOFTHEWEEKSHORT		= ['Sun', 
												 'Mon', 
												 'Tue', 
												 'Wed', 
												 'Thu', 
												 'Fri', 
												 'Sat'];
DAYSINAMONTH						= [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
MONTHS								= ['January', 
												 'February', 
												 'March', 
												 'April', 
												 'May',
												 'June', 
												 'July', 												 
												 'August', 
												 'September', 
												 'October', 
												 'November',
												 'December'];
}

EventCalendar.prototype.init										= EventCalendar_Initialize;
EventCalendar.prototype.setType									= EventCalendar_SetType;
EventCalendar.prototype.load										= EventCalendar_Load;
EventCalendar.prototype.clear										= EventCalendar_Clear;
EventCalendar.prototype.next										= EventCalendar_Next;
EventCalendar.prototype.prev										= EventCalendar_Prev;
EventCalendar.prototype.isLeapYear							= EventCalendar_IsLeapYear;
EventCalendar.prototype.build										= EventCalendar_Build;
EventCalendar.prototype.startDay								= EventCalendar_MonthStartDayOfWeek;
EventCalendar.prototype.setEventDay							= EventCalendar_SetEventDate;
EventCalendar.prototype.setEventPath						= EventCalendar_SetPathToGetEventDatesFile;
EventCalendar.prototype.colourToday							= EventCalendar_ColourToday;
EventCalendar.prototype.updateDetails						= EventCalendar_UpdateDetails;

function EventCalendar_Initialize(id) {
	var today 	= new Date();
	
	this.id			= id;
	this.div 		= document.getElementById(id);
		
	// if attempting to generate large calendar on non-large-calendar page_id
	// Leave emediatly
	if(!this.type && !!!this.div)
		return;
	
	// Note: these count from 0;
	this.year 	= today.getFullYear();
	this.month 	= today.getMonth();
	this.day 		= today.getDate();
	
	// TODO: Build Outside of table.
	if(this.type) { 
		this.div.style.height	 	= '230px';	
		this.div.style.width 		= '222px';
	} else {	
		this.div.style.width 		= '520px';
		this.div.style.height	 	= '550px';
	}

	// Append to body
	this.daysTable = document.createElement('table');
	this.div.appendChild(this.daysTable);	
	
	// Event Calendar	
	this.build();	
	this.load();
}
function EventCalendar_Clear() {
	// Can't use innerHTML=''. It'll blow up IE.
	while(this.daysTable.rows.length)  
		this.daysTable.deleteRow(this.daysTable.rows.length-1);
		
	this.eventDays					 		= [];
	this.details				 				= null;
}
function EventCalendar_Build() {
	this.clear();
	
	this.tbody = document.createElement('tbody');
	this.daysTable.appendChild(this.tbody);
	
	// Style Table
	this.daysTable.style.fontFamily		 		= 'Arial';
	this.daysTable.style.fontSize		 			= '14px';
	this.daysTable.style.width			 			= '100%';
	
	if(this.type) {
		this.daysTable.style.backgroundColor 	= '#812C0E';
		this.daysTable.style.color						= '#ffffff';	
	} else {
		this.daysTable.style.backgroundColor 	= '#ffffff';
		this.daysTable.style.color						= '#000000';
	}
	
	// Title
	var titleRow  = document.createElement('tr');
	var titleCell = document.createElement('td');
	titleCell.colSpan = 7;
	if(this.type) {
		titleCell.style.paddingBottom = '10px';
		titleCell.innerHTML = '<b>Event Calendar: <span style="color:#db781a">' + MONTHS[this.month] + ' ' + this.year + '</span></b>';
	} else {
		titleCell.align='center';
		titleCell.style.padding = '10px 10px';
		titleCell.style.backgroundColor = '#812C0E';
		
		// LEFT
		var that = this;
		var left = document.createElement('img');
		left.src = 'http://www.guidetogame.com/wp-content/themes/guidetogame/images/left-arrow.png';
		left.style.left = '0px';
		left.onclick = function(e) {
			that.prev();
		}
		
		// TITLE
		var title = document.createElement('span');
		title.style.color = '#fff';
		title.style.fontSize = '20px';
		title.style.padding = '0 155px';
		title.innerHTML = MONTHS[this.month] + ' ' + this.year;
		
		// RIGHT
		var right = document.createElement('img');
		right.src = 'http://www.guidetogame.com/wp-content/themes/guidetogame/images/right-arrow.png';
		right.style.right = '0px';
		right.onclick = function(e) {
			that.next();
		}
		
		titleCell.appendChild(left);
		titleCell.appendChild(title);
		titleCell.appendChild(right);
		
		/*titleCell.innerHTML = '<img src="http://www.guidetogame.com/wp-content/themes/guidetogame/images/right-arrow.png" style="left:0px;" onclick=""> ' +
													'<span style="color:#ffffff;font-size:20px;">' + MONTHS[this.month] + ' ' + this.year + '</span>' + 
													'<img src="http://www.guidetogame.com/wp-content/themes/guidetogame/images/left-arrow.png" style="left:0px;"> ';*/
	}
	titleRow.appendChild(titleCell);
	this.tbody.appendChild(titleRow);
		
	// Build week Letters
	var theRow = document.createElement('tr');
	if(this.type) {
		theRow.style.backgroundColor = '#9C340E';
		this.tbody.appendChild(theRow);
		for(var i=0;i<DAYSOFTHEWEEK.length;i++) {
			var theCell = document.createElement('td');
			theCell.align='center';
			theCell.style.padding = '4px 0px';
			theCell.innerHTML = DAYSOFTHEWEEK[i]
			theRow.appendChild(theCell);		
		}
	} else {
		theRow.style.backgroundColor = '#dddddd';
		this.tbody.appendChild(theRow);
		for(var i=0;i<DAYSOFTHEWEEKSHORT.length;i++) {
			var theCell = document.createElement('td');
			theCell.align='center';
			theCell.style.width = '74px';
			theCell.innerHTML = DAYSOFTHEWEEKSHORT[i]
			theRow.appendChild(theCell);		
		}
	}
	
	// Build First Week
	var day = 1;
	theRow = document.createElement('tr');
	if(!this.type)
		theRow.style.height = '50px';
	var startDay = this.startDay(this.month, this.year);
	this.tbody.appendChild(theRow);
	for(var i=0;i<DAYSOFTHEWEEK.length;i++) {
		var theDay = document.createElement('td');
		if(i < startDay) {
			theDay.innerHTML = '';
			theDay.style.padding = '4px 0px';
			if(!this.type) {
				theDay.style.borderRight 	= '1px solid #ddd';
				theDay.style.borderBottom = '1px solid #ddd';
			}
			theRow.appendChild(theDay);
		} else {
			theDay.innerHTML = day;
			if(this.type) {
				theDay.align='center';
				theDay.id = 'day_' + day;			
				theDay.style.padding = '4px 0px';
			} else {
				theDay.align='left';
				theDay.id = 'lday_' + day;	
				theDay.style.padding = '5px';
				theDay.style.borderRight 	= '1px solid #ddd';
				theDay.style.borderBottom = '1px solid #ddd';
				theDay.style.width = '74px';
			}
			theRow.appendChild(theDay);
			day++;
		}
	}
	
	// Build Weeks
	while(day <= DAYSINAMONTH[this.month]) {		
		theRow = document.createElement('tr');
		this.tbody.appendChild(theRow);
		if(!this.type)
			theRow.style.height = '50px';
		
		// Build 1 week
		for(var i=0;i<DAYSOFTHEWEEK.length;i++) {
			var theDay = document.createElement('td');
			
			if(day <= DAYSINAMONTH[this.month])
				theDay.innerHTML = day;
			
			if(this.type) {
				theDay.id = 'day_' + day;			
				theDay.align='center';
				theDay.style.padding = '4px 0px';
			} else {
				theDay.align='left';
				theDay.id = 'lday_' + day;	
				theDay.style.padding = '5px';
				theDay.style.borderRight 	= '1px solid #ddd';
				theDay.style.borderBottom = '1px solid #ddd';
				theDay.style.width = '74px';
			}
			theRow.appendChild(theDay);

			if(day <= DAYSINAMONTH[this.month])
				day++;
		}		
	}
	
	// "Full Event Calander - LINK"
	if(this.type) {		
		var theRow = document.createElement('tr');
		var theCell = document.createElement('td');
		theCell.innerHTML = '<a href="http://www.guidetogame.com/?page_id=85">Full Event Calander</a>';
		theCell.style.fontSize = '10px';
		theCell.align='center';
		theCell.colSpan		= 7;
		theRow.appendChild(theCell);
		this.tbody.appendChild(theRow);
	}
	this.colourToday();
}
function EventCalendar_Next() {
	this.month++;
	
	// spills into next year.
	if(this.month>MONTHS.length-1) {
		this.month = 0;
		this.year++;
	}
	
	this.build();
	this.load();
}
function EventCalendar_Prev() {
	this.month--;
	
	//  spills into previous year
	if(this.month<0) {
		this.month = 11;
		this.year--;
	}
	
	this.build();
	this.load();
}
function EventCalendar_MonthStartDayOfWeek(_month, _year) {
	var tmpYear = 2006;
	var offSet	= 0;
	
	function increment(_amount) {
		offSet = (offSet+_amount) % 7;
	}	
	
	// Year OffSet
	while(tmpYear<_year) {
		if(this.isLeapYear(tmpYear))
			increment(1);
		increment(1);
		tmpYear++;
	}
	
	// Month OffSet
	for(var i=0;i<this.month;i++) {
		increment(DAYSINAMONTH[i]);	
		// if feburary + this year is a leapyear
		if(i==1 && this.isLeapYear(this.year))
			increment(1);			
	}
	
	return offSet;
}
function EventCalendar_SetType(_type) {
	this.type = _type;
}
function EventCalendar_SetPathToGetEventDatesFile(_path) {
	this.eventsfile = _path;
}
function EventCalendar_IsLeapYear(_year) {
	if(_year % 400 == 0)
		return true;
	if(_year % 100 == 0)
		return false;
	if(_year % 4 == 0)
		return true;
	return false;	
}
function EventCalendar_Load() {
	this.eventDays = [];

	// if attempting to generate large calendar on non-large-calendar page_id
	// Leave emediatly
		
	// TODO : AJAX out to database, and get all the event dates for this month/year
	var xmlhttp = null;
	
	if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
  else // code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	
	xmlhttp.that=this;
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			var records = JSON.parse(this.responseText);
			
			//alert(mysqlTimeStampToDate(records[0][4]).getMonth());
			
			for(var i=0;i<records.length;i++) {
				this.that.setEventDay(records[i], this.that.type, this.that);
			}	
					
			this.that.colourToday();
		} 
	}
	
	xmlhttp.open("GET",this.eventsfile+"?month="+this.month+"&year="+this.year,true);
	xmlhttp.send();
}

function EventCalendar_SetEventxDate(_record, _type, _ctx) {

}

function EventCalendar_SetEventDate(_record, _type, _ctx) {
	var _day = mysqlTimeStampToDate(_record[4]).getDate();

	if(_type) {
		var newDiv = document.createElement('div');
		newDiv.style.backgroundColor = '#d4520f';	
		newDiv.innerHTML = _day;
		newDiv.className = 'roundedCorners';
		newDiv.style.maxWidth		 = '22px';
		newDiv.style.maxHeight		 = '22px';
		var theDay = document.getElementById('day_' + _day)
		theDay.innerHTML = '';
		theDay.appendChild(newDiv);
	}

	var theDay = document.getElementById('lday_' + _day);
	if(theDay) {
		var _event = _record[1];				
		var theDay = document.getElementById('lday_' + _day);
		theDay.onclick = function() {
			_ctx.updateDetails(_ctx, _record);
		};
		theDay.innerHTML = _day + '<br /><h3><span style="font-size:8px;">' + _event + '</span></h3>';
	}	
}
function EventCalendar_ColourToday() {
	// Color today
	if(this.type) {
		var theDay = document.getElementById('day_' + this.day)
		
		if(theDay.children.length) {
			theDay.children[0].style.color = '#000000';
			//theDay.children[0].style.height = '16px';
			//theDay.children[0].style.width	= '20px';
		} else {
			var newDiv = document.createElement('div');
			newDiv.style.backgroundColor = '#9C340E';	
			newDiv.innerHTML = this.day;
			newDiv.className = 'roundedCorners';
			newDiv.style.maxWidth		 = '22px';
			newDiv.style.maxHeight		 = '22px';
			
			theDay.innerHTML = '';
			theDay.appendChild(newDiv);
		}
	}
}
function EventCalendar_UpdateDetails(_ctx, _record) {
	// Create new details panel.
	if(_ctx.details==null) {
		var row 															= document.createElement('tr');
		var cell 															= document.createElement('td');
		cell.colSpan													= 7;
		_ctx.details 													= document.createElement('div');
		_ctx.details.style.backgroundColor 		= '#812C0E';
		_ctx.details.style.padding 						= '10px';
		_ctx.details.style.color							= '#fff';
		_ctx.details.style.height							= '180px';
		
		_ctx.tbody.appendChild(row);
		row.appendChild(cell);
		cell.appendChild(_ctx.details);
	}
	
	// Set contents
	_ctx.details.innerHTML = '<h3 style="font-size:20px;">' + _record[1] + '</h3>' + '<br />' +
													 '<u>Event Type:</u> ' + ((_record[2]=='cert')?'Certification':_record[2]) + '<br />' +
													 '<u>Event Location:</u> ' +_record[3]+ '<br />' +
													 '<u>Details:</u> ' +_record[6]+ '<br />';
	var a 								= document.createElement('a');
	var button 						= document.createElement('img');
	a.href 								= 'http://www.guidetogame.com/?page_id=4';
	button.src 						= 'http://www.guidetogame.com/wp-content/themes/guidetogame/images/booknow.png';
	
	a.appendChild(button);
	_ctx.details.appendChild(a);
}

/*

		Utility functions

*/
function mysqlTimeStampToDate(timestamp) {
	//function parses mysql datetime string and returns javascript Date object
	//input has to be in this format: 2007-06-05 15:26:02
	var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
	var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
	return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
}
