// Класс для отображение даты
// v1.1
if(!date_list){
	var date_list = new Array();
}
var date_class = function(name, date, format){
	// Имя и Id поля
	this.name = name;
	this.path = 'date_list[\''+name+'\']';
	this.events = {};
	
	//Значение даты в формате JD даты
	this.date = SQL_date_to_js(date) || new Date();
	this.month = this.date.getMonth();
	this.year = this.date.getFullYear();
	this.day = this.date.getDate();
	this.hours = this.date.getHours();
	this.minutes = this.date.getMinutes();
	
	//Формат представления даты
	this.format = format || '$D $M $Y $H:$i';
	
	//Размеры и стиль
	this.width = null;
	this.height = null;
	this.style_root = 'calendar';
	
	//Через сколько закроется окно, если увести мышку
	this.close_time = 300;
	this.close_timer = null;
	
	//Формат месяцев
	this.month_names = ["январь", "февраль", "март", "апрель", "май", "июнь", "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь"];
	
	//Строит окно с календарём (bild)
	this.get_calendar = function(){
		var d = new Date();
		var start_year = this.start_year ? this.start_year : d.getFullYear();
		var arr_month = [];
		for(var i = 0; i<12; i++){
			arr_month[i] = '<option value="'+ i +'" '+ (i==this.month?'selected':'') +'>'+ this.month_names[i];
		}
		var arr_year = [];
		for(var i = 0; i<5; i++){
			arr_year[i] = '<option value="'+ (i + start_year) +'" '+ ((i + start_year) == this.year?'selected':'') +'>'+ (i + start_year);
		}
		var arr_wik = []; var arr_date = [];
		var dateObj = new Date(this.year, this.month+1, 0);
		var max_days = dateObj.getDate();
		dateObj = new Date(this.year*1, this.month*1, 1);
		var wik = dateObj.getDay();
		if(wik == 0) wik = 7;
		var row; var cell; var day;
		var is_now_month = (d.getFullYear() == this.year) && (d.getMonth() == this.month);
		for(var i = 7; i < 49; i++){
			row = (i-i%7)/7; 
			cell = i%7;
			day = i-5-wik;
			if((i > wik+5) && (i <= wik+5+max_days)){
				arr_date[cell] = '<td class="'+this.style_root+'_day'+(is_now_month && d.getDate()==day ? '_now' : '')+(this.day==day ? '_selected' : '')+'" onclick="'+this.path+'.update(\'day\', this);">'+day+'</td>';
			}else{
				arr_date[cell] = '<td class="'+this.style_root+'_null">&nbsp;</td>';
			}
			if(cell == 6 && (i-i%7 <= wik+5+max_days)){ 
				arr_wik[row] = '<tr>'+arr_date.join('')+'</tr>';
			}
		}
		return '<table cellpadding="0" cellspacing="3" class="'+this.style_root+'_table" onmouseout="'+this.path+'.on_calendar_mouse_out()" onmouseover="'+this.path+'.on_calendar_mouse_over()">'+
				'<tr><td>'+
					'<table border="0" cellpadding="0" cellspacing="0" class="'+this.style_root+'_month_table"><tr>'+
						'<td>'+
							'<select onchange="'+this.path+'.update(\'month\', this);" class="'+this.style_root+'_month">'+arr_month.join('')+'</select>'+
						'</td>'+
						'<td align="right">'+
							'<select onchange="'+this.path+'.update(\'year\', this);" class="'+this.style_root+'_year">'+arr_year.join('')+'</select>'+
						'</td>'+
					'</tr></table>'+
				'</td></tr>'+
				'<tr><td>'+
					'<table cellpadding="2" cellspacing="1" class="'+this.style_root+'_day_table">'+
						'<tr>'+
							'<td class="'+this.style_root+'_day_header">Пн</td>'+
							'<td class="'+this.style_root+'_day_header">Вт</td>'+
							'<td class="'+this.style_root+'_day_header">Ср</td>'+
							'<td class="'+this.style_root+'_day_header">Чт</td>'+
							'<td class="'+this.style_root+'_day_header">Пт</td>'+
							'<td class="'+this.style_root+'_day_header">Сб</td>'+
							'<td class="'+this.style_root+'_day_header">Вс</td>'+
						'</tr>'+
						arr_wik.join('')+
					'</table>'+
				'</td></tr>'+
				'<tr><td>'+
					'<table cellpadding="0" cellspacing="2" class="'+this.style_root+'_time_table"><tr>'+
						'<td>Время</td>'+
						'<td><input type="text" onchange="'+this.path+'.update(\'hour\', this);" value="'+("0" + this.hours).slice(-2)+'" class="'+this.style_root+'_hour" maxlength="2"></td>'+
						'<td><b>:</td>'+
						'<td><input type="text" onchange="'+this.path+'.update(\'minut\', this);"  value="'+("0" + this.minutes).slice(-2)+'" class="'+this.style_root+'_minut" maxlength="2"></td>'+
						'<td width="100%" align="right"><input type="button" value="Закрыть" class="'+this.style_root+'_set" onclick="'+this.path+'.close()"></td>'+
					'</tr></table>'+
				'</td></tr>'+
			'</table>'
		;
	}
	
	//Вызывается при наведение мышки
	this.on_calendar_mouse_over = function(){
		if(this.close_timer){
			window.clearTimeout(this.close_timer);
			this.close_timer = null;
		}
	}
	
	//Вызывается при уводе мышки
	this.on_calendar_mouse_out = function(){
		if(!this.close_timer){
			this.close_timer = window.setTimeout(this.path+'.close()', this.close_time);
		}
	}
	
	//Обновляет и открывает окно с календарём
	this.open = function(){
		var div = document.getElementById(this.name +'_div');
		div.innerHTML = this.get_calendar();
		div.style.visibility = 'visible';
		document.getElementById(this.name +'_text').blur();
	}
	
	//Скрывает календарь (set)
	this.close = function(){
		document.getElementById(this.name +'_div').style.visibility = 'hidden';
	}
	
	//Обновляет дату (click)
	this.update = function(type, obj){
		switch (type) {
		case 'year':
			var dateObj = new Date(obj.value*1, this.month+1, 0);
			this.date = new Date(obj.value, this.month, Math.min(dateObj.getDate(), this.day), this.hours, this.minutes);
			break;
		case 'month':
			var dateObj = new Date(this.year, obj.value*1+1, 0);
			this.date = new Date(this.year, obj.value, Math.min(dateObj.getDate(), this.day), this.hours, this.minutes);
			break;
		case 'day':
			this.date = new Date(this.year, this.month, obj.innerHTML, this.hours, this.minutes);
			break;
		case 'hour':
			this.date = new Date(this.year, this.month, this.day, obj.value, this.minutes);
			break;
		case 'minut':
			this.date = new Date(this.year, this.month, this.day, this.hours, obj.value);
			break;
		}
		document.getElementById(this.name +'_text').value = js_date_to_format(this.date, ' '+ this.format);
		document.getElementById(this.name).value = js_date_to_format(this.date, '$Y-$m-$d $H:$i:$s');
		this.year = this.date.getFullYear();
		this.month = this.date.getMonth();
		this.day = this.date.getDate();
		this.hours = this.date.getHours();
		this.minutes = this.date.getMinutes();
		if(type!='hour' && type!='minut'){
			this.open();
		}
		if(this.events.onchange){
			this.events.onchange();
		}
	}
	
	//Возвращает элемент формы
	this.get_form_element = function(){
		return 	'<input type="hidden" name="'+this.name+'" id="'+this.name+'" value="'+ js_date_to_format(this.date, '$Y-$m-$d $H:$i:$s') +'">'+
				'<table border=0 cellspacing="0"><tr><td>'+
					'<div style="z-index:1; position:absolute; visibility:hidden;" id="'+ this.name +'_div"></div>'+
					'<input type="text" id="'+this.name+'_text" value="'+ js_date_to_format(this.date, ' '+this.format) +'" onclick="'+this.path+'.open()">'+
				'</td></tr></table>';
	}
	
	//Заменяет элементом содержимое соответствующей для него области (до этого содержащий его вариант для людей отключающих скрипты)
	this.bild = function(){
		if(document.getElementById(this.name +'_root')){
			document.getElementById(this.name +'_root').innerHTML = this.get_form_element();
		}
	}
	
	
	date_list[name] = this;
}
