// inspired by engadget archive calendar

$(function(){
	var cal = $('.cal');


	$('.prevnext li a').click(function(){
		var m =this.id;
		rotateMonth(m);
		console.log(m);
		return false;
		});

		if (!ie6) { $('.tooltip_count').each(initTooltip); }

	
});

//русские окончания -rusform(tt, "день","дня","дней")
function rusform(n, form1, form2, form5) {n = Math.abs(n) % 100;n1 = n % 10;if (n > 10 && n < 20) return form5;if (n1 > 1 && n1 < 5) return form2;if (n1 == 1) return form1;return form5;}

function rotateMonth(month){
	var cal = $('.cal');
	var dataString = 'm=' + month;
		$.ajax({
			type: 'POST',
			url: 'http://www.bashvest.ru/ajax/' + month + '.php',
			dataType: 'html',
			data: dataString,
			beforeSend: function(){
				cal.html('<i class="loader"></i>');
				},
			success: function(data, textStatus, XMLHttpRequest) {
				//console.log(data);
				cal.fadeTo('fast',0,function(){
					cal.html(data).fadeTo('fast',1);
					cal.find('.tooltip_count').each(initTooltip);
					cal.find('.prevnext li a').click(function(){
						var m =this.id;
						rotateMonth(m);
						return false;
					});
				});
			}
		});
}




function initTooltip(i) {
	this.ttRel = this.rel.split('|');
	var ttID = 'tooltip-' + i;
	var ttDate = this.ttRel[0] + ' ' + this.ttRel[1] + ' ' + this.ttRel[2];
	var ttCount  = this.ttRel[3];
	//console.log(i);

	var html = '';
		html += '<div class="arr"></div>';
		html += '<div class="date">' + ttDate + ' года</div>';
		html += '<div class="count">'+ ttCount + ' ' + rusform(ttCount, 'статья','статьи','статей') + '</div>';
		html += '';

	//$('<div class="post_count" id="' + ttID + '">' + html + '</div>').insertAfter(this); // .insertAfter(this);
	
	$('.cal .cell a').live('mouseover',function(){
		$(this).siblings('.post_count').show('fast', showFix);
	});
	$('.cal .cell a').live('mouseout',function(){
		$(this).siblings('.post_count').hide('fast', hideFix);
	});
}


function showFix(){
	$('.cell .post_count').hide();
	$(this).show();	
}
function hideFix(){
	$(this).hide();	
}
