﻿(function($) {
    $.GoogleMapObjectDefaults = {
        zoomLevel: 13,
        address: '2600 pennsylvania ave washington dc',
        clickElement: null,
        directionAddressElement: '#google-address',
        textInfo: null
    };

    function GoogleMapObject(elementId, options) {
        /* private variables */
        this._inited = false;
        this._map = null;
        this._geocoder = null;

        /* Public properties */
        this.ElementId = elementId;
        this.Settings = $.extend({}, $.GoogleMapObjectDefaults, options || '');
    }

    $.extend(GoogleMapObject.prototype, {
        init: function() {
            if (!this._inited) {
                if (GBrowserIsCompatible()) {
                    this._map = new GMap2(document.getElementById(this.ElementId));
                    var topLeft = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(330, 10));
                    this._map.addControl(new GSmallMapControl(), topLeft);
                    this._map.addControl(new GMapTypeControl());
                    this._geocoder = new GClientGeocoder();
                }

                this._inited = true;
            }
        },
        load: function() {
            //ensure existence
            this.init();
            if (this._geocoder) {
                //"this" will be in the wrong context for the callback
                var zoom = this.Settings.zoomLevel;
                var address = this.Settings.address;
                var text = this.Settings.textInfo;
                if (text == null) text = this.Settings.address;
                var map = this._map;
                var wOffset = this.Settings.widthOffset;
                var hOffset = this.Settings.heightOffset;
                var point = new GLatLng(38.760629, -9.192022);

                // Create our "tiny" marker icon
                var blueIcon = new GIcon(G_DEFAULT_ICON);
                blueIcon.image = "images/tilmarker.png";

                // Set up our GMarkerOptions object
                markerOptions = { icon: blueIcon };
                var marker = new GMarker(point, markerOptions);
                map.addOverlay(marker);

                //add the info box
                marker.openInfoWindowHtml(text);
                var xOffSet = 0.0003;
                var yOffSet = 0.001;
                //set center on the map
                
                map.setCenter(new GLatLng(point.lat() + xOffSet, point.lng() - yOffSet), zoom, G_SATELLITE_MAP);
                window.setTimeout(function() {
                    map.panTo(new GLatLng(point.lat() + xOffSet, point.lng() - yOffSet), zoom);
                }, 2000);
                window.setTimeout(function() {
                    map.panTo(new GLatLng(point.lat() + xOffSet, point.lng() - yOffSet), zoom);
                }, 4000);
                window.setTimeout(function() {
                    map.panTo(new GLatLng(point.lat() + xOffSet, point.lng() - yOffSet), zoom);
                }, 8000);
            }
            //make this available to the click element
            if (!this.Settings.textInfo == null)
                $.data($(this.Settings.clickElement)[0], 'inst', this);

            $(this.Settings.clickElement).click(function(e) {
                e.preventDefault();
                var obj = $.data(this, 'inst');
                var from = $(obj.Settings.directionAddressElement).val();
                var to = obj.Settings.address;

                //open the google window
                window.open("http://maps.google.com/maps?saddr=" + from + "&daddr=" + to, "GoogleWin", "menubar=1,resizable=1,scrollbars=1,width=750,height=500,left=10,top=10");
            });

            return this;
        }
    });

    $.extend($.fn, {
        googleMap: function(options) {
            // check if a map was already created
            var mapInst = $.data(this[0], 'googleMap');
            if (mapInst) {
                return mapInst;
            }

            //create a new map instance
            mapInst = new GoogleMapObject($(this).attr('id'), options);
            $.data(this[0], 'googleMap', mapInst);
            return mapInst;
        }
    });
})(jQuery);

