var displayed_date;
var initial_date;
var IntrvlID;
var isOver;

function calendar(date, flags){
	if(date == null){
	 	displayed_date = new Date();
	 	initial_date = new Date();
	} else {
    	displayed_date = date;
	 	initial_date = date;
	}
	/*for(i=0; i < hdays.length; i++){
		alert(hdays[i][1]);
	}*/
	//alert(new Date().toDateString());
     var c_html = '<h3>'+get_calender_html(date, "m")+'</h3>';
     c_html += get_calender_html(date, "ds");
	var targ = getEl("clndr");
	targ.innerHTML = c_html;
	setNow();
}

function setNow(){
	chtm = '';
	for(i=0; i < hdays.length; i++){
		idd  = initial_date.toDateString();
		cdd1 = new Date(hdays[i][2]*1000).toDateString();
		cdd2 = new Date(hdays[i][3]*1000).toDateString();
		if((idd == cdd1)||(idd == cdd2)){
			chtm += '<h3><a href="extra/hday/'+hdays[i][0]+'.html">'+hdays[i][1]+'</a></h3>';
		}
	}
	if(chtm != ''){
		var o_o = getEl("nowhday");
		o_o.style.display = "block";
		o_o.innerHTML = chtm;
	}
}

function get_calender_html(date, flags){
	 //If no parameter is passed use the current date.
	 if(date == null){
	 	date = new Date();
	 }
	 if(flags == null){
	 	//flags = "hds";
	 }
	 
	 init_day = initial_date.getDate();
	 init_month = initial_date.getMonth();
	 init_year = initial_date.getFullYear();
	 day = date.getDate();
	 month = date.getMonth();
	 year = date.getFullYear();
	 
	 var months = new Array('Январь', 'Февраль',
	                    'Март',
	                    'Апрель',
	                    'Май',
	                    'Июнь',
	                    'Июль',
	                    'Август',
	                    'Сентябрь',
	                    'Октябрь',
	                    'Ноябрь',
	                    'Декабрь');
	 
	 daysmap = Array("пн", "вт", "ср", "чт", "пт", "сб", "вс");
	 
	 
	 this_month = new Date(year, month, 1);
	 next_month = new Date(year, month + 1, 1);
	 
	 //Find out when this month starts and ends.         
	 first_week_day = this_month.getDay()-1;
	 days_in_this_month = Math.floor((next_month.getTime() - this_month.getTime()) / (1000 * 60 * 60 * 24));
	 
	 var calendar_html = '';
	 if(flags.indexOf("m") != -1){
	 	calendar_html += months[month] + ' ' + year;
	 	return calendar_html;
	 }
	 
	 calendar_html += '<table class="m" cellpadding="0" cellspacing="0">';
	 
	 if(flags.indexOf("h") != -1){
	 	calendar_html += '<tr><td></td><td colspan="7" align="center">' + 
	                  months[month] + ' ' + year + '</td><td></td></tr>';
	 }
	 
	 
	 
	 if(flags.indexOf("d") != -1){
	 	// строка дней
	    calendar_html += '<tr>';
	    calendar_html += '<tr><td></td>';
	     for(i in daysmap){
	     	if(i == 0){
	        	calendar_html += '<td class="dmf">'+daysmap[i]+'</td>';
	     	} else {
	        	calendar_html += '<td class="dm">'+daysmap[i]+'</td>';
	        }
	     }
	    calendar_html += '<td></td></tr>';
	    calendar_html += '</tr>';
	 	
	 }
	 
	if(flags.indexOf("s") != -1){
	    /*if(flags.indexOf("h") != -1){
	    
	    } else {
	         week_day = first_week_day;
	    }*/
	 
	 /*if(first_week_day == 0){
	 	week = 1;
	 } else {
	 }*/
	 week = 1;

   	calendar_html += '<td class="ar"><a href="javascript:c_prev();"><img src="/img/c/al.gif" alt="&lt;"/></a></td>'
	 week_cntr = 1;
	 
	 if(first_week_day < 0){
	 	 first_week_day = 6;
	 } else if(first_week_day == 0){
	 	week_cntr = 0;
	 }
	 
	 //Fill the first week of the month with the appropriate number of blanks.       
	 for(week_day = 0; week_day < first_week_day; week_day++){
	    calendar_html += '<td align="center"><br/></td>';
	 }
	 week_day = first_week_day;
	 for(day_counter = 1; day_counter <= days_in_this_month; day_counter++){
	    week_day %= 7;
	    
	    if(week_day == 0){
		    if(week_cntr == 1){
		       calendar_html += '<td class="ar"><a href="javascript:c_next();"><img src="/img/c/ar.gif" alt="&gt;" /></a></td></tr><tr><td class="tl" rowspan="5"><img src="/img/c/tl.gif" alt="" /></td>';
		    }
		    if(week_cntr == 2){
		       calendar_html += '<td class="tr" rowspan="5"><img src="/img/c/tr.gif" alt="" /></td>';
		    }
		    if(week_cntr > 1){
			    if(first_week_day != 0){
			    	calendar_html += '</tr><tr>';
			    } else {
			    	if(week_cntr != 0){
			    		calendar_html += '</tr><tr>';
			    	}
			    }
		    }
			week++;
			week_cntr++;
	    }
	    
	    //Do something different for the current day.
	    if((init_day == day_counter)&&(init_year == year)&&(init_month == month)){
	    	var append = ' class="now"';
	    	var a2 = 'now';
	    } else {
		    var a2 = '';
	    	var append = '';
	    }
	    this_day_date = new Date(year, month, day_counter, 0, 0, 0, 0);
		cds = this_day_date.toDateString();
	    var app1 = '';
	    var app2 = '';
	    dco = day_counter;
	    for(ic=0; ic < hdays.length; ic++){
			cdd1 = new Date(hdays[ic][2]*1000);
			cdd2 = new Date(hdays[ic][3]*1000);
			if((cdd1.toDateString() == cds)||(cdd2.toDateString() == cds)){
				app1 = ' class="hd ';
				app1 += a2;
				app1 += '"';
				app2 = ', \''+hdays[ic][1]+'\', \''+hdays[ic][0]+'\'';
				//calendar_html += '*';
				dco = '<a href="extra/hday/'+hdays[ic][0]+'.html">'+dco+'</a>';  
			}
		}
	    calendar_html += '<td align="center" '+app1+' onmouseout="clo();" onmouseover="dh(\''+civ2heb(this_day_date, "text")+'\' '+app2+');" '+append+'>'+dco+'</td>';
	    
	    week_day++;
	 }
	    
	 calendar_html += '</tr>';
	 if((week == 5)||(first_week_day == 0)){
	 	// добавим пустую неделю
	 	calendar_html += '<tr>';
	 	for(i=0; i<6; i++){
	 		calendar_html += '<td><br/></td>';
	 	}
	 	calendar_html += '</tr>';
	 }
	 calendar_html += '</table>';
	 
	 
	 }
	 return calendar_html;
}

function c_next(){
	var Y = displayed_date.getFullYear();
	var m = displayed_date.getMonth();
	var d = displayed_date.getDate();
	
	var nextYear = new Date(initial_date.getFullYear()+1, initial_date.getMonth(), initial_date.getDate(), 0, 0, 0, 0);
	m++;
	if(m >= 12){
		m = 0;
		Y++;
	}
	var nDate = new Date(Y, m, d, 0, 0, 0, 0);
	if(nDate > nextYear){
		return;
	}
	displayed_date = nDate;
	ih = '<h3>'+get_calender_html(nDate, "m")+'</h3>'+get_calender_html(nDate, "ds")
	var targ = getEl("clndr");
    targ.innerHTML = ih;
}

function c_prev(){
	//alert(displayed_date.getMonth());
	var Y = displayed_date.getFullYear();
	var m = displayed_date.getMonth();
	var d = displayed_date.getDate();
	var prevYear = new Date(initial_date.getFullYear()-1, initial_date.getMonth(), initial_date.getDate(), 0, 0, 0, 0);
	
	m--;
	//alert(m);
	if(m < 0){
		m = 11;
		Y--;
	}
	var nDate = new Date(Y, m, d, 0, 0, 0, 0);
	if(nDate < prevYear){
		//alert("sgsg");
		return;
	}
	displayed_date = nDate;
	ih = '<h3>'+get_calender_html(nDate, "m")+'</h3>'+get_calender_html(nDate, "ds");
	var targ = getEl("clndr");
    targ.innerHTML = ih;
}

function dh(text, hd, hdid){
	IntrvlID = setTimeout('clos()', 3*1000);
	isOver = true;
	ob = getEl("roll");
	ob.style.display = "block";
	if(hd != undefined){
		text += '<h3>'+hd+'</h3>';
	}
	ob.innerHTML = text;
}

function clos(){
	if(!isOver){
		clearTimeout(IntrvlID);
		ob = getEl("roll");
		ob.style.display = "none";
	}
}

function clo(){
	isOver = false;
}