/**
*  Calendar script
*/

//alert('calendar active');
/**
* logical xor operation
*/
function xor(a,b) {
  return ( a || b ) && !( a && b );
}

/**
* converts western day of week into a russian day of week
*/
function dayOfWeek(day) {
	return day == 0 ? 7 : day;
}


Calendar.prototype.calendarHolder;
Calendar.prototype.navigationHolder;
Calendar.prototype.selectedDate = new Date();
Calendar.prototype.displayMonth = new Date();
Calendar.prototype.hrefBase = '\/events\/';

Calendar.prototype.tableId = 'calendar';
Calendar.prototype.offClass = 'next_mouth_data';
Calendar.prototype.onClass = 'date_on';

Calendar.prototype.monthNames = Array('Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь');
Calendar.prototype.dayNames = Array('Пн','Вт','Ср','Чт','Пт','Сб','Вс');

/**
* creates a calendar table and stores it in memory
*/
Calendar.prototype.displayCalendarNav = function(holderId) {
	this.navigationHolder = document.getElementById(holderId);
	var monthRef = this.navigationHolder.firstChild;
	
	if (monthRef) { 
		monthRef.data = this.monthNames[this.displayMonth.getMonth()];
	} else {
		monthRef = document.createTextNode(this.monthNames[this.displayMonth.getMonth()]);
	}
	this.navigationHolder.appendChild(monthRef);

}


/**
* creates a calendar table and stores it in memory
*/
Calendar.prototype.buildCalendar = function() {
	  	
	//this.displayCalendarNav('monthHolder');
  	this.daysDisplayMonth = this.daysInMonth(this.displayMonth.getMonth()+1,this.displayMonth.getYear());
	  	

  	var lastMonth = new Date(this.displayMonth.getTime());
  	lastMonth.setMonth(lastMonth.getMonth()-1);
  	var daysLastMonth = this.daysInMonth(lastMonth.getMonth()+1,lastMonth.getYear());
  	
  	var startDate = new Date(this.displayMonth.getTime());
  	startDate.setDate(1);
  	var startDay = dayOfWeek(startDate.getDay());
  	var col = 0, row = 0, day = daysLastMonth - (startDay - 2) ;
  	
  	// creating table
  	this.calendarTable = document.createElement('table');
  	var tBody = this.calendarTable.appendChild(document.createElement('tbody'));
  	
  	
  	this.calendarTable.id = this.tableId;
  	var trElements = new Array();
  	var tdElements = new Array();
  	var aElements = new Array();
  	var txtElemets = new Array();
  	var strDate = new String();
  	var thElements = new Array();
  	var strongEl = new Array();
  	
  	
  	//<td><a href="javascript: calendar.previousMonth();">&lt;&lt;</a></td>
	//<td colspan="5" align="center" class="mounth" id="monthHolder"></td>
	//<td><a href="javascript: calendar.nextMonth();">&gt;&gt;</a></td>
  	var tmpA;
  	var tmpTd;
  	var tmpTxt;
  	
	trElements[row] = document.createElement('tr');
  	
  	// control - move left
	tmpTd = document.createElement('td');
  	tmpA = document.createElement('a');
  	tmpA.href = "javascript:calendar.previousMonth();";
  	tmpTxt = document.createTextNode('<<');
  	
  	tmpTd.appendChild(tmpA);
  	tmpA.appendChild(tmpTxt);
	trElements[row].appendChild(tmpTd);
	
	// control - month name
	tmpTd = document.createElement('td');
	tmpTd.className= 'mounth';
	tmpTd.setAttribute('colSpan', 5);
	tmpTd.align='center';

  	tmpTxt = document.createTextNode(this.monthNames[this.displayMonth.getMonth()]);
  	
  	tmpTd.appendChild(tmpTxt);
	trElements[row].appendChild(tmpTd);
	
	// control - move right
	tmpTd = document.createElement('td');
  	tmpA = document.createElement('a');
  	tmpA.href = "javascript:calendar.nextMonth();";
  	tmpTxt = document.createTextNode('>>');
  	
  	tmpTd.appendChild(tmpA);
  	tmpA.appendChild(tmpTxt);
	trElements[row].appendChild(tmpTd);
	
	tBody.appendChild(trElements[row]);
  	
	  row ++;
  	
	// creating first row (first month week)
  	trElements[row] = document.createElement('tr');
  	//trElements[row].id = 'cal_head';
  	for(col ; col < 7 ; col ++) {
  		thElements[col] = document.createElement('td');
  		strongEl[col] = document.createElement('strong');
		txtElemets[col] = document.createTextNode(this.dayNames[col]);
  		trElements[row].appendChild(thElements[col]);
  		thElements[col].appendChild(strongEl[col]);
		strongEl[col].appendChild(txtElemets[col]);
  	}
  	tBody.appendChild(trElements[row]);
  	
  	row ++;
  	col = 0;

	// creating first row (first month week)
  	trElements[row] = document.createElement('tr');
  	// adding to first week days from last month
  	for(day ; day<=daysLastMonth ; day++, col ++) {
  		
  		tdElements[col] = document.createElement('td');
  		aElements[col] = document.createElement('a');
  		aElements[col].className = this.offClass;
		  strDate = day + '-' + (lastMonth.getMonth() + 1) + '-' + lastMonth.getFullYear()+'\/';
  		aElements[col].href = this.hrefBase + strDate;
  		txtElemets[col] = document.createTextNode(day);
  		
  		tdElements[col].appendChild(aElements[col]);
		aElements[col].appendChild(txtElemets[col]);
		trElements[row].appendChild(tdElements[col]);
  	}
  	// adding to first week days from this month
  	for(col, day = 1; col<7 ; col++, day++ ) {
  		tdElements[col] = document.createElement('td');
  		  		if (this.selectedDate.getDate()==day && this.selectedDate.getMonth() == this.displayMonth.getMonth() && this.selectedDate.getYear() == this.displayMonth.getYear())
  			tdElements[col].className = this.onClass;
  		aElements[col] = document.createElement('a');
  		strDate = day + '-' + (this.displayMonth.getMonth() + 1) + '-' + this.displayMonth.getFullYear()+'\/';
  		aElements[col].href = this.hrefBase + strDate;
  		
  		txtElemets[col] = document.createTextNode(day);
  		
  		tdElements[col].appendChild(aElements[col]);
		aElements[col].appendChild(txtElemets[col]);
		trElements[row].appendChild(tdElements[col]);
  	}
  	tBody.appendChild(trElements[row]);

  	
	  // building main part
  	row++;
  	trElements[row] = document.createElement('tr');
  	//alert(this.selectedDate);
  	for (day, col=1 ; day <= this.daysDisplayMonth; day++, col++) {
		
  		tdElements[col] = document.createElement('td');
  		
  		aElements[col] = document.createElement('a');
  		if (this.selectedDate.getDate()==day && this.selectedDate.getMonth() == this.displayMonth.getMonth() && this.selectedDate.getYear() == this.displayMonth.getYear())
  			aElements[col].className = this.onClass;
  		strDate = day + '-' + (this.displayMonth.getMonth() + 1) + '-' + this.displayMonth.getFullYear()+'\/';
  		aElements[col].href = this.hrefBase + strDate;

  		txtElemets[col] = document.createTextNode(day);
  		
  		tdElements[col].appendChild(aElements[col]);
		aElements[col].appendChild(txtElemets[col]);
		trElements[row].appendChild(tdElements[col]);
  	
  		if(col==7) {
   			tBody.appendChild(trElements[row]);
  			row ++;
  			trElements[row] = document.createElement('tr');
  			col = 0;
  		}
  	}
  	
  	var endDate = this.displayMonth
  	endDate.setDate(this.daysDisplayMonth);
  	var endDay = dayOfWeek(endDate.getDay());
  	var nextMonth = new Date(this.displayMonth.getTime());
  	nextMonth.setMonth(lastMonth.getMonth()+1);
  	
  	if (endDay <7 ) {
  	//alert(endDay);
	  	for (col=endDay, day=1 ; col<7; col++, day++) {
	  		tdElements[col] = document.createElement('td');
	  		aElements[col] = document.createElement('a');
	  		aElements[col].className = this.offClass;
	  		strDate = day + '-' + (nextMonth.getMonth() + 2) + '-' + nextMonth.getFullYear()+'\/';
	  		aElements[col].href = this.hrefBase + strDate;

	  		txtElemets[col] = document.createTextNode(day);
	  		
	  		tdElements[col].appendChild(aElements[col]);
			aElements[col].appendChild(txtElemets[col]);
			trElements[row].appendChild(tdElements[col]);
	  	}
	  	tBody.appendChild(trElements[row]);
  	}
  	
  	
} 

/**
* displays the calendar
*/
Calendar.prototype.displayCalendar = function(containerId) {
	
	if(this.calendarTable) {
		this.calendarHolder.appendChild(this.calendarTable);
	}
}

/**
* move calendar to next month
*/
Calendar.prototype.nextMonth = function() {
	this.clear();
	this.displayMonth.setMonth(this.displayMonth.getMonth()+1,1);
	this.buildCalendar();
	this.displayCalendar();
 
}

/**
* move calendar to previos month
*/
Calendar.prototype.previousMonth = function () {
	this.clear();
	this.displayMonth.setMonth(this.displayMonth.getMonth()-1,1);
	this.buildCalendar();
	this.displayCalendar();
}

/**
* deletes calendar table
*/
Calendar.prototype.clear = function() {
	this.calendarHolder.removeChild(this.calendarTable);
	this.calendarTable = null;
}

/**
* calculates the quantity of days in selected month
*/
Calendar.prototype.daysInMonth = function(month,year) {
	
	if ( month != 2) {
		return 30 + xor(month%2,month>7);
	} else {
		return !(year%400)||!(year%4)&&(year%25)?29:28;
	}
}


Calendar.prototype.show = function() {
	
	if(this.calendarHolder.style.display == 'none'){
		this.calendarHolder.style.display = 'block'
	} else {
		this.calendarHolder.style.display = 'none'
	}
}
/**
* Calendar constructor
* @param String holderId - id of div element used as holder for calendar
* @param int selectedDate - timestamp of selected date
*/
function Calendar(holderId,selectedDate,displayDate) {
	
	if (holderId) {
		this.calendarHolder = document.getElementById(holderId);
	}
	
	if (selectedDate) {
		this.selectedDate = new Date(selectedDate*1000);
	}
	
	if (displayDate) {
		this.displayMonth = new Date(displayDate*1000);
	}
}

