var gDestinationListIATA = Array();
var gDestinationListCities = Array();
var gDestinationListCitiesIndex = Array();
var gDestinationListCountries = Array();
var gDestinationListCountriesIndex = Array();
var gDestinationListAirports = Array();
var gDestinationListAirportsIndex = Array();

var nMinOptionLinesCount;
var nMaxOptionLinesCount;
var nMaxAvailOptionsCount;

/**
 * Class AirportList
 *
 * @param string idActive
 * @param string idAirportList
 * @param string idTargetInput
 */
function AirportList(idActive, idAirportList, idCountriesSelect, idAirportListTableRow, idAirportListContainer ) {
    /*
	 * @var idActive string ID DOM objektu, ktery zobrazi seznam letist - obrazek, span, input atp.
	 */
	this.idActive = '';
	/*
	 * @var idCal string ID DOM objektu, ktery obaluje seznam letist
	 */
	this.idAirportList = idAirportList;	
	/*
	 * @var idCountriesSelect string ID DOM selectu, ktery obsahuje seznam zemi
	 */
	this.idCountriesSelect = idCountriesSelect;
	/*
	 * @var idAirportListTableRow string ID DOM objektu, ktery je radkem tabulky - defaultne skryty
	 */
	this.idAirportListTableRow = idAirportListTableRow;
	/*
	 * @var idAirportListContainer string ID DOM objektu, kam se vlozi letiste vybrane zeme
	 */
	this.idAirportListContainer = idAirportListContainer;
	/*
	 * @var elAirportList DOMobjekt Obaluje kalendar
	 */
	this.elAirportList = null;
	/*
	 * @var destOpenAirportListId pole ID DOM objektu, ktere po kliknuti vyvolaji popup se seznamem letist
	 */
	this.destOpenAirportListId = null;
	/*
	 * @var idTargetInput string ID DOM objektu, kam se vlozi vybrana hodnota
	 */
	this.idTargetInput = '';
}
	
/*
 * Hlavni medota pro zavolani seznamu letist podle zeme
 */
AirportList.prototype.setAirportList = function(destOpenAirportListId) {
		var sIdDomElements = '';
		var thisObject = this;
		this.elAirportList = jQ(this.idAirportList);
        this.destOpenAirportListId = destOpenAirportListId;

		// Nastavuje udalost pro zobrazeni vyberu seznamu letist
		for(i = 0; i < this.destOpenAirportListId.length; i++){
		    if((destOpenAirportListId.length-1) != i) {
		        sIdDomElements += "#"+destOpenAirportListId[i]+", ";
		    } else {
		        sIdDomElements += "#"+destOpenAirportListId[i];
		    }
		}
		
		jQ("#closeAirportList").bind('click.closeAirportList',function(){
		    thisObject.closeAirportList();
		});
		
		jQ(sIdDomElements).click(function(event) {
            thisObject.idTargetInput = '#'+jQ('#'+jQ(this).attr('id')).parent().children('input').attr('id');
		  
            // Zavira ostatni kalendare  
            jQ('body').click();
            event.stopPropagation();
		  
            idClickedElement = jQ(this).attr('id');
            //nastaveni class pro popup - fyzicky je v DOMu pouze jedno popup okno, ktere se
            //posouva podle toho, kde uzivatel popup okno airport list vyvolal
            var clickedElePositionInArray = thisObject.getIndexOfArrayElement(thisObject.destOpenAirportListId, idClickedElement);
            
            // Zobrazuje airpot list
            thisObject.openAirportList(clickedElePositionInArray);
            
            jQ('body').bind('click.closeAirportList', function() {
                jQ('body').unbind('click.closeAirportList');
                
                if (jQ(thisObject.idAirportList).css('display') == 'block') {
                    jQ(thisObject.idAirportList).hide("slide", { direction: "up" }, 600); 
                }
            });
            
            jQ(thisObject.idAirportList).unbind('click.idAirportList').bind('click.idAirportList', function(event){
                event.stopPropagation();
            });
	      
		});		
}

/**
 * Najde v poli dany string (element) a vrati pozici v poli
 */
AirportList.prototype.getIndexOfArrayElement = function(aElements,idElement) {
	var indexElement = -1;

	for (var i = 0; i < aElements.length; i++){
		if(aElements[i] == idElement) {
		    indexElement = i; break;
		}
	}
	
	return indexElement;
}

AirportList.prototype.openAirportList = function(clickedElePositionInArray) {	
	var thisObject = this;
	
	//skryje radek tabulky zobrazujici letiste
	jQ(this.idAirportListTableRow).hide();
	//vyprazdni obsah bloku zobrazujici vybrany seznam letist 
	jQ(this.idAirportListContainer).empty();

    //ziska vsechny class ze stejnojmeneho atributu class
    eleClasses = jQ('#aiportListPopup').attr('class').split(' ');
    
    //vlozi vsechny puvodni class + nova class pro posunuti okna
    var popupAirportListClass = 'pa_position'+(clickedElePositionInArray+1);
	
	if(eleClasses[3] != popupAirportListClass){               
        jQ('#aiportListPopup').removeAttr('class');
        jQ('#aiportListPopup').attr('class',eleClasses[0]+' '+eleClasses[1]+' '+eleClasses[2]+' '+popupAirportListClass);
        	    
	    //nastaveni nadpisu - kazdy sudy = 'Odkud', kazdy lichy = 'Kam'
        if((clickedElePositionInArray % 2) == 0) {
            jQ('#titleAirportList').empty().append('Von');
        } else {
            jQ('#titleAirportList').empty().append('Nach');
        }
	} 
	
	jQ(this.idAirportList).show("slide", { direction: "up" }, 600);
	
	this.generateCountriesCombo();
    
    aagForms.refreshSelect(this.idCountriesSelect);
}

/**
 * Pole zacinajici g - z pohledu indexu jsou v pomeru 1:1 
 */
AirportList.prototype.getCountriesArray = function() {
	var countriesArray = gDestinationListCountries; //Array('Německo', 'Německo', 'Uzbekistán', , 'Dánsko', 
	return countriesArray;
}
AirportList.prototype.getCountriesIndexArray = function() {
	var countriesIndexArray = gDestinationListCountriesIndex; //Array('NEMECKO', 'NEMECKO', 'UZBEKISTAN', 'DANSKO',
	return countriesIndexArray;
}
AirportList.prototype.getCountriesCodeArray = function() {
	var countriesCodeArray = gDestinationListCountriesCode; //Array('DE', 'DE', 'UZ'
	return countriesCodeArray;
}

AirportList.prototype.generateCountriesCombo = function() {
	var thisObject = this;
	var countriesArray = this.getCountriesArray();
	var countriesCodeArray = this.getCountriesCodeArray();  //vrati string indexy napr. NEMECKO

	if(typeof countriesCodeArray != 'undefined') {
    	countriesArray = this.makeUnique(countriesArray);	//vyrazeni duplicit	//	countriesArray.sort(function(a, b) { return a[0].localeCompare(b[0]); });	//serazeni podle ceske abecedy, neumi (starsi?) Opera
    	countriesCodeArray = this.makeUnique(countriesCodeArray);
    																							
    	countriesCount = countriesArray.length; 
    	
    	if (!(jQ(this.idCountriesSelect).hasClass('filled'))) {
    		jQ(this.idCountriesSelect).addClass('filled'); 
    
    		for (var i = 0; i < countriesCount; i++) {		//dynamicke generovani falesnych optionu 
    			aagForms.appendOption(this.idCountriesSelect, countriesCodeArray[i], countriesArray[i]);
    	    };
    	    aagForms.refreshSelect(jQ(this.idCountriesSelect));
    		//aagForms.createSelectBox();			//oziveni
    		
    		//pridani reakce na vyber statu
    		jQ(this.idCountriesSelect).change(function() {
                thisObject.replaceAirportList(this.value);
                jQ(thisObject.idAirportListTableRow).show();
                // Scroll na airpoty vybrane zeme
                jQ(thisObject.idAirportListContainer).jScrollPane({
                    showArrows: true, 
                    scrollbarWidth: 17
                });
    	    });
    	}
	
	} else {
	    alert('predict_new.txt musi obsahovat pole - gDestinationListCountriesCode');
	}

}

/**
 * Vrati pole jen s unikatnimi prvky
 */
AirportList.prototype.makeUnique = function(oldArr) {
	var tmpArr = oldArr.slice(0);	//slice(0) vytvori kopii puvodniho pole, abych i to puvodni nechtene neseradil
	//tmpArr.sort();
	var j=0;
	var newArr = new Array()

	for (var i = 0; i < tmpArr.length;i++){
		newArr[j] = tmpArr[i];
		j++;
		if ((i>0) && (tmpArr[i] == tmpArr[i-1])){
			newArr.pop();j--;
		}
	}
	return newArr;
}

AirportList.prototype.getAirportStringsByCountry = function(country) {
	var listCountriesCode = this.getCountriesCodeArray();
	var countryAirports = new Array();

	for (var i = 0; i < listCountriesCode.length; i++) {
		if (listCountriesCode[i] == country) {
			countryAirports.push(gDestinationListCities[i] + ', ' + gDestinationListAirports[i] + ', (' + gDestinationListIATA[i] + ')');  
		} 
	}
	return countryAirports;
} 

AirportList.prototype.replaceAirportList = function(country) {
	var thisObject = this;
	//vykresleni seznamu letist
	countryAirports = this.getAirportStringsByCountry(country);
	var container = jQ(this.idAirportListContainer);			//TODORE rozlisovat ruzne popupy
	var listEl = jQ('<ul id="ulAirportList" class="vyber_letist"></ul>');
	
	for (var i = 0; i < countryAirports.length; i++) {
		jQ(listEl).append('<li><a href="javascript:dummy();">' + countryAirports[i] + '</a></li>');
	}
    
	//pridani eventu
	jQ(container).empty().append(listEl);
	jQ(container).find('li').bind('click', function() {
        var airportString = jQ(this).text();
        jQ(thisObject.idTargetInput).attr('value', airportString);
        jQ(thisObject.idAirportList).hide("slide", { direction: "up" }, 500);
        
        return true;
    });
	
	
}

AirportList.prototype.closeAirportList = function() {
    jQ(this.idAirportList).hide("slide", { direction: "up" }, 600); 
}