﻿/// <reference name="jquery-1.3.2.min.js"/>
/// <reference name="googleMaps-API.js"/>

this.jGoogleMaps = function (div) {
    var _I = this;

    this.map = null;
    this.geocoder = null;
    this.gdir = null;
    this.bounds = null;

    if (GBrowserIsCompatible()) {
        _I.map = new GMap2(div);

        _I.map.addControl(new GLargeMapControl());
        _I.map.addControl(new GMapTypeControl());
        _I.map.addControl(new GScaleControl());

        _I.map.enableContinuousZoom();
        _I.map.enableScrollWheelZoom();

        _I.geocoder = new GClientGeocoder();
        _I.bounds = new GLatLngBounds();

    } else {
        //alert('Google Maps kann nicht verwendet werden.');
    }

    this.AddMakerByAddress = function (address, html) {
        if (_I.geocoder) {
            _I.geocoder.getLatLng(address, function (point) {
                if (!point) {
                    //alert(address + ' wurde nicht gefunden');
                } else {
                    _I.AddMaker(point, html);
                    _I.Center();
                }
            });
        }
    }

    this.AddMarkerByLatLan = function (lat, lan, html) {
        var point = new GLatLng(lat, lan);
        _I.AddMaker(point, html);
    }

    this.AddMaker = function (point, html) {
        var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function ()
        { marker.openInfoWindowHtml(html) });
        _I.map.addOverlay(marker);
        _I.bounds.extend(point);
        //_I.Center();
    }

    this.CenterLatLanZoom = function (lat, lng, zoom) {
        _I.map.setCenter(new GLatLng(lat, lng), zoom);

    }

    this.Center = function () {
        if (_I.bounds.isEmpty() == false) {
            //_I.map.setCenter(_I.bounds.getCenter(), _I.map.getBoundsZoomLevel(_I.bounds, new GSize(533, 300)));
            _I.map.setCenter(_I.bounds.getCenter());
            _I.map.setZoom(_I.map.getBoundsZoomLevel(_I.bounds));
        }
    }

    this.SetZoom = function (zoom) {
        _I.map.setZoom(zoom);
    }

    this.Resize = function () {
        _I.map.checkResize();
    }

    this.AddRoute = function (fromAddress, toAddress, directions) {
        _I.gdir = new GDirections(_I.map, directions);
        GEvent.addListener(_I.gdir, "load", onGDirectionsLoad);
        GEvent.addListener(_I.gdir, "error", handleErrors);

        _I.gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": "de" });
    }


    this.PrintRoute = function (fromAddress, toAddress) {
        var url = "http://maps.google.de/maps?saddr={0}&daddr={1}&z=7&pw=2";
        url = url.replace("{0}", fromAddress).replace("{1}", toAddress);
        fenster = window.open(url, "_blank", "width=800,height=600,status=yes,scrollbars=yes,resizable=yes");
        fenster.focus();

    }


    //Events
    function handleErrors() {
        if (_I.gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
            alert("Start- oder auch Zieladresse konnten nicht gefunden werden. Entweder sind sie nicht bekannt, nicht eindeutig oder die Eingabe ist nicht korrekt. Bitte überprüfen Sie die Eingabe.\nError code: " + _I.gdir.getStatus().code);
        else if (_I.gdir.getStatus().code == G_GEO_SERVER_ERROR)
            alert("Die Route konnte nicht berechnet werden.\n Error code: " + _I.gdir.getStatus().code);

        else if (_I.gdir.getStatus().code == G_GEO_MISSING_QUERY)
            alert("Bitte geben Sie eine Startadresse ein.\n Error code: " + _I.gdir.getStatus().code);

        else if (_I.gdir.getStatus().code == G_GEO_BAD_KEY)
            alert("Falscher Google Maps Key. \n Error code: " + _I.gdir.getStatus().code);

        else if (_I.gdir.getStatus().code == G_GEO_BAD_REQUEST)
            alert("Die Anfrage konnte nicht geparsed werden.\n Error code: " + _I.gdir.getStatus().code);

        else alert("Unbekannter Fehler. Bitte überprüfen Sie die Eingabe.");

    }

    function onGDirectionsLoad() {
        // Use this function to access information about the latest load()
        // results.

        // e.g.
        // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
        // and yada yada yada...
    }

}
