/*
 * Zentrale Klasse für die Karten-Darstellung der Flughafen-Mehrfachauswahl
 * 
 * @author Wolfgang Merkens <wm@traveltainment.de>
 * @copyright  2010 Traveltainment AG
 */   

// Konstruktor
function TTMapForAirportMultiChoice(thisName) { 
    this.oApCus               ; // kundenspez. AFH der Strecke
    this.oApDef               ; // alle default definierten AFH mit Basis-Positionsdaten
    this.oApMerge             = new Array(); // Schnittmenge aus this.oApCus und this.oApDef
    this.oApOut               ; // diese AFH für Ausgabe auf kundenspez. Karte verwenden
    this.cusMapWidth          = 335; // Breite der Kunden-Karte    
    this.cusMapHeight         = 245; // Höhe der Kunden-Karte    
    this.cusIconWidth         = 11;  // Breite des Kunden-Icon
    this.cusIconHeight        = 12;  // Höhe des Kunden-Icon 
    this.selectedAirports     = new Array(); // bereits ausgewählte AFH der Mehrfachauswahl 
    this.callbackMouseAction  = 'ttMultiAirport.mouseAction'; // Mouse-Funktion der AFH-Auswahl 
    this.callbackDisplayIcon  = ''; // callback-Funktion: HTML-Ausgabe eines Icon      
    this.mainDiv              = 'ttBoxAirportMapImg'; // Standard-Name des Main Divs
    this.name                 = 'ttMapAirport';
    if (typeof(thisName) == 'string' && thisName != '') {
        this.name = thisName;
    }    
}

// cusAp: array mit den Flughäfen des Kunden (TTAFHDLC)
TTMapForAirportMultiChoice.prototype.init = function (cusAp) {     
    if (!cusAp) {
        var cusAp = ttMultiAirport.oApCus;    
    } 
    if (typeof(cusAp) == 'object') {
        // wenn Elemente klassisch als string, dann erst json kodieren
        if (typeof(cusAp[0]) == 'string') {                 
            var tmpObj;
            var tmpArr  = [];                 
            var jsonTxt = '[';
            for (var i = 0; i < cusAp.length; i++) {
               tmpArr   = cusAp[i].split(';');
               jsonTxt += '{"name":"'+tmpArr[0]+'","dlc":"'+tmpArr[1]+'"},';
            }
            jsonTxt     = jsonTxt.replace(/,$/,'');
            jsonTxt    += ']';     
            tmpObj      = eval('(' + jsonTxt + ')');
            this.oApCus = tmpObj;                 
        } else {
            this.oApCus = cusAp;
        }          
        // Positionsberechnung
        this.generateAllAirports(); 
    } else {
        return false;
    }             
}

// AFH-Arrays generieren und Positionen berechnen
TTMapForAirportMultiChoice.prototype.generateAllAirports = function () {
    // !!! Werte der Basiskarte - NICHT ändern!!!
    var baseMapWidth   = 335;
    var baseMapHeight  = 245;
    var baseIconWidth  = 11;
    var baseIconHeight = 12;
    // Werte für Mittellinien anhand Basiskarte
    var baseMidWi   = (baseMapWidth / 2);
    var baseMidHe   = (baseMapHeight / 2);
    var baseMidWiIc = (baseIconWidth / 2);
    var baseMidHeIc = (baseIconHeight / 2);
    // Werte für Mittellinien anhand Kundenkarte
    var cusMidWi   = (this.cusMapWidth / 2);
    var cusMidHe   = (this.cusMapHeight / 2);
    var cusMidWiIc = (this.cusIconWidth / 2);
    var cusMidHeIc = (this.cusIconHeight / 2); 
    // !!! Default-Positionen aller AFH - NICHT ändern!!!
    this.oApDef = [
        {"dlc":"AMS", "xPos":-119,  "yPos":31.5},
        {"dlc":"AOC", "xPos":36.5,  "yPos":3},   
        {"dlc":"BER", "xPos":56,    "yPos":38},         
        {"dlc":"BLL", "xPos":-33,   "yPos":113.5},            
        {"dlc":"BRE", "xPos":-42,   "yPos":52.5},
        {"dlc":"BRU", "xPos":-130,  "yPos":-0.5},
        {"dlc":"CGN", "xPos":-73,   "yPos":-3.5},
        {"dlc":"CSO", "xPos":12,    "yPos":27},
        {"dlc":"DRS", "xPos":58,    "yPos":7},
        {"dlc":"DTM", "xPos":-62,   "yPos":11.5},
        {"dlc":"DUS", "xPos":-78,   "yPos":6},
        {"dlc":"MGL", "xPos":-85,   "yPos":6},
        {"dlc":"EIN", "xPos":-107,  "yPos":15.5},
        {"dlc":"ENS", "xPos":-80,   "yPos":32.5},
        {"dlc":"ERF", "xPos":5.5,   "yPos":3},
        {"dlc":"FDH", "xPos":-24.5, "yPos":-82},
        {"dlc":"FKB", "xPos":-51,   "yPos":-56.5},
        {"dlc":"FMM", "xPos":-8,    "yPos":-72.5},
        {"dlc":"FMO", "xPos":-60,   "yPos":26.5},
        {"dlc":"FRA", "xPos":-40.5, "yPos":-20.5},
        {"dlc":"HNH", "xPos":-65.5, "yPos":-20.5},
        {"dlc":"GRQ", "xPos":-84,   "yPos":52.5},
        {"dlc":"GRZ", "xPos":91,    "yPos":-93.5},
        {"dlc":"GVA", "xPos":-90.5, "yPos":-114},
        {"dlc":"HAJ", "xPos":-25,   "yPos":33.5},
        {"dlc":"HAM", "xPos":-18,   "yPos":62.5},
        {"dlc":"KIL", "xPos":-16,   "yPos":80.5},
        {"dlc":"HHN", "xPos":-65,   "yPos":-25.5},
        {"dlc":"INN", "xPos":13,    "yPos":-92.5},
        {"dlc":"KLU", "xPos":63,    "yPos":-103.5},
        {"dlc":"LBC", "xPos":-4,    "yPos":70},
        {"dlc":"LEJ", "xPos":30.5,  "yPos":14},
        {"dlc":"LGG", "xPos":-105,  "yPos":-11.5},
        {"dlc":"LNZ", "xPos":64,    "yPos":-64.5},
        {"dlc":"LUX", "xPos":-91.5, "yPos":-31.5},
        {"dlc":"MLH", "xPos":-68,   "yPos":-81.5},
        {"dlc":"MST", "xPos":-98,   "yPos":-1.5},
        {"dlc":"MUC", "xPos":19,    "yPos":-64.5},
        {"dlc":"AGB", "xPos":8,    "yPos":-60},
        {"dlc":"NRN", "xPos":-89.5, "yPos":17},
        {"dlc":"NUE", "xPos":7,     "yPos":-35.5},
        {"dlc":"PAD", "xPos":-45,   "yPos":16.5},
        {"dlc":"KSF", "xPos":-35,   "yPos":8.5},
        {"dlc":"RLG", "xPos":28,    "yPos":75},
        {"dlc":"HOQ", "xPos":18,    "yPos":-15},
        {"dlc":"SCN", "xPos":-72.5, "yPos":-43.5},
        {"dlc":"STR", "xPos":-29,   "yPos":-59.5},
        {"dlc":"SXB", "xPos":-64,   "yPos":-63},
        {"dlc":"SXF", "xPos":52,    "yPos":34},
        {"dlc":"SZG", "xPos":47,    "yPos":-78.5},
        {"dlc":"SZW", "xPos":17.5,  "yPos":58.5},
        {"dlc":"TXL", "xPos":47,    "yPos":38},
        {"dlc":"FNB", "xPos":47,    "yPos":57},
        {"dlc":"ROS", "xPos":32,    "yPos":75},
        {"dlc":"VIE", "xPos":113,   "yPos":-68.5}, 
        {"dlc":"WAW", "xPos":158,   "yPos":32.5},
        {"dlc":"ZQW", "xPos":-63.5, "yPos":-44.5},
        {"dlc":"ZRH", "xPos":-46,   "yPos":-89.5},
        {"dlc":"BSL", "xPos":-61,   "yPos":-85},
        {"dlc":"SLZ", "xPos":45,    "yPos":-75},
        {"dlc":"BRN", "xPos":-68,   "yPos":-105}
    ];    
    this.oApOut = this.oApDef;     
    // wenn Kundenkarte von Basiskarte abweicht, dann Positionswerte neu berechnen
    if (baseMapWidth != this.cusMapWidth || baseMapHeight != this.cusMapHeight || baseIconWidth != this.cusIconWidth || baseIconHeight != this.cusIconHeight) {
        for (var a = 0; a < this.oApOut.length; a ++) {  
            // unterschiedliche x-Werte
            if (baseMapWidth != this.cusMapWidth || baseIconWidth != this.cusIconWidth) {
                // Left-Pos in Basiskarte
                var oldIcPosLeft = Math.ceil((baseMidWi + this.oApOut[a].xPos) - baseMidWiIc);
                // Quotient der Left-Pos zur Basis-Karten-Mittellinie
                var quotWidthOrig =  baseMidWi / oldIcPosLeft;
                // Left-Pos berechnen aus Quotient und Mittellinie der Kundenkarte
                var newIcPosLeft = cusMidWi / quotWidthOrig;    
                // Neuer xPos-Wert
                var newXpos = newIcPosLeft + cusMidWiIc - cusMidWi;  
                this.oApOut[a].xPos = newXpos;  
            } 
            // unterschiedliche y-Werte
            if (baseMapHeight != this.cusMapHeight || baseIconHeight != this.cusIconHeight) {
                // Top-Pos in Basiskarte
                var oldIcPosTop  = Math.ceil((baseMidHe - this.oApOut[a].yPos) - baseMidHeIc);                     
                // Quotient der Top-Pos zur Basis-Karten-Mittellinie
                var quotHeightOrig =  baseMidHe / oldIcPosTop; 
                // Top-Pos berechnen aus Quotient und Mittellinie der Kundenkarte
                var newIcPosTop = cusMidHe / quotHeightOrig; 
                // Neuer yPos-Wert
                var newYpos = cusMidHe - cusMidHeIc - newIcPosTop;
                this.oApOut[a].yPos = newYpos;                        
            }                  
        }         
    }     
    // Ausgabe-Array this.oApOut und Kunden-AFH-Array this.oApCus mergen (-> this.oApMerge)
    var nrMerge = 0;
    for (var c = 0; c < this.oApCus.length; c ++) {             
        for (var d = 0; d < this.oApOut.length; d ++) {                 
            if (this.oApCus[c].dlc == this.oApOut[d].dlc) {                                         
                var icPosLeft = Math.ceil((cusMidWi + this.oApOut[d].xPos) - cusMidWiIc);
                var icPosTop  = Math.ceil((cusMidHe - this.oApOut[d].yPos) - cusMidHeIc);
                if (icPosLeft >= 0 && icPosTop >= 0 && (icPosLeft + this.cusIconWidth) <= this.cusMapWidth && (icPosTop + this.cusIconHeight) <= this.cusMapHeight) {
                    this.oApMerge.push({"dlc":this.oApCus[c].dlc,"label":this.oApCus[c].label,"left":icPosLeft,"top":icPosTop}); 
                }                   
                nrMerge ++;
                break;
            }                   
        } 
        if (nrMerge >= this.oApOut.length || nrMerge >= this.oApCus.length) {
            break;
        }
    }   
}

// Ausgabe und Werte setzen
TTMapForAirportMultiChoice.prototype.generateMap = function () {    
    // HTML-Ausgabe der AFH-Icon    
    var outHtml = '';     
    if (this.oApMerge.length > 0) {                 
        // Kundenspez. Ausgabe-Funktion?
        if (this.callbackDisplayIcon != '') {
            outHtml = eval(this.callbackDisplayIcon + '(this.oApMerge)');         
        } else {
            outHtml = this.printAirportsOnMap();
        }            
        document.getElementById(this.mainDiv).innerHTML = outHtml;                                            
    }    
}

// HTML-Ausgabe der icon-Divs
TTMapForAirportMultiChoice.prototype.printAirportsOnMap = function () {     
    var strHtml = '';    
    for (var i = 0; i < this.oApMerge.length; i ++) {
        var isActAp = false;
        // Airport aktiv?
        if (this.selectedAirports.length > 0 && this.isInArray(this.oApMerge[i].dlc, this.selectedAirports)) {
            isActAp = true;                             
        }
        var theIconStyle = 'ttAirportMapIcon';
        if (isActAp) {
            theIconStyle = 'ttAirportMapIconClick';            
        }          
        strHtml += '<div id="ttMapAirport_' + this.oApMerge[i].dlc + '" dlc="' + this.oApMerge[i].dlc + '" class="' + theIconStyle + '" title="' + this.oApMerge[i].label + '" style="left: ' + this.oApMerge[i].left + 'px; top: ' + this.oApMerge[i].top + 'px;" onmouseover="' + this.name + '.mouseAction(\'over\',this, \'1\')" onmouseout="' + this.name + '.mouseAction(\'out\',this, \'1\')" onclick="' + this.name + '.mouseAction(\'click\',this, \'1\')"></div>';            
    } 
    return strHtml;          
}

// Mausaktionen der icon-divs bei click, over und out
TTMapForAirportMultiChoice.prototype.mouseAction = function (action, obj, callback) { 
    // zusätzliche callback-Funktion aufrufen?   
    var setCallback = false;     
    if (typeof(callback) != 'undefined' && callback == '1') {
        setCallback = true;
    }  
    if (obj != null && typeof(obj) == 'object') {    
        if (action == 'over' && obj.className != 'ttAirportMapIconClick') {
            obj.className = 'ttAirportMapIconHover';
            if (setCallback && this.callbackMouseAction != '') {                
                eval(this.callbackMouseAction + '(\'' + action + '\',\'' + obj.id + '\')');
            }
        } else if (action == 'out' && obj.className != 'ttAirportMapIconClick') {
            obj.className = 'ttAirportMapIcon';
            if (setCallback && this.callbackMouseAction != '') {
                eval(this.callbackMouseAction + '(\'' + action + '\',\'' + obj.id + '\')');
            }
        } else if (action == 'click') {
            if (obj.className == 'ttAirportMapIconClick') {
                obj.className = 'ttAirportMapIcon';
            } else {
                obj.className = 'ttAirportMapIconClick';
            } 
            if (setCallback && this.callbackMouseAction != '') {
                eval(this.callbackMouseAction + '(\'' + action + '\',\'' + obj.id + '\')');
            }   
        } else if (action == 'normal' && obj.className != 'ttAirportMapIcon') {
            obj.className = 'ttAirportMapIcon';
        } else if (action == 'active' && obj.className != 'ttAirportMapIconClick') {             
            obj.className = 'ttAirportMapIconClick';
        } 
    }  
}

// Array mit selektierten AFH definieren
TTMapForAirportMultiChoice.prototype.setArraySelectedAirports = function(selAp) {     
    if (selAp != null && typeof(selAp) == 'object') {
        this.selectedAirports = selAp;
    }    
}

// zusätzliche Funktion, die bei Icon-Mausaktionen aufgerufen wird
TTMapForAirportMultiChoice.prototype.setCallbackMouseAction = function(theFunction) {
    if (typeof(theFunction) == 'string' && theFunction != '') {
        this.callbackMouseAction = theFunction;
    }    
} 

// Callback-Funktion, die für die HTML-Ausgabe eines AFH-Icon alternativ zur Defaultausgabe aufgerufen wird
TTMapForAirportMultiChoice.prototype.setCallbackDisplayIcon = function(theFunction) {
    if (typeof(theFunction) == 'string' && theFunction != '') {
        this.callbackDisplayIcon = theFunction;
    }    
}

// Element in Array suchen
TTMapForAirportMultiChoice.prototype.isInArray = function(needle, haystack) {
    for (var i = 0; i < haystack.length; i ++) {
        if (haystack[i] === needle) {
            return true;
        }
    }
    return false;
}

// Standard-Instanz
var ttMapAirport = new TTMapForAirportMultiChoice();
