/**
 * Simple Calendar Class
 *
 * @param     string date Curent date (mm/dd/yyyy)
 * @copyright (c) 2007-2008 Microsite (http://microsite.ru)
 * @author    Kevin <grieve@ya.ru>
 */
function KevinCalendar(blockId, date)
{
	// containerID for calendar
	//this.blockId = "calendar";
	// Calendar titles
	this.blockId = blockId;
	this.calendar              = new Array();
	this.calendar.monthTitle   = ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'];
	this.calendar.monthTitle2  = ['января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря'];
	this.calendar.weekDayTitle = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'];
	// Tags
	this.tag = {
		'startTab':  "<table>\n",
		'endTab':    "</table> <p class='nowbtn'><a href='#' onclick='kCal_"+this.blockId+".setToday();return false;'>Сейчас</a></p>",
		'startTr':   "<tr>",
		'endTr':     "</tr>\n",
		
		'markTh':    "<th class='holiday'>",
		'startTh':   "<th>",
		'endTh':     "</th>",
		
		'markTd':    "<td class='holiday'>",
		'startTd':   "<td>",
		'endTd':     "</td>",
		
		'startHide': "<span>",
		'endHide':   "</span>"
	};
	// Operation date
	this.selectedDate = new Date();
	if (date!='')
	{
		this.selectedDate.setTime(Date.parse(date));
	}
	this.selectedDate.setDate(1);
	
	// Selected date
	this.selDate =  new Date();
	if (date!='')
	{
		this.selDate.setTime(Date.parse(date));
	}
	// Real today
	this.today = new Date();
	
	// Set date period
	this.dateFrom = new Date();
	this.dateTo = new Date();
	
	// Init
	this.changeMonth(0);
	this.writeDatePole();
	document.getElementById(this.blockId+"-time").style.display = 'none';
	document.getElementById(this.blockId+"-block").style.display = 'none';
}
/**
 * Set now date-time
 */
KevinCalendar.prototype.setToday = function()
{
	this.selectedDate = new Date();
	this.selDate =  new Date();
	this.changeMonth(0);
	this.writeDatePole();
	document.getElementById(this.blockId+"-block").style.display = 'none';
}
/**
 * Calendar show 
 */ 
KevinCalendar.prototype.show = function()
{
    document.getElementById(this.blockId+"-time").style.display = 'none';
    block = document.getElementById(this.blockId+"-block").style;
    block.display = block.display == "block" ? "none" : "block";
    //this.selectedDate = this.selDate;
    this.changeMonth(0);
}
/**
 * time show 
 */ 
KevinCalendar.prototype.showTime = function()
{
    document.getElementById(this.blockId+"-block").style.display = 'none';
    block = document.getElementById(this.blockId+"-time").style;
    block.display = block.display == "block" ? "none" : "block";
    if (block.display == "block")
    {
    	document.getElementById(this.blockId+"-h").select();
    	document.getElementById(this.blockId+"-h").focus();
    }
}
/**
 * Write data to input in MySQL date format
 */
KevinCalendar.prototype.writeDatePole = function()
{
	this.changeMonth(0);
	year  = this.selDate.getFullYear();
    month = this.selDate.getMonth()+1;
    day   = this.selDate.getDate();
    hour  = this.selDate.getHours();
    min   = this.selDate.getMinutes();
    date  = year+'-'+month+'-'+day+' '+hour+':'+min+':00';
    document.getElementById(this.blockId+'-h').value = hour;
    document.getElementById(this.blockId+'-m').value = min;
	document.getElementById(this.blockId+"-mysql-date").value = date;
	document.getElementById(this.blockId+"-l-date").innerHTML = day+" "+this.calendar.monthTitle2[month-1]+" "+year+" года";
	hour = hour < 10 ? "0"+hour : hour;
	min = min < 10 ? "0"+min : min;
	document.getElementById(this.blockId+"-l-time").innerHTML = hour+":"+min;
}
/**
 * Choise Date
 */ 
KevinCalendar.prototype.choiceDate = function(year, month, day)
{
    this.selDate.setFullYear(year);
    this.selDate.setMonth(month);
    this.selDate.setDate(day);
    this.writeDatePole();
    this.show();
}
/**
 * Choise Time
 */ 
KevinCalendar.prototype.choiceTime = function()
{
    hour = document.getElementById(this.blockId+'-h').value;
    min  = document.getElementById(this.blockId+'-m').value;
    if (!this.checkTime(hour, 23) || !this.checkTime(min, 60) )
    {
    	alert("Неверно задано время!");
    	return;
    }
    this.selDate.setHours(hour);
    this.selDate.setMinutes(min);
    this.writeDatePole();
    this.showTime();
}
/**
 * Check Time
 */
KevinCalendar.prototype.checkTime = function(i, max)
{
	if (isNaN(i) || i < 0 || i > max )
    {
    	return false;
    }
    return true;
}

/**
 * Set month correct from current date
 */
KevinCalendar.prototype.changeMonth = function(monthCorrect)
{
	this.selectedDate.setMonth   (this.selectedDate.getMonth()+(monthCorrect));
	this.prevMonth        = new Date(this.selectedDate.getFullYear(), this.selectedDate.getMonth()-1, 1);
	this.nextMonth        = new Date(this.selectedDate.getFullYear(), this.selectedDate.getMonth()+1, 1);
	this.prevMonthNumDays = this.getLastMonthDay(this.prevMonth);
	this.monthTitle       = this.calendar.monthTitle[this.selectedDate.getMonth()]+", "+this.selectedDate.getFullYear();
	days = this.makeMonthTable();
	this.compileMonth(days);
	return false;
}
/**
 * Calc last month day
 */
KevinCalendar.prototype.getLastMonthDay = function(date)
{
	tmpDay = new Date(date.getFullYear(), date.getMonth(), 1);
	for (i=28; i<=32; i++)
	{
		tmpDay.setDate(i);
		if (tmpDay.getMonth() != date.getMonth())
		{
			return --i;
		}
	}
	return 0;
}
/**
 * Compile month
 */
KevinCalendar.prototype.makeMonthTable = function()
{
	days = "";
	a = true;

	for (i=1; i<=32; i++)
	{
		tempDay = new Date(this.selectedDate.getFullYear(), this.selectedDate.getMonth(),   i);
		// days of next month
		if (tempDay.getMonth() != this.selectedDate.getMonth())
		{
			lastDay = lastDay == 0 ? 7 : lastDay;
			num  = 7 - lastDay;
			for (j=0; j<num; j++)
			{
				year  = this.nextMonth.getFullYear();
				month = this.nextMonth.getMonth();
				day   = (j+1);
				tNextDay = new Date(year, month, day);

				days = days 
				     + (tNextDay.getDay() == 0 || tNextDay.getDay() == 6 ? this.tag.markTd : this.tag.startTd)
				     + this.tag.startHide
				     + "<a href='#' onclick='kCal_"+this.blockId+".choiceDate("+ year+","+month+","+day + "); return false;'>"
				     + day 
				     + "</a>"
				     + this.tag.endHide+this.tag.endTd;
			}
			break;
		}
		// days of previous month
		if (i==1)
		{
			num  = tempDay.getDay() - 1;
			num  = num == -1 ? 6 : num;
			
			for (j=0; j<num; j++)
			{
				year  = this.prevMonth.getFullYear();
				month = this.prevMonth.getMonth();
				day   = this.prevMonthNumDays;
				tPrevDay = new Date(year, month, day);

				days = (tPrevDay.getDay() == 0 || tPrevDay.getDay() == 6 ? this.tag.markTd : this.tag.startTd)
					 + this.tag.startHide
					 + "<a href='#' onclick='kCal_"+this.blockId+".choiceDate("+ year+","+month+","+day + "); return false;'>"
					 + day
					 + "</a>"
					 + this.tag.endHide+this.tag.endTd + days;
				this.prevMonthNumDays--;
			}
		}
		// days of current month
		year  = this.selectedDate.getFullYear();
		month = this.selectedDate.getMonth();
		day   = i;

		dayStr = (this.selDate.getFullYear() == tempDay.getFullYear() &&
    		      this.selDate.getMonth()    == tempDay.getMonth() &&
    		      this.selDate.getDate()     == tempDay.getDate() ) 
    		   ? '<b>'+day+'</b>'
    		   : (this.today.getFullYear() == tempDay.getFullYear() &&
    		      this.today.getMonth()    == tempDay.getMonth() &&
    		      this.today.getDate()     == tempDay.getDate() ) 
    		   ? '<i>'+day+'</i>' 
    		   : day;
		
		days += (tempDay.getDay() == 0 || tempDay.getDay() == 6 ? this.tag.markTd : this.tag.startTd)
			  + "<a href='#' onclick='kCal_"+this.blockId+".choiceDate("+ year+","+month+","+day + "); return false;'>"
			  + dayStr
			  + "</a>"
			  + this.tag.endTd;
	   
		// make new line
		if (tempDay.getDay()==0)
		{
			days += this.tag.endTr+this.tag.startTr;
		}
		lastDay = tempDay.getDay();
	}
	return days;
}
/**
 * Display calendar
 */
KevinCalendar.prototype.compileMonth = function(days)
{
    ///alert(this.today);
	weekDays = "";
	for (i=0; i<7;i++) 
	{
		weekDays += (i==5 || i==6) ? this.tag.markTh : this.tag.startTh;
		weekDays += this.calendar.weekDayTitle[i] +this.tag.endTh;
	}
	cal = "<p>"
		+ "<b><a href='#' onclick='kCal_"+this.blockId+".changeMonth(-1); return false;'>&#8592;</a></b>"
		+ "<span>&nbsp;&nbsp;&nbsp;&nbsp;"+this.monthTitle+"&nbsp;&nbsp;&nbsp;&nbsp;</span>"
		+ "<b><a href='#' onclick='kCal_"+this.blockId+".changeMonth(1); return false;'>&#8594;</a></b>"
		+ "</p>"
		+ this.tag.startTab
		+ this.tag.startTr+ weekDays +this.tag.endTr
		+ this.tag.startTr+ days     +this.tag.endTr
		+ this.tag.endTab;
	document.getElementById(this.blockId+"-block").innerHTML = cal;	  
}
