var searcher_action;

$(function () {
    $("#DepDate,#ArrDate").datepicker({
        duration: "",
        firstDay: 1,
        dateFormat: "dd.mm.yy",
        beforeShow: customRange,
        onSelect: checkDate,
        showOn: "both",
        buttonImage: "/public/themes/images/calendar.gif",
        buttonText: "Kalendár",
        numberOfMonths: 2,
        showOtherMonths: true,
        buttonImageOnly: true
    });
    $("#dialog_destinations").dialog({
        bgiframe: true,
        autoOpen: false,
        modal: true,
        resizable: false,
        draggable: true,
        width: 300,
        height: 450,
        title: 'Destinácie'
    });
    
    /* zmena formulara podla typu cesty */
    if ($('#sector_spiatocna')[0].checked) {
        hideOtherArr();
        showArrDate();
    }
    else if ($('#sector_jednosmerna')[0].checked) {
        hideOtherArr();
        hideArrDate();
    }
    else if ($('#sector_inynavrat')[0].checked) {
        showOtherArr();
        showArrDate();
    }
    $('#sector_inynavrat').click(function () {
        showOtherArr();
        showArrDate();
    });
    $('#sector_spiatocna').click(function () {
        hideOtherArr();
        showArrDate();
    });
    $('#sector_jednosmerna').click(function () {
        hideOtherArr();
        hideArrDate();
    });
    
    /* Vyhladavanie destinacii - dialog*/
    $('#DepAirportFolder').click(function () {
        searcher_action = 'addDepAirport';
        loadDestinationsDialog();
    });
    
    $('#ArrAirportFolder').click(function () {
        searcher_action = 'addArrAirport';
        loadDestinationsDialog();
    });
    
    $('#DepAirportFolder_2').click(function () {
        searcher_action = 'addDepAirport_2';
        loadDestinationsDialog();
    });
    
    $('#ArrAirportFolder_2').click(function () {
        searcher_action = 'addArrAirport_2';
        loadDestinationsDialog();
    });
   $("a").bind('click', function(){
        if ($(this).attr('rel') == 'symphony') {showProgressSymphony(); return true;}
        if ($(this).attr('rel') == 'skyeurope') {showProgressSkyEurope(); return true;}
    })
    
    /* Vyhladavanie destinacii - autocomplete */
    //$('#cl_destination_from').autocomplete("../files/airports/airports_autocomplete.php",{
    $('#cl_city').autocomplete("/ajax/goglobal-destination-autocomplete",{
        minChars: 3, 
        matchSubset:3, 
        matchContains:true, 
        cacheLength:3, 
        matchCase: true
    }).result(function(event, data, formatted){
        addCity(data[1], data[0]);
    }).click(function(){
        addCity('', '');
    });

    $('#cl_destination_from').autocomplete("/ajax/destination-autocomplete-new",{
        minChars: 3, 
        matchSubset:3, 
        matchContains:true, 
        cacheLength:3, 
        matchCase: true
    }).result(function(event, data, formatted){
        addDepAirport(data[1], data[0]);
    }).click(function(){
        addDepAirport('', '');
    });

    $('#cl_destination_from_2').autocomplete("/ajax/destination-autocomplete-new",{
        minChars: 3, 
        matchSubset:3, 
        matchContains:true, 
        cacheLength:3, 
        matchCase: true
    }).result(function(event, data, formatted){
        addDepAirport_2(data[1], data[0]);
    }).click(function(){
        addDepAirport_2('', '');
    });

    $('#cl_destination_to').autocomplete("/ajax/destination-autocomplete-new",{
        minChars: 3, 
        matchSubset:3, 
        matchContains:true, 
        cacheLength:3, 
        matchCase: true
    }).result(function(event, data, formatted){
        addArrAirport(data[1], data[0]);
    }).click(function(){
        addArrAirport('', '');
    });

    $('#cl_destination_to_2').autocomplete("/ajax/destination-autocomplete-new",{
        minChars: 3, 
        matchSubset:3, 
        matchContains:true, 
        cacheLength:3, 
        matchCase: true
    }).result(function(event, data, formatted){
        addArrAirport_2(data[1], data[0]);
    }).click(function(){
        addArrAirport_2('', '');
    });
    
    $('#Airline').autocomplete("/ajax/airline-autocomplete",{
        minChars: 2, 
        matchSubset:2, 
        matchContains:0, 
        cacheLength:2,
        selectOnly:1
    });
});


function loadDestinationsDialog() {
    $('#dialog_destinations').dialog('open');
    if($('#dialog_destinations').html() == ''){
        $('#dialog_destinations body').addClass('ajaxLoader');
        $.ajax({
            url: '/ajax/countries',
            data: 'action=addAirport',
            success: function (req) {
                $('#dialog_destinations').removeClass('ajaxLoader');
                $('#dialog_destinations').html(req);
                $('a.searcher_country').click(function () {
                    var id = $(this).attr('id');
                    $('#listAirport_'+id.substr(id.length - 2, 2)).toggle();
                });
                $('a.AirportLink').click(function () {
                    addAirport($(this).attr('id'),trim($(this).text()));
                    $('#dialog_destinations').dialog('close');
                });
            }
        });
    }
}
function customRange(input) {
    return {minDate: (input.id == "ArrDate" ? $("#DepDate").datepicker("getDate") : new Date())}; 
        //maxDate: (input.id == "DepDate" ? $("#ArrDate").datepicker("getDate") : null)}; 
}
function checkDate(input) {
    var odlet = input.substring(6,8)+input.substring(3,4)+input.substring(0,1);
    var input2 = $("#ArrDate").val();
    var navrat = input2.substring(6,8)+input2.substring(3,4)+input2.substring(0,1);
    
    if(odlet>navrat){
        $("#ArrDate").val(input);
    }
}
function hideOtherArr() {
    $('#dep_airport2_row').hide();
    $('#arr_airport2_row').hide();
    $('#other_return_row').hide();
}

function showOtherArr() {
    $('#dep_airport2_row').show();
    $('#arr_airport2_row').show();
    $('#other_return_row').show();
}

function showArrDate() {
    $('#arr_date_row').show();
}
function hideArrDate() {
    $('#arr_date_row').hide();
}


function addAirport(iata, name) {
    if(searcher_action == 'addDepAirport') {
        addDepAirport(iata, name);
    }
    else if(searcher_action == 'addArrAirport') {
        addArrAirport(iata, name);
    }
    if(searcher_action == 'addDepAirport_2') {
        addDepAirport_2(iata, name);
    }
    else if(searcher_action == 'addArrAirport_2') {
        addArrAirport_2(iata, name);
    }
    $('#dialog_destinations').dialog("close");
}

function addDepAirport(iata, name) {
    $('#cl_destination_from').attr('name', 'dep_tmp');
    $('#cl_destination_from').val('');
    $('#dep_tmp').attr('name', 'DepAirport');
    
    $('#cl_destination_from').val(name);
    $('#dep_tmp').val(iata);
    
    //$('#searcher').remove();
    $('#searcher').hide();
    $('#searcher_overlay').hide();
}
function addDepAirport_2(iata, name) {
    $('#cl_destination_from_2').attr('name', 'dep_tmp_2');
    $('#cl_destination_from_2').val('');
    $('#dep_tmp_2').attr('name', 'DepAirport_1');
    
    $('#cl_destination_from_2').val(name);
    $('#dep_tmp_2').val(iata);
    
    $('#searcher').hide(); //$('#searcher').remove();
    $('#searcher_overlay').hide();
}
function addArrAirport(iata, name) {
    $('#cl_destination_to').attr('name', 'arr_tmp');
    $('#cl_destination_to').val('');
    $('#arr_tmp').attr('name', 'ArrAirport');
    
    $('#cl_destination_to').val(name);
    $('#arr_tmp').val(iata);
    
    $('#searcher').hide(); //$('#searcher').remove();
    $('#searcher_overlay').hide();
}
function addArrAirport_2(iata, name) {
    $('#cl_destination_to_2').attr('name', 'arr_tmp_2');
    $('#cl_destination_to_2').val('');
    $('#arr_tmp_2').attr('name', 'ArrAirport_1');
    
    $('#cl_destination_to_2').val(name);
    $('#arr_tmp_2').val(iata);
    
    $('#searcher').hide(); //$('#searcher').remove();
    $('#searcher_overlay').hide();
}

jQuery(function($){
	$.datepicker.regional['sk'] = {
		clearText: 'Zmazať', clearStatus: '',
		closeText: 'Zavrieť', closeStatus: '',
		prevText: '',  prevStatus: '',
		prevBigText: '', prevBigStatus: '',
		nextText: '', nextStatus: '',
		nextBigText: '', nextBigStatus: '',
		currentText: 'Dnes', currentStatus: '',
		monthNames: ['Január','Február','Marec','Apríl','Máj','Jún',
		'Júl','August','September','Október','November','December'],
		monthNamesShort: ['Jan','Feb','Mar','Apr','Máj','Jún',
		'Júl','Aug','Sep','Okt','Nov','Dec'],
		monthStatus: '', yearStatus: '',
		weekHeader: 'Ty', weekStatus: '',
		dayNames: ['Nedel\'a','Pondelok','Utorok','Streda','Štvrtok','Piatok','Sobota'],
		dayNamesShort: ['Ned','Pon','Uto','Str','Štv','Pia','Sob'],
		dayNamesMin: ['Ne','Po','Ut','St','Št','Pi','So'],
		dayStatus: 'DD', dateStatus: 'D, M d',
		dateFormat: 'dd.mm.yy', firstDay: 0, 
		initStatus: '', isRTL: false,
        showButtonPanel: true,
        minDate: new Date()
    };
	$.datepicker.setDefaults($.datepicker.regional['sk']);
});
