function GetDayOfWeek( y, m, d )
{
    if( m < 3 ) --y;
    return (Math.floor(2.6*(m<3?m+10:m-2)-0.1999)+d+(y%100)+Math.floor((y%100)/4)+Math.floor(y/400)-2*Math.floor(y/100)+776)%7;
}

function GetDaysInMonth( y, m )
{
    var d = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
    if( !(y%4) ^ !(y%100) ^ !(y%400) ) d[1]++;
    return d[m];
}

function PrintCalendarMonth()
{
	
	
	
    var month = document.getElementById( 'calendarMonth' ).selectedIndex;
    var table = document.getElementById( 'calendarTable' );
    var realnow = new Date();
    var realnow_y = realnow.getFullYear();
    var realnow_m = realnow.getMonth();
    var realnow_d = realnow.getDate();
    var now = new Date();
    var now_y = now.getFullYear();
    var year = now_y;
    
    
    if(!window.now_y || window.now_y==0) {
    	var now_y = now.getFullYear();
    	var year=now_y;
    	
    	
    } else {
   		var now_y = now.getFullYear();
   		var year=window.now_y;
   		
    }
  
    
    if(window.now_m==-1) {
    	var now_m = now.getMonth();
    	var sel_month = realnow_m;
    	
    } else {
   		var now_m = window.now_m;
   		 var sel_month = now_m;
    }
     window.sel_m = sel_month;
    window.sel_y = year;
    
   	 if(window.now_d==0) {   	  
    	var now_d = now.getDate();
    	var sel_day = 0; 
    } else {
   		var now_d = window.now_d;
   		var sel_day = now_d;
    }
    var dow = GetDayOfWeek( year, month+1, 1 );
    var days = GetDaysInMonth( year, month );
	
	
	
	
	
	
    var s = '<table>';             // future HTML of table
    // print names of days of week
    s += '<tr>';
    var dwnames = [ "ПН", "ВТ", "СР", "ЧТ", "ПТ", "СБ", "ВС" ];
    for( var i in dwnames )
        s += '<th>' + dwnames[i] + '</th>';
    s += '</tr>';
    // print empty cells before 1st day of this month
    s += '<tr>';
    for( var d = 0; d < dow; ++d )
        s += '<td>&nbsp;</td>';
    // print current month
    // days before now() are printed as links, after - simple text
    for( var d = 1; d <= days; ++d, dow = ( dow == 6 ? 0 : dow+1 ) )
    {
        if( dow == 0 ) s += ( d == 1 ? '' : '</tr>' ) + '<tr>';

        var cur_sel = month == sel_month && d == sel_day;
       
        s += '<td' + ( cur_sel ? ' class="selected">' : '>' );
        if( (month < realnow_m && year<=realnow_y)|| ( month == realnow_m && d <= realnow_d &&year==realnow_y ) && !cur_sel ){
        	if(now_m==month && now_d == d && now_y == year){
        		  s += '' + d;
        	}else {
        	   if(window.days[d]) {
         	  	 s += "<a href=\"?day="+d+"&month="+(month+1)+"&year="+year+"\">" + d + "</a>";           // insert a link to date here	
        	   } else {
        	   	 s+= '' +d;
        	   }
        	}	
        } else {
            s += '' + d;
            
        }
        s += '</td>';
    } 
    // print empty cells after month
    for( dow = !dow ? 7 : dow; dow < 7; ++dow )
        s += '<td>&nbsp;</td>';
    // calendar is built
    s += '</tr></table>';
    table.innerHTML = s;
}

function PrintCalendar( div )
{
	  if(!window.now_y || window.now_y==0) {
    	var now_y = (new Date()).getFullYear();    	
    } else {
   		var now_y = window.now_y;
    }
    var s = '';
    s += '<div class="round"><div class="r8"></div><div class="r6"></div><div class="r4"></div><div class="r3"></div><div class="r2"></div><div class="r2"></div><div class="r1"></div><div class="r1"></div></div>';
    var months = [ "Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь" ];
    s += '<div class="all"><div class="selmonth"><select id="calendarMonth" onchange="window.now_m=this.options[this.selectedIndex].value;window.sel_m=this.options[this.selectedIndex].value; 	ajaxm.post(\'/ajax.php\',{widget:\'newslist\',action:\'setdays\',month:window.sel_m,year:window.sel_y});ChangeCalendarLink(); ">';
	
    
    if(window.now_m==0)
    	var now_m = (new Date()).getMonth();
    else
   		var now_m = window.now_m;
    
   	
   		
    for( var i in months ){
        s += "<option value='" + (parseInt(i)+1) + "'" + ( i==now_m ? 'selected' : '' ) + ">" + months[i] + "</option>";
       
    }
    var years='';
    years+='<option value="2009" '+((now_y==2009)?'selected="selected"':'')+'>2009</option>';
     years+='<option value="2010" '+((now_y==2010)?'selected="selected"':'')+'>2010</option>';
      years+='<option value="2011" '+((now_y==2011)?'selected="selected"':'')+'>2011</option>';
    years+='<option value="2012" '+((now_y==2012)?'selected="selected"':'')+'>2012</option>';
 	years+='<option value="2013" '+((now_y==2013)?'selected="selected"':'')+'>2013</option>';
    s += '</select><select style="width:55px;" id="calendarYear" onchange="window.now_m=$(\'calendarMonth\').options[$(\'calendarMonth\').selectedIndex].value;window.now_y=this.options[this.selectedIndex].value;window.sel_y=this.options[this.selectedIndex].value; ajaxm.post(\'/ajax.php\',{widget:\'newslist\',action:\'setdays\',month:window.sel_m,year:window.sel_y});ChangeCalendarLink();">'+years+'</select><img style="visibility:hidden" src="/img/arr.gif" alt="" id="calendarGo" style="cursor:pointer;" onclick="window.location=\'/news?day=0&month='+now_m+'&year='+now_y+'\';" /></div>';
    s += '<div class="table" id="calendarTable"></div>'
    s += '</div><div class="round"><div class="r1"></div><div class="r1"></div><div class="r2"></div><div class="r2"></div><div class="r3"></div><div class="r4"></div><div class="r6"></div><div class="r8"></div></div>';
    div.innerHTML = s;
    PrintCalendarMonth();
}

function ChangeCalendarLink() {
	$('calendarGo').onclick= function() {window.location='/news?day=0&month='+window.sel_m+'&year='+window.sel_y;}
	$('calendarGo').style.visibility='visible';
}

function OnLoad()
{
    var div_cal = document.getElementById( 'divCalendar' );
    if( div_cal ) PrintCalendar( div_cal );
}




























































