$(document).ready(function() {

    // konfiguracja
    var msg="";
    var msgPort="wpisz nazwę lub wybierz z mapy";
    var msgDate="DD.MM.RRRR";

    // ustawienia domyslne
    var msg="";
    $("#origin_id").val(msgPort);
    $("#destination_id").val(msgPort);
    $("#depDate_id").val(msgDate);
    $("#retDate_id").val(msgDate);

    // klikniecie: Lot w obie strony
    $('#flightTypeReturn_id').click(function() {
    	$('#retDate_id').removeAttr("disabled");
		$('#retDate_id').datepicker('enable');
		$('#retDate_id').val(msgDate);

	    $('#depOffset_id').removeAttr("disabled");
	    $('#retOffset_id').removeAttr("disabled");

		$('#retTimeId').removeAttr('disabled');
		$('#retTimeBefore_id').removeAttr('disabled');
		$('#retTimeBefore_id').removeAttr('disabled');
		$('#retTimeAfter_id').removeAttr('disabled');
    });

    // klikniecie: Lot w jedna strone
    $('#flightTypeSingle_id').click(function() {
        $('#retDate_id').attr("disabled", true);
		$('#retDate_id').datepicker('disable');
		$('#retDate_id').val("");
		$("#depDate_id").datepicker('option', 'maxDate', '+12m');

		$('#retOffset_id').attr("disabled", true);
    	$('#retOffset_id').attr("checked", false);

		$('#retTimeId').attr('disabled', true);
		$('#retTimeBefore_id').attr('disabled', true);
		$('#retTimeBefore_id').attr('disabled', true);
		$('#retTimeAfter_id').attr('disabled', true);
    });

    // czyszczenie po kliknieciu
    $('#origin_id').click(function() {
        $('#origin_id').val("");
    });
    $('#destination_id').click(function() {
        $('#destination_id').val("");
    });
    $('#depDate_id').click(function() {
        $('#depDate_id').val("");
    });
    $('#retDate_id').click(function() {
        $('#retDate_id').val("");
    });

    // porty wylotu pobieramy z SIRIUSA
//    $('#origin_id').autocomplete('/api/portyList/view.php', {
//		max: 5,
//		minChars: 2,
//		cacheLength: 10,
//		extraParams: {
//    	    method: "getPorts"
//        } 
//    }); 

	$('#origin_id').autocomplete({
		source: "/api/portyList/view.php",
		minLength: 2
	});

    // porty powrotu pobieramy z SIRIUSA
//    $('#destination_id').autocomplete('/api/portyList/view.php', {
//		max: 5,
//		minChars: 2,
//		cacheLength: 10,
//		extraParams: {
//    	    method: "getPorts"
//        } 
//    }); 

	$('#destination_id').autocomplete({
		source: "/api/portyList/view.php",
		minLength: 2
	});

    // atlas flash portu wylotu
    $('#origin_button').click(function(){
		window.open("atlasPorts.php?type=origin_id","displayWindow",'width=800,height=600,resizable=1,scrollbars=yes,menubar=yes,left=20,top=150' );
    });

    // atlas flash portu powrotu
    $('#destination_button').click(function(){
		window.open("atlasPorts.php?type=destination_id","displayWindow",'width=800,height=600,resizable=1,scrollbars=yes,menubar=yes,left=20,top=150' );
    });

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

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

    // sprawdzamy formularz
    $('#submit_id').click(function() {

        if ( ($("#origin_id").val() == "") || ($("#origin_id").val() == msgPort) ) {
            msg="Wybierz lub wpisz port wylotu!\n";
        }

        if ( ($("#depDate_id").val() == "") || ($("#depDate_id").val() == msgDate) ) {
            msg=msg+"Podaj datę wylotu!\n";
		}

		if ($('#flightTypeReturn_id').attr("checked") == true) {
    	    if ( ($("#destination_id").val() == "") || ($("#destination_id").val() == msgPort) ) {
    	    	msg=msg+"Wybierz lub wpisz port docelowy!\n";
    	    }
    		if ( ($("#retDate_id").val() == "") || ($("#retDate_id").val() == msgDate) ) {
        		msg=msg+"Podaj datę powrotu!\n";
    	    }
        }

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

		return true;
    });

    $('#sendId').click(function() {

        msg="Nie wypełniłeś formualrza wyszukiwarki!\n";
        var error = false;

        if ( ($("#origin_id").val() == "") || ($("#origin_id").val() == msgPort) ) {
		    error	= true;
            msg		= msg+"Wybierz lub wpisz port wylotu!\n";
        }

        if ( ($("#depDate_id").val() == "") || ($("#depDate_id").val() == msgDate) ) {
		    error	= true;
            msg		= msg+"Podaj datę wylotu!\n";
        }

		if ($('#flightTypeReturn_id').attr("checked") == true) {
    	    if ( ($("#destination_id").val() == "") || ($("#destination_id").val() == msgPort) ) {
				error	= true;
        		msg	= msg+"Wybierz lub wpisz port docelowy!\n";
    	    }
    	    if ( ($("#retDate_id").val() == "") || ($("#retDate_id").val() == msgDate) ) {
				error	= true;
        		msg	= msg+"Podaj datę powrotu!\n";
    	    }
        }

        if (error == false) {
    	    $.post(
        		'/api/search/view.php',
        		$("#search").serialize(),
        		function(data) {
            	    $('#resultId').html(data);
        		}
    	    );
        } else {
            alert(msg);
            msg="";
        }

    });

});


