﻿/**
* Hirschsiegel jQuery GoogleMap
*
*/

(function($) {

    // jQuery extention für googlemap
    $.fn.hirschGoogleMap = function(options) {
        return this.each(function() {
            if (this) {
                HirschGoogleMap(this, options);
            }
        });
    }

    // Konstruktor
    function HirschGoogleMap(container, options) {
        return this instanceof HirschGoogleMap
            ? this.init(container, options)
            : new HirschGoogleMap(container, options);
    }

    $.extend(HirschGoogleMap.prototype, {

        markerImage: "http://www.google.com/mapfiles/marker.png",
        markerHtml: null,
        markerHtmlAnchor: [9, 2],
        markerSize: [20, 34],
        markerAnchor: [9, 34],
        address: null,
        zoom: 13,
        container: null,

        map: null,
        marker: null,
        geocoder: null,
        markerIcon: null,

        init: function(container, options) {
            if (GBrowserIsCompatible() && options.address) {
                this.container = container;

                this.markerImage = options.markerImage || this.markerImage;
                this.markerHtml = options.markerHtml || this.markerHtml;
                this.markerSize = options.markerSize || this.markerSize;
                this.markerAnchor = options.markerAnchor || this.markerAnchor;
                this.markerHtmlAnchor = options.markerHtmlAnchor || this.markerHtmlAnchor;
                this.zoom = options.zoom || this.zoom;
                this.address = options.address;

                this.map = new GMap2(container);
                this.map.addControl(new GMapTypeControl());
                this.map.addControl(new GSmallMapControl());

                this.geocoder = new GClientGeocoder();

                this.initIcon();

                var instance = this;
                this.geocoder.getLatLng(this.address, function(point) {
                    instance.setMarkerCallback.apply(this, [point, instance]);
                });
            }
        },

        initIcon: function() {
            this.markerIcon = new GIcon();
            this.markerIcon.iconSize = new GSize(this.markerSize[0],this.markerSize[1]);
            this.markerIcon.image = this.markerImage;
            this.markerIcon.iconAnchor = new GPoint(this.markerAnchor[0],this.markerAnchor[1]);
            if (this.markerHtml && this.markerHtmlAnchor) {
                this.markerIcon.infoWindowAnchor = new GPoint(this.markerHtmlAnchor[0],this.markerHtmlAnchor[1]);
            }
        },

        setMarkerCallback: function(point, instance) {
            if (point) {
                var map = instance.map;
                var marker = instance.createMarker(point, instance.markerHtml);

                map.setCenter(point, instance.zoom);
                map.addOverlay(marker);
            }
        },

        createMarker: function(point, info) {

            var marker = new GMarker(point, { icon: this.markerIcon });
            if (this.markerHtml && this.markerHtmlAnchor) {
                var markerHtml = this.markerHtml;
                GEvent.addListener(marker, "click", function() {
                    marker.openInfoWindowHtml(markerHtml);
                }
                );
            }
            return marker;
        }
    }
);
})(jQuery);

