	$(document).ready(function() {
//by natali
		$('#scrolling').scrollbarPaper();
		$('#scrolling2').scrollbarPaper();
		//count days for difference; date2 = date1 + difference
		var difference = 2;
		//critical difference; date2 is incorrect, if date2 < date1 + c_difference
		var c_difference = 1
 
		var currentTime = new Date();
		var year = currentTime.getFullYear();
		var month = currentTime.getMonth() + 1;
		var day = currentTime.getDate();
		$('#in_god_'+year).addClass('act');$('#in_month_'+month).addClass('act');$('#in_day_'+day).addClass('act');
		//change day and month from 1 to 01, 2 to 02, ...
		if (month < 10) {month = '0' + month;}
		if (day < 10) {day = '0' + day;}
		
		var in_changed = 0;
		var out_changed = 0;
 
		//var month_s = {'01':'Янв','02':'Фев','03':'Март','04':'Апр','05':'Май','06':'Июнь','07':'Июль','08':'Авг','09':'Сен','10':'Окт','11':'Ноя','12':'Дек'};
    els = $('#in_months').find('a');
    var month_s = new Array();
		$.each(els, function(index, value) { 
      month_s[$(this).attr('val')] = $(this).html();
    });
    
    //alert(month_s[month]);

		
		//Setup in date 
        $('#in_year').val(year);
		$('#in_month').val(month_s[month]);
		$('#in_day').val(day);
		
		$('#container').click(function(){ 
		   close_all();
		});
		
		
		//Setup out date
		refresh_out_date(new Date(year,Number(month)-1,day),difference,month_s,false,c_difference);
		
		$('.scrollbarpaper-container').click(function(event){event.stopPropagation();});
		
		$('#in_textbox').datepicker(
		{
			showWeek: true, 
			firstDay: 1, 
			minDate: new Date(2010, 2, 1), 
			maxDate:  new Date(3010, 2, 1),
			dateFormat: 'dd.mm.yy',
	        onSelect: function(dateText, inst) {
				mass = dateText.split(".");
				$('#in_year').val(mass[2]);
				$('#in_day').val(mass[0]);
				$('#in_month').val(month_s[mass[1]]);
				in_changed = 1;
				out_changed = 1;
				refresh_out_date(new Date(mass[2],Number(mass[1])-1,mass[0]),difference,month_s,true,c_difference);
			}
		});
		
		$("#in_cal").click(function(){ $("#in_textbox").datepicker("show"); });
		
		//Change year in
		$("#in_year_arrow,#in_year").click(function(event){event.stopPropagation(); if (!$('#in_years').is(':visible')) {close_all();$('#in_years').show();}else $('#in_years').hide(); });
		
		$('#in_years').find('a').click(function(event){
			event.stopPropagation();
			$('#in_years').find('a').removeClass('act');
			new_year = $(this).addClass('act').html();
			$('#in_year').val(new_year);
			if(!in_changed)
			{
				$('#in_textbox').val(day + '.' + month + '.' + new_year);
				in_changed = 1;
				out_changed = 1;
				refresh_out_date(new Date(new_year,Number(month)-1,day),difference,month_s,true,c_difference);
			}
			else
			{
				old = $('#in_textbox').val();
				mass_date = old.split('.');
				$('#in_textbox').val(mass_date[0] + '.' + mass_date[1] + '.' + new_year);
				refresh_out_date(new Date(new_year,Number(mass_date[1])-1,mass_date[0]),difference,month_s,true,c_difference);
			}
			$('#in_years').hide();
		});
		
		//Change month in
		$("#in_month_arrow,#in_month").click(function(event){ event.stopPropagation(); if(!$('#in_months').is(':visible')){close_all();$('#in_months').show(); } else $('#in_months').hide(); });
		
		$('#in_months').find('a').click(function(event){
			event.stopPropagation();
			$('#in_months').find('a').removeClass('act');
			new_month = $(this).addClass('act').html();
			new_month_val = $(this).attr('val');
			$('#in_month').val(new_month);
			if(!in_changed)
			{
				$('#in_textbox').val(day + '.' + new_month_val + '.' + year);
				in_changed = 1;
				out_changed = 1;
				refresh_out_date(new Date(year,Number(new_month_val)-1,day),difference,month_s,true,c_difference);
			}
			else
			{
				old = $('#in_textbox').val();
				mass_date = old.split('.');
				$('#in_textbox').val(mass_date[0] + '.' + new_month_val + '.' + mass_date[2]);
				refresh_out_date(new Date(mass_date[2],Number(new_month_val)-1,mass_date[0]),difference,month_s,true,c_difference);
			}
			$('#in_months').hide();
		});
		
		//Change day in
		$("#in_day_arrow,#in_day").click(function(event){event.stopPropagation();  if(!$('#in_days').is(':visible')){close_all();$('#in_days').show();}  else $('#in_days').hide(); });
		
		$('#in_days').find('ul > li > a').click(function(event){
			event.stopPropagation();
			$('#in_days').find('a').removeClass('act');
			new_day = $(this).addClass('act').html();
			if(new_day[0]!=0)if (new_day<10) new_day = '0' + new_day;
			$('#in_day').val(new_day);
			if(!in_changed)
			{
				$('#in_textbox').val(new_day + '.' + month + '.' + year);
				in_changed = 1;
				out_changed = 1;
				refresh_out_date(new Date(year,Number(month)-1,new_day),difference,month_s,true,c_difference);
			}
			else
			{
				old = $('#in_textbox').val();
				mass_date = old.split('.');
				$('#in_textbox').val(new_day + '.' + mass_date[1] + '.' + mass_date[2]);
				refresh_out_date(new Date(mass_date[2],Number(mass_date[1])-1,new_day),difference,month_s,true,c_difference);
			}
			$('#in_days').hide();
		});
		
		$('#out_textbox').datepicker(
		{
			showWeek: true, 
			firstDay: 1, 
			minDate: new Date(2010, 2, 1), 
			maxDate:  new Date(3010, 2, 1),
			dateFormat: 'dd.mm.yy',
	        onSelect: function(dateText, inst) {
				mass = dateText.split(".");
				$('#out_year').val(mass[2]);
				$('#out_day').val(mass[0]);
				$('#out_month').val(month_s[mass[1]]);
				out_changed = 1;
				check_correct(difference,c_difference,month_s);
			}
		});
		
		$("#out_cal").click(function(event){event.stopPropagation(); $("#out_textbox").datepicker("show"); });
		
		//Change year out
		$("#out_year_arrow,#out_year").click(function(event){event.stopPropagation(); if(!$('#out_years').is(':visible')){close_all();$('#out_years').show();}  else $('#out_years').hide(); });
		
		$('#out_years').find('a').click(function(event){
			event.stopPropagation();
			$('#out_years').find('a').removeClass('act');
			new_year = $(this).addClass('act').html();
			$('#out_year').val(new_year);
			if(!out_changed)
			{
				$('#out_textbox').val(day + '.' + month + '.' + new_year);
				out_changed = 1;
				check_correct(difference,c_difference,month_s)
			}
			else
			{
				old = $('#out_textbox').val();
				mass_date = old.split('.');
				$('#out_textbox').val(mass_date[0] + '.' + mass_date[1] + '.' + new_year);
				check_correct(difference,c_difference,month_s)
			}
			$('#out_years').hide();
		});
		
		//Change month out
		$("#out_month_arrow,#out_month").click(function(event){event.stopPropagation();  if(!$('#out_months').is(':visible')){close_all();$('#out_months').show();}  else $('#out_months').hide(); });
		
		$('#out_months').find('a').click(function(event){
			event.stopPropagation();
			$('#out_months').find('a').removeClass('act');
			new_month = $(this).addClass('act').html();
			new_month_val = $(this).attr('val');
			$('#out_month').val(new_month);
			if(!out_changed)
			{
				$('#out_textbox').val(day + '.' + new_month_val + '.' + year);
				out_changed = 1;
				check_correct(difference,c_difference,month_s)
			}
			else
			{
				old = $('#out_textbox').val();
				mass_date = old.split('.');
				$('#out_textbox').val(mass_date[0] + '.' + new_month_val + '.' + mass_date[2]);
				check_correct(difference,c_difference,month_s)
			}
			$('#out_months').hide();
		});
		
		//Change day out
		$("#out_day_arrow,#out_day").click(function(event){event.stopPropagation();  if(!$('#out_days').is(':visible')){close_all();$('#out_days').show();}  else $('#out_days').hide(); });
		
		$('#out_days').find('a').click(function(event){
			event.stopPropagation();
			$('#out_days').find('a').removeClass('act');
			new_day = $(this).addClass('act').html();
			if(new_day[0]!=0)if (new_day<10) new_day = '0' + new_day;
			$('#out_day').val(new_day);
			if(!out_changed)
			{
				$('#out_textbox').val(new_day + '.' + month + '.' + year);
				out_changed = 1;
				check_correct(difference,c_difference,month_s)
			}
			else
			{
				old = $('#out_textbox').val();
				mass_date = old.split('.');
				$('#out_textbox').val(new_day + '.' + mass_date[1] + '.' + mass_date[2]);
				check_correct(difference,c_difference,month_s)
			}
			$('#out_days').hide();
		});
		
		function refresh_out_date(in_date,difference,month_s,change_txt,c_difference)
		{
			in_date.setDate(in_date.getDate() + difference);
			_year = in_date.getFullYear();
			_month  = in_date.getMonth() + 1;
			_day = in_date.getDate();
			$('#out_years').find('a').removeClass('act');$('#out_months').find('a').removeClass('act');$('#out_days').find('a').removeClass('act');
			$('#out_god_'+_year).addClass('act');$('#out_month_'+_month).addClass('act');$('#out_day_'+_day).addClass('act');
			//change day and month from 1 to 01, 2 to 02, ...
			if(_month[0]!=0) if (_month < 10) {_month = '0' + _month;}
			if(_day[0]!=0) if (_day < 10) {_day = '0' + _day;}
			$('#out_year').val(_year);
			$('#out_month').val(month_s[_month]);
			$('#out_day').val(_day);
			if (change_txt) {
				$('#out_textbox').val(_day + '.' + _month + '.' + _year);
				check_correct(difference,c_difference,month_s);
			}
		}
		function check_correct(difference,c_difference,month_s)
		{
			one_day=1000*60*60*24;
			now = new Date();
			d0 = new Date(now.getFullYear(),now.getMonth(),now.getDate());
			m1 = $('#in_textbox').val();
		    mass1 = m1.split('.');
			if(mass1[2]>0) d1 = new Date(mass1[2],Number(mass1[1])-1,mass1[0]);
			else d1 = d0;
			
			m2 = $('#out_textbox').val();
		    mass2 = m2.split('.');
			d2 = new Date(mass2[2],Number(mass2[1])-1,mass2[0]);
			
			if(d1.getTime() < d0.getTime())
			{
				alert('Дата въезда должна быть больше или равной сегоднешней дате');
				d1_month = d0.getMonth() + 1; d1_day = d0.getDate();
				$('#in_years').find('a').removeClass('act');$('#in_months').find('a').removeClass('act');$('#in_days').find('a').removeClass('act');
				$('#in_god_'+d0.getFullYear()).addClass('act');$('#in_month_'+d1_month).addClass('act');$('#in_day_'+d1_day).addClass('act');
				if(d1_month[0]!=0) if (d1_month < 10) {d1_month = '0' + d1_month;}
			    if(d1_day[0]!=0) if (d1_day < 10) {d1_day = '0' + d1_day;}
				$('#in_textbox').val(d1_day + '.' + d1_month +'.' + d0.getFullYear());
				$('#in_year').val(d0.getFullYear());
				$('#in_month').val(month_s[d1_month]);
				$('#in_day').val(d1_day);
				
				d0.setDate(d0.getDate() + difference);
				d2_month = d0.getMonth() + 1; d2_day = d0.getDate();
				$('#out_years').find('a').removeClass('act');$('#out_months').find('a').removeClass('act');$('#out_days').find('a').removeClass('act');
				$('#out_god_'+d0.getFullYear()).addClass('act');$('#out_month_'+d2_month).addClass('act');$('#out_day_'+d2_day).addClass('act');
				if(d2_month[0]!=0) if (d2_month < 10) {d2_month = '0' + d2_month;}
			    if(d2_day[0]!=0) if (d2_day < 10) {d2_day = '0' + d2_day;}
				$('#out_textbox').val(d2_day + '.' + d2_month +'.' + d0.getFullYear());
				$('#out_year').val(d0.getFullYear());
				$('#out_month').val(month_s[d2_month]);
				$('#out_day').val(d2_day);
 
			}
			else if(  (d2.getTime() - d1.getTime())/(one_day) < c_difference  ) 
			{
				//console.log(  (d2.getTime() - d1.getTime())/(one_day)  );
				alert('Дата выезда должна быть позднее даты заезда');
				d1.setDate(d1.getDate() + difference);
				d2_month = d1.getMonth() + 1; d2_day = d1.getDate();
				$('#out_years').find('a').removeClass('act');$('#out_months').find('a').removeClass('act');$('#out_days').find('a').removeClass('act');
				$('#out_god_'+d1.getFullYear()).addClass('act');$('#out_month_'+d2_month).addClass('act');$('#out_day_'+d2_day).addClass('act');
				if(d2_month[0]!=0) if (d2_month < 10) {d2_month = '0' + d2_month;}
			    if(d2_day[0]!=0) if (d2_day < 10) {d2_day = '0' + d2_day;}
				$('#out_textbox').val(d2_day + '.' + d2_month +'.' + d1.getFullYear());
				$('#out_year').val(d1.getFullYear());
				$('#out_month').val(month_s[d2_month]);
				$('#out_day').val(d2_day);
			
			}
		}
		function close_all()
		{
			$("#in_years").hide();
			$("#out_years").hide();
			$("#in_months").hide();
			$("#out_months").hide();
			$("#in_days").hide();
			$("#out_days").hide();
		}
})
