var ARCalendar = {
/*
	TODO:	1. deal better with multiple events on the same day
*/

	w : true,
	calendar_div_id : 'calendar',
	year : 1583,
	month : 'JANU-ERROR',
	index : -1,
	numdays : 31,
	dayofmonth : 1,
	offset : 0,
	startday : 0,
	events_schedule : {},
	event_dates : {},
	latest : '',
	earliest : '',

	start : function(when,where) {
		var date_string = 'january 1 1970';
		var calendar_div_id = '';
		if (when) {
			date_string = when;
		}
		if (where) {
			this.calendar_div_id = where;
		}
		
		this.ie_cluestick.array_thump();
		
		this.month = date_string.toLowerCase().replace(/^([a-z]+).*/,'$1');
		this.dayofmonth = date_string.toLowerCase().replace(/.*\s+(\d{1,2})\s+\d+$/,'$1');
		this.year = this.warn(date_string.toLowerCase().replace(/^.*\s+(\d{4,})$/,'$1'));
		this.index = this.days_functions.label.indexOf(this.month);
		this.earliest = this.year + this.twodig(this.index);
		this.latest = this.earliest;
		this.get_event_dates();
		this.draw();
	},
	
	ie_cluestick : {
		// first clue: add indexOf method to Arrays
		array_thump : function() {
			if (!Array.indexOf) {
  			Array.prototype.indexOf = function (obj, start) {
    			for (var i = (start || 0); i < this.length; i++) {
      			if (this[i] == obj) {
        			return i;
      			}
    			}
  			}
			}
		} /*,
		 Additional ie clues go here
		 */
	},

	twodig : function(x) {
		x = '0' + x
		return x.replace(/0?(\d{2})$/,'$1');
	},

	days_functions : {
		label : ['january','february','march','april','may','june','july','august','september','october','november','december'],
		january : function(year) { return ['JANUARY', 31, (0 + (( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))) * 6, 0]; },
		february : function(year) { return ['FEBRUARY', 28 + ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) - 0, 3 - (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)), 1]; },
		march : function(year){ return ['MARCH', 31, 3, 2]; },
		april : function(year){ return ['APRIL', 30, 6, 3]; },
		may : function(year){ return ['MAY', 31, 1, 4]; },
		june : function(year){ return ['JUNE', 30, 4, 5]; },
		july : function(year){ return ['JULY', 31, 6, 6]; },
		august : function(year){ return ['AUGUST', 31, 2, 7]; },
		september : function(year){ return ['SEPTEMBER', 30, 5, 8]; },
		october : function(year){ return ['OCTOBER', 31, 0, 9]; },
		november : function(year){ return ['NOVEMBER',30, 3, 10]; },
		december : function(year){ return ['DECEMBER', 31, 5, 11]; } 
	},

	get_days : function(m,y) {
		var profile = ['JANUARY',31,0,0];
		if (typeof m == 'NaN') {
			profile = this.days_functions[this.days_functions.label[m]](y);
		} else {
			profile = this.days_functions[m](y);
		}
		var century_counter = 2 * (3 - ((y.toString().replace(/^(\d{2}).*/, '$1') - 0) % 4));
		var year_counter = ((y.toString().replace(/.*(\d{2})$/, '$1') - 0) / 4 ).toString().split(/\./)[0] - 0;
		var offset = profile[2];
		
		this.name = profile[0];
		this.numdays = profile[1];
		this.index = profile[3];
		this.startday = ((century_counter - 0 + y.toString().replace(/.*(\d{2})$/, '$1') - 0 + year_counter - 0 + offset - 0 + 1) + 0) % 7;
	},
	
	back : function() {
		var n = (12 + ARCalendar.index - 1) % 12;
		if (n == 11) { ARCalendar.year--; }
		ARCalendar.month = ARCalendar.days_functions.label[n];
		ARCalendar.draw();
		return false;
	},
	
	next : function() {
		var n = (ARCalendar.index + 1) % 12;
		if (n == 0) { ARCalendar.year++; }
		ARCalendar.month = ARCalendar.days_functions.label[n];
		ARCalendar.draw();
		return false;
	},

	get_event_dates : function() {
		var dates_div = document.getElementById('dates').cloneNode(true);
		var dates = dates_div.getElementsByTagName('div');
		this.earliest = this.latest = (this.year + this.twodig(this.index));
		for (i = 0; i < dates.length; i++) {
			var r = dates[i].title.replace(/^.*header=\[([A-Za-z]+)\,?\s+\d{1,2}\,?\s(\d{4,})\s*\].*$/,'$2 $1').toLowerCase().split(/ /);
			var n = this.days_functions.label.indexOf(r[1]);
			if ((r[0] + '' + this.twodig(n)) -0 < this.earliest) {
				this.earliest = (r[0] + '' + this.twodig(n)) -0;
			}
			if ((r[0] + '' + this.twodig(n)) -0 > this.latest) {
				this.latest = (r[0] + '' + this.twodig(n)) -0;
			}
			var d = dates[i].title.replace(/^.*header=\[([A-Za-z]+)\,?\s+(\d{1,2})\,?\s(\d{4,})\s*\].*$/,'$1$2$3').toLowerCase();
			this.event_dates[d] = dates[i];
			
		}
	},
	
	draw : function() {
		/* Find out the number of additional cells are needed to fill out the last week. */
		/* Then make the heading and navigation buttons */
		this.get_days(this.month,this.year);

		var pad = ((7 - (this.startday - 0 + 1 + this.numdays) % 7) % 7);
		var cal_head = document.createElement('div');
		cal_head.id = 'cal_head';
		cal_head.style.className = this.month.toLowerCase();
		var days = document.createElement('div');
		days.id='days';
		var dates = document.createElement('div');
		dates.id = 'dates';
		var cal_foot = document.createElement('div');
		cal_foot.id = 'cal_foot';
		this.calendar_div = document.getElementById(this.calendar_div_id);
		var current_view = this.calendar_div.cloneNode(false);
		
		var back_button = document.createElement('a');
		back_button.id = 'last_month';
		back_button.href = '#';
		back_button.innerHTML = '&lt;';
		if ((this.earliest - 0) < (this.year + '' + this.twodig(this.index)) - 0 ) {
			back_button.style.visibility = 'visible';
		} else {
			back_button.style.visibility = 'hidden';
		}
		back_button.onclick = this.back;
		cal_head.appendChild(back_button);
		
		var next_button = document.createElement('a');
		next_button.id = 'next_month';
		next_button.href = '#';
		next_button.innerHTML = '&gt;';
		if ((this.latest - 0) >= (this.year + '' + this.twodig(this.index)) - 0 ) {
			next_button.style.visibility = 'visible';
		} else {
			next_button.style.visibility = 'hidden';
		}
		next_button.onclick = this.next;
		cal_head.appendChild(next_button);
		
		var heading_text = document.createElement('h3');
		heading_text.innerHTML = this.month.toUpperCase() + ' ' + this.year;
		cal_head.appendChild(heading_text);
		
		current_view.appendChild(cal_head);
		
		var days = document.createElement('div');
		days.id='days';
		
		var day_labels = 'SMTWTFS'.split('');
		for (var i = 0; i < day_labels.length; i++) {
			var day = document.createElement('div');
			day.style.className = 'day';
			day.innerHTML = day_labels[i];
			days.appendChild(day);
		}
		current_view.appendChild(days);
		
		var cal_foot = document.createElement('div');
		cal_foot.id = 'cal_foot';
		cal_foot.innerHTML = '<a href="schedule.html">&nbsp;</a>';

		for (var i = 0; i <= this.startday; i++) {
			if (this.startday < 6) {
				var edate = document.createElement('div');
				edate.innerHTML = '&nbsp;';
				dates.appendChild(edate);
			}
		}
		for (var i = 0; i < ((this.numdays - 0) ); i++) {
			var event_index = this.month + (i - 0 + 1) + this.year.toString();
			if ((i + this.startday - 0 + 1) % 7 == 1) {
				classes = 'cal_date_air';
			} else {
				classes = 'cal_date';
			}
			if (this.event_dates[event_index]) {
				edate = this.event_dates[event_index];
			} else {
				edate = document.createElement('div');
				edate.className = classes;
				edate.innerHTML = i + 1;
			}
//	alert((this.month + ' ' + (i + 1) + ' ' + this.year) + ' ' + todaysdate.toLowerCase());
			if (this.month + ' ' + (i + 1) + ' ' + this.year == todaysdate.toLowerCase().replace(/\s+/g, ' ') ) {
				edate.className += ' today';
			}
					
			dates.appendChild(edate);
		}
				
		for (var i = 0; i < pad; i++) {
			edate = document.createElement('div');
			edate.innerHTML = '&nbsp;';
			dates.appendChild(edate);
		}
		current_view.appendChild(dates);
		current_view.appendChild(cal_foot);

		this.calendar_div = this.calendar_div.parentNode.replaceChild(current_view, this.calendar_div);
	},
	
	warn : function(y) {
		if (this.w && y < 1583) {
			alert('Julian Calendar dates not yet implemented. To stop receiving this warning, set this.w = false.');
		} else if (this.w && y > 9999) {
			alert('This clendar was created in the early part of the 21st century, when many programmers did not consider addressing the Y10K bug to be an urgent priority. The date you have requested, in ' + this.month + ' of ' + y + ', indicates that it might be time for you to consider upgrading this application. To stop receiving this warning, set this.w = false.');
		}
		return y;
	}
	
};
