#!/usr/local/ActivePerl-5.6/bin/perl -w
use strict;
use warnings;

my $meta_no     = 1;   # Is Corel Draw layer number in header, has to be sequential to display layers
my $path_new    = '../../trunkHG/src/table/airport_movement.h';  # Changed file path.
my $path_org    = '../../trunk/src/table/airport_movement.h';    # Unchanged file path.
my $path_out    = 'airport_movement.svg';                        # SVG output file path.
my $path_jav    = 'array.es';                                    # Javascript array file path.

open FILEin,       $path_new    or die $!;
my @file = <FILEin>;
close FILEin;

open FILEout, ">$path_out" or die $!;
print FILEout header();
print FILEout script();
open SCRIPTout, ">$path_jav" or die $!;
print SCRIPTout javaScriptArray('AirportMovingData', ' _air');
print SCRIPTout javaScriptArray('AirportFTAbuildup', ' _airport_fta_');
close SCRIPTout;
print FILEout csstyle();
print FILEout AirportDrawings();
print FILEout AirportMovingDataPoints ('');

open FILEin,    $path_org    or die $!;
@file = <FILEin>;
close FILEin;

print FILEout AirportMovingDataPoints ('_old');
print FILEout footer();

close FILEout;

#----------------------------------------------------------------------------------------------------------------
sub CleanString {

    my $line = shift;

    chomp ($line);
    $line =~ s/\r$//;
    $line =~ s/\/.+//g;                            # Kill comments
    $line =~ s{^\s+|\s+$|\t| }{}g;                 # Clean whitespace.
    return if $line eq '';                         #
    $line =~ s/AMD\(/\(/g;                         # substitute to (.
    $line =~ s/\),\(|\},\{/\t/g;                   # Substitute inner brackets for delimiter;
    $line =~ s/\),|\(|\},|\{//g;                   # Clean rest of brackets;
    return split(/\t/, $line);
}

#----------------------------------------------------------------------------------------------------------------
sub javaScriptArray {

my $in_block   = 0;
my $fieldCnt   = 0;
my $substring  = '';
my $outstring  = '';
my $name       = shift;
my $identifier = shift;

    foreach my $line (@file) {
        if ( $line =~ m/$name$identifier/ ) {
            $in_block = 1;
            $substring = '';    # really empty
            next;
        }

        if ( $line =~ /\};|MAX_ELEMENTS/ && $in_block) {
            $in_block = 0;
            $substring =~ s/\s+$//g;    # clean whitespace at end of string.
            chop $substring;            # take last comma away, crashes firefox  ], ]; => ]];
            $outstring .= $name . '[' . $fieldCnt . '] = [' . "\n" . $substring .'];' . "\n\n";
            $fieldCnt++;
        }

        if ( $in_block ) {
            my @Arr = &CleanString($line);
            if (!@Arr) {next;}
            $substring .= "\t";
            foreach (@Arr) {
                my @t4 = split(/,/, $_);
                $substring .= qq!\[$t4[0],$t4[1],"$t4[2]","$t4[3]"\], ! if $name eq 'AirportMovingData';
                $substring .= qq!\[$t4[0],"$t4[1]","$t4[2]",$t4[3]\], ! if $name eq 'AirportFTAbuildup';
            }
            $substring .= "\n";
        }
    }
    return $outstring;
}

#----------------------------------------------------------------------------------------------------------------
sub AirportMovingDataPoints {
    my $in_block    = 0;
    my $fieldCnt    = 0;
    my $dummy_id    = 0;    # Number for dummy field ID's
    my $dup_key     = 0;
    my %hash        = ();   # Used for duplicate key checking
    my $outstring   = '';
    my @local       = @file;
    my $regex       = 'AirportMovingData _airport';
    my $oldnew      = shift;

    foreach my $line (@file) {
        chomp ($line);

        if ($line =~ m/$regex/ ) {
            $in_block = 1;
            $line =~ s/\[.*//;              # Clean after '['.
            $line =~ /_/;
            $outstring .= qq! <g id="$'$oldnew" visibility="hidden">\n! . q!  <metadata id="CorelCorpID_! . $meta_no++ . qq!Corel-Layer"/>\n!;
            next;
        }
        if ($line =~ /\}\;/ && $in_block) {          # Stop at ");
            $outstring .= " </g>\n";
            $in_block = 0;
            $fieldCnt++;
            next;
        }
        if ($line =~ m/AMD\(/ && $in_block) {
            my @arr = split(/,|\(|\//, $line);
            s{\&|\#}{ }g        foreach @arr;  # Clean none friendly characters & and #.
            $arr[7] =~ s/\-/ /g if $arr[7];
            s/ +/ /g            foreach @arr;  # match at least 2 spaces, replace with a single space.
            s{^\s+|\s+$}{}g     foreach @arr;  # clean whitespace.
            s/ /_/g             foreach @arr;  # match 1 spaces, replace with "_".
            s/^\_|_$//g         foreach @arr;  # take _ from front and back.

            my $key = $arr[7];       # Format keys and rename dupplicate keys e.g. ID's
            if (!defined $key || $key eq '') {
                $key .= sprintf("%02d", $dummy_id++) . '_dummykey';
            }
            $key = sprintf("%02d", $fieldCnt) . '_' . $key; #Now we can find parent array when clickingon the screen.
            if (exists $hash{$key}) {
              $key .= '_dk' . sprintf("%02d", $dup_key++);
            }
            $hash{ $key } = '';

            my @colArr = split(/,/, $line);
            my $fill = '';
            my $stroke = '';
            my $s0 = '';
            my $f1 = '';
            ($s0,$f1) = split(/\|/, $colArr[2]);
            $s0 =~ s{^\s+|\s+$}{}g if ($s0);                  # clean whitespace.
            $f1 =~ s{^\s+|\s+$}{}g if ($f1);                  # clean whitespace.


            if (!$s0) {
                if (!defined $s0) {
                    $stroke = 'N';  #Null
                } else {
                    $stroke = '0';
                };
            } else {
                if ($s0 =~ m/AMED_NOSPDCLAMP/) { $stroke = '1'}
                if ($s0 =~ m/AMED_TAKEOFF/)    { $stroke = '2'}
                if ($s0 =~ m/AMED_SLOWTURN/)   { $stroke = '3'}
                if ($s0 =~ m/AMED_LAND/)       { $stroke = '4'}
                if ($s0 =~ m/AMED_EXACTPOS/)   { $stroke = '5'}
                if ($s0 =~ m/AMED_BRAKE/)      { $stroke = '6'}
                if ($s0 =~ m/AMED_HELI_RAISE/) { $stroke = '7'}
                if ($s0 =~ m/AMED_HELI_LOWER/) { $stroke = '8'}
                if ($s0 =~ m/AMED_HOLD/)       { $stroke = '9'}
            }
            if (!$f1) {
                if (!defined $f1) {
                    $fill = 'N';
                } else {
                    $fill = '0';
                };
            } else {
                if ($f1 =~ m/AMED_NOSPDCLAMP/) { $fill = '1'}
                if ($f1 =~ m/AMED_TAKEOFF/)    { $fill = '2'}
                if ($f1 =~ m/AMED_SLOWTURN/)   { $fill = '3'}
                if ($f1 =~ m/AMED_LAND/)       { $fill = '4'}
                if ($f1 =~ m/AMED_EXACTPOS/)   { $fill = '5'}
                if ($f1 =~ m/AMED_BRAKE/)      { $fill = '6'}
                if ($f1 =~ m/AMED_HELI_RAISE/) { $fill = '7'}
                if ($f1 =~ m/AMED_HELI_LOWER/) { $fill = '8'}
                if ($f1 =~ m/AMED_HOLD/)       { $fill = '9'}
            }

            my $class = "fCi$fill sCi$stroke";

            # Att -1 added to come back to correct reading used for flipping drawing on horizontal axis.
            if ($oldnew =~ m/_old/) { 
                $outstring .= '  <circle class="' . $class . '" cx="' . $arr[1] * -1 . '" cy="' . $arr[2] * 1 . '" r="3"/>' . "\n";
            } else {
                $outstring .= '  <circle id="' . $key . '" class="' . $class . '" cx="' . $arr[1] * -1 . '" cy="' . $arr[2] * 1 . '" r="3"/>' . "\n";
            }

            next;
        }
    }
    return $outstring;
}

#----------------------------------------------------------------------------------------------------------------
sub header {

return <<"ENDOFTEXT";<?xml version="1.0" encoding="UTF-16"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="1000mm" height="800mm" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd"
viewBox="-600 -300 1000 800"
 xmlns:xlink="http://www.w3.org/1999/xlink" onload="init(evt);">
<script type="application/ecmascript"> <![CDATA[
ENDOFTEXT
}

#----------------------------------------------------------------------------------------------------------------
sub script {
return <<"ENDOFTEXT";
var svgDoc, pageWidth, pageHeight, offsetx, offsety, invX, invY, activeCircle, airports, AirportMovingData, AirportFTAbuildup;

airports = ['dummy', 'country', 'commuter', 'city', 'metropolitan', 'international', 'intercontinental', 'heliport', 'helidepot', 'helistation', 'oilrig'];
AirportMovingData = [];
AirportFTAbuildup = [];

function show(id) {
    'use strict';

    svgDoc.getElementById(id).setAttributeNS(null, 'visibility', 'visible');
}
function hide(id) {
    'use strict';

    svgDoc.getElementById(id).setAttributeNS(null, 'visibility', 'hidden');
}

function getArrayLocation(circle) {
    'use strict';

    var arrLoc, id;

    id = circle.getAttributeNS(null, 'id');
    id = id.replace(new RegExp('_', 'g'), ' '); // clean '_' character, in names.

    arrLoc = id.split(" ");              // Extract airfield arraynr and position for [0] and [1]
    arrLoc[0] = parseInt(arrLoc[0], 10); // Array position airport in Movement Positions array
    arrLoc[1] = parseInt(arrLoc[1], 10); // Position in Movement Positions array == First element of FTA "terminals array" (example 15) in FTA { 15, 0, NOTHING_block, 16 },.
    arrLoc[2] = id;                      // [2] used for cleaned string

    return arrLoc;
}

function logToConsole() {
    'use strict';

    var i, nr, arrLoc;

    if (!activeCircle) return;

    arrLoc = getArrayLocation(activeCircle);

    console.log('AMD = \\[' + arrLoc[0] + ', ' + arrLoc[1] + '\\], value = \\[' + AirportMovingData[arrLoc[0]][arrLoc[1]] + '\\]');

    for (i = 0; i < AirportFTAbuildup[arrLoc[0]].length; i++) {
        nr = AirportFTAbuildup[arrLoc[0]][i][0];
        if (nr === arrLoc[1]) {
            console.log(' - FTA ,\\[' + AirportFTAbuildup[arrLoc[0]][i] + "\\]\\n");
        }
    }
}

function showAssociations(cx, cy) {
    'use strict';

    var count, arrLoc, i, nr, line;

    if (!activeCircle) { return; }

    count = 0;
    arrLoc = getArrayLocation(activeCircle);

    // iterate through airport FTA to get assoiated points and draw respective lines.
    for (i = 0; i < AirportFTAbuildup[arrLoc[0]].length; i++) {
        nr = AirportFTAbuildup[arrLoc[0]][i][0];
        if (nr === arrLoc[1]) {
            nr = AirportFTAbuildup[arrLoc[0]][i][3]; // (example 16) in FTA { 15, 0, NOTHING_block, 16 }
            line = svgDoc.getElementById('as' + count++);

            line.setAttributeNS(null, "x1", cx);
            line.setAttributeNS(null, "y1", cy);
            line.setAttributeNS(null, "x2", invX * AirportMovingData[arrLoc[0]][nr][0]); // retreive x pos in Movement Positions array
            line.setAttributeNS(null, "y2", invY * AirportMovingData[arrLoc[0]][nr][1]); // retreive y pos in Movement Positions array
        }
    }
}

function hideAssociations() {
    'use strict';

    var dad, lines, i;

    dad = svgDoc.getElementById('associations');
    lines = dad.getElementsByTagName('line');

    for (i = 0; i < lines.length; i++) {
        lines[i].setAttributeNS(null, "x1", -50);
        lines[i].setAttributeNS(null, "y1", 600);
        lines[i].setAttributeNS(null, "x2", 170);
        lines[i].setAttributeNS(null, "y2", 600);
    }
}

function showTooltip(evt) {
    'use strict';

    var tooltip, arrLoc, cx, cy;

    activeCircle = evt.target;

    cx = activeCircle.getAttributeNS(null, 'cx');
    cy = activeCircle.getAttributeNS(null, 'cy');
    arrLoc = getArrayLocation(activeCircle);

    tooltip = svgDoc.getElementById('tooltip');
    tooltip.setAttributeNS(null, 'x', cx - 100);
    tooltip.setAttributeNS(null, 'y', cy - 10);
    tooltip.firstChild.data = arrLoc[2] + ' (' + cx * invX + ', ' + cy * invY + ')'; // Att -1 added to come back to correct reading used for flipping drawing vertical.
    show('tooltip');
    showAssociations(cx, cy);
}

function hideTooltip() {
    'use strict';

    hide('tooltip');
    hideAssociations();
}

function showHideFieldOld(evt) {
    'use strict';

    var showOldDots, i;

    showOldDots = false;
    if (svgDoc.getElementById('_old').getAttributeNS(null, 'class') ===  'fBu1 sBu0') {
        showOldDots = true;
    }
    for (i = 0; i < airports.length; i += 1) {
        if (showOldDots) {
            if (svgDoc.getElementById(airports[i]).getAttributeNS(null, 'class') ===  'fBu1 sBu0'){
                show('airport_moving_data_' + airports[i] + '_old');
            } else {
                hide('airport_moving_data_' + airports[i] + '_old');
            }
        } else {
            hide('airport_moving_data_' + airports[i] + '_old');
        }
    }
}

function clickOldNew(evt) {
    'use strict';

    var button = svgDoc.getElementById('_old');
    if (button.getAttributeNS(null, 'class') ===  'fBu1 sBu0') {
        button.setAttributeNS(null, 'class', 'fBu0 sBu0');
    } else {
        button.setAttributeNS(null, 'class', 'fBu1 sBu0');
    }
    showHideFieldOld();
}

function showHideField(evt) {
    'use strict';

    var button, buttonOld;

    button    = evt.target;
    buttonOld = svgDoc.getElementById('_old');

    if (button.getAttributeNS(null, 'class') ===  'fBu1 sBu0') {
        button.setAttributeNS(null, 'class', 'fBu0 sBu0');
        hide('airport_moving_data_' + button.id);
        hide(button.id + '_field');
    } else {
        button.setAttributeNS(null, 'class', 'fBu1 sBu0');
        show('airport_moving_data_' + button.id);
        show(button.id + '_field');
    }
    showHideFieldOld();
}

function movePoint(xm, ym) {
    'use strict';

    var x, y, arrLoc;

    if (!activeCircle) { return; }

    x = parseInt(activeCircle.getAttributeNS(null, 'cx'), 10);
    y = parseInt(activeCircle.getAttributeNS(null, 'cy'), 10);

    arrLoc = getArrayLocation(activeCircle);

    AirportMovingData[arrLoc[0]][arrLoc[1]][0] = (x + xm) * invX;
    AirportMovingData[arrLoc[0]][arrLoc[1]][1] = (y + ym) * invY;

    activeCircle.setAttributeNS(null, 'cx', x + xm);
    activeCircle.setAttributeNS(null, 'cy', y + ym);

    showAssociations(x + xm, y + ym);
}

function keyBoardEvents(evt) {
    'use strict';

    var cKey;

    cKey = {  a : 65, b : 66, c : 67, d : 68, g : 71, h : 72, s : 83, w : 87 };

    switch (evt.keyCode) {
    case cKey.b:
        if (svgDoc.getElementById('buttons').getAttributeNS(null, 'visibility') === 'visible') {
            hide('buttons');
        } else {
            show('buttons');
        }
        break;
    case cKey.c:
        logToConsole();
        break;
    case cKey.g:
        if (svgDoc.getElementById('grid').getAttributeNS(null, 'visibility') === 'visible') {
            hide('grid');
        } else {
            show('grid');
        }
        break;
    case cKey.h:
        if (svgDoc.getElementById('help').getAttributeNS(null, 'visibility') === 'visible') {
            hide('help');
        } else {
            show('help');
        }
        break;
    case cKey.w:
        movePoint(0, -1);  // Move active circle up.
        break;
    case cKey.s:
        movePoint(0, 1);  // Move active circle down.
        break;
    case cKey.a:
        movePoint(-1, 0);  // Move active circle left.
        break;
    case cKey.d:
        movePoint(1, 0);  // Move active circle right.
        break;
    }
}

function NewObj(objtype, props, dad, text) { //   Creates a new object with properties, and optionally appends to a parent element.
    'use strict';

    var el, a;

    el = svgDoc.createElementNS('http://www.w3.org/2000/svg', objtype);

    if (props) {
        for (a in props) {
            if (props.hasOwnProperty(a)) {
                el.setAttributeNS(null, a, props[a]);
            }
        }
    }
    if (dad) {
        dad.appendChild(el);
    }
    if (text) {
        el.appendChild(svgDoc.createTextNode(text));
    }
    return el;
}

function drawGrid() {
    'use strict';

    var layer, size, fieldCnt, i, line;

    layer    = svgDoc.getElementById('grid');
    size     = 16; // Set grid spacing

    fieldCnt = 4; // horizontal lines
    for (i = 0; i < pageHeight; i += 1) {
        line = new NewObj('line', {class: 'fLin sGr0', x1 : offsetx, y1 : offsety + i, x2 : offsetx + pageWidth, y2 : offsety + i}, layer);
        if (fieldCnt === size) {
            line.setAttributeNS(null, 'class', 'fLin sGr1');
            fieldCnt = 0;
        }
        fieldCnt += 1;
    }
    fieldCnt = 8; // vertical lines
    for (i = 0; i < pageWidth; i += 1) {
        line = new NewObj('line', {class: 'fLin sGr0', x1 : offsetx + i, y1 : offsety, x2 : offsetx + i, y2 : offsety + pageHeight}, layer);
        if (fieldCnt === size) {
            line.setAttributeNS(null, 'class', 'fLin sGr1');
            fieldCnt = 0;
        }
        fieldCnt += 1;
    }
    show('grid');
}

function adEvents() {
    'use strict';

    var i, circles;

    circles = svgDoc.getElementsByTagName('circle');

    for (i = 0; i < circles.length; i += 1) {

        if (circles[i].id) {
            circles[i].addEventListener('mouseleave', hideTooltip, false);
            circles[i].addEventListener('mouseenter', showTooltip, false);
        }
    }
    for (i = 0; i < airports.length; i += 1) {
        svgDoc.getElementById(airports[i]).addEventListener('click', showHideField, false);
    }
    svgDoc.documentElement.addEventListener('keydown', keyBoardEvents, false);

}

function init(evt) {
    'use strict';

    var svg, box;

    svgDoc = evt.target.ownerDocument;

    svg = svgDoc.getElementsByTagName('svg')[0];
    pageWidth = parseInt(svg.getAttribute('width').replace(/[^0-9]+/g, ''), 10);
    pageHeight = parseInt(svg.getAttribute('height').replace(/[^0-9]+/g, ''), 10);
    box = svg.viewBox.baseVal;
    offsetx = box.x;
    offsety = box.y;
    invX = -1;  // Flip image y-axis see perl script.
    invY = 1; // Flip image x-axis see perl script.

    adEvents();
    drawGrid();
    show('airport_moving_data_intercontinental');
    show('intercontinental_field');
}

ENDOFTEXT
}


#----------------------------------------------------------------------------------------------------------------
sub csstyle {

return <<"ENDOFTEXT";
]]> </script>
 <script type="application/ecmascript" xlink:href="array.es"/> 

 <defs>
  <style type="text/css">
   <![CDATA[
    .sLin {stroke:#1F1A17;stroke-width:0.075}
    .sGr0 {stroke:#97AFA0;stroke-width:0.075}
    .sGr1 {stroke:#0093DD;stroke-width:0.075}
    .sAs0 {stroke:#28166F;stroke-width:0.7}
    .sCiN {stroke:#808080;stroke-width:1}
    .sCi0 {stroke:#FFFF00;stroke-width:1}
    .sCi1 {stroke:#00FF00;stroke-width:1}
    .sCi2 {stroke:#008000;stroke-width:1}
    .sCi3 {stroke:#00FFFF;stroke-width:1}
    .sCi4 {stroke:#FFA500;stroke-width:1}
    .sCi5 {stroke:#000000;stroke-width:1}
    .sCi6 {stroke:#FF0000;stroke-width:1}
    .sCi7 {stroke:#CD853F;stroke-width:1}
    .sCi8 {stroke:#FF1493;stroke-width:1}
    .sCi9 {stroke:#0000FF;stroke-width:1}
    .sC99 {stroke:#97AFA0;stroke-width:1}
    .sBu0 {stroke:#000000;stroke-width:1.2}
    .stxB {stroke:#000000;stroke-width:0.2}
    .stxH {stroke:#66CC00;stroke-width:0.8}
    .stxN {stroke:#000000;stroke-width:0.2}
    .fLin {fill:none}
    .fAP0 {fill:#DEDEDD}
    .fCiN {fill:#F8F8FF}
    .fCi0 {fill:#FFFF00}
    .fCi1 {fill:#00FF00}
    .fCi2 {fill:#008000}
    .fCi3 {fill:#00FFFF}
    .fCi4 {fill:#FFA500}
    .fCi5 {fill:#000000}
    .fCi6 {fill:#FF0000}
    .fCi7 {fill:#CD853F}
    .fCi8 {fill:#FF1493}
    .fCi9 {fill:#000000}
    .fC99 {fill:#FFFFFF}
    .fBu0 {fill:#F8F8FF}
    .fBu1 {fill:#FFA500}
    .ftxG {fill:#66CC00}
    .ftxO {fill:#FFA500}
    .fnt0 {font-weight:normal;font-size:0.35pt;font-family:'Arial'}
    .fnt1 {font-weight:normal;font-size:10pt;font-family:'Arial'}
    .fnt3 {font-weight:normal;font-size:7pt;font-family:'Arial'}
    .fnt4 {font-weight:normal;font-size:4pt;font-family:'Arial'}
   ]]>
ENDOFTEXT
}

#----------------------------------------------------------------------------------------------------------------
sub AirportDrawings {

return q!

  </style>
 </defs>
 
 <g id="dir">
  <metadata id="CorelCorpID_! . $meta_no++ . q!Corel-Layer"/>
 </g>
 <g transform="translate(-160,0)">
 <g id="dummy_field" visibility="hidden">
  <metadata id="CorelCorpID_! . $meta_no++ . q!Corel-Layer"/>
  <rect class="fLin sLin" x="96" width="64" height="48"/>
 </g>
 <g id="country_field" visibility="hidden">
  <metadata id="CorelCorpID_! . $meta_no++ . q!Corel-Layer"/>
  <rect class="fLin sLin" x="96" width="64" height="48"/>
  <rect class="fAP0 sLin" x="96" y="32" width="64" height="16"/>
  <line class="fLin sCiN" x1="112" y1="0" x2="112" y2= "16" />
  <line class="fLin sCiN" x1="112" y1="0" x2="96" y2= "0" />
  <line class="fLin sCiN" x1="96" y1="0" x2="96" y2= "16" />
 </g>
 <g id="commuter_field" visibility="hidden">
  <metadata id="CorelCorpID_! . $meta_no++ . q!Corel-Layer"/>
  <rect class="fLin sLin" x="80" width="80" height="64"/>
  <rect class="fAP0 sLin" x="80" y="48" width="80" height="16"/>
  <line class="fLin sCiN" x1="96" y1="0" x2="96" y2= "16" />
  <line class="fLin sCiN" x1="96" y1="0" x2="80" y2= "0" />
  <line class="fLin sCiN" x1="80" y1="0" x2="80" y2= "16" />
 </g>
 <g id="city_field" visibility="hidden">
  <metadata id="CorelCorpID_! . $meta_no++ . q!Corel-Layer"/>
  <rect class="fLin sLin" x="64" width="96" height="96"/>
  <rect class="fAP0 sLin" x="64" y="80" width="96" height="16"/>
  <line class="fLin sCiN" x1="80" y1="0" x2="80" y2= "16" />
  <line class="fLin sCiN" x1="80" y1="0" x2="64" y2= "0" />
  <line class="fLin sCiN" x1="64" y1="0" x2="64" y2= "16" />
 </g>
 <g id="metropolitan_field" visibility="hidden">
  <metadata id="CorelCorpID_! . $meta_no++ . q!Corel-Layer"/>
  <rect class="fLin sLin" x="64" width="96" height="96"/>
  <rect class="fAP0 sLin" x="64" y="80" width="96" height="16"/>
  <rect class="fAP0 sLin" x="64" y="64" width="96" height="16"/>
  <line class="fLin sCiN" x1="80" y1="0" x2="80" y2= "16" />
  <line class="fLin sCiN" x1="80" y1="0" x2="64" y2= "0" />
  <line class="fLin sCiN" x1="64" y1="0" x2="64" y2= "16" />
 </g>
 <g id="international_field" visibility="hidden">
  <metadata id="CorelCorpID_! . $meta_no++ . q!Corel-Layer"/>
  <rect class="fLin sLin" x="48" width="112" height="112"/>
  <rect class="fAP0 sLin" x="48" y="96" width="112" height="16"/>
  <rect class="fAP0 sLin" x="48" width="112" height="16"/>
  <line class="fLin sCiN" x1="160" y1="48" x2="160" y2= "64" />
  <line class="fLin sCiN" x1="160" y1="48" x2="144" y2= "48" />
  <line class="fLin sCiN" x1="144" y1="48" x2="144" y2= "64" />
  <line class="fLin sCiN" x1="64" y1="16" x2="64" y2= "32" />
  <line class="fLin sCiN" x1="64" y1="16" x2="48" y2= "16" />
  <line class="fLin sCiN" x1="48" y1="16" x2="48" y2= "32" />
 </g>
 <g id="intercontinental_field" visibility="hidden">
  <metadata id="CorelCorpID_! . $meta_no++ . q!Corel-Layer"/>
  <polygon class="fLin sLin" points="160,0 16,0 16,160 32,160 32,176 160,176 "/>
  <rect class="fAP0 sLin" x="32" y="160" width="128" height="16"/>
  <rect class="fAP0 sLin" x="16" width="128" height="16"/>
  <rect class="fAP0 sLin" x="32" y="16" width="128" height="16"/>
  <rect class="fAP0 sLin" x="16" y="144" width="128" height="16"/>
  <line class="fLin sCiN" x1="160" y1="80" x2="160" y2= "96" />
  <line class="fLin sCiN" x1="160" y1="80" x2="144" y2= "80" />
  <line class="fLin sCiN" x1="144" y1="80" x2="144" y2= "96" />
  <line class="fLin sCiN" x1="32" y1="64" x2="32" y2= "80" />
  <line class="fLin sCiN" x1="32" y1="64" x2="16" y2= "64" />
  <line class="fLin sCiN" x1="16" y1="64" x2="16" y2= "80" />
 </g>
 <g id="heliport_field" visibility="hidden">
  <metadata id="CorelCorpID_! . $meta_no++ . q!Corel-Layer"/>
  <rect class="fAP0 sLin" x="144" width="16" height="16"/>
 </g>
 <g id="helidepot_field" visibility="hidden">
  <metadata id="CorelCorpID_! . $meta_no++ . q!Corel-Layer"/>
  <rect class="fAP0 sLin" x="144" y="16" width="16" height="16"/>
  <rect class="fLin sLin" x="128" width="32" height="32"/>
  <line class="fLin sCiN" x1="144" y1="0" x2="144" y2= "16" />
  <line class="fLin sCiN" x1="144" y1="0" x2="128" y2= "0" />
  <line class="fLin sCiN" x1="128" y1="0" x2="128" y2= "16" />
 </g>
 <g id="helistation_field" visibility="hidden">
  <metadata id="CorelCorpID_! . $meta_no++ . q!Corel-Layer"/>
  <rect class="fLin sLin" x="96" width="64" height="32"/>
  <rect class="fAP0 sLin" x="112" width="16" height="16"/>
  <rect class="fAP0 sLin" x="96" y="16" width="16" height="16"/>
  <rect class="fAP0 sLin" x="96" width="16" height="16"/>
  <line class="fLin sCiN" x1="160" y1="0" x2="160" y2= "16" />
  <line class="fLin sCiN" x1="160" y1="0" x2="144" y2= "0" />
  <line class="fLin sCiN" x1="144" y1="0" x2="144" y2= "16" />
 </g>
  <g id="oilrig_field" visibility="hidden">
  <metadata id="CorelCorpID_12Corel-Layer"/>
  <rect class="fLin sLin" x="128" y="0" width="32" height="48"/>
  <rect class="fAP0 sLin" x="128" y="0" width="16" height="16"/>
 </g>
 </g>
 <g id="grid" visibility="visible"> </g>
!;
}

#----------------------------------------------------------------------------------------------------------------
sub footer {

return q! <g id="associations">
  <metadata id="CorelCorpID_! . $meta_no++ . q!Corel-Layer"/>
  <line id="as0" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as1" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as2" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as3" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as4" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as5" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as6" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as7" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as8" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as9" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as10" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as11" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as12" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as13" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as14" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as15" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as16" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as17" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as18" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as19" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
  <line id="as20" class="fLin sAs0" x1="-50" y1="600" x2="170" y2= "600" />
 </g>
 <g transform="translate(-580,-30)" id="buttons" visibility="visible">
  <metadata id="CorelCorpID_! . $meta_no++ . q!Corel-Layer"/>
  <rect id="dummy" class="fBu0 sBu0" x="0" y="0" width="16" height="16"/><text x="32" y="16" class="ftxG stxB fnt1">dummy</text>
  <rect id="country" class="fBu0 sBu0" x="0" y="20" width="16" height="16"/><text x="32" y="36" class="ftxG stxB fnt1">country</text>
  <rect id="commuter" class="fBu0 sBu0" x="0" y="40" width="16" height="16"/><text x="32" y="56" class="ftxG stxB fnt1">commuter</text>
  <rect id="city" class="fBu0 sBu0" x="0" y="60" width="16" height="16"/><text x="32" y="76" class="ftxG stxB fnt1">city</text>
  <rect id="metropolitan" class="fBu0 sBu0" x="0" y="80" width="16" height="16"/><text x="32" y="96" class="ftxG stxB fnt1">metropolitan</text>
  <rect id="international" class="fBu0 sBu0" x="0" y="100" width="16" height="16"/><text x="32" y="116" class="ftxG stxB fnt1">international</text>
  <rect id="intercontinental" class="fBu1 sBu0" x="0" y="120" width="16" height="16"/><text x="32" y="136" class="ftxG stxB fnt1">intercontinental</text>
  <rect id="heliport" class="fBu0 sBu0" x="0" y="140" width="16" height="16"/><text x="32" y="156" class="ftxG stxB fnt1">heliport</text>
  <rect id="helidepot" class="fBu0 sBu0" x="0" y="160" width="16" height="16"/><text x="32" y="176" class="ftxG stxB fnt1">helidepot</text>
  <rect id="helistation" class="fBu0 sBu0" x="0" y="180" width="16" height="16"/><text x="32" y="196" class="ftxG stxB fnt1">helistation</text>
  <rect id="oilrig" class="fBu0 sBu0" x="0" y="200" width="16" height="16"/><text x="32" y="216" class="ftxG stxB fnt1">oilrig</text>
  <rect id="_old" class="fBu0 sBu0" x="440" y="0" width="16" height="16" onclick="clickOldNew()"/><text x="472" y="16" class="ftxG stxB fnt1">Show old points</text>
 </g>
 <g>
  <text x="320" y="390" id="tooltip" class="fil1 fnt3">tooltip don’t delete, by HackaLittleBit.;-)</text>
 </g>
 <g id="help" visibility="visible">
  <metadata id="CorelCorpID_! . $meta_no++ . q!Corel-Layer"/>
  <svg x="50" y="-235">
   <text x="45" y="20" class="ftxO stxH fnt1">Code colors</text>
   <ellipse class="fCiN sCiN" cx="15" cy="35" rx="10.5" ry="6.5"/><text x="45" y="40" class="ftxO stxN fnt1">null</text>
   <ellipse class="fCi0 sCi0" cx="15" cy="55" rx="10.5" ry="6.5"/><text x="45" y="60" class="ftxO stxN fnt1">0</text>
   <ellipse class="fCi1 sCi1" cx="15" cy="75" rx="10.5" ry="6.5"/><text x="45" y="80" class="ftxO stxN fnt1">AMED_NOSPDCLAMP</text>
   <ellipse class="fCi2 sCi2" cx="15" cy="95" rx="10.5" ry="6.5"/><text x="45" y="100" class="ftxO stxN fnt1">AMED_TAKEOFF</text>
   <ellipse class="fCi3 sCi3" cx="15" cy="115" rx="10.5" ry="6.5"/><text x="45" y="120" class="ftxO stxN fnt1">AMED_SLOWTURN</text>
   <ellipse class="fCi4 sCi4" cx="15" cy="135" rx="10.5" ry="6.5"/><text x="45" y="140" class="ftxO stxN fnt1">AMED_LAND</text>
   <ellipse class="fCi5 sCi5" cx="15" cy="155" rx="10.5" ry="6.5"/><text x="45" y="160" class="ftxO stxN fnt1">AMED_EXACTPOS</text>
   <ellipse class="fCi6 sCi6" cx="15" cy="175" rx="10.5" ry="6.5"/><text x="45" y="180" class="ftxO stxN fnt1">AMED_BRAKE</text>
   <ellipse class="fCi7 sCi7" cx="15" cy="195" rx="10.5" ry="6.5"/><text x="45" y="200" class="ftxO stxN fnt1">AMED_HELI_RAISE</text>
   <ellipse class="fCi8 sCi8" cx="15" cy="215" rx="10.5" ry="6.5"/><text x="45" y="220" class="ftxO stxN fnt1">AMED_HELI_LOWER</text>
   <ellipse class="fCi9 sCi9" cx="15" cy="235" rx="10.5" ry="6.5"/><text x="45" y="240" class="ftxO stxN fnt1">AMED_HOLD</text>
  </svg>
  <svg x="-350" y="-235">
  <text y="20">
   <tspan x="0" class="ftxO stxH fnt1">Keyboard:</tspan>
   <tspan x="0" dy="20" class="ftxO stxN fnt1">h = Help show-hide.</tspan>
   <tspan x="0" dy="20" class="ftxO stxN fnt1">Ctrl + (mouseScroll or +/-) = Zoom in/out.</tspan>
   <tspan x="0" dy="20" class="ftxO stxN fnt1">b = Buttons show-hide.</tspan>
   <tspan x="0" dy="20" class="ftxO stxN fnt1">c = Show array values in browser logging console.(F12 with FF)</tspan>
   <tspan x="0" dy="20" class="ftxO stxN fnt1">g = Grid show-hide.</tspan>
   <tspan x="0" dy="20" class="ftxO stxN fnt1">After activating point with cursor:</tspan>
   <tspan x="0" dy="20" class="ftxO stxN fnt1">w = Up</tspan>
   <tspan x="0" dy="20" class="ftxO stxN fnt1">s = Down</tspan>
   <tspan x="0" dy="20" class="ftxO stxN fnt1">a = Left</tspan>
   <tspan x="0" dy="20" class="ftxO stxN fnt1">d = Right</tspan>
  </text>
  </svg>
  <g>
   <text x="-200" y="-275" class="ftxO stxH fnt1">OpenTTD Airport Movement Layout</text>
  </g>
  <g>
   <image x="-580" y="-290" width="150" height="145" xlink:href="data:image/png;base64, " />
  </g>
  <g transform="translate(16,-16)" id="directions">
   <text x="-5" y="16"  class="fil0 fnt4">se</text>
   <text x="-5" y="11.5"  class="fil0 fnt4">1</text>
   <text x="-5" y="-9.5"  class="fil0 fnt4">3</text>
   <text x="-5" y="-14"  class="fil0 fnt4">nw</text>
   <text x="10" y="-2.5"  class="fil0 fnt4">ne</text>
   <text x="13" y="2"  class="fil0 fnt4">0</text>
   <text x="-16" y="2"  class="fil0 fnt4">2</text>
   <text x="-16" y="-2.5"  class="fil0 fnt4">sw</text>
   <line class="fLin sCi5" x1="-10" y1="0" x2="10" y2= "0" />
   <ellipse class="fil2" cx="-10" rx="2.5" ry="1.5"/>
   <ellipse class="fil2" cx="10" rx="2.5" ry="1.5"/>
   <line class="fLin sCi5" x1="0" y1="10" x2="0" y2= "-10" />
   <ellipse class="fil2" cy="10" rx="1.5" ry="2.5"/>
   <ellipse class="fil2" cy="-10" rx="1.5" ry="2.5"/>
   <g transform="rotate(315)">
    <text x="15" y="2"  class="fil0 fnt3">North &gt;</text>
   </g>
  </g>
 </g>
</svg>!;
}
