﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("CashStoreLocator.Web.js.GoogleMap");

CashStoreLocator.Web.js.GoogleMap = function() {

    this._map = null;
    this._bounds = null;
    this._windowUnloadMapHandler = null;    
    
    CashStoreLocator.Web.js.GoogleMap.initializeBase(this);

}

CashStoreLocator.Web.js.GoogleMap.prototype = {

    get_map: function() {
        return this._map;
    },

    set_map: function(value) {
        this._map = value;
    },

    get_bounds: function() {
        return this._bounds;
    },

    set_bounds: function(value) {
        this._bounds = value;
    },

    /*
    setMapCenter: This function takes a latitude/longitude for the searched for location.
    It also takes in a text value that it assigns to an Infowindow.
    */
    setMapCenter: function(latitude, longitude, text) {

        //this checkResize method should be called after a google maps div is hidden/shown
        this._map.checkResize();

        // Create our "tiny" marker icon
        var tinyIcon = new GIcon();
        tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
        tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
        tinyIcon.iconSize = new GSize(12, 20);
        tinyIcon.shadowSize = new GSize(22, 20);
        tinyIcon.iconAnchor = new GPoint(6, 20);
        tinyIcon.infoWindowAnchor = new GPoint(5, 1);

        this._bounds = new GLatLngBounds();

        var point = new GLatLng(latitude, longitude);

        this._map.setCenter(point, 13);

        var marker = new GMarker(point, { icon: tinyIcon });

        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(text);
        });

        this._map.addOverlay(marker);

        this._bounds.extend(point);
    },

    /*
    setPointOnMap: This function adds a marker to the map based on the lat/long passed into it.
    It takes in text for the Infowindow
    It takes in an extra elementName for additional triggering opening of the 
    InfoWindow (seperate from the normal Marker)
                       
    */
    setPointOnMap: function(latitude, longitude, text, elementName) {

        var element = $get(elementName);

        var point = new GLatLng(latitude, longitude);

        var marker = new GMarker(point);

        GEvent.addDomListener(element, "click", function() {
            marker.openInfoWindowHtml(text);
        });

        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(text);
        });

        this._map.addOverlay(marker);

        this._bounds.extend(point);
    },

    /*
    setBounds: This function uses the built-in bounds to calculate the 
    proper zoom level (and center) for the Google Map.              
    */
    setBounds: function() {
        if (this._map != null) {
            this._map.setCenter(this._bounds.getCenter(), this._map.getBoundsZoomLevel(this._bounds));
        }
    },

    /*
    This function initializes the Google Map and adds the "unload" handler for
    removing the Google Map
    */

    initialize: function() {

        CashStoreLocator.Web.js.GoogleMap.callBaseMethod(this, 'initialize');

        this.loadMap();

        this._windowUnloadMapHandler = Function.createDelegate(this, this.unloadMap);
        $addHandler(window, 'unload', this._windowUnloadMapHandler);
    },

    loadMap: function(e, context) {

        if (GBrowserIsCompatible()) {
            var mapElement = document.getElementById("map_canvas");
            if (mapElement != null) {
                this._map = new GMap2(mapElement);
                this._map.addControl(new GLargeMapControl());
                this._map.addControl(new GMapTypeControl());
                this._map.enableScrollWheelZoom();
            }
        }
    },

    unloadMap: function() {

        GUnload();
        this.dispose();
    },

    dispose: function() {

        if (typeof (this._windowUnloadMapHandler) !== 'undefined') {
            $removeHandler(window, 'unload', this._windowUnloadMapHandler);
            delete this._windowUnloadMapHandler;
        }

        CashStoreLocator.Web.js.GoogleMap.callBaseMethod(this, 'dispose');
    }
}

CashStoreLocator.Web.js.GoogleMap.registerClass('CashStoreLocator.Web.js.GoogleMap', Sys.Component);

//Notify the page ScriptManager that we're done loading
if (typeof (Sys) !== 'undefined') {
    Sys.Application.notifyScriptLoaded();
}
