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 hello_icon = new GIcon(G_DEFAULT_ICON);
hello_icon.image = "/images/hr-marker.png";
hello_icon.iconAnchor = new GPoint(20, 36);
hello_icon.iconSize = new GSize(40, 36);
hello_icon.shadow = "/images/hr-marker-shadow.png";
hello_icon.shadowSize = new GSize(62,36);
hello_icon.infoWindowAnchor = new GPoint(20, 36);

var center_icon = new GIcon(G_DEFAULT_ICON);
center_icon.image = "/images/centru.png";
center_icon.iconSize = new GSize(19, 41);

var hotel_icon = new GIcon(G_DEFAULT_ICON);
hotel_icon.image = "/images/hotel.png";
hotel_icon.iconAnchor = new GPoint(12, 43);
hotel_icon.iconSize = new GSize(32, 37);
hotel_icon.infoWindowAnchor = new GPoint(12, 43);


var apart_icon = new GIcon(G_DEFAULT_ICON);
apart_icon.image = "/images/apartment.png";
apart_icon.iconAnchor = new GPoint(20, 36);
apart_icon.iconSize = new GSize(32, 37);
apart_icon.infoWindowAnchor = new GPoint(20, 36);

var pens_icon = new GIcon(G_DEFAULT_ICON);
pens_icon.image = "/images/pension.png";
pens_icon.iconAnchor = new GPoint(24, 31);
pens_icon.iconSize = new GSize(32, 37);
pens_icon.infoWindowAnchor = new GPoint(24, 31);

var hotel_icon2 = new GIcon(G_DEFAULT_ICON);
hotel_icon2.image = "/images/hotel.png";
hotel_icon2.shadow = "";
hotel_icon2.iconSize = new GSize(32, 37);
hotel_icon2.iconAnchor = new GPoint(10, 30);
hotel_icon2.infoWindowAnchor = new GPoint(10, 30);

var apart_icon2 = new GIcon(G_DEFAULT_ICON);
apart_icon2.image = "/images/apartment.png";
apart_icon2.shadow = "";
apart_icon2.iconSize = new GSize(32, 37);
apart_icon2.iconAnchor = new GPoint(15, 26);
apart_icon2.infoWindowAnchor = new GPoint(15, 26);

var pens_icon2 = new GIcon(G_DEFAULT_ICON);
pens_icon2.image = "/images/pension.png";
pens_icon2.shadow = "";
pens_icon2.iconSize = new GSize(32, 37);
pens_icon2.iconAnchor = new GPoint(18, 20);
pens_icon2.infoWindowAnchor = new GPoint(18, 20);

var icons = {"hello":hello_icon, "center":center_icon, 
		"unit_1":hotel_icon2, 
		"unit_2":pens_icon2, 
		"unit_3":apart_icon2,
		"unit_4":pens_icon2,
		"unit_5":pens_icon2,
		"unit_6":pens_icon2,
		"unit_7":pens_icon2
		};


// read information of all other stations from the xml-file and run the createMarker-function for each station
//
function insertOtherStats() {
/*    var url = "/fileadmin/files/modules/seo/StatInfo_" +language+ ".xml";
    GDownloadUrl(url, function (doc) {
    var xmlDoc = GXml.parse(doc);
    var stations = xmlDoc.documentElement.getElementsByTagName("station");
    for (var i = 0; i < stations.length; i++) {
        // obtain the attributes of each marker
        var nr      = stations[i].getAttribute("nr");
        var name    = stations[i].getAttribute("name");
        var town    = stations[i].getAttribute("town");
        var pcode   = stations[i].getAttribute("pcode");
        var street  = stations[i].getAttribute("street");
        var country = stations[i].getAttribute("country");
        var tel     = stations[i].getAttribute("tel");
        var open    = stations[i].getAttribute("open");
        var link    = stations[i].getAttribute("link");
        var lat     = parseFloat(stations[i].getAttribute("lat"));
        var lng     = parseFloat(stations[i].getAttribute("lng"));
        var marker  = createMarker(lat, lng, nr, name, street, pcode, town, tel, open, "", "", "", false, "", link);
        if (nearestArray[nr] == null) gmarkersA.push(marker);
    }

    //mm.addMarkers(gmarkersA, 8, 17);
    });
	*/
}


function insertTopMarkers(mm) {
    mm.addMarkers(nearestArray, 1, 6);
	mm.refresh();
}

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


// show all HR stations on the map
//
function showAllStats() {
    if (document.getElementById("showall").checked == true){
        mm.addMarkers(gmarkersA, minzoom, 17);
        mm.refresh();
    }
    else {
        mm.clearMarkers();
        mm.refresh();
    }
}

// adds a marker for the search query location (poi) onto the map
//
function setMarkerPoi (lat, lng, mp) {
    markerOptions = { icon:center_icon };
    var marker = new GMarker(new GLatLng(lat, lng), markerOptions);
    if(mp) mp.addOverlay(marker);
	else map.addOverlay(marker);
}    


// adjust map's viewport so that all 10 nearest stations are shown
//
function centerMap(mp) {
    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);

	if(mp) mp.setCenter(center, mp.getBoundsZoomLevel(bounds));
    else map.setCenter(center, map.getBoundsZoomLevel(bounds)==0?6:map.getBoundsZoomLevel(bounds));
}

function createSimpleMarker (lat, lng, id, title, content, iconname) {
	var point = new GLatLng(lat, lng);
	bounds.extend(point);
	markerOptions = { icon:icons[iconname], title: title };
	var marker = new GMarker(new GLatLng(lat, lng), markerOptions);

    if(content && content.length>0) marker.bindInfoWindowHtml(content);
    marker.value = id;
    nearestArray[id] = marker;	
    return marker;
}


// 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, iconname, reslink) {
    var open_times_html = "<table>"+open+"</table>";

	var point = new GLatLng(lat, lng);
    var distance_html = "";
    if (isnearest == true) {
        bounds.extend(point);
        markerOptions = { icon:icons[iconname], title: name };
        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 {
        markerOptions = { icon:center_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.bindInfoWindowHtml(open);
//		marker.bindInfoWindowTabsHtml([ new GInfoWindowTab(name, open),
//		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;
}

