
var Callendar = Class.create();

Callendar.prototype = {

	initialize: function(containerId, start_month, start_year, months_labels, day_labels) {

		this.container = $(containerId);

		this.cal_days_labels = day_labels;
		this.cal_months_labels = months_labels;

		this.date = new Date();

		this.cur_day = this.date.getDate();
		this.cur_month = this.date.getMonth();
		this.cur_year = this.date.getFullYear();

		this.month = (isNaN(start_month) || start_month == null) ? this.date.getMonth() : start_month;
		this.year  = (isNaN(start_year) || start_year == null) ? this.date.getFullYear() : start_year;

		this.instanceId = Math.round(Math.random(1) * 1000 );
		this.btnLeftMonthId = 'btnLeft' + this.instanceId;
		this.btnRightMonthId = 'btnRight' + this.instanceId;

		this.html = '';
		this.showed = false;

		this.dateContainer = null;

		this.display();
	},



	show: function(ev, dateContainer) {

		if ( dateContainer != this.dateContainer ) { this.hide(); }
		this.dateContainer = dateContainer;

		mpos = this.mouseCoords(ev);

		ptop = mpos.y;
		pleft = mpos.x - 215;

		this.container.setStyle({top:ptop+'px', left:pleft+'px'});

		if ( this.showed ) {
			this.hide();
		}
		else {
			this.showed = true;
			this.container.show();
		}
	},



	hide: function() {
		this.showed = false;
		this.container.hide();
	},



	insertDate: function(day) {

		var dateStr = this.year + '-';
		dateStr += ( (this.month+1) < 10 ? '0' + (this.month+1) : this.month+1 ) + '-'
		dateStr += ( day < 10 ? '0' + day : day );

		$(this.dateContainer).value = dateStr;

		this.hide();

	},



	doNextMonth: function() {

		this.month++;
		if ( this.month > 11 ) {

			this.month = 0;
			this.year++;

		}
		this.display();

	},



	doPrevMonth: function() {

		//alert(this.year + ' - ' + this.cur_year);

		if ( ( this.month - 1 ) >= this.cur_month || this.year > this.cur_year ) {

			this.month--;
			if ( this.month < 0 ) {

				this.month = 11;
				this.year--;

			}
			this.display();

		}

	},



	display: function() {

		// get first day of month
		var firstDay = new Date(this.year, this.month, 1);
		var startingDay = firstDay.getDay();

		// find number of days in month
		var monthLength = this._getDaysInMonth(this.month, this.year);

		// do the header
		var monthName = this.cal_months_labels[this.month]
		var html = '<table cellpadding="0" cellspacing="0">';
		html += '<tr><th colspan="7">';
			html += '<table style="width:100%;border:none;" class="head"><tr>';
			html += '<td><a href="javascript:void(0);" onclick="callendar.doPrevMonth();"> <<< </a></td>';
			html += '<td>' + monthName + "&nbsp;" + this.year + '</td>';
			html += '<td><a href="javascript:void(0);" onclick="callendar.doNextMonth();"> >>> </a></td>';
			html += '</tr></table>';
		html += '</th></tr>';
		html += '<tr class="calendar-header">';

		for(var i = 0; i <= 6; i++ ) {
			html += '<td class="calendar-header-day">';
			html += this.cal_days_labels[i];
			html += '</td>';
		}

		html += '</tr><tr>';

		var day = 1;

		for (var i = 0; i < 9; i++) {
			for (var j = 0; j <= 6; j++) {
				html += '<td class="calendar-day">';
				if (day <= monthLength && (i > 0 || j >= startingDay)) {

					if ( this.month == this.cur_month && this.year == this.cur_year && this.cur_day >= day ) {

						html += '<a href="javascript:void(0);" title="' + day + '" class="disabled">' + day + '</a>';

					}
					else {

						html += '<a href="javascript:void(0);" title="' + day + '" onclick="callendar.insertDate('+day+');">' + day + '</a>';

					}

					day++;
				}
				html += '</td>';
			}

			if (day > monthLength) {
				break;
			}
			else {
				html += '</tr><tr>';
			}
		}

		html += '</tr></table>';

		this.container.innerHTML = html;

	},



	createHtml: function() {

		this.html = '';

		this.html = '<table cellpadding="0" cellspacing="0">';
		this.html += '<tr><th colspan="7">';

		this.html += '<table class="head"><tr>';
		this.html += '<td id="btn_place' + this.instanceId + '"></td>';
		this.html += '<td id="month_name_' + this.instanceId + '"></td>';
		this.html += '<td id="btn_place' + this.instanceId + '"></td>';
		this.html += '</tr></table>';

		this.html += '</th></tr>';

		this.html += '<tr class="calendar-header">';

		for(var i = 0; i <= 6; i++ ) {

			this.html += '<td class="calendar-header-day"></td>';

		}

		this.html += '</tr><tr>';

		var day = 1;
		for (var i = 0; i < 9; i++) {
			for (var j = 0; j <= 6; j++) {
				this.html += '<td class="calendar-day" id="day_' + this.instanceId + '_' + j + '_' + i + '"></td>';
			} // for //

			if (day > monthLength) {
				break;
			}
			else {
				this.html += '</tr><tr>';
			}
		} // for //

		this.html += '</tr></table>';


	},



	_getDaysInMonth: function(month, year) {

		var days;
		month++;
		if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) {
			days=31;
		}
		else if (month==4 || month==6 || month==9 || month==11) {
			days=30;
		}
		else if (month==2) {

			if (this._getLeapYear(year)) {
				days=29;
			}
			else {
				days=28;
			}
		}
		return (days);

	},

	_getLeapYear: function(year) {

		if (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0)) {
			return (true);
		}
		else {
			return (false);
		}

	},



	mouseCoords: function(ev) {

		if ( ev ) {

			if(ev.pageX || ev.pageY){
				return { x:ev.pageX, y:ev.pageY }
			}
			else {
				return {
					x: ev.clientX + document.documentElement.scrollLeft - document.body.clientLeft,
					y: ev.clientY + document.documentElement.scrollTop  - document.body.clientTop
				}
			}
		} // if (ev)

	}


} // class Callendar