$(document).ready(function()
{
  changeDp_lot();
  updateDepCal_lot();
  update_date_hid_input();

  $(".direction").click(function()
  {
    if($(this).attr("value")=='1')
    {
      $("#day_powrot").attr("disabled", true); 
      $("#mon_powrot").attr("disabled", true); 
      $("#yrs_powrot").attr("disabled", true); 
      $(".data_powrotu2").hide(); 
      $("#przylot").hide(); 
    }
		else
		{
      $("#day_powrot").removeAttr("disabled", true); 
      $("#mon_powrot").removeAttr("disabled", true); 
      $("#yrs_powrot").removeAttr("disabled", true); 
      $(".data_powrotu2").show(); 
      $("#przylot").show(); 
    }
	});
	
	$('.person').change(function()
	{
    var a = parseInt($('#adult').val());
    var c = parseInt($('#child').val());
    var i = parseInt($('#infant').val());
    
    if(a<=3) inf = parseInt(a * 2);
    else if(a>3) inf = parseInt(9 - a - c);
    
    if(inf+a+c>9)
      inf = 9 - a - c;
      
    if($(this).attr('id')=='adult')
    {
      createoption('child',0,9-a-i);
      createoption('infant',0,inf);
    }
    
    if($(this).attr('id')=='child')
    {	
      createoption('adult',0,9-c-i);
      createoption('infant',0,inf);
    }
    
    if($(this).attr('id')=='infant')
    {
      adt = Math.round(i / 2);
      
      createoption('adult',adt,9-c-i);
      createoption('child',0,9-a-i);
    }
    
    $('#adult').val(a);
    $('#child').val(c);
    $('#infant').val(i);
  });
  
  function createoption(selectid,min,max)
  {
    $('#'+selectid).html('');
    
    for(i=min;i<=max;i++)
      $('#'+selectid).append($('<option></option').val(i).html(i));
  }

	$("#miasto1").click(function()
	{
    $(this).attr("value","");
  });
	
	$("#miasto1").autocomplete('http://beta.hotele.net/ajax/Autocomplete/' , {
		width: 360,
		autoFill: true,
		scrollHeight: 300,	
		scroll: true,
		formatItem: formatItem,
		formatResult: formatResult,
		minChars: 3,
		mustMatch: false,
		selectFirst: true
	});
	
	$("#miasto1").result(function(event, data, formatted) {
		if(data)
		{
			$(".from_0").attr("value",data[1]);
			$(".to_1").attr("value",data[1]);
		}
	});
  
  $("#miasto2").click(function()
  {
    $(this).attr("value","");
	});
	
	$("#miasto2").autocomplete('http://beta.hotele.net/ajax/Autocomplete/' , {
		width: 360,
		autoFill: true,
		scrollHeight: 300,	
		scroll: true,
		formatItem: formatItem,
		formatResult: formatResult,
		minChars: 3,
		mustMatch: false,
		selectFirst: true
	});
	
	$("#miasto2").result(function(event, data, formatted) {
		if(data)
		{
			$(".to_0").attr("value",data[1]);
			$(".from_1").attr("value",data[1]);
		}
	});

	function updateArrDates_wylot(date) {
		var sdate = date.split('-');

		$('#mon_wylot').val(sdate[1]); 
		$('#day_wylot').val(sdate[2]); 
		$('#yrs_wylot').val(sdate[0]);
		
		changeDays_lot('wylot',sdate[0],sdate[2]);
		changeAr_lot();
		setDep_lot();
		update_date_hid_input();
		
		var newDepDay_lot = $('#yrs_powrot').val()+'-'+$('#mon_powrot').val()+'-'+$('#day_powrot').val();
		$("#data_powrotu2").val(newDepDay_lot);
	} 
	
	function updateDepDates_powrot(date) {
		var sdate = date.split('-');

		$('#mon_powrot').val(sdate[1]); 
		$('#day_powrot').val(sdate[2]); 
		$('#yrs_powrot').val(sdate[0]);
		
		update_date_hid_input();
	}

	$("#data_wylotu2").datepicker({
		dateFormat: 'yy-m-d',
		defaultDate: +3,
		numberOfMonths: 1,
		minDate: '+3D',
		maxDate: '+1Y',
		onSelect: updateArrDates_wylot,
		showOn: 'button', buttonImage: '/public/images/icon_calendar.gif', buttonImageOnly: true
	});
	
	$("#data_powrotu2").datepicker({
		dateFormat: 'yy-m-d',
		defaultDate: +10,
		numberOfMonths: 1,
		minDate: '+3D',
		maxDate: '+1Y',
		onSelect: updateDepDates_powrot,
		showOn: 'button', buttonImage: '/public/images/icon_calendar.gif', buttonImageOnly: true
	});

	$("select#day_wylot").change(function(){
		changeAr_lot();
		setDep_lot();
		updateArCal_lot();
		updateDepCal_lot();
		update_date_hid_input();
	});

	$("select#mon_wylot").change(function(){
		changeAr_lot();
		setDep_lot();
		updateArCal_lot();
		updateDepCal_lot();
		update_date_hid_input();
	});
	
	$("select#yrs_wylot").change(function(){
		changeAr_lot();
		setDep_lot();
		updateArCal_lot();
		updateDepCal_lot();
		update_date_hid_input();
	});
	
	$("select#day_powrot").change(function(){
		updateDepCal_lot();
		update_date_hid_input();
	});
	
	$("select#mon_powrot").change(function(){
		changeDp_lot();
		updateDepCal_lot();
		update_date_hid_input();
	});
	
	$("select#yrs_powrot").change(function(){
		changeDp_lot();
		updateDepCal_lot();
		update_date_hid_input();
	});
	
	function updateDepCal_lot() {
		var updateCal_lot = $('#yrs_powrot').val()+'-'+$('#mon_powrot').val()+'-'+$('#day_powrot').val();
		$("#data_powrotu2").val(updateCal_lot);
		update_date_hid_input();
	}
	
	function updateArCal_lot() {
		var updateCal_lot = $('#yrs_wylot').val()+'-'+$('#mon_wylot').val()+'-'+$('#day_wylot').val();
		$("#data_wylotu2").val(updateCal_lot);
		update_date_hid_input();
	}
	
	function update_date_hid_input(){
    var day_wylot = $('#day_wylot').val();
    var mon_wylot = $('#mon_wylot').val();
    var day_powrot = $('#day_powrot').val();
    var mon_powrot = $('#mon_powrot').val();
    
    if(mon_wylot == '1' || mon_wylot == '2' || mon_wylot == '3' || mon_wylot == '4' || mon_wylot == '5' || mon_wylot == '6' || mon_wylot == '7' || mon_wylot == '8' || mon_wylot == '9' ){
      var mon_wylot = '0'+mon_wylot;
    }
    
    if(day_wylot == '1' || day_wylot == '2' || day_wylot == '3' || day_wylot == '4' || day_wylot == '5' || day_wylot == '6' || day_wylot == '7' || day_wylot == '8' || day_wylot == '9' ){
      var day_wylot = '0'+day_wylot;
    }
    
    if(mon_powrot == '1' || mon_powrot == '2' || mon_powrot == '3' || mon_powrot == '4' || mon_powrot == '5' || mon_powrot == '6' || mon_powrot == '7' || mon_powrot == '8' || mon_powrot == '9' ){
      var mon_powrot = '0'+mon_powrot;
    }
    
    if(day_powrot == '1' || day_powrot == '2' || day_powrot == '3' || day_powrot == '4' || day_powrot == '5' || day_powrot == '6' || day_powrot == '7' || day_powrot == '8' || day_powrot == '9' ){
      var day_powrot = '0'+day_powrot;
    }
    
    var data_wylotu_i = $('#yrs_wylot').val()+'-'+mon_wylot+'-'+day_wylot;
    var data_powrotu_i = $('#yrs_powrot').val()+'-'+mon_powrot+'-'+day_powrot;
    
    $(".data_wylotu_i").attr("value",data_wylotu_i);
    $(".data_powrotu_i").attr("value",data_powrotu_i);
	}
		
	function changeAr_lot() {
		var ard = $('#day_wylot').val();
		var arm = $('#mon_wylot').val();
		var ary = $('#yrs_wylot').val();
		var d = new Date();
		var today = d.getDate()+3;
		var cm = d.getMonth();	// current month
		var cy = d.getFullYear();	// current year
		var i = 1;
		cm = cm + 1;	// months in js 0-11 not 1-12
		
		if(ary != cy) {
			$("select#mon_wylot").find('option').attr("disabled", false); 
		} else {
			$("select#mon_wylot option").each(function(i){
   				 if($(this).val() < cm) {
					$(this).attr("disabled", true);
				 }
			});
			if ($("select#mon_wylot option:selected").val() < cm) {
				$("select#mon_wylot option[value='"+cm+"']").attr('selected', 'selected');
			}
		}

		if(arm <= cm && ary == cy) {
			i = today;
		}
		
		changeDays_lot('wylot',ary,i);
		$('#day_wylot').val(ard);
	}
	
	function changeDp_lot() {
		var ard = $('#day_wylot').val();
		var arm = $('#mon_wylot').val();
		var ary = $('#yrs_wylot').val();
		
		var dpd = $('#day_powrot').val();
		var dpm = $('#mon_powrot').val();
		var dpy = $('#yrs_powrot').val();
		
		var i;

		if(ary == dpy) {
			$("select#mon_powrot option").each(function(i){
				 if($(this).val() < parseInt(arm)) {
					$(this).attr("disabled", true);
				 } else {
					 $(this).attr("disabled", false);
				 }
			});
		} else {
			$("select#mon_powrot").find('option').attr("disabled", false);
		}
		
		$("select#yrs_powrot option").each(function(i){
			 if($(this).val() < parseInt(ary)) {
				$(this).attr("disabled", true);
			 } else {
				 $(this).attr("disabled", false);
			 }
		});
		
		// set dpd to ard + 1
		if( $("select#day_wylot").val() == ($("select#day_wylot option:last").val()-6) && $("select#mon_wylot").val() != 12 ) {
      i = 1;
			$('#mon_powrot').val(parseInt(arm)+1);
			dpd = 1;
		} else if( $("select#day_wylot").val() == ($("select#day_wylot option:last").val()-5) && $("select#mon_wylot").val() != 12 ) {
      i = 1;
			$('#mon_powrot').val(parseInt(arm)+1);
			dpd = 2;
		} else if( $("select#day_wylot").val() == ($("select#day_wylot option:last").val()-4) && $("select#mon_wylot").val() != 12 ) {
      i = 1;
			$('#mon_powrot').val(parseInt(arm)+1);
			dpd = 3;
		} else if( $("select#day_wylot").val() == ($("select#day_wylot option:last").val()-3) && $("select#mon_wylot").val() != 12 ) {
      i = 1;
			$('#mon_powrot').val(parseInt(arm)+1);
			dpd = 4;
		} else if( $("select#day_wylot").val() == ($("select#day_wylot option:last").val()-2) && $("select#mon_wylot").val() != 12 ) {
      i = 1;
			$('#mon_powrot').val(parseInt(arm)+1);
			dpd = 5;
		} else if( $("select#day_wylot").val() == ($("select#day_wylot option:last").val()-1) && $("select#mon_wylot").val() != 12 ) {
      i = 1;
			$('#mon_powrot').val(parseInt(arm)+1);
			dpd = 6;
		} else if( $("select#day_wylot").val() == $("select#day_wylot option:last").val() && $("select#mon_wylot").val() != 12 ) {
			i = 1;
			$('#mon_powrot').val(parseInt(arm)+1);
			dpd = 7;
		} else if ($("select#day_wylot").val() == ($("select#day_wylot option:last").val()-6) && $("select#mon_wylot").val() == 12) {
			i = 1;
		} else if ($("select#day_wylot").val() == ($("select#day_wylot option:last").val()-5) && $("select#mon_wylot").val() == 12) {
			i = 1;
		} else if ($("select#day_wylot").val() == ($("select#day_wylot option:last").val()-4) && $("select#mon_wylot").val() == 12) {
			i = 1;
		} else if ($("select#day_wylot").val() == ($("select#day_wylot option:last").val()-3) && $("select#mon_wylot").val() == 12) {
			i = 1;
		} else if ($("select#day_wylot").val() == ($("select#day_wylot option:last").val()-2) && $("select#mon_wylot").val() == 12) {
			i = 1;
		} else if ($("select#day_wylot").val() == ($("select#day_wylot option:last").val()-1) && $("select#mon_wylot").val() == 12) {
			i = 1;
		} else if ($("select#day_wylot").val() == $("select#day_wylot option:last").val() && $("select#mon_wylot").val() == 12) {
			i = 1;
		} else {
			i = parseInt(ard)+7;	
		}
		
		if(dpm > arm) i = 1;
				
		changeDays_lot('powrot',dpy,i);
		$('#day_powrot').val(dpd);

		if(dpy > ary) $("select#mon_powrot").find('option').attr("disabled", false);
				
	}
	
	function changeDays_lot(f,y,i) { // ar,dep + year + start day
		var field = 'day_'+f;
		var m = $("select#mon_"+f).val();
		
		$("select#day_"+f).find('option').remove().end();
		
		if(m == 2) {
			var year = y-2000;
			if (Math.floor(year/4)==year/4) days=29; else days=28;
			for(i;i<=days;i++) {
				$("select#day_"+f).append('<option value="'+i+'">'+i+'</option>');
			}
		} else if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12) {
			for (i ; i<=31 ; i++){
				$("select#day_"+f).append('<option value="'+i+'">'+i+'</option>');
			}
		} else {
			for (i ; i<=30 ; i++){
				$("select#day_"+f).append('<option value="'+i+'">'+i+'</option>');
			}	
		}	
	}
	
	function setDep_lot() {
		var ard = $('#day_wylot').val();
		var arm = $('#mon_wylot').val();
		var ary = $('#yrs_wylot').val();
		
		$('#mon_powrot').val(arm);
		$('#yrs_powrot').val(ary);

		changeDp_lot();

		// Happy new year addon
		if(ard == 25 && arm == 12) {
			ary = parseInt(ary)+1;
			$("#day_powrot").val(1);
			$("#mon_powrot").val(1);
			$("#yrs_powrot").val(ary);
			$("select#mon_powrot").find('option').attr("disabled", false);
		} else if(ard == 26 && arm == 12) {
			ary = parseInt(ary)+1;
			$("#day_powrot").val(2);
			$("#mon_powrot").val(1);
			$("#yrs_powrot").val(ary);
			$("select#mon_powrot").find('option').attr("disabled", false);
		} else if(ard == 27 && arm == 12) {
			ary = parseInt(ary)+1;
			$("#day_powrot").val(3);
			$("#mon_powrot").val(1);
			$("#yrs_powrot").val(ary);
			$("select#mon_powrot").find('option').attr("disabled", false);
		} else if(ard == 28 && arm == 12) {
			ary = parseInt(ary)+1;
			$("#day_powrot").val(4);
			$("#mon_powrot").val(1);
			$("#yrs_powrot").val(ary);
			$("select#mon_powrot").find('option').attr("disabled", false);
		} else if(ard == 29 && arm == 12) {
			ary = parseInt(ary)+1;
			$("#day_powrot").val(5);
			$("#mon_powrot").val(1);
			$("#yrs_powrot").val(ary);
			$("select#mon_powrot").find('option').attr("disabled", false);
		} else if(ard == 30 && arm == 12) {
			ary = parseInt(ary)+1;
			$("#day_powrot").val(6);
			$("#mon_powrot").val(1);
			$("#yrs_powrot").val(ary);
			$("select#mon_powrot").find('option').attr("disabled", false);
		} else if(ard == 31 && arm == 12) {
			ary = parseInt(ary)+1;
			$("#day_powrot").val(7);
			$("#mon_powrot").val(1);
			$("#yrs_powrot").val(ary);
			$("select#mon_powrot").find('option').attr("disabled", false);
		}
  }

	$(".search_button_flight").click(function(){

		var m1 = $("#miasto1").val();
		var m2 = $("#miasto2").val();
		
		if($('#miasto1').val()=='' || $('#miasto2').val()=='')
		{
			alert("Proszę wybrać miasto wylotu i/lub przylotu.");
			return false;
		}
			
		if(m1.length==3)
		{
			$("#from_0").val($("#miasto1").val().toUpperCase());
			$("#to_1").val($("#miasto1").val().toUpperCase());
		}
		
		if(m2.length==3)
		{
			$("#from_1").val($("#miasto2").val().toUpperCase());
			$("#to_0").val($("#miasto2").val().toUpperCase());
		}
				
		if($('#data_wylotu2').val() > $('#data_powrotu2').val() && $(".direction").attr("value")!='1')
		{
			alert("Data powrotu musi być późniejsza lub taka sama jak data wylotu.");
			return false;
		}
		
		$("#logotype").hide();
		$("input").hide();
		$("select").hide();

		$('.preloader_f').show();
		$("#preloader_arrdate").html($("#data_wylotu2").val());
		$("#preloader_depdate").html($("#data_powrotu2").val());
		$("#preloader_city_form").html($("#miasto1").val());
		$("#preloader_city_to").html($("#miasto2").val());
	});
	
});
	
	function formatItem(row) {
		if (row[0]==1)
			return "<div id=Item2>" + row[3]+ "</div><div id=Item3><img src='/public/images/flag/"+row[6]+"'></div><div id=Item4>[" + row[4]+   "]</div>";
		else if (row[0]==2)
			return "<div id=Item1>&nbsp;" + row[3]+ "</div><div id=Item2> , " + row[5]+ "</div><div id=Item3><img src='/public/images/flag/"+row[6]+"'></div><div id=Item4>[" + row[4]+   "]</div>";
		else if ( row[0]==3)
			return "<div id=Item2>" + row[3]+ "</div><div id=Item2> , " + row[5]+ "</div><div id=Item3><img src='/public/images/flag/"+row[6]+"'></div><div id=Item4>[" + row[4]+   "]</div>";
		else
			return "<div id=Item2>" + row[3]+ "</div><div id=Item3><img src='/public/images/flag/"+row[6]+"'></div><div id=Item4>[" + row[4]+   "]</div>";
	}
	
	function formatResult(row) {
		return row[3].replace(/(<.+?>)/gi, '');
	}
	
