/*******************************************************************************************************************
 *	Calendar class constructor.
 *	Params:
 *		box_handle: container that has invoked the calendar
 *		date:		the selected date on the calendar on showing (dd/mm/yyyy)
 *		start_date	the first day that the calendar can show (dd/mm/yyyy)
 *		end_date	the last day that the calendar can show (dd/mm/yyyy)
 */
function c_Calendar( box_handle, date, start_date, end_date ) {

	this.browser						= (navigator.appName == "Microsoft Internet Explorer" ? 'IE' : 'FF');

	//class variables
	this.name							= (box_handle.id ? box_handle.id : new Date().getTime());
	this.main_container					= null;
	this.box_handle						= box_handle;

	this.month_table					= new Array();
	this.date							= new c_MyDate(date);
	this.current_date					= new c_MyDate();

	if (start_date) {
		this.start_date					= new c_MyDate(start_date);
	}
	else {
		this.start_date					= this.current_date.clone();
	}
	this.start_date_string				= (this.start_date.day<10 ? '0'+this.start_date.day : this.start_date.day) +'/'+ (this.start_date.month<10 ? '0'+this.start_date.month : this.start_date.month) +'/'+ this.start_date.year;

	if (end_date) {
		this.end_date					= new c_MyDate(end_date);
	}
	else {
		this.end_date					= this.current_date.clone();
		this.end_date.year				+= 1;
	}
	this.end_date_string				= (this.end_date.day<10 ? '0'+this.end_date.day : this.end_date.day) +'/'+ (this.end_date.month<10 ? '0'+this.end_date.month : this.end_date.month) +'/'+ this.end_date.year;

	this.week_day						= null;

	//Setting the calendar handle
	if (!window.eipol_calendar)
		window.eipol_calendar			= new Array();

	window.eipol_calendar[this.name]	= this;

	//Configuring calendar
	this.week_day						= this.getFirstWeekDay();

	this.createMonth();

	//Drawing the calendar interface
	this.draw();
}


/**
 *	Returns a the month first week day (0 sunday - 6 saturday)
 */
c_Calendar.prototype.getFirstWeekDay = function () {

	var aux_week_day		= new Date(this.date.year, this.date.month-1, 1).getDay();
	return (aux_week_day==0 ? 7 : aux_week_day);
}


/**
 *	Fills a month vector with month days
 */
c_Calendar.prototype.createMonth = function () {

	for (var n=0;n<6;n++)
		this.month_table[n]	= new Array();

	var aux_current_day	= 1;

	for (var i=0;i<6;i++) {
		for (var j=0;j<7;j++) {
			if ( ((i*7)+j+1)>=this.week_day && aux_current_day<=this.date.month_days[this.date.month]) {
				this.month_table[i][j]				= new Array();
				this.month_table[i][j]['day']		= aux_current_day;
				this.month_table[i][j]['current']	= (this.date.day && this.current_date.day==aux_current_day ? true : false);

				var aux_date						= (aux_current_day<10 ? '0' + aux_current_day : aux_current_day) + '/' + (this.date.month<10 ? '0' + this.date.month : this.date.month) + '/' + this.date.year;
				this.month_table[i][j]['active']	= true;

				if (new c_MyDate(aux_date).smaller(this.start_date_string)) {
					this.month_table[i][j]['active']= false;
				}
				if (new c_MyDate(aux_date).bigger(this.end_date_string) || new c_MyDate(aux_date).equals(this.end_date_string)) {
					this.month_table[i][j]['active']= false;
				}

				aux_current_day++;
			}
		}
	}
}


/**
 *	Clears the calendar drawn interface
 */
c_Calendar.prototype.clear = function () {

	this.main_container.innerHTML	= '';
}


/**
 *	Calculates the associated container position. Returns an [x,y] array
 */
c_Calendar.prototype.getPosition = function () {

	var offsetElement	= null;
	var x 				= 0;
	var y 				= 0;

	offsetElement		= this.box_handle;

	while (offsetElement && offsetElement.offsetLeft!=null) {
		x 				+= offsetElement.offsetLeft;
		offsetElement	= offsetElement.offsetParent;
	}

	offsetElement		= this.box_handle;

	while (offsetElement && offsetElement.offsetTop!=null) {
		y 				+= offsetElement.offsetTop;
		offsetElement	= offsetElement.offsetParent;
	}

	return new Array(x,y);
}


/**
 *	Returns the associated container real dimensions like an [x,y] array
 */
c_Calendar.prototype.getDimensions = function () {

	return new Array(this.box_handle.offsetWidth, this.box_handle.offsetHeight);
}


/**
 *	Draws the calendar global container and positionates it next to the asociated html control
 */
c_Calendar.prototype.drawMainContainer = function () {

	this.main_container					= document.createElement('div');
	this.main_container.align			= 'center';
	this.main_container.className		= 'calendar_main_container';

	var position						= this.getPosition();
	var dimensions						= this.getDimensions();
	this.main_container.style.left		= position[0];
	this.main_container.style.top		= position[1] + dimensions[1] + 3;

	document.body.appendChild(this.main_container);
}


/**
 *	Draws the calendar controls box
 */
c_Calendar.prototype.drawControlsContainer = function () {

	var controls_div				= document.createElement('div');
		controls_div.align			= 'center';
		controls_div.className		= 'calendar_controls_container';

	var aux_calendar				= this;

	var previous_year				= document.createElement('div');
		previous_year.align			= 'center';
		previous_year.className		= 'calendar_controls_previous_year_' + this.browser;
		previous_year.innerHTML		= '&lt;&lt;';

		previous_year.onclick		= function() {

			aux_calendar.date.year	-= 1;
			aux_calendar.date.setLeapYear();
			aux_calendar.week_day	= aux_calendar.getFirstWeekDay();
			aux_calendar.createMonth();
			aux_calendar.draw();
		}
		controls_div.appendChild(previous_year);

	var previous_month				= document.createElement('div');
		previous_month.align		= 'center';
		previous_month.className	= 'calendar_controls_previous_month_' + this.browser;
		previous_month.innerHTML	= '&lt;';

		previous_month.onclick		= function() {

			if (aux_calendar.date.month>1)
				aux_calendar.date.month	-= 1;
			else {
				aux_calendar.date.month	= 12;
				aux_calendar.date.year	-= 1;
			}
			aux_calendar.date.setLeapYear();
			aux_calendar.week_day	= aux_calendar.getFirstWeekDay();
			aux_calendar.createMonth();
			aux_calendar.draw();
		}
		controls_div.appendChild(previous_month);

	var date_info					= document.createElement('div');
		date_info.className			= 'calendar_controls_date_info';
		date_info.innerHTML			= this.date.getMonthName() + ' ' + this.date.getYear();
		controls_div.appendChild(date_info);

	var next_month					= document.createElement('div');
		next_month.align			= 'center';
		next_month.className		= 'calendar_controls_next_month_' + this.browser;
		next_month.innerHTML		= '&gt;';

		next_month.onclick		= function() {

			if (aux_calendar.date.month<12)
				aux_calendar.date.month	+= 1;
			else {
				aux_calendar.date.month	= 1;
				aux_calendar.date.year	+= 1;
			}
			aux_calendar.date.setLeapYear();
			aux_calendar.week_day	= aux_calendar.getFirstWeekDay();
			aux_calendar.createMonth();
			aux_calendar.draw();
		}
		controls_div.appendChild(next_month);

	var next_year					= document.createElement('div');
		next_year.align				= 'center';
		next_year.className			= 'calendar_controls_next_year_' + this.browser;
		next_year.innerHTML			= '&gt;&gt;';

		next_year.onclick		= function() {

			aux_calendar.date.year	+= 1;
			aux_calendar.date.setLeapYear();
			aux_calendar.week_day	= aux_calendar.getFirstWeekDay();
			aux_calendar.createMonth();
			aux_calendar.draw();
		}
		controls_div.appendChild(next_year);

	var close_button				= document.createElement('div');
		close_button.align			= 'center';
		close_button.className		= 'calendar_controls_close_button_' + this.browser;
		close_button.innerHTML		= 'x';

		close_button.onclick		= function() { aux_calendar.hide(); }

		controls_div.appendChild(close_button);

	this.main_container.appendChild(controls_div);
}


/**
 *	Draws the calendar month table
 */
c_Calendar.prototype.drawMonthContainer = function () {

	var calendar						= this;

	var month_div						= document.createElement('div');
		month_div.align					= 'center';
		month_div.className				= 'calendar_month_container';

	//Create table
	var table							= document.createElement('table');
		table.cellPadding				= 0;
		table.cellSpacing				= 0;
		table.className					= 'calendar_month_table';

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

		var row 						= table.insertRow(i);

		for (var j=0;j<7;j++) {

			//Create row cells
			var cell					= row.insertCell(j);
				cell.align				= 'center';
				cell.valign				= 'middle';

			var aux_div					= document.createElement('div');
				aux_div.className		= 'calendar_month_cell';
				cell.appendChild( aux_div );

			if (this.month_table[i][j] && this.month_table[i][j]['day']) {
				aux_div.innerHTML		= this.month_table[i][j]['day'];

				if (!this.month_table[i][j]['active']) {
					aux_div.style.color	= '#333333';
				}
				else {
					aux_div.style.cursor	= 'pointer';

					aux_div.onmouseover	= function() {
						this.style.color			= 'black';
						this.style.backgroundColor	= 'white';
					}

					aux_div.onmouseout	= function() {
						this.style.color			= 'white';
						this.style.backgroundColor	= '';
					}

					aux_div.onclick		= function() {
						var aux_day					= parseInt(this.innerHTML, 10);
							aux_day					= (aux_day<10 ? '0'+aux_day : aux_day);
						calendar.box_handle.value	= aux_day +'/'+ (calendar.date.month<10 ? '0'+calendar.date.month : calendar.date.month) +'/'+ calendar.date.year;
						calendar.hide();
					}
				}
			}
		}
	}
	month_div.appendChild(table);

	this.main_container.appendChild(month_div);
}


/**
 *	Clears the calendar if this exists and draws its container, controls and month table
 */
c_Calendar.prototype.draw = function () {

	//Draws container
	if (!this.main_container) {
		this.drawMainContainer();
	}
	else
		this.clear();

	//Draws controls
	this.drawControlsContainer();

	//Draws month table
	this.drawMonthContainer();
}


/**
 *	Shows calendar container
 */
c_Calendar.prototype.show = function () {

	this.main_container.style.display	= 'block';
}


/**
 *	Hides calendar container
 */
c_Calendar.prototype.hide = function () {

	//Drawing the calendar interface
	this.main_container.style.display	= 'none';
}