(function($) {
	$.fn.newsCalendar = function(userMonth, userYear){

		// default configuration properties
		return this.each(function() {
			var months = ['январь', 'февраль', 'март', 'апрель', 'май', 'июнь', 'июль', 'август', 'сентябрь', 'октябрь', 'ноябрь', 'декабрь'];
			var curDate = new Date();
			var curMonth;
			var curYear;
			if(userMonth==''||userMonth==0){
				curMonth = curDate.getMonth();
			}else{
				curMonth = userMonth-1;
			}
			if(userYear==''||userYear==0){
				curYear = curDate.getFullYear();
			}else{
				curYear = userYear;
			}
			
			var mainObj = $(this);
			var years = $('div.year', mainObj);
			var totalYears = years.length;
			var firstYear = years[0].id;
			var lastYear = years[years.length-1].id;
			var mainObjWrapper = $('div.newsCalendarWrapper', mainObj);
			var prevMonth = $('#prevMonth', mainObj);
			var nextMonth = $('#nextMonth', mainObj);
			var prevYear = $('#prevYear', mainObj);
			var nextYear = $('#nextYear', mainObj);
			var dateContainer = $('span.dateContainer', mainObj);

			mainObjWrapper.css('marginLeft', (-316*curMonth)+"px");
			mainObjWrapper.css('marginTop', (-205*(curYear-firstYear))+"px");

			prevMonth.bind('click', function(){
				rotateNewsCalendar('month', 'prev', mainObjWrapper);
			});
			nextMonth.bind('click', function(){
				rotateNewsCalendar('month', 'next', mainObjWrapper);
			});
			prevYear.bind('click', function(){
				rotateNewsCalendar('year', 'prev', mainObjWrapper);
			});
			nextYear.bind('click', function(){
				rotateNewsCalendar('year', 'next', mainObjWrapper);
			});
			function rotateNewsCalendar(type, direction, obj){
				if(type=="month"){
					if(direction == "next"){
						if(curMonth==11){
							if(curYear<lastYear){
								curMonth = 0;
								curYear++;
							}
						}else{
							curMonth++;
						}
					}else{
						if(curMonth==0){
							if(curYear>firstYear){
								curMonth = 11;
								curYear--;
							}
						}else{
							curMonth--;
						}
					}
				}
				if(type=="year"){
					if(direction == "next"){
						if(curYear<lastYear){
							curYear++;
						}
					}else{
						if(curYear>firstYear){
							curYear--;
						}
					}
					
				}
				dateContainer.html(months[curMonth] + ' ' + curYear);
				mainObjWrapper.css('marginLeft', (-316*curMonth)+"px");
				mainObjWrapper.css('marginTop', (-205*(curYear-firstYear))+"px");
			}
		});
	};
})(jQuery);
