var current_month = 0;
var current_year = 0;

function resize_bohemia(){
	$('#wrap, #side, #content').css({height:'auto'});
	var h = $('#content').height();
	$('#side').height(h);
	if ($('#wrap').height() < h)
		if ($('#index-slider').size() != 0) $('#wrap').height(h-376);
		else
			$('#wrap').height(h-85);
	
	$('#coolbg2').height($('#coolbg1').height()-640);
}

$(document).ready(function(){
	var tmr = setInterval(resize_bohemia, 700);
});

(function($) {
	$.fn.calendar = function(s) {
		var s = $.extend({
			delay: 250,
			months: ['январь', 'февраль', 'март', 'апрель', 'май', 'июнь', 'июль', 'август', 'сентябрь', 'октябрь', 'ноябрь', 'декабрь']
		}, s);
		
		var now = new Date();
		now = now.getMonth();
		
		var c = {
			it: null,
			span: null
		};

		return this.each(function(i, e) {
			c.it = $(this);
			c.span = c.it.find('.calendar-slider span');
			
			c.it.find('.calendar-slider').hide();
			c.it.find('.calendar-slider .right').click(_next);
			c.it.find('.calendar-slider .left').click(_prev);
			
			c.it.find('a.hidden.selectmonth').click(select_month);
			
			find_next();
			find_prev();
			//_setmonth();
			c.it.find('.calendar-slider .right').click();
			c.it.find('.calendar-slider .left').click();
		});
		
		function _setmonth(){
			if (c.lastnow == now && c.lastyear == current_year) return;
			if (c.it.find('#cm-'+now).attr('data-year') != current_year) now=666;
			
			c.span.fadeOut(s.delay);
			c.it.find('.calendar-month').fadeOut(s.delay);
			c.it.find('.inner').stop().animate({height:c.it.find('#cm-'+now).height()},{
				step: resize_bohemia
			}, s.delay*0.9);
			
			$('.calendar-item:first-child').addClass('calendar-item-first');
    		$('.calendar-item:last-child').addClass('calendar-item-last');
			
			setTimeout(function(){
				//c.span.text(s.months[now]).fadeIn(s.delay);
				c.it.find('#cm-'+now).fadeIn(s.delay);
				/*
				var h = 0;
				$('#side > *').each(function(i,e){
					if ($.browser.msie && $.browser.version.split('.')[0] <= 8)
						h += $(e).outerHeight();
					else
						h += $(e).height();
				});
				
				if ($.browser.msie && $.browser.version.split('.')[0] <= 8)
					h = h;
				else
					h -= 80;
				
				if ($('#side').outerHeight(true) < h)
					$('#side').animate({height:h});*/
				/*
				$('#wrap, #side, #content').css({height:'auto'});
				var h = $('#content').height();
				$('#side').animate({height:h});
				if ($('#wrap').height() < h)
					if ($('#index-slider').size() != 0) $('#wrap').animate({height:h-376});
					else
						$('#wrap').animate({height:h-85});
				*/
			}, s.delay);
			
			c.lastnow = now;
			c.lastyear = current_year;
		}
		
		function _next(){
			find_next();
			_setmonth();
			
			return false;
		}
		
		function _prev(){
			find_prev();
			_setmonth();
			
			return false;
		}
		
		function find_next(){
			var i = 0;
			do{
				now++;
				if (now > 11) now = 0;
			}while (c.it.find('#cm-'+now).size() == 0 || i++ > 20)
		}
		
		function find_prev(){
			do{
				now--;
				if (now < 0) now = 11;
			}while (c.it.find('#cm-'+now).size() == 0)
		}
		
		function select_month(){
			now = current_month;
			if (now < 0) now = 11;
			else if (now > 11) now = 0;
			_setmonth();
			return false;
		}
	}
})(jQuery);

