(function($) {
    $.GoogleMapObjectDefaults = {        
        zoomLevel: 13,
        address: '2333 El Jobean Road Port Charlotte, FL 33948',
        clickElement: '#google-button',
        directionAddressElement: '#google-address'
    };

    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));
                    this._map.addControl(new GSmallMapControl());
                    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 map = this._map;
				var tooltipinfo = "<h2>Stevefest</h2><br />" + address;

                this._geocoder.getLatLng(address, function(point) {
                    if (!point) { alert(address + " not found"); }
                    else {
                        //set center on the map
                        map.setCenter(point, zoom);

                        //add the marker
			var stevefesticonsize = new GSize(55, 55);
			var stevefesticon = new GIcon(G_DEFAULT_ICON, "images/map_pic.png");
			stevefesticon.iconSize = stevefesticonsize;
                        var marker = new GMarker(point, { icon: stevefesticon });
                        map.addOverlay(marker);
                        marker.openInfoWindowHtml(tooltipinfo);
                    }
                });
            }


            //make this available to the click element
            $.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;
				gdir  = new GDirections(map, document.getElementById("directions"));
				
				gdir.load("from: " + from + " to: " + to);

                //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);
