/*
Pork.PopupCalendar 1.0
usage: new PopupCalendar(someTextInputElement);
*/
var PopupCalendar = new Class({
	initialize: function(targetInput, options)
	{
		this.setOptions(this.defaultOptions, options);
		this.targetElement = $(targetInput);
		this.calendar = false;
		this.render(this.targetElement.value);
	},
	defaultOptions:{
		/*Months : ["Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus", "September", "Oktober", "November", "December"],*/
		 Months : ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], 
		/*WeekDays : ["Zo", "Ma", "Di", "Wo", "Do", "Vr", "Za"],*/
		 WeekDays : ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"], 
		StartDay : 1, // normal startday of week 1 for monday, 0 for sunday.
		DateRegexp : /^(\d{1,2})-(\d{1,2})-(\d{4})$/,
		DateTimeRegexp : /^(\d{1,2})-(\d{1,2})-(\d{4}) (\d{2}):(\d{2}):(\d{2})$/
	},
	setDate: function(targetValue)
	{ 
		this.targetElement.value = targetValue;
		this.calendar.setStyle('display', "none");
	},
	render: function(startDate)
	{	
		var currentDate = (!startDate || typeof(startDate) == 'undefined') ?  new Date() : (!isNaN(startDate))? new Date(startDate) : this.stringToDate(startDate);
		//this.tbody.$$('TD').each(function(el) { el.removeEvents() } );
		this.lastMonth = new Date(currentDate).setMonth(currentDate.getMonth()-1);
		this.nextMonth = new Date(currentDate).setMonth(currentDate.getMonth()+1);
		var firstDay = new Date(currentDate);
		firstDay.setDate(1);
		firstDay.setDate(1-(7+currentDate.getDay()-this.options.StartDay)%7);
		var lastDay = new Date(this.nextMonth).setDate(0);
		this.table = new Element('table').addClass('calendarTable');
		this.tbody = new Element('tbody').injectInside(this.table)
		Row = new Element('TR').injectInside(this.tbody);
		new Element('TD', {
			'class': 'monthSwitch',
				'events': { 'click' : function() { this.render(this.lastMonth); }.bind(this)	}
		}).appendText('<<').injectInside(Row);
		new Element('TD', {	
			'colSpan': 5, 
			'class' : 'currentMonth' }
		).appendText(this.options.Months[currentDate.getMonth()]+" "+currentDate.getFullYear()).injectInside(Row);
		new Element('TD', {
			'class': 'monthSwitch',
			'align': 'right',
			'events': { 'click' : function() { this.render(this.nextMonth); }.bind(this)	}
		}).appendText('>>').injectInside(Row);
		var currentDay = new Date(firstDay);
		Row = new Element('TR').injectInside(this.tbody);
		for (var i=0; i<7; i++)	{ 
			new Element('TH').appendText(this.options.WeekDays[(this.options.StartDay+i)%7]).injectInside(Row);
		}
		while (currentDay.getMonth() == currentDate.getMonth() || currentDay.getMonth() == firstDay.getMonth()) {
			var Row = new Element('TR').injectInside(this.tbody);			
			for (var j=0; j<7; j++) 
			{
				dayEl = new Element('TD',
				{
					'currentDay' : currentDay.toString(),
					'events': { 
						'click': function() { 
							this.editor.setDate(this.editor.dateToString(this.getAttribute('currentDay'))) ;
						},
						'mouseover': function() {
							this.addClass('hovering');
						},
						'mouseout': function() {
							this.removeClass('hovering');
						}
					} 	
				}).appendText(currentDay.getDate()).injectInside(Row);
				dayEl.editor = this;
				if (currentDay.getDate() == currentDate.getDate() && currentDay.getMonth() == currentDate.getMonth()) {	dayEl.addClass('currentdate');	}
				else if (currentDay.getDay() == 0 || currentDay.getDay() == 6)	{ dayEl.addClass('weekend'); }
				else { dayEl.addClass('workday'); }
				if (currentDay.getMonth() != currentDate.getMonth()) dayEl.addClass('othermonth');
				currentDay.setDate(currentDay.getDate()+1);
			}
		}
		if (!this.calendar)
		{
			pos = this.targetElement.getCoordinates();
			this.calendar = new Element('DIV',
			{
				'styles':
				{
					'position': 'absolute',
					'display': 'block',
					'left': pos.left+pos.width+10+'px',
					'top' : pos.top+'px'
				}
			}).injectInside(document.body);	
		}
		this.calendar.innerHTML = ''; // dump original contents
		this.table.injectInside(this.calendar);
	
	},	
	stringToDate: function(inputString)
	{
		result = this.options.DateRegexp.exec(inputString);
		if (!result && !this.options.DateTimeRegexp.exec(inputString)) alert("Invalid Datetime format: "+ inputString);
	//	return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4, RegExp.$5, RegExp.$6)); // dutch notation
		return (new Date ( RegExp.$3, RegExp.$1, RegExp.$2-1, RegExp.$4, RegExp.$5, RegExp.$6)); // USA notation
	},

	dateToString: function(currentDate) 
	{

		currentDate = new Date(currentDate)
	//	dat = currentDate.getDate()+"-"+(currentDate.getMonth()+1)+"-"+currentDate.getFullYear(); // dutch notation
		dat = (new String ((currentDate.getMonth()+1)+"-"+currentDate.getDate()+"-"+currentDate.getFullYear())); // USA notation
		return(dat);
	},
	dateToTimeString: function(currentDate)
	{
		return new String(currentDate.getHours()+":"+currentDate.getMinutes()+":"+currentDate.getSeconds());
	}
});
PopupCalendar.implement(new Options);