// =====================================================================
// ================== Js-  .Ru ==================
// == (c) 2007   (3dartist.ru, webmaster@pravoslavie.ru) ==
// == (c) 2008 clerical.ru ==
// =====================================================================

// =======   Date, Array, String ==============
//    Date    YYMMDD  YYMM

////
for (var i = 0; i<no_data_s.length  ; i++)
{
   var s=no_data_s[i];
   var e=no_data_e[i];
   for(var j=s;j<=e;j++)
   {
   		var d=1000000+Number(j);
        s=''+d;
        s=s.substr(1);
       no_data[no_data.length]=s;
   }
}
////

Date.prototype.setYYMMDDdate = function (dateYYMMDD)
{
	if (dateYYMMDD.length == 4)
		return new Date(dateYYMMDD.substr(0,2)-0+2000, dateYYMMDD.substr(2,2)-1, 1);
	return new Date(dateYYMMDD.substr(0,2)-0+2000, dateYYMMDD.substr(2,2)-1, dateYYMMDD.substr(4,2)-0);
}
//      1..7,   1..6,0
Date.prototype.getWeekDay = function ()
{
	var temp = new Date(this.getTime());
	var week_day =  temp.getDay()-0;
	return (week_day) ? week_day : 7;
}
//  -   
Date.prototype.getDaysInMonth = function ()
{
	var temp = new Date(this.getTime());
	temp.setDate(32);
	return 32 - temp.getDate();
}
//     
Date.prototype.format = function (string_pattern)
{
	var temp = new Date(this.getTime());
	var yyyy = temp.getFullYear();
	var m = temp.getMonth()+1;
	var mm = twoDigits(m);
	var d = temp.getDate();
	var dd = twoDigits(d);
	switch (string_pattern)
	{
		case "YYMM":
		return yyyy.toString().substr(2,2)+mm;
		break;
		case "YY":
		return yyyy.toString().substr(2,2);
		break;
		case "MM":
		return mm;
		break;
		case "month_year":
		return months[temp.getMonth()] + " " + temp.getFullYear() + "&nbsp;г.";
		break;
		case "day_month_year":
		return temp.getDate() + "&nbsp;" + months_rp[temp.getMonth()] + " " + temp.getFullYear() + "&nbsp;г.";
		break;
		case "day_month_br_year":
		return temp.getDate() + "&nbsp;" + months_rp[temp.getMonth()] + "<BR>" + temp.getFullYear() + "&nbsp;г.";
		break;
		case "day_month_year_in_pics":
		var div = "<img src=\"/img/spacer.gif\" height=1 width=15 border=0>";
		var month_pic = "<img src=\"/img/archive/m"+m+".gif\" border=0>";
		var dy = "<img src=\"/img/archive/dy.gif\" height=28 width=17 border=0>";
		return d.toString().inPics() + div + month_pic + div + yyyy.toString().inPics() + dy;
		break;
	}
	return yyyy.toString().substr(2,2)+mm+dd; // YYMMDD
}
//      
Date.prototype.getPrevValidDate = function ()
{
	var temp = new Date(this.getTime());
	if (temp.format("YYMMDD").outOfRange())
		return false;
	temp.setDate(temp.getDate()-1);
	var tempYYMMDD = temp.format("YYMMDD");
	while(tempYYMMDD >= first_note)
	{
		if (!no_data.ifContains(tempYYMMDD)){
			return temp;
			}
		temp.setDate(temp.getDate()-1);
		tempYYMMDD = temp.format("YYMMDD");
	}
	return false;
}
//      
Date.prototype.getNextValidDate = function ()
{
	var temp = new Date(this.getTime());
	if (temp.format("YYMMDD").outOfRange())
		return false;
	temp.setDate(temp.getDate()+1);
	var tempYYMMDD = temp.format("YYMMDD");
	while(tempYYMMDD <= last_note)
	{
		if (!no_data.ifContains(tempYYMMDD)){
			return temp;
			}
		temp.setDate(temp.getDate()+1);
		tempYYMMDD = temp.format("YYMMDD");
	}
	return false;
}
//        
Array.prototype.ifContains = function (element)
{
	for(var i=0; i<this.length; i++) {
		if (this[i] == element)
			return true;
	}
	return false;
}
//     ,   YYMM,    Date
String.prototype.getFirstValidDateOfMonth = function ()
{
	var yymm01 = this+"01";
	var yymm01_obj = new Date().setYYMMDDdate(yymm01);
	if(no_data.ifContains(yymm01))
		return yymm01_obj.getNextValidDate();
	return yymm01_obj;
}
//     YYMMDD     .  
String.prototype.outOfRange = function ()
{
	if ((this < first_note) || (this > last_note))
		return true;
	return false;
}
//     YYMMDD      
String.prototype.ifValid = function ()
{
	if (this.outOfRange())
		return false;
	if (no_data.ifContains(this))
		return false;
	return true;
}
//     
String.prototype.inPics = function ()
{
	var arr = this.split("");
	var str = "<nobr>";
	for(var i=0; i<arr.length; i++) {
		str += "<img width=16 height=28 src=\"/img/archive/d"+arr[i]+".gif\" border=0>";
	}
	str += "</nobr>";
	return str;
}
// =======     ==============
var wdays = new Array("Пн", "Вт", "Ср", "Чт", "Пт", "Сб", "Вс" );
var months = new Array("Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь");
var months_rp = new Array("января", "февраля", "марта", "апреля", "мая", "июня", "июля", "августа", "сентября", "октября", "ноября", "декабря");

// =======   ==============
//  ,  :    YYMMDD  YYMM, ID
function generateCalendar(dateYYMMDD, container)
{
	var the_date_obj = new Date().setYYMMDDdate(dateYYMMDD);
	var the_yymm = the_date_obj.format("YYMM");
	var the_yyyy = the_date_obj.getFullYear();
	var the_m = the_date_obj.getMonth();
	var the_d = the_date_obj.getDate();

	var str = "<TABLE border=0 cellspacing=0 cellpadding=0 class=\"calendar\">";

	// , , 
	str += generateNavigation(the_date_obj, container);

	//  
	var i = -1;
	str += "<tr class=\"wdays\">";
	while(++i < 7)
		str += "<td>"+wdays[i]+"</td>";
	str += "</tr>";

	//    
	var first_day_of_month = new Date().setYYMMDDdate(dateYYMMDD);
	first_day_of_month.setDate(1);
	var week_day_of_1stdate_of_month = first_day_of_month.getWeekDay()-1;

	for(i=1; i<43; i++)
	{
		var temp_date_obj = new Date(the_yyyy, the_m, i-week_day_of_1stdate_of_month);
		var yymmdd = temp_date_obj.format("YYMMDD");
		var yymm = temp_date_obj.format("YYMM");
		var d = temp_date_obj.getDate();

		if ((i%7) == 1)
			str += "\n<TR>";

		if (yymm != the_yymm)//   
			str += "<TD CLASS=\"other_month\">"+d+"</TD>";
		else
		{
			if (!yymmdd.ifValid()) //      
			{
				str += "<TD CLASS=\"this_month_nodata\">"+d+"</TD>";
			}
			//   ,   -  
			else if ((d != the_d) || ((d == the_d) && (dateYYMMDD.length == 4)))
			{
				str += "<TD CLASS=\"this_month\">";
				str += "<a href=\""+dir+yymmdd+ext+"\">"+d+"</a></TD>";
			}
			else //    
				str += "<TD CLASS=\"this_month_day\">"+d+"</TD>";
		}

		if (!(i%7))
			str += "</TR>";
	}
	str += "</TABLE>";
	document.getElementById(container).innerHTML = str;
}



var yearsAndMonths_shown = false;
//   ,    "", ""
function generateNavigation(temp_date_obj, container)
{
	var prevMonthDate = new Date(temp_date_obj.getTime());
	prevMonthDate.setDate(-1); // .  . 
	var nextMonthDate = new Date(temp_date_obj.getTime());
	nextMonthDate.setDate(nextMonthDate.getDaysInMonth()+1); //   . 

	var str = "<tr class=\"nav\">";
	//   . 
	if (prevMonthDate.format("YYMMDD").outOfRange())
		str += "<td><img src=\"/img/month_no.gif\" width=11 height=12 border=0 title=\" \"></td>";
	else
	{
		str += "<td><a href=\"javascript:void(0);\" title=\"" + prevMonthDate.format("month_year") + "\"";
		str += " onClick=\"generateCalendar('"+ prevMonthDate.format("YYMM") +"', '"+container+"')\">";
		str += "<img src=\"/img/month_prev.gif\" width=11 height=12 border=0></a></td>";
	}

	//  
	//var onclick_f = "generateYearsAndMonths('"+temp_date_obj.format("YY")+"', '"+temp_date_obj.format("MM")+"', '"+container+"');";
	var onclick_f = "clickMonth('"+temp_date_obj.format("YY")+"', '"+temp_date_obj.format("MM")+"', '"+container+"');"
	var imgd = "<img src=\"/img/yymm_sh.gif\" height=7 width=9 border=0 >";
	str += "<td colspan=5 class=\"monthyyyy\"><a href=\"javascript:void(0);\" onClick=\""+onclick_f+"\"><b>" + temp_date_obj.format("month_year") + imgd + "</b></a></td>";

	//   . 
	if (nextMonthDate.format("YYMMDD").outOfRange())
		str += "<td><img src=\"/img/month_no.gif\" width=11 height=12 border=0 title=\" \"></td>";
	else
	{
		str += "<td><a href=\"javascript:void(0);\" title=\"" + nextMonthDate.format("month_year") + "\"";
		str += " onClick=\"generateCalendar('"+ nextMonthDate.format("YYMM") +"', '"+container+"')\">";
		str += "<img src=\"/img/month_next.gif\" width=11 height=12 border=0></a></td>";
	}
	return str+"</tr>";
}



//     
function generateYearsAndMonths(yy, mm, container)
{
	var last_note_y = 0+last_note.substr(0,2);
	var last_note_m = 0+last_note.substr(2,2);
	//   
	var y = 0 + yy;
	var str = "<table border=0 cellpadding=0 cellspacing=0>";
	for (var i=0; i<=last_note_y; i++)
	{
		yyyy = 2000+i;
		if (i != y)
		{
			var onclick_f = "generateYearsAndMonths('"+twoDigits(i)+"','','"+container+"');";
			str += "<tr><td><a href=\"javascript:void(0);\" onClick=\""+onclick_f+"\"><b>";
			str += yyyy + "</b></a></td></tr>";
		}
		else
			str += "<tr><td class=\"this_on\"><b>" + yyyy+ "</b></td></tr>";
	}
	str += "</table>";
	document.getElementById('all_years').innerHTML = str;

	//   
	var str = "<table border=0 cellpadding=0 cellspacing=0>";
	var m = ""; var m_class = ""; var m1_class = "";
	if (mm != "")
		m = 0 + mm;
	for (i=1; i<=12; i++)
	{
		m_ru = months[i-1];
		if (i==m)
			m_class = m1_class = " class=\"this_on\"";
		else{
			m_class = " class=\"this_off\"";
			m1_class = "";
		}

		if ((y<last_note_y) || (i<=last_note_m))
		{
			str += "<tr><td" + m1_class + ">";
			var yymm = yy+twoDigits(i);
			var href_f = yymm.getFirstValidDateOfMonth().format("YYMMDD")+ext;
			onclick_f = "document.getElementById('all_years_and_months').style.display='none';";
			//onclick_f += " generateCalendar('"+yy+twoDigits(i)+"', '"+container+"');";
			str += "<a href=\""+href_f+"\" onClick=\""+onclick_f+"\"><b>";
			str += m_ru + "</b></a></td></tr>";
		}
		else
			str += "<tr><td" + m_class + ">" + months[i-1] + "</td></tr>";
	}
	str += "</table>";
	document.getElementById('all_months').innerHTML = str;
}



//
function prevNextDays(dateYYMMDD)
{
	var the_day = new Date().setYYMMDDdate(dateYYMMDD);

	if (the_day.getPrevValidDate())
		document.getElementById("prevDay").innerHTML = "<a href=\""+dir+the_day.getPrevValidDate().format("YYMMDD")+ext+"\">"+the_day.getPrevValidDate().format("day_month_br_year")+"</a>";
	else
		document.getElementById("prevDay").innerHTML = "";

	document.getElementById("theDay").innerHTML = the_day.format("day_month_year_in_pics");

	if (the_day.getNextValidDate())
		document.getElementById("nextDay").innerHTML = "<a href=\""+dir+the_day.getNextValidDate().format("YYMMDD")+ext+"\">"+the_day.getNextValidDate().format("day_month_br_year")+"</a>";
	else
		document.getElementById("nextDay").innerHTML = "";
}



//  5 -> 05
function twoDigits(digit){
	return (digit < 10) ? "0"+digit : digit+"";
}



//
function clickMonth(yy, mm, container)
{
	if(yearsAndMonths_shown == false){
		document.getElementById('all_years_and_months').style.display='block';
		generateYearsAndMonths(yy, mm, container);
		yearsAndMonths_shown = true;
	}
	else{
		document.getElementById('all_years_and_months').style.display='none';
		yearsAndMonths_shown = false;
	}
}
