function daysInMonth(iMonth, iYear) 
{
  return 32 - new Date(iYear, iMonth, 32).getDate();
}

function createMonthCal(date,width)
{
	if(typeof(monthes)!='object') return '';
	
	var curent=new Date();	
	var str='<div class="month" style="width:'+width+'px;"><span>'+monthes[date.getMonth()]+' '+ date.getFullYear();
	str=str+'</span><div class="days"><div class="noborder"><ul>';	
	date.setDate(1);
	var sum=date.getDay();
	var day=date.getDay();
	day=(day==0)?6:(day-1);
	for(var i=0;i<day;i++)
		str=str+'<li></li>';
	var days=daysInMonth(date.getMonth(),date.getFullYear());
	sum=sum+days;
	for(i=1;i<=days;i++)
	{
		try{
			if(date_blog[date.getFullYear()][date.getMonth()+1][i])
				if(cur_date==date.getFullYear()+'-'+(date.getMonth()<9?('0'+(date.getMonth()+1)):(date.getMonth()+1))+'-'+((i<10)?('0'+i):i))
				str=str+'<li class="curent">'+i+'</li>';
				else
				str=str+'<li class="active">'+i+'</li>';
			else
				str=str+'<li>'+i+'</li>';	
		}
		catch(e)
		{
			str=str+'<li>'+i+'</li>';
		};
/*		if((date.getMonth()==curent.getMonth())&&(i==curent.getDate()))
			str=str+'<li class="active">'+i+'</li>';
		else
			str=str+'<li>'+i+'</li>';*/
	};
	for(i=0;i<(42-sum);i++)
		str=str+'<li></li>';
	str=str+'</ul></div></div></div>';
	return str;
		
}
$(document).ready(function(){
	var curent=new Date();
	curent.setMonth(curent.getMonth()-1);
	var str=createMonthCal(curent,250);
	curent.setMonth(curent.getMonth()+1);
	str=str+createMonthCal(curent,250);
	curent.setMonth(curent.getMonth()+1);
	str=str+createMonthCal(curent,250);
	$('#calendar .monthes').empty().html(str);
	
	curent.setMonth(curent.getMonth()-1);
	$('#calendar .left-button').live('click',function(){
			$('#calendar .monthes .month:last').remove();
			curent.setMonth(curent.getMonth()-2);
			var str=createMonthCal(curent,0);
			$('#calendar .monthes').prepend(str);
			$('#calendar .monthes .month:first').animate({'width':250});
			curent.setMonth(curent.getMonth()+1);
	  });
	$('#calendar .right-button').live('click',function(){
			if($('#calendar .monthes .month:animated').length!=0) return;
			$('#calendar .monthes .month:first').animate({'width':0},300,function(){
			curent.setMonth(curent.getMonth()+2);
			var str=createMonthCal(curent,250);
			$('#calendar .monthes').append(str);
			curent.setMonth(curent.getMonth()-1);
			$('#calendar .monthes .month:first').remove();		
		});
	  });
	$('#calendar ul li.active').live('click',function(){
		var day=parseInt($(this).html());
		var urls="/?day="+day+"&month="+(curent.getMonth()+1)+"&year="+curent.getFullYear();
		var cl=$(this);
		$.ajax({
			  url:cur_url+urls,
			  success: function(data) {
			    $('.blog-ajax').html(data);
				$('#calendar ul li.curent').removeClass('curent').addClass('active');
				cl.removeClass('active').addClass('curent');
			  }
			  });
		
		
	   });
	$('.sitefriends a.tags').live('click',function(){
		var urls=$(this).attr('href');
		$.ajax({
			  url:urls,
			  success: function(data) {
			    $('.blog-ajax').html(data);
			  }
		  });
		
		
	   });
   });
//alert(createMonthCal(curent));

