jQuery(document).ready(function(){

    $('#advanced').hide();

    $('#show_adv').click(function() {
	$('#advanced').toggle();
    });

    var msg="";
    $("form:first").submit(function() {
	if ($("#departure_port").val() == "") {
	    msg="Wybierz lub wpisz port wylotu!\n";
	}
      
	if ($("#arrival_port").val() == "") {
	    msg=msg+"Wybierz lub wpisz port docelowy!\n";
	}
	if ($("#dep_date").val() == "") {
	    msg=msg+"Podaj datę wylotu!\n";
	}

	if ($("#arr_date").val() == "") {
	    msg=msg+"Podaj datę powrotu!\n";
	}

	if (msg!="") {
	    alert(msg);
	    msg="";
	    return false;      
	}
      
	 return true;
    });

    $('#oneway').click(function() {
	$('#arr_date').attr("disabled", true);
	$('#arr_date').hide();
    }); 

    $('#return').click(function() {
	$('#arr_date').removeAttr("disabled");
	$('#arr_date').show();
    }); 

    $('#copy_flights').click(function() {
	var city=$('#arrival_port').val();
	$('#city').val(city.substr(0,strpos(city, '[', 0)-1));
	$('#checkin').val($('#dep_date').val());
	$('#checkout').val($('#arr_date').val());
    });

    $('#departure_port').click(function() {
	$('#departure_port').val("");
    });

    $('#arrival_port').click(function() {
	$('#arrival_port').val("");
    });

    $('#arrival_port_2').click(function() {
	$('#arrival_port_2').val("");
    });

    $("span.apt").live("click", function(){
	$("#departure_port").val($(this).text() );
	$('#dep_port').dialog("close");
        $("#adt_count").show();
	$("#child_count").show();
	$("#inf_count").show();
	$("#flex_date").show();
	$("#specific_airline1").show();
    });

    $("span.apt2").live("click", function(){
	$("#arrival_port").val($(this).text() );
	$('#arr_port').dialog("close");
	$("#modal").hide();
	$("#adt_count").show();
	$("#child_count").show();
	$("#inf_count").show();
	$("#flex_date").show();
        $("#specific_airline1").show();
    });

    $("#departure_port").autocomplete('/Search/airportSuggestsForSirius.html');
    $("#arrival_port").autocomplete('/Search/airportSuggestsForSirius.html');
        

    $("#multi_dep_1").autocomplete(dep_apt_table);
    $("#multi_dep_2").autocomplete(dep_apt_table);

    $("#multi_arr_1").autocomplete(dep_apt_table);
    $("#multi_arr_2").autocomplete(dep_apt_table);

    $(function() {
	$("#dep_date").datepicker({dateFormat: 'yy-mm-dd',
	    buttonImage: 'view/default/img/buttonCal.jpg',
	    buttonImageOnly: true,
	    maxDate:'+12m',
	    minDate:'0d',
	    rangeSelect: false, 
	    numberOfMonths: 2, 
	    showOn: 'both',
	    showAnim: 'slideDown',
	    speed:'fast',
	    onClose: function(date) { 
		if ($('#arr_date').val()<date) $('#arr_date').val(date);
		var data=date.split("-"); 
		$('#arr_date').datepicker('option', 'minDate', new Date(data[0],data[1]-1,data[2]));
	    },
	    onSelect: function(date){
		if ($('#arr_date').val()<date) $('#arr_date').val(date);
		var data=date.split("-");
		setTimeout(function(){
		    $("#arr_date").datepicker('option', 'minDate', new Date(data[0],data[1]-1,data[2])).focus();
		},1);
	    }
	});
    });

    $(function() {
	$("#checkin").datepicker({dateFormat: 'yy-mm-dd' ,
	    maxDate:'+12m',
	    minDate:'0d',
	    rangeSelect: false, 
	    numberOfMonths: 2, 
	    showOn: 'focus',
	    showAnim: 'slideDown',
	    speed:'fast',
	    onClose: function(date) { 
	        if ($('#checkout').val()<date) $('#checkout').val(date);
	        var data=date.split("-"); 
	        $('#h_arr_date').datepicker('option', 'minDate', new Date(data[0],data[1]-1,data[2]));
	    }
	});
    });

    $(function() {
	$("#d_birth_chd1").datepicker({dateFormat: 'yy-mm-dd' ,
	    maxDate:'0d',
	    minDate:'-144m',
	    rangeSelect: false, 
	    numberOfMonths: 2, 
	    showOn: 'focus',
	    showAnim: 'slideDown',
	    speed:'fast',
	    onClose: function(date) { 
	    }
	});
    });

    $(function() {
	$("#checkout").datepicker({dateFormat: 'yy-mm-dd' ,
	    maxDate:'+12m',
	    minDate:'0d',
	    rangeSelect: false, 
	    numberOfMonths: 2, 
	    showOn: 'focus',
	    showAnim:'slideDown',
	    speed:'fast'
	});
    });

    $(function() {
	$("#arr_date").datepicker({dateFormat: 'yy-mm-dd' ,
	    buttonImage: 'view/default/img/buttonCal.jpg',
	    buttonImageOnly: true,
	    maxDate:'+12m',
	    minDate:'0d',
	    rangeSelect: false, 
	    numberOfMonths: 2, 
    	    showOn: 'both',
    	    showAnim:'slideDown',
	    speed:'fast',
	    onSelect: function(dateText, inst) {
        	$('#dep_date').datepicker('option','maxDate', new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay));
            }
	});
    });

    function strpos( haystack, needle, offset){
        // http://kevin.vanzonneveld.net
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
        // +   improved by: Onno Marsman    
	// +   bugfixed by: Daniel Esteban
        // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
	// *     returns 1: 14
    
        var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
	return i === -1 ? false : i;
    }

    $('#dep_port').dialog({
	autoOpen: false,
	width: 500,
	height:450,
	close: function() {
	    $("#modal").hide();
    	    $("#adt_count").show();
	    $("#child_count").show();
	    $("#inf_count").show();
    	    $("#flex_date").show();
	    $("#specific_airline1").show();
	}
    });

    $('#arr_port').dialog({
	autoOpen: false,
	width: 500,
	height:450 ,
	close: function() { 
	    $("#modal").hide();
	    $("#adt_count").show();
	    $("#child_count").show();
	    $("#inf_count").show();
	    $("#flex_date").show();
    	    $("#specific_airline1").show();
	}
    });

    $("#dep_combo").change(function () {
	var str = "";
	$("#dep_combo option:selected").each(function () {
    	    str += $(this).val();
        });

	$.ajax({
	   type: "GET",
	   url: "/Search/getCountryDepPort.html",
	    data: "country="+str,
	    error: function(){
	        alert('Blad polaczenia z baza danych');
	    },
	    success: function(msg){
		$("#dep_apt_list").html(msg);
	    }
	});
    });

    $("#arr_combo").change(function () {
	var str = "";
        $("#arr_combo option:selected").each(function () {
	    str += $(this).val();
        });

	$.ajax({
	    type: "GET",
	    url: "/Search/getCountryArrPort.html",
	    data: "country="+str,
	    error: function(){
		alert('Blad polaczenia z baza danych');
	    },
	    success: function(msg){
		$("#arr_apt_list").html(msg);
	    }
	});
    });

    $('#last_search').click(function() {
	$("#modal").show();
        $("#adt_count").hide();
        $("#child_count").hide();
        $("#inf_count").hide();
        $("#flex_date").hide();
	$("#specific_airline1").hide();
	$('#last_search_win').dialog('open');
    });

    $('#dep_list').click(function() {
	$("#modal").show();
	$("#adt_count").hide();
	$("#child_count").hide();
	$("#inf_count").hide();
	$("#flex_date").hide();
	$("#specific_airline1").hide();
	$('#dep_port').dialog('open');
    });

    $('#arr_list').click(function() {
	$("#modal").show();
	$("#adt_count").hide();
	$("#child_count").hide();
	$("#inf_count").hide();
	$("#flex_date").hide();
	$("#specific_airline1").hide();
	$('#arr_port').dialog('open');
    });

});
