
var activeField;



$(document).ready(function() {

    // #############################################################################################
    // FLIGHT ######################################################################################

    $('#flightReq_isRoundTrip').live('click', function(event) {
        if ($('#flightReq_isRoundTrip').attr("checked") == true) {
            $('#retDateField').fadeIn("slow");
            $('#flightReq_isRoundTrip').val("true");
        } else {
            $('#retDateField').fadeOut("slow");
            $('#flightReq_isRoundTrip').val("false");
        }
    });


    $('#carRequest_isStartPlaceEndPlace').live('click', function(event) {
        if ($('#carRequest_isStartPlaceEndPlace').attr("checked") == true) {
            $('#endPlace').slideUp("slow");
            $('#carRequest_isStartPlaceEndPlace').val("true");
        } else {
            $('#endPlace').slideDown("slow");
            $('#carRequest_isStartPlaceEndPlace').val("false");
        }
    });

    // #############################################################################################
    // MAP #########################################################################################

    $(".place").autocomplete({
        select: function(event, ui) {
            $($(this).attr('id') + 'ID').val(ui.item.id);
        },
        source: function(request, response) {
            $.ajax({
                url: "/place/list",
                dataType: "json",
                data: {
                    term: request.term
                },
                success: function(data) {
                    response($.map(data, function(item) {
                        return {
                            id: item.id,
                            label: item.disNameEN,
                            value: item.disNameEN,
                            lat: item.lat,
                            lng: item.lng
                        }
                    }))
                }
            })
        }
    });

    $(".place").focusout(function () {
        if ($(this).val().length > 0) {
            $(this).val($(this).val().replace(/%/g,""));
            var tmpField = "#" + this.id;
            $.getJSON("/place/check?term=" + $(this).val().replace(/ /g,"_") + "&lat=0&lng=0",
                function(json) {
                    if (json != null) {
                        $(tmpField).val(json.disNameEN);
                        $(tmpField + "Lat").val(json.lat);
                        $(tmpField + "Lng").val(json.lng);
                        $(tmpField + "ID").val(json.id);
                    }
                });
        }
    });

    $(".place_de").autocomplete({
        select: function(event, ui) {
            $($(this).attr('id') + 'ID').val(ui.item.id);
        },
        source: function(request, response) {
            $.ajax({
                url: "/place/list",
                dataType: "json",
                data: {
                    term: request.term
                },
                success: function(data) {
                    response($.map(data, function(item) {
                        return {
                            id: item.id,
                            label: item.disNameDE,
                            value: item.disNameDE,
                            lat: item.lat,
                            lng: item.lng
                        }
                    }))
                }
            })
        }
    });

    $(".place_de").focusout(function () {
        if ($(this).val().length > 0) {
            var tmpField = "#" + this.id;
            $(this).val($(this).val().replace(/%/g,""));
            $.getJSON("/place/check?term=" + $(this).val().replace(/ /g,"_") + "&lat=0&lng=0",
                function(json) {
                    if (json != null) {
                        $(tmpField).val(json.disNameDE);
                        $(tmpField + "Lat").val(json.lat);
                        $(tmpField + "Lng").val(json.lng);
                        $(tmpField + "ID").val(json.id);
                    }
                });
        }
    });

    $('.toggleAdvField').live('click', function(event) {
        $("#AdvField").slideToggle("slow");
        var more_options = $("#more_options").val();
        var less_options = $("#less_options").val();
        if ($('.toggleAdvField').text() == less_options) {
            $('.toggleAdvField').text(more_options);
        } else {
            $('.toggleAdvField').text(less_options);
        }
        event.preventDefault();
    });

    $("#AdvField").slideUp("slow");


    
});

// #################################################################################################
// CALENDER ########################################################################################

$(function() {
    var today = new Date();
    var tomorrow = new Date();
    tomorrow.setDate(today.getDate() +1);
    var dates = $(".startDate, .endDate").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        closeText: 'OK',
        numberOfMonths: 3,
        dateFormat: 'dd-mm-yy',
        minDate: tomorrow,
        maxDate: '+12m',
        showButtonPanel: true,
        onSelect: function( selectedDate ) {
            var option = this.className.indexOf("startDate") > 0 ? "minDate" : "maxDate",
            instance = $( this ).data( "datepicker" ),
            date = $.datepicker.parseDate(
                instance.settings.dateFormat ||
                $.datepicker._defaults.dateFormat,
                selectedDate, instance.settings );
            dates.not( this ).datepicker( "option", option, date );
        }
    });
});

// #################################################################################################
// OTHER ###########################################################################################

function check(url) {

    var status = $('#status').val();

    if ((status == "idle") || (status == "running")) {
        $('#runningStatus').load(url);
    } else if (status == "finished") {
        clearInterval(interval);
        window.location.href = url.replace('check', 'list');
    }

}


