jQuery(document).ready(function(){

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

    // ustawienia domyslne
    var msg="";
//    $('#onlyRegularProvider_id').attr("checked", true);
//    $('#depOffset_id').attr("disabled", true);
//    $('#retOffset_id').attr("disabled", true);
    $("#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);

//	if ($('#onlyRegularProvider_id').attr("checked") == false) {
    	    $('#depOffset_id').removeAttr("disabled");
    	    $('#retOffset_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');

//	if ($('#onlyRegularProvider_id').attr("checked") == false) {
    	    $('#retOffset_id').attr("disabled", true);
    	    $('#retOffset_id').attr("checked", false);
//	}
    });

    // klikniecie: Tanie linie
//    $('#onlyRegularProvider_id').click(function() {
//
//	if ($('#onlyRegularProvider_id').attr("checked") == true) {
//    	    $('#depOffset_id').attr("disabled", true);
//    	    $('#retOffset_id').attr("disabled", true);
//
//	} else {
//    	    $('#depOffset_id').removeAttr("disabled");
//	    if ($('#flightTypeSingle_id').attr("checked") == false) {
//    		$('#retOffset_id').removeAttr("disabled");
//	    }
//	}
//    });

    // klikniecie: Wylot +/- 3dni
//    $('#depOffset_id').click(function() {
//	if ($('#depOffset_id').attr("checked") == true) {
//    	    $('#onlyRegularProvider_id').attr("disabled", true);
//	} else {
//	    if ($('#retOffset_id').attr("checked") == false) {
//    		$('#onlyRegularProvider_id').removeAttr("disabled");
//	    }
//	}
//    });

    // klikniecie: Przylot +/- 3dni
//    $('#retOffset_id').click(function() {
//	if ($('#retOffset_id').attr("checked") == true) {
//    	    $('#onlyRegularProvider_id').attr("disabled", true);
//	} else {
//	    if ($('#depOffset_id').attr("checked") == false) {
//    		$('#onlyRegularProvider_id').removeAttr("disabled");
//	    }
//	}
//    });

    // 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(siriusScriptPath + 'APIsirius.php', {
	max: 5,
	minChars: 2,
	cacheLength: 10,
	extraParams: {
    	    method: "getPorts"
        } 
    }); 

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

    // atlas flash portu wylotu
    $('#origin_button').click(function(){
	window.open(siriusScriptPath + "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(siriusScriptPath + "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: siriusScriptPath + '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: siriusScriptPath + '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;
    });

});


