var is_ie6;
var adr_lat;
var adr_lng;
var cloneDiv;
var input = false;
var bounds = new GLatLngBounds();
var opening = new Array();
var mm;
var gmarkersA = [];
var markerArray = new Array();
var nearestArray = new Array();
var nearestIDs = new Array();
var minzoom = 9;

var icons = new Array ("/common/img/app/stationsfinder/sx-marker-1.png",
                       "/common/img/app/stationsfinder/sx-marker-2.png",
                       "/common/img/app/stationsfinder/sx-marker-3.png",
                       "/common/img/app/stationsfinder/sx-marker-4.png",
                       "/common/img/app/stationsfinder/sx-marker-5.png",
                       "/common/img/app/stationsfinder/sx-marker-6.png",
                       "/common/img/app/stationsfinder/sx-marker-7.png",
                       "/common/img/app/stationsfinder/sx-marker-8.png",
                       "/common/img/app/stationsfinder/sx-marker-9.png",
                       "/common/img/app/stationsfinder/sx-marker-10.png",
                       "/common/img/app/stationsfinder/sx-marker-11.png",
                       "/common/img/app/stationsfinder/sx-marker-12.png",
                       "/common/img/app/stationsfinder/sx-marker-13.png",
                       "/common/img/app/stationsfinder/sx-marker-14.png",
                       "/common/img/app/stationsfinder/sx-marker-15.png",
                       "/common/img/app/stationsfinder/sx-marker-16.png",
                       "/common/img/app/stationsfinder/sx-marker-17.png",
                       "/common/img/app/stationsfinder/sx-marker-18.png",
                       "/common/img/app/stationsfinder/sx-marker-19.png",
                       "/common/img/app/stationsfinder/sx-marker-20.png",
                       "/common/img/app/stationsfinder/sx-marker-21.png",
                       "/common/img/app/stationsfinder/sx-marker-22.png",
                       "/common/img/app/stationsfinder/sx-marker-23.png",
                       "/common/img/app/stationsfinder/sx-marker-24.png",
                       "/common/img/app/stationsfinder/sx-marker-25.png",
                       "/common/img/app/stationsfinder/sx-marker-26.png",
                       "/common/img/app/stationsfinder/sx-marker-27.png",
                       "/common/img/app/stationsfinder/sx-marker-28.png",
                       "/common/img/app/stationsfinder/sx-marker-29.png",
                       "/common/img/app/stationsfinder/sx-marker-30.png",
                       "/common/img/app/stationsfinder/sx-marker.png",
                       "/common/img/app/stationsfinder/icon-position.gif"
                      );


function insertNearest() {
    for (i in nearestArray) {
        if (nearestArray[i] != null) map.addOverlay(nearestArray[i]);
    }
}

// generates html code to represent the opening times of a station
//
function getOpening(open) {
    var text = "";
    text = "<table><colgroup><col width='25'><col width='75'></colgroup>" +
           " <tr><td><b> Mo. - Fr.: </b></td><td>" +open[0]+ "</td></tr>" +
           "<tr><td><b> Sa.: </b></td><td>" +open[1]+ "</td></tr>" +
           "<tr><td><b> So.: </b></td><td>" +open[2]+ "</td></tr>" +
           "<tr><td><b> Feiertag: </b></td><td>" +open[3]+ "</td></tr></table>";
    return text;
}

// adds a marker for the search query location (poi) onto the map
//
function setMarkerPoi (lat, lng) {
    var sx_icon = new GIcon(G_DEFAULT_ICON);
    //this is the small flag icon
    sx_icon.image = icons[31];
    sx_icon.iconSize = new GSize(30, 20);
    sx_icon.shadow = "";
    sx_icon.iconAnchor = new GPoint(11, 21);
    markerOptions = { icon:sx_icon };
    var marker = new GMarker(new GLatLng(lat, lng), markerOptions);
    map.addOverlay(marker);
}    
    
// center map to one of the 10 nearest stations selected by the user
//
function centerToStation (lat, lng) {
    //var stnr = parseInt(id);
    map.setCenter(new GLatLng(lat, lng), 13);
    //nearestArray[stnr].bindInfoWindowTabsHtml("Old Text");
}

// adjust map's viewport so that all 10 nearest stations are shown
//
function centerMap() {
    var center_lat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2.0;
    center_lat = center_lat * 1.0001; //move area a bit to the north
    var center_lng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2.0;
    if(bounds.getNorthEast().lng() < bounds.getSouthWest().lng()){
        center_lng += 180;
    }
    var center = new GLatLng(center_lat, center_lng);
    map.setCenter(center, map.getBoundsZoomLevel(bounds));
}


// funtion to create a marker and set it onto the map
//
function createMarker (lat, lng, statnr, name, str, pcode, town, tel, open, distance, duration_hours, duration_mins, isnearest, pos, reslink) {
    if (true) {
        var open_times_html = "<table>"+open+"</table>";
    }
    else {
        var open_times_html = getOpening(open);
    }
    var point = new GLatLng(lat, lng);
    var distance_html = "";
    if (isnearest == true) {
        bounds.extend(point);
        var sx_icon = new GIcon(G_DEFAULT_ICON);
        sx_icon.image = icons[pos-1];
        sx_icon.iconSize = new GSize(29, 38);
        sx_icon.shadow = "";
        markerOptions = { icon:sx_icon, title: "" +name+ " (" +distance_string+ ": " +parseInt(distance)+ " km)" };
        var marker = new GMarker(new GLatLng(lat, lng), markerOptions);
        if (distance != '') {
            if (!(duration_hours == 0 && duration_mins == 0)) {
                distance_html = distance_string+ ": " + distance + " km";
            }
            else {
                distance_html = distance_string+ ": " + distance + " km (" +airline_string+ ")";
            }
        }
        else {
            distance_html = distance_string+ ": N\/A";
        }
    }
    else {
        var sx_icon = new GIcon(G_DEFAULT_ICON);
        sx_icon.image = icons[20];
        sx_icon.iconSize = new GSize(37, 29);
        sx_icon.shadow = "";
        markerOptions = { icon:sx_icon, title: name };
        var marker = new GMarker(new GLatLng(lat, lng), markerOptions);
    }
    if (unique) {
        linkto   = ref+"?start=" +address+ "&end=" +lat+ ", " +lng+ "&adr=" +str+ ", " +pcode+ " " +town+ "&dir=to&referer=1";
        linkfrom = ref+"?start=" +lat+ ", " +lng+ "&end=" +address+ "&adr=" +str+ ", " +pcode+ " " +town+ "&dir=from&referer=1";
    }
    else {
        linkto   = ref+"?start=" +address+ "&poi_geo=" +poi_lat+ ", " +poi_lng+ "&end=" +lat+ ", " +lng+ "&adr=" +str+ ", " +pcode+ " " +town+ "&dir=to&referer=1";
        linkfrom = ref+"?start=" +lat+ ", " +lng+ "&poi_geo=" +poi_lat+ ", " +poi_lng+ "&end=" +address+ "&adr=" +str+ ", " +pcode+ " " +town+ "&dir=from&referer=1";
    }
    if (!(duration_hours == 0 && duration_mins == 0)) {
        marker.bindInfoWindowTabsHtml([ new GInfoWindowTab(address_string, '<div style="width: 270px;"></div><b>' +name+ '</b><br /><br />' +str+ '<br>' +pcode+ ' '
                                            +town+ '<p>Tel: ' +tel+ '<p><b>' +distance_html+ '</b><p><a href=\"' +reslink+ '\">'+book_string),
                                        new GInfoWindowTab(opening_string, '<div style="width: 270px height: 200px;"></div><b>' + open_times_html),
                                        new GInfoWindowTab(routing_string, '<br><p><a href=\"' +linkto+ '\" target=\"_blank\">' +routeto_string+ '<p><p><a href=\"' +linkfrom+ '\" target=\"_blank\">' +routefrom_string )]);
    }
    else {
        marker.bindInfoWindowTabsHtml([ new GInfoWindowTab(address_string, '<div style="width: 270px;"></div><b>' +name+ '</b><br /><br />' +str+ '<br>' +pcode+ ' '
                                            +town+ '<p>Tel: ' +tel+ '<p><b>' +distance_html+ '</b><p><a href=\"' +reslink+ '\">'+book_string),
                                        new GInfoWindowTab(opening_string, '<div style="width: 270px height: 200px;"></div><b>' + open_times_html)]);
    }
    if (isnearest == true) {
        marker.value = statnr;
        nearestArray[statnr] = marker;
    }
    return marker;
}

