﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("AntennaWeb");

AntennaWeb.SimpleMap = function(element) {
    AntennaWeb.SimpleMap.initializeBase(this, [element]);

    this._latitude = 0;
    this._longitude = 0;
    this._zoomLevel = 15;
    this._imageRoot = '';
    this._northArrow = '';

    this.map = null;
    this.isMoved = false;
    this.btnSubmit = null;

    this.smallMapControl = null;
    this.mapTypeControl = null;
}

AntennaWeb.SimpleMap.prototype = {
    initialize: function() {
        AntennaWeb.SimpleMap.callBaseMethod(this, 'initialize');

        var location = new GLatLng(this._latitude, this._longitude);

        this.map = new GMap2(document.getElementById("map"));
        this.map.setCenter(location, this._zoomLevel);
        this.map.disableDoubleClickZoom();

        this.smallMapControl = new GSmallMapControl();
        this.map.addControl(this.smallMapControl);

        this.mapTypeControl = new GMapTypeControl();
        this.map.addControl(this.mapTypeControl);

        var homeIcon = this.getHomeIcon();
        this.centerMarker = new GMarker(location, { draggable: true, title: this._address, icon: homeIcon });
        this.centerMarker.enableDragging();

        var dragEndDelegate = Function.createDelegate(this, this.onDragEnd);
        GEvent.addListener(this.centerMarker, "dragend", dragEndDelegate);

        this.map.addOverlay(this.centerMarker);

        var onDoubleClickDelegate = Function.createDelegate(this, this.onDoubleClick);
        GEvent.addListener(this.map, "dblclick", onDoubleClickDelegate);

        var onZoomEndDelegate = Function.createDelegate(this, this.onZoomEnd);
        GEvent.addListener(this.map, "zoomend", onZoomEndDelegate);

        var size = this.map.getSize();
        var northIcon = this.getNorthIcon();
        var newLocation = this.map.fromContainerPixelToLatLng(new GPoint(32, size.height - 96));
        markerOptions = { icon: northIcon };
        this.northMarker = new GMarker(newLocation, markerOptions);
        this.map.addOverlay(this.northMarker);

        this.isMoved = false;
    },

    dispose: function() {

        GEvent.clearListeners(this.centerMarker, "dragend");
        GEvent.clearListeners(this.map, "dblclick");

        AntennaWeb.SimpleMap.callBaseMethod(this, 'dispose');
    },

    onSubmitClick: function(obj, args) {

        var myPlace = new Object();
        myPlace.Latitude = this._latitude;
        myPlace.Longitude = this._longitude;
        if (this.isMoved == true)
            myPlace.Accuracy = 10;
        else
            myPlace.Accuracy = -1;

        var jsonPlace = JSON.encode(myPlace);

        __doPostBack('__PAGE', jsonPlace);

        return false;
    },

    drawMap: function() {
        var location = new GLatLng(this._latitude, this._longitude);

        this.map.setCenter(location);

        this.centerMarker.setLatLng(location);
    },

    onDragEnd: function(latlng) {
        this._latitude = latlng.lat();
        this._longitude = latlng.lng();
        this.isMoved = true;

        this.drawMap();

        this.setNorthArrow();

        this.raise_onChangeLocation();

        if (this.onDragEndDerived != null) {
            this.onDragEndDerived(latlng);
        }
    },

    onDoubleClick: function(overlay, latlng) {
        this._latitude = latlng.lat();
        this._longitude = latlng.lng();
        this.isMoved = true;

        this.drawMap();

        this.setNorthArrow();

        this.raise_onChangeLocation();

        if (this.onDoubleClickDerived != null) {
            this.onDoubleClickDerived(overlay, latlng);
        }
    },

    onZoomEnd: function(oldLevel, newLevel) {

        this._zoomLevel = newLevel;

        if (this.onZoomEndDerived != null) {
            this.onZoomEndDerived(oldLevel, newLevel);
        }

        this.setNorthArrow();
    },

    setNorthArrow: function() {
        var size = this.map.getSize();
        var newLocation = this.map.fromContainerPixelToLatLng(new GPoint(32, size.height - 64));
        this.northMarker.setLatLng(newLocation);
    },

    get_btnSubmit: function() {
        return this.btnSubmit;
    },

    set_btnSubmit: function(value) {
        this.btnSubmit = value;

        var onSubmitClickDelegate = Function.createDelegate(this, this.onSubmitClick);

        this.btnSubmit.onclick = onSubmitClickDelegate;
    },

    get_latitude: function() {
        return this._latitude;
    },

    set_latitude: function(value) {
        this._latitude = value;
    },

    get_longitude: function() {
        return this._longitude;
    },

    set_longitude: function(value) {
        this._longitude = value;
    },

    get_zoomLevel: function() {
        return this._zoomLevel;
    },

    set_zoomLevel: function(value) {
        if (value > 0) {
            this._zoomLevel = value;
        }
    },

    get_northArrow: function() {
        return this._northArrow;
    },

    set_northArrow: function(value) {
        this._northArrow = value;
    },

    get_imageRoot: function() {
        return this._imageRoot;
    },

    set_imageRoot: function(value) {
        this._imageRoot = value;
    },

    add_onChangeLocation: function(handler) {
        this.get_events().addHandler("changeLocation", handler);
    },

    remove_onChangeLocation: function(handler) {
        this.get_events().removeHandler("changeLocation", handler);
    },

    raise_onChangeLocation: function() {
        var handler = this.get_events().getHandler("changeLocation");

        if (handler != null) {

            var args = {
                "latitude": this._latitude,
                "longitude": this._longitude,
                "zoom": this._zoomLevel
            };

            handler(this, args);
        }
    },

    getNorthIcon: function() {

        var northIcon = new GIcon();
        northIcon.image = this._northArrow + '?lat=' + this._latitude + '&lon=' + this._longitude;
        northIcon.shadow = this._northArrow + '?lat=' + this._latitude + '&lon=' + this._longitude;
        northIcon.iconSize = new GSize(64, 64);
        northIcon.shadowSize = new GSize(64, 64);
        northIcon.iconAnchor = new GPoint(16, 16);

        return northIcon;
    },

    getHomeIcon: function() {

        var homeIcon = new GIcon();
        homeIcon.image = this._imageRoot + 'image.png';
        homeIcon.shadow = this._imageRoot + 'shadow.png';
        homeIcon.iconSize = new GSize(32, 37);
        homeIcon.shadowSize = new GSize(51, 37);
        homeIcon.iconAnchor = new GPoint(16, 37);
        homeIcon.infoWindowAnchor = new GPoint(16, 0);
        homeIcon.printImage = this._imageRoot + 'printImage.gif';
        homeIcon.mozPrintImage = this._imageRoot + 'mozPrintImage.gif';
        homeIcon.printShadow = this._imageRoot + 'printShadow.gif';
        homeIcon.transparent = this._imageRoot + 'transparent.png';
        homeIcon.imageMap = [29, 0, 30, 1, 31, 2, 31, 3, 31, 4, 31, 5, 31, 6, 31, 7, 31, 8, 31, 9, 31, 10, 31, 11, 31, 12, 31, 13, 31, 14, 31, 15, 31, 16, 31, 17, 31, 18, 31, 19, 31, 20, 31, 21, 31, 22, 31, 23, 31, 24, 31, 25, 31, 26, 31, 27, 31, 28, 31, 29, 30, 30, 29, 31, 23, 32, 22, 33, 21, 34, 20, 35, 19, 36, 12, 36, 11, 35, 10, 34, 9, 33, 8, 32, 2, 31, 1, 30, 0, 29, 0, 28, 0, 27, 0, 26, 0, 25, 0, 24, 0, 23, 0, 22, 0, 21, 0, 20, 0, 19, 0, 18, 0, 17, 0, 16, 0, 15, 0, 14, 0, 13, 0, 12, 0, 11, 0, 10, 0, 9, 0, 8, 0, 7, 0, 6, 0, 5, 0, 4, 0, 3, 0, 2, 1, 1, 2, 0];

        return homeIcon;
    }
}

AntennaWeb.SimpleMap.registerClass('AntennaWeb.SimpleMap', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
