var GMap = {
    initRoutePlaner: function (id, x, y, z) {
        if(!GBrowserIsCompatible()) {
            throw 'Your browser is not compatible with this mapping tool.';
        }

       /**
        * Init Routevars
        */
        this.route_id        = id;
        this.route_x         = x;
        this.route_y         = y;
        this.route_zoom      = z;
        this.route_el        = $(id);
        this.route           = null;
        this.controlForm     = null;
        this.route_map       = new GMap2(this.route_el);
        this.route_map.setCenter(new GLatLng(x,y), z);
        this.route_map.enableContinuousZoom();
        this.route_map.addControl(new GLargeMapControl());
        this.route_map.addControl(new GMapTypeControl());
    },

   /**
    * Set Maker by Adress
    *
    * @param string adress
    */
    setMarkerByAdress: function (adress, tpl) {
            GMap.lastMsg = tpl;
        var Geo  = new GClientGeocoder();
            Geo.getLatLng(adress, function (point) {
                if(!point) {
                    alert("\'" + pos_x + "\' not found");
                } else {
                    GMap.setMarker(point, GMap.lastMsg);
                }
            });
    },

   /**
    * Set Marker
    *
    */
    setMarker: function (latLng, tpl) {
        var rect   = new GLatLngBounds();
        var marker = new GMarker(latLng);

        if(tpl) {
            marker.bindInfoWindowHtml(tpl);
        }

       // Set Rect 
        this.route_map.addOverlay(marker);
        rect.extend(latLng);
        this.route_map.setCenter(rect.getCenter(), this.route_map.getBoundsZoomLevel(rect));
    },

   /**
    * Enable routing
    *
    * @param string elementId
    */
    enableRouting: function (elementId, callback) {
        this.route_container = $(elementId);
        this.controlForm     = callback;
        this.route           = new GDirections(this.route_map, this.route_container);
    },

   /**
    * Calculate Route
    *
    * @param string from
    * @param string to
    */
    calcRoute: function (from, to) {
        if(from) {
           // Calculate route
            this.route_container.style.display = '';
            this.route.load('from: ' + from + ' to: ' + to);
            GEvent.addListener(this.route, 'addoverlay', function (obj) {
                GMap.route_container.style.display = 'block';
            });
            this.errorHandler = GEvent.addListener(this.route, 'error', this.routeError);
        } else {
            this.routeError(G_GEO_MISSING_QUERY);
        }
    },

    _error: [
        "Die eingegebene Adresse konnte nich gefunden werden!",
        "Eine Routenberechnung ist zur Zeit nicht möglich!",
        "Es wurde keine Adresse angegeben!",
        "Der API Key ist nicht korrekt!",
        "Die Anfrage konnte nicht bearbeitet werden!",
        "Es ist ein unbekannter Fehler aufgetreten!"
    ],

   /**
    * RouteErrors
    */
    routeError: function(status) {
        if(!status) {
            status = GMap.route.getStatus().code;
        }

        if (status == G_GEO_UNKNOWN_ADDRESS 
        ||  status == G_GEO_UNAVAILABLE_ADDRESS 
        ||  status == G_GEO_UNKNOWN_DIRECTIONS)
            alert(GMap._error[0]);
        else if (status == G_GEO_SERVER_ERROR)
            alert(GMap._error[1]);
        else if (status == G_GEO_MISSING_QUERY
             ||  status == G_GEO_MISSING_ADDRESS)
            alert(GMap._error[2]);
        else if (status == G_GEO_BAD_KEY)
            alert(GMap._error[3]);
        else if (status == G_GEO_BAD_REQUEST)
            alert(GMap._error[4]);
        else
            alert(GMap._error[5]);

        if(GMap.errorHandler) {
            GEvent.removeListener(GMap.errorHandler);
            GMap.errorHandler = null;
        }
    }
};

