//-----------------------------------------------------------------------------
//глобальные переменные
//-----------------------------------------------------------------------------
var day;
var month;
var year;
var actday;
var actmonth;
var actyear;
var tries=0;





Array.prototype.hasValue=function(value) 
 {  
  for (var i=0; i < this.length; i++) 
  {
   if (this[i]===value) 
    return true;
  }
  return false;
 };




//=============================================================================
function setToday(lang)
{
	if (lang!='en') lang='ru';
	var now=new Date();	
	actday  =now.getDate();	
	day  =actday;
	actmonth=now.getMonth();
	month=actmonth;
	year  =now.getYear();	
	if (year < 2000)    	
	 year = year + 1900; 
	actyear=year;
	this.focusDay = day;
	//------------
	document.getElementById("curdate").innerHTML='';
	document.getElementById("curdate").innerHTML=getMonthText(month+1,lang)+' '+year;	
	displayCalendar(month+1, year); 	
}
//-----------------------------------------------------------------------------
function setPreviousMonth(lang)
{			
	if (lang!='en') lang='ru';
	if (month==0)
	{
	 month = 11;
		if (year>1000){
		 year--;
		}
	}
	else{
		month--;
	}	
	//------------
	document.getElementById("curdate").innerHTML='';
	document.getElementById("curdate").innerHTML=getMonthText(month+1,lang)+' '+year;	
	displayCalendar(month+1, year);
}
function setNextMonth(lang)
{		
	if (lang!='en') lang='ru';
	if (month==11)
	{
		month=0;
		year++;	
	}
	else{
		month++;
	}
	//------------
	document.getElementById("curdate").innerHTML='';
	document.getElementById("curdate").innerHTML=getMonthText(month+1,lang)+' '+year;	
	displayCalendar(month+1,year);
}
//-----------------------------------------------------------------------------
function displayCalendar(month, year)
{  			
	month=parseInt(month);
	year =parseInt(year);
	var i=0;
	var curday;
	var dayclass;
	var Calendar;
	var days=getDaysInMonth(month,year);
	var firstOfMonth=new Date (year, month-1, 1);	
	var startingPos=firstOfMonth.getDay()-1;	
	if (startingPos==-1) startingPos=6;
	days +=startingPos;			
	//------------
	Calendar='';
	for (i=0;i<startingPos;i++){		
		dayclass=getDayClass(i);	 
	 Calendar+='<div class="'+dayclass+'"></div>';
	}		
	for (i=startingPos;i<days;i++)
	{				
  curday=i-startingPos+1;
  //------------
  dayclass=getDayClass(i);	   
  if (month==(actmonth+1) && year==actyear && curday==actday){  	  	
  	 dayclass+=' activ';
  }  
  //------------
	 Calendar+='<div class="'+dayclass+'">'+curday+'</div>';		
	}	
	document.getElementById("calendar_days").innerHTML='';
	document.getElementById("calendar_days").innerHTML=Calendar;	
	bindEvents();
}
//-----------------------------------------------------------------------------
function isLeapYear (Year)
{
	if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
	return (true);
	} else { return (false); }
}
//-----------------------------------------------------------------------------
function getDaysInMonth(month,year)
{	
	var days;
	if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)  days=31;
	else if (month==4 || month==6 || month==9 || month==11) days=30;
	else if (month==2)  {
	if (isLeapYear(year)) { days=29; }
	else { days=28; }
	}
	return (days);
}
function getDayClass(i)
{	
	switch(i%7)
	{
		case 0: return 'one';
		case 1: return 'two';
		case 2: return 'one';
		case 3: return 'two';
		case 4: return 'one';
		case 5: return 'three';
		case 6: return 'three';		
	}
}
function getMonthText(month,lang)
{
	//-----------------------------------------------------------------------------------
	// по номеру меяца получить его название
	//-----------------------------------------------------------------------------------
	if (lang=='ru')
	{
	 switch (month)
	 {
	 	case 1:  return 'Январь';
	 	case 2:  return 'Февраль';
	 	case 3:  return 'Март';
	 	case 4:  return 'Апрель';
	 	case 5:  return 'Май';
	 	case 6:  return 'Июнь';
	 	case 7:  return 'Июль';
	 	case 8:  return 'Август';
	 	case 9:  return 'Сентябрь';
	 	case 10: return 'Октябрь';
	 	case 11: return 'Ноябрь';
	 	case 12: return 'Декабрь'; 	
	 }
	}
	else{
		switch (month)
	 {
	 	case 1:  return 'January';
	 	case 2:  return 'February';
	 	case 3:  return 'March';
	 	case 4:  return 'April';
	 	case 5:  return 'May';
	 	case 6:  return 'June';
	 	case 7:  return 'July';
	 	case 8:  return 'August';
	 	case 9:  return 'September';
	 	case 10: return 'October';
	 	case 11: return 'November';
	 	case 12: return 'December'; 	
	 }
	}
}
function bindEvents()
{
	var month1=(month+1<10 ? '0'+(month+1) : month+1); 
 var year1 =year;  
 //------------------------
 // Получаем события на текущий месяц:
 // ->[]: global month, year
 // 
 if (!(month1+'-'+year1 in aEvents)) return;
 var lMonthEvents=aEvents[month1+'-'+year1];    
 // []->: lMonthEvents
 //------------------------
 // Получаем первый день месяца:
 // ->[]: id="calendar_days"
 //
 var eDays=document.getElementById('calendar_days');
 var eDay=eDays.firstChild;
 while (eDay.nodeType!=1) // ELEMENT_NODE
 {
  eDay=eDay.nextSibling;
 } 
 // []->: eDay
 //------------------------
 // Расставляем маркеры цвета:
 // ->[]: eDay 
 //          
 while (eDay!=null)
 {     
  if (eDay.nodeType==1)
  {
   var sDate=eDay.innerHTML;
   if (lMonthEvents.hasValue(sDate))
   {
    //------------------------
    // Добавляем класс элементу:
    //
    eDay.className+=' event';
    //------------------------
    // Пишем ссылку вместо числа:
    //
    var sZDate=(sDate.length==1 ? '0'+sDate : sDate);
    eDay.innerHTML='<a href="/index.php?id=8&date='+year1+'-'+month1+'-'+sZDate+'">'+sDate+'</a>';
   }                  
  }     
  //------------
  eDay=eDay.nextSibling;    
 }     
}