jQuery(document).ready(function() {

	var mConfig = new Array("d2","d4","d5","d2","d7","d3","d7","d6","d4","d2","d3","d1");
	var mStart = (new Date).getMonth();
	var leapYear = (0 == (new Date).getYear() % 4);
	var cMonths = new Array("Январь","Февраль","Март","Апрель","Май","Июнь","Июль","Август","Сентябрь","Октябрь","Ноябрь","Декабрь");
	function setCalendar(month, year){
		// Загрузить 
//		if ( !month ) month = (new Date).getMonth();
		if ( !year ) year = (new Date).getFullYear();
		mYear = year
		jQuery('#dates').load('/sys_days', {authenticity_token:auth,year :year, month:month+1, periodical_name:periodical, rubric_name: rubric },function(txt,status){
			leapYear = (0 == year % 4);
			jQuery(".calendar #dates li:first").attr("class", _getFirstDayOfMonth(year,month));
			if (month == 3 || month == 5 || month == 8 || month == 10) {
				jQuery(".calendar #dates li:eq(30)").css("display", "none");
				jQuery(".calendar #dates li:eq(29), .calendar #dates li:eq(28)").css("display", "block");
			}
			else 
				if (month == 1) {
					if (leapYear) {
						jQuery(".calendar #dates li:eq(30), .calendar #dates li:eq(29), .calendar #dates li:eq(28)").css("display", "none");
					}
					else {
						jQuery(".calendar #dates li:eq(30), .calendar #dates li:eq(29)").css("display", "none");
					}
				}
				else {
					jQuery(".calendar #dates li:eq(30), .calendar #dates li:eq(29), .calendar #dates li:eq(28)").css("display", "block");
				};
			jQuery(".calendar h2 .month").text(cMonths[month]);
		}).html('Загружаю....');
		return month;
	};

	function _getFirstDayOfMonth(year, month){
		return 'd' + (1 + (6+(new Date(year, month, 1)).getDay()) % 7);
	}

	var mCurrent = setCalendar(mStart);
	jQuery(".announces ul li:last").addClass("last");
	jQuery(".calendar h2 .monthFwrd").click(function() { if (mCurrent == 11) { setCalendar(mCurrent = 0, mYear+1); } else { mCurrent = mCurrent + 1; setCalendar(mCurrent,mYear); }; });
	jQuery(".calendar h2 .monthBack").click(function() { if (mCurrent == 0) { setCalendar(mCurrent = 11, mYear-1); } else { mCurrent = mCurrent - 1; setCalendar(mCurrent,mYear); }; });
	jQuery("#regForm dl dt:odd").addClass("odd");
	jQuery("#auth #login").toggle(function() { jQuery(".block.authForm").slideDown(); }, function() { jQuery(".block.authForm").slideUp(); });
	if( jQuery('#login_errors').html() ) jQuery(".block.authForm").slideDown();
	jQuery("#navigation li").hover(function() { jQuery(this).addClass("hover").find(".submenu").css("display","block"); }, function() { jQuery(this).removeClass("hover").find(".submenu").css("display","none"); });
	jQuery("#content .box img").load(function() { var width = jQuery(this).width(); jQuery(this).parent().width(width); });

	jQuery("#profilePrefs").toggle(function() { jQuery(this).addClass("active"); jQuery(".panel #profile").addClass("active").load('/sys_user_preferences', init_preferences); }, function() { jQuery(this).removeClass("active"); jQuery(".panel #profile").removeClass("active"); })
    function init_preferences(){ jQuery("#profile .profileColumn ul li .arrow").toggle(function() { jQuery(this).parent().addClass("show"); }, function() { jQuery(this).parent().removeClass("show"); }); jQuery("#profile .profileColumn > ul li input").click(function() { if (jQuery(this).is(":checked")) { jQuery(this).parent().find("ul li input").attr("checked","checked"); }; if (jQuery(this).is(":not(:checked)")) { jQuery(this).parent().find("ul li input").attr("checked",""); }; }); };
	jQuery("#content table tr").find("td:even").addClass("odd");
	jQuery(".print").click(function() { $("link[href*=styles.css]").attr("href","/print.css"); return false; });
});


