/**
* funkce vraci cast url, ktera je mezi hostname a samotnym souborem.
* Tedy z napr. http://ibs.ibs.kubat.prg.aag.cz/cs/halffareav.php metoda vrati /cs/
*/
function parseURL() {
    //parsovani queryStringu
    sCallingURL = String(window.location.href);
    queryStr = sCallingURL.substring(sCallingURL.indexOf('?') + 1, sCallingURL.length);
    //vyparsovani dodatecnych URL, ktere nejsou obsazeny v hostname, ale jsou pred samotnym filename
    var re=/(.*):\/\/(.*)/;
    var result=sCallingURL.match(re);
    sExtCallingURL = String(result[2]);
    //vyparsovani rozsireneho querystringu BEZ hostname (jazyk, modul)
    extQueryStr = sExtCallingURL.substring(sExtCallingURL.indexOf('/') + 1, sExtCallingURL.lastIndexOf('/'));
    return extQueryStr;
}

function formatDestination(val, inList) {
  	var dest = val.toString().split(";");
  	if(dest[6] == '2') {
  		var str = dest[0] + ", " + dest[1] + ", " + dest[5] + " (" + dest[2] + ")";
  		return (inList) ? "<img src='images/flags/" + dest[4] + ".gif'/> " + str : str;
  	} else if(dest[6] == '1') {
  		var str = dest[0] + ", " + dest[1] + " (" + dest[2] + ")";
  		return (inList) ? "<img src='images/space.gif'/>" + str : str;
  	} else {
  		var str = dest[0] + ", " + dest[5] + " (" + dest[3] + ")";
  		return (inList) ? "<img src='images/flags/" + dest[4] + ".gif'/> " + str : str;
  	}
}

function formatAirport(val, inList) {
  	var dest = val.toString().split(";");
  	if (dest[dest.length-1] == '2') {
  		if (dest[2] != '') {
  			var str = dest[1] + ", " + dest[2] + ", " + dest[5] + " (" + dest[0] + ")";
  		} else {
  			var str = dest[1] + ", " + dest[5] + " (" + dest[0] + ")";
  		}
  		return (inList) ? "<img src='images/flags/" + dest[4] + ".gif'/> " + str : str;
  	} else if (dest[dest.length-1] == '1') {
  		if (dest[2] == '') {
	  		var str = dest[1] + " (" + dest[0] + ")";
	  	} else {
	  		var str = dest[1] + ", " + dest[2] + " (" + dest[0] + ")";
		}
  		return (inList) ? "<img src='images/space.gif'/>" + str : str;
  	} else {
  		var str = dest[1] + ", " + dest[5] + " (" + dest[3] + ")";
  		return (inList) ? "<img src='images/flags/" + dest[4] + ".gif'/> " + str : str;
  	}
}

function appendAirportAutoComplete(id) {
    var extQueryStr = parseURL();
    var jsHost = (("https:" == document.location.protocol) ? "https://" : "http://");
    var sUrlPathOffer = jsHost + window.location.hostname + "/" + extQueryStr + "/naseptavac-letiste.php";
    if (typeof language != 'undefined' && language != "") {
        sUrlPathOffer += "?lng=" + language;
    }
    jQ("#" + id).autocomplete(sUrlPathOffer, {
        formatItem: function(value, i, n, search) {
            return formatAirport(value, true)
        },
        formatResult: function(value) {
            return formatAirport(value, false);
        },
        matchSubset: false,
        minChars: 3,
        max: 30,
        width: 300
	});
}

function formatCity(val, inList) {
    var dest = val.toString().split(";");
    var str = dest[1] + ", " + dest[3] + " (" + dest[0] + ")";
    return (inList) ? "<img src='images/flags/" + dest[2] + ".gif'/> " + str : str;
}

function appendCityAutoComplete(id) {
    var extQueryStr = parseURL();
    var jsHost = (("https:" == document.location.protocol) ? "https://" : "http://");
    var sUrlPathOffer = jsHost + window.location.hostname + "/" + extQueryStr + "/naseptavac-mesta.php";
    if (typeof language != 'undefined' && language != "") {
        sUrlPathOffer += "?lng=" + language;
    }
    
    jQ("#" + id).autocomplete(sUrlPathOffer, {
        formatItem: function(value, i, n, search) {
            return formatCity(value, true)
        },
        formatResult: function(value) {
            return formatCity(value, false);
        },
        matchSubset: false,
        minChars: 3,
        max: 30,
        width: 300
    });
}

jQ(function() {

    // defaultni nastaveni formu
    //var defaultTrip = 'RT';
    //switchForm(defaultTrip);
    //jQ(".passengerTypeInputs").hide();

    // atribut 'autocomplete' je nevalidni, proto ho setujeme pres JS (validator ho pak nevidi)
    //jQ(".autoCompleteOff").attr('autocomplete','off');



    // vyvolani airport-listu po clicku na odkaz
//    jQ("a[@id^='airportList_']").click(function(event){
//        var id = jQ(this).attr('id');
//        var inputId = id.substr(12);
//        showAirportList(inputId);
//        event.preventDefault();
//    });

    // vyvolani/skryvani bloku s volbou pasazeru + overeni minimalniho poctu 1 pasazera
//    jQ("input[@id^='total_']").click(function(){
//        var id = jQ(this).attr('id');
//        var pos = id.substr(6);
//        verifyPassengersCount(pos);
//    });

    // prepocitavani poctu pasazeru po zmene v selectu
//    jQ(".passengerTypeInputs select").change(function(){
//        var id = jQ(this).attr('id');
//        jQ('#' + id + '_hdn').attr('value', jQ("#" + id).val());
//        var pos = id.substr(4);
//        var wrapId = "#passengerTypeInputs_" + pos;
//        var totalPassengers = 0;
//        jQ(wrapId + " select").each(function(){
//            totalPassengers += 1 * jQ(this, "option:selected").val();
//        });
//        jQ("#total_" + pos).attr('value', totalPassengers);
//    });

//    jQ("form").each(function(){
//        var formId = jQ(this).attr('id');
//        jQ("#" + formId + " select[@name='ADTCOUNT'] option:eq(1)").attr('selected', 'selected');
//    });

    // prepocitani poctu pasazeru pred skrytim bloku se selecty
//    jQ("button[@id^='passengersOk_']").click(function(){
//        var id = jQ(this).attr('id');
//        var pos = id.substr(13);
//        verifyPassengersCount(pos);
//    });

    // naseptavac flight search form
    appendAirportAutoComplete('DEP_0');
    appendAirportAutoComplete('ARR_0');
    appendAirportAutoComplete('DEP_1'); // multicity from 2
    appendAirportAutoComplete('ARR_1'); // multicity to 2

    // naseptavac fly with form
    appendAirportAutoComplete('DEP_2');
    appendAirportAutoComplete('ARR_2');

    // naseptavac pro hotelovy form
    //appendCityAutoComplete('destination');

    /*
    // kalendar flight search form
    createCalendars({               // nastaveni pro jednu dvojici inputu
        id1 : 'depDate0',           // id 1. inputu
        id2 : 'arrDate0',           // id 2. inputu
        maximumAllowedDays : 330,   // max pocet povolenych dni, pocinaje datumem v prvnim inputu
        firstDay : 1,               // 1 = tyden zacina pondelkem, 0 = nedeli
        numberOfMonths : 1,         // pocet kalendaru a jejich rozlozeni [radky, sloupce]
        moveSecondDay : 10          // posun predvybraneho dne pro 2. input - nepovinne (pouze pokud nemame k dispozici 'date1' a 'date2')
    });

    // kalendar fly with form
    createCalendars({               // nastaveni pro jednu dvojici inputu
        id1 : 'depDate1',           // id 1. inputu
        id2 : 'arrDate1',           // id 2. inputu
        maximumAllowedDays : 330,   // max pocet povolenych dni, pocinaje datumem v prvnim inputu
        firstDay : 1,               // 1 = tyden zacina pondelkem, 0 = nedeli
        numberOfMonths : 1,         // pocet kalendaru a jejich rozlozeni [radky, sloupce]
        moveSecondDay : 7           // posun predvybraneho dne pro 2. input - nepovinne (pouze pokud nemame k dispozici 'date1' a 'date2')
    });

    // kalendar pro hotel search form
    createCalendars({               // nastaveni pro jednu dvojici inputu
        id1 : 'checkin0',           // id 1. inputu
        id2 : 'checkout0',          // id 2. inputu
        numberOfMonths: 1,          // pocet kalendaru a jejich rozlozeni [radky, sloupce]
        moveSecondDay : 5,          // posun predvybraneho dne pro 2. input - nepovinne (pouze pokud nemame k dispozici 'date1' a 'date2')
        minDayDifference : 1        // pokud je potreba nastavit minimalne 1 den rozdil (1 noc pro hotel)
    });
    */
});
