// Statics
var routepanel;
var directions;
var map;
var geocoder;

//Public
var _address = null;
var _overlayText = null;
var _coordinates = null;
var _marker = null;
var _control = null;
var _mapswitch = false;
var _point = null;
var _icon = null;
var _myIcon = null;
var _myIconShadow = null;

function initialize(){
    map = new google.maps.Map2(document.getElementById("map")); //Mapobject
    routepanel = document.getElementById("routepanel");         //Routepanel
    directions = new google.maps.Directions(map, routepanel);   //Directionsobject
    geocoder = new google.maps.ClientGeocoder();                //Geocoder
    //if (_point != null || _point != "") {
    //    map.setCenter(
    //        new google.maps.LatLng(_point,13));
    //} else {
        geoAddress(_address);
    //}             //geocode the given Address
    
    _overlayText = document.getElementById("OverlayTextDiv").innerHTML;

    //Set the MapControl
    switch (_control) {
        case "SmallMapControl": map.addControl(new google.maps.SmallMapControl()); break;
        case "LargeMapControl": map.addControl(new google.maps.LargeMapControl()); break;
        default: map.addControl(new google.maps.SmallMapControl());
    }
    //Set the MapType ShowOption
    if (_mapswitch) { map.addControl(new google.maps.MenuMapTypeControl()); }

    //Create MarkerImage
    _icon = new google.maps.Icon();
	_icon.image = "modMaps/images/google_pin.png";
	_icon.shadow = "modMaps/images/google_shadow.png";
	_icon.iconAnchor = new google.maps.Point(12, 27);
	_icon.infoWindowAnchor = new google.maps.Point(12,0);
    
}


// Find the LatLng() from the given address
function geoAddress(address) {
	
    geocoder.getLocations(
        address,
        function(response) {
            if (response.Status.code == 200) {
                _coordinates = response.Placemark[0].Point.coordinates;
                point = new google.maps.LatLng(
                    _coordinates[1],
                    _coordinates[0]
                    );
                map.setCenter(point, 13);
                createMarker(point, true);
            }
        });
}

//Display the Marker and the given Content
function createMarker(point, showInfo) {
    _marker = new google.maps.Marker(point, _icon);
    map.addOverlay(_marker);
    if (showInfo) {
        if (_overlayText != null) {
            _marker.openInfoWindowHtml(_overlayText);
        }
    }
}

//Calculate Route (fired by Form)
function calcRoute(toAddress) {
    _marker.closeInfoWindow();
	directions.load("from: " + toAddress + " to: " + _address);
	
	 geocoder.getLocations(
        toAddress,
        function(response) {
            if (response.Status.code != 200) {
				document.getElementById("routeerror").style.display = "block";
            }else{
				document.getElementById("routeerror").style.display = "none";
			}
        });
}
