﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="../js-docs/GMAPJSHelper_Release.js" />

Type.registerNamespace("NextDigital.CityOfBallarat.Web.scripts");

NextDigital.CityOfBallarat.Web.scripts.GMapWrapper = function(element) {
    NextDigital.CityOfBallarat.Web.scripts.GMapWrapper.initializeBase(this, [element]);
    this._lon = 0;
    this._lat = 0;
    this._zoom = 14;
    this._title = null;
    this._desc = null;
    this._pinImage = null;
    this._map = null;
}

NextDigital.CityOfBallarat.Web.scripts.GMapWrapper.prototype = {
    initialize: function() {
        NextDigital.CityOfBallarat.Web.scripts.GMapWrapper.callBaseMethod(this, 'initialize');

        // Add custom initialization here
        this._map = new GMap2(this.get_element());
        Sys.Application.add_unload(GUnload);
        this.showMap();
    },
    dispose: function() {
        //Add custom dispose actions here
        NextDigital.CityOfBallarat.Web.scripts.GMapWrapper.callBaseMethod(this, 'dispose');
        Sys.Application.remove_unload(GUnload);
    },
    set_lon: function(val) {
        if (this._lon !== val) {
            this._lon = val;
            Sys.Application.raisePropertyChanged("lon");
        }
    },
    get_lon: function() {
        return this._lon;
    },
    set_lat: function(val) {
        if (this._lat !== val) {
            this._lat = val;
            Sys.Application.raisePropertyChanged("lat");
        }
    },
    get_lat: function() {
        return this._lat;
    },
    set_zoom: function(val) {
        if (this._zoom !== val) {
            this._zoom = val;
            Sys.Application.raisePropertyChanged('zoom');
        }
    },
    get_zoom: function() {
        return this._zoom;
    },
    set_title: function(val) {
        if (this._title !== val) {
            this._title = val;
            Sys.Application.raisePropertyChanged('title');
        }
    },
    get_title: function() {
        return this._title;
    },
    set_desc: function(val) {
        if (this._desc !== val) {
            this._desc = val;
            Sys.Application.raisePropertyChanged('desc');
        }
    },
    get_desc: function() {
        return this._desc;
    },
    _createPoint: function(lon, lat) {
        /// <summary> Creates a new point for the map </summary>
        /// <returns type="GLatLng" />
        return new GLatLng(lat, lon);
    },
    _createPin: function(lon, lat) {
        /// <summary> Creates a pin to go onto the map </summary>
        /// <returns type="GMarker" />
        var point = this._createPoint(lon, lat);
        var marker = new GMarker(point);
        GEvent.addListener(marker, "click", Function.createDelegate(this, function() {
            marker.openInfoWindowHtml(this._createPopup(), "", { maxWidth: 100 });
        }));
        return marker;
    },
    _createPopup: function() {
        return "<div class='gmapPopup'><h2>" + this.get_title() + "</h2><p>" + this.get_desc() + "</p></div>";
    },
    showMap: function() {
        var map = new GMap2(this.get_element());

        map.setCenter(this._createPoint(this.get_lon(), this.get_lat()), this.get_zoom());
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        var pin = this._createPin(this.get_lon(), this.get_lat());
        map.addOverlay(pin);

        this._map = map;
    }
}
NextDigital.CityOfBallarat.Web.scripts.GMapWrapper.registerClass('NextDigital.CityOfBallarat.Web.scripts.GMapWrapper', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
