(function($) {
	$.fn.calendarMain = function(data, currentTime){

		// default configuration properties
		var events = data;
		var months = ['январь', 'февраль', 'март', 'апрель', 'май', 'июнь', 'июль', 'август', 'сентябрь', 'октябрь', 'ноябрь', 'декабрь'];
		var monthWeeks = ['ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ', 'ВС'];
		var daysToRus = [6, 0, 1, 2, 3, 4, 5];
		
		return this.each(function() {
			var mainObj = $(this);
			mainObj.html(events);
			var currentDate;
			if(typeof(currentTime) == 'undefined'){
				currentDate = new Date();
			}else{
				currentDate = new Date(currentTime);
			}
			var currentMonth = currentDate.getMonth();
			var currentDay = currentDate.getDate();
			var currentYear = currentDate.getFullYear();
			var mainObjWrap = $('div.calendarWrapper', mainObj); 
			var nextMonthObj = $('#calendarMainNextMonth', mainObj);
			var prevMonthObj = $('#calendarMainPrevMonth', mainObj);
			var currentMonthObj = $('div.calendarMainMonthNavigate a.calendarMainCurrentMonth');
			var lastMonth = $('div.calendarWrapperWrapper', mainObj).length;
			var rotateValue = 937;

			$('#calendarMainDayTab', mainObj).bind('click',
				function(){
					$.get('/version2/blocks/calendar_blocks/document62624.phtml?date='+currentYear+'-'+(currentMonth+1)+'-'+currentDay, function(data){
						loc = window.location.href;
						loc = loc.replace(window.location.hash, '');
						window.location = loc+'#Day/'+currentMonth+'-'+currentDay+'-'+currentYear;
						mainObj.calendarMainDay(data, currentDate.getTime());
					});
			});
			$('#calendarMainWeekTab', mainObj).bind('click',
				function(){
					$.get('/version2/blocks/calendar_blocks/week.phtml?date='+currentYear+'-'+(currentMonth+1)+'-'+currentDay, function(data){
						loc = window.location.href;
						loc = loc.replace(window.location.hash, '');
						window.location = loc+'#Week/'+currentMonth+'-'+currentDay+'-'+currentYear;
						mainObj.calendarMainWeek(data, currentDate.getTime());
					});
			});
			if(currentMonth>2){
				prevMonthObj.bind('click', function(){
					moveCalendar(this, 'left', 'megaClick');
				});
				prevMonthObj.bind('megaClick', function(){
					moveCalendar(this, 'left');
				});
			}
//			console.log(currentMonth);
			if(currentMonth<(lastMonth-2)){
				nextMonthObj.bind('click', function(){
					moveCalendar(this, 'right', 'megaClick');
				});
				nextMonthObj.bind('megaClick', function(){
					moveCalendar(this, 'right');
				});
			}
			var currentPos = currentMonth; 
			function moveCalendar(obj, direction, type){
				if(direction=='right'){
					currentPos++;
					if(currentPos>(lastMonth-2)){
						nextMonthObj.unbind('click');
					}
					prevMonthObj.unbind('click');
					prevMonthObj.bind('click', function(){
						moveCalendar(this, 'left', 'megaClick');
					});
					if(type=="megaClick"){
						$('#calendarNavigate a.calendarRight').trigger('megaClick');
					}
				}else{
					currentPos--;
					if(currentPos<1){
						prevMonthObj.unbind('click');
					}
					nextMonthObj.unbind('click');
					nextMonthObj.bind('click', function(){
						moveCalendar(this, 'right', 'megaClick');
					});
					if(type=="megaClick"){
						$('#calendarNavigate a.calendarLeft').trigger('megaClick');
					}
				}
				currentDate = new Date(currentYear, currentPos, 1);
				currentMonthObj.html('<strong>'+months[currentDate.getMonth()] + '</strong> ' + currentDate.getFullYear());
				loc = window.location.href;
				loc = loc.replace(window.location.hash, '');
				window.location = loc+'#Month/'+currentDate.getMonth()+'-'+currentDate.getDate()+'-'+currentDate.getFullYear();

				$('#calendarMainDayTab', mainObj).unbind('click');
				$('#calendarMainDayTab', mainObj).bind('click',
					function(){
						$.get('/version2/blocks/calendar_blocks/document62624.phtml?date='+currentDate.getFullYear()+'-'+(currentDate.getMonth()+1)+'-'+currentDate.getDate(), function(data){
							loc = window.location.href;
							loc = loc.replace(window.location.hash, '');
							window.location = loc+'#Day/'+currentDate.getMonth()+'-'+currentDate.getDate()+'-'+currentDate.getFullYear();
							mainObj.calendarMainDay(data, currentDate.getTime());
						});
				});
				mainObjWrap.animate({
					marginLeft: (-(rotateValue*currentPos))+"px"
				}, 500);
				var currentHeight = parseInt($('#'+(currentDate.getMonth()+1)+''+currentDate.getFullYear(), mainObj).css('height'))+50;
				mainObjWrap.css('height', currentHeight);
				mainObj.css('height', currentHeight);
				mainObj.css('overflowY', 'hidden');
			}
			
			currentMonthObj.html('<strong>'+months[currentDate.getMonth()] + '</strong> ' + currentDate.getFullYear());
			var currentHeight = parseInt($('#'+(currentMonth+1)+''+currentYear, mainObj).css('height'))+50;
			mainObjWrap.css('marginLeft', -(rotateValue*currentMonth));
			mainObjWrap.css('height', currentHeight);
			mainObj.css('height', currentHeight);
			mainObj.css('overflowY', 'hidden');
		});
	};

	$.fn.calendarMainDay = function(data, currentTime){
		// default configuration properties
		var events = data;
		var months = ['январь', 'февраль', 'март', 'апрель', 'май', 'июнь', 'июль', 'август', 'сентябрь', 'октябрь', 'ноябрь', 'декабрь'];
		var monthWeeks = ['ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ', 'ВС'];
		var daysToRus = [6, 0, 1, 2, 3, 4, 5];
		return this.each(function() {
			var mainObj = $(this);
			mainObj.html(events);
			if(typeof(currentTime) == 'undefined'){
				currentDate = new Date();
			}else{
				currentDate = new Date(currentTime);
			}
			
			var currentMonth = currentDate.getMonth();
			var currentDay = currentDate.getDate();
			var currentYear = currentDate.getFullYear();
			var mainObjWrap = $('div.calendarWrapper', mainObj); 
			mainObj.height($('#fullDay', mainObj).height()+46);
			
			$('#calendarMainMonthTab', mainObj).bind('click',
				function(){
					$.get("/version2/blocks/calendar_blocks/calendarMain.phtml", function(data) { 
						loc = window.location.href;
						loc = loc.replace(window.location.hash, '');
						window.location = loc+'#Month/'+currentMonth+'-'+currentDay+'-'+currentYear;
						$("#calendarMain").calendarMain(data, currentDate.getTime());}
					);
			});
			$('#calendarMainWeekTab', mainObj).bind('click',
				function(){
					$.get('/version2/blocks/calendar_blocks/week.phtml?date='+currentYear+'-'+(currentMonth+1)+'-'+currentDay, function(data){
						loc = window.location.href;
						loc = loc.replace(window.location.hash, '');
						window.location = loc+'#Week/'+currentMonth+'-'+currentDay+'-'+currentYear;
						mainObj.calendarMainWeek(data, currentDate.getTime());
					});
			});
			$('#calendarMainPrevMonth', mainObj).bind('click', function(){
					var newDate = new Date(currentYear, currentMonth, currentDay-1, 0, 0, 0);
					$.get('/version2/blocks/calendar_blocks/document62624.phtml?date='+newDate.getFullYear()+'-'+(newDate.getMonth()+1)+'-'+newDate.getDate(), function(data){
						loc = window.location.href;
						loc = loc.replace(window.location.hash, '');
						window.location = loc+'#Day/'+newDate.getMonth()+'-'+newDate.getDate()+'-'+newDate.getFullYear();
						mainObj.calendarMainDay(data, newDate.getTime());
					});
			});
			$('#calendarMainNextMonth', mainObj).bind('click', function(){
					var newDate = new Date(currentYear, currentMonth, currentDay+1, 0, 0, 0);
					$.get('/version2/blocks/calendar_blocks/document62624.phtml?date='+newDate.getFullYear()+'-'+(newDate.getMonth()+1)+'-'+newDate.getDate(), function(data){
						loc = window.location.href;
						loc = loc.replace(window.location.hash, '');
						window.location = loc+'#Day/'+newDate.getMonth()+'-'+newDate.getDate()+'-'+newDate.getFullYear();
						mainObj.calendarMainDay(data, newDate.getTime());
					});
			});
		});
	};
	$.fn.calendarMainWeek = function(data, currentTime){
		// default configuration properties
		var events = data;
		var months = ['январь', 'февраль', 'март', 'апрель', 'май', 'июнь', 'июль', 'август', 'сентябрь', 'октябрь', 'ноябрь', 'декабрь'];
		var monthWeeks = ['ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ', 'ВС'];
		var daysToRus = [6, 0, 1, 2, 3, 4, 5];
		return this.each(function() {
			var mainObj = $(this);
			if(typeof(currentTime) == 'undefined'){
				currentDate = new Date();
			}else{
				currentDate = new Date(currentTime);
			}
			
			var currentMonth = currentDate.getMonth();
			var currentDay = currentDate.getDate();
			var currentYear = currentDate.getFullYear();

			mainObj.html(events);
			mainObj.height($('div.calendarWrapperWrapper', mainObj).height()+46);
			$('#calendarMainDayTab', mainObj).bind('click',
				function(){
					$.get('/version2/blocks/calendar_blocks/document62624.phtml?date='+currentYear+'-'+(currentMonth+1)+'-'+currentDay, function(data){
						loc = window.location.href;
						loc = loc.replace(window.location.hash, '');
						window.location = loc+'#Day/'+currentMonth+'-'+currentDay+'-'+currentYear;
						mainObj.calendarMainDay(data, currentDate.getTime());
					});
			});
			$('#calendarMainMonthTab', mainObj).bind('click',
				function(){
					$.get("/version2/blocks/calendar_blocks/calendarMain.phtml", function(data) { 
						loc = window.location.href;
						loc = loc.replace(window.location.hash, '');
						window.location = loc+'#Month/'+currentMonth+'-'+currentDay+'-'+currentYear;
						$("#calendarMain").calendarMain(data, currentDate.getTime());}
					);
			});
			$('#calendarMainPrevMonth', mainObj).bind('click', function(){
					var newDate = new Date(currentYear, currentMonth, currentDay-7, 0, 0, 0);
					$.get('/version2/blocks/calendar_blocks/week.phtml?date='+newDate.getFullYear()+'-'+(newDate.getMonth()+1)+'-'+newDate.getDate(), function(data){
						loc = window.location.href;
						loc = loc.replace(window.location.hash, '');
						window.location = loc+'#Day/'+newDate.getMonth()+'-'+newDate.getDate()+'-'+newDate.getFullYear();
						mainObj.calendarMainWeek(data, newDate.getTime());
					});
			});
			$('#calendarMainNextMonth', mainObj).bind('click', function(){
					var newDate = new Date(currentYear, currentMonth, currentDay+7, 0, 0, 0);
					$.get('/version2/blocks/calendar_blocks/week.phtml?date='+newDate.getFullYear()+'-'+(newDate.getMonth()+1)+'-'+newDate.getDate(), function(data){
						loc = window.location.href;
						loc = loc.replace(window.location.hash, '');
						window.location = loc+'#Day/'+newDate.getMonth()+'-'+newDate.getDate()+'-'+newDate.getFullYear();
						mainObj.calendarMainWeek(data, newDate.getTime());
					});
			});
		});
	};

})(jQuery);
