﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("SpatialTest");

SpatialTest.ContextMenu = function(map) {
    this.map = map;    
    this.initialize();
}

SpatialTest.ContextMenu.prototype = {
    initialize: function() {

        this.contextmenu = document.createElement("div");
        this.contextmenu.style.display = "none";
        //CSS class name of the menu
        this.contextmenu.className = "contextmenu";
        this.ul_container = document.createElement("ul");
        this.ul_container.id = "context_menu_ul";
        this.contextmenu.appendChild(this.ul_container);
        this.initLink();
        this.map.getContainer().appendChild(this.contextmenu);

        //Event listeners that will interact with our context menu
        this._listener1 = GEvent.addListener(this.map, "singlerightclick", Function.createDelegate(this, this._onSingleRightClick));
        this._listener2 = GEvent.addListener(this.map, "move", Function.createDelegate(this, this._onMapMove));
        this._listener3 = GEvent.addListener(this.map, "click", Function.createDelegate(this, this._onMapClick));
    },
    dispose: function() {
        GEvent.removeListener(this._listener1);
        GEvent.removeListener(this._listener2);
        GEvent.removeListener(this._listener3);
    },
    initLink: function() {
        var that = this;
        var a_link = document.createElement("li");
        a_link.innerHTML = '<a href="javascript:void(0);">Naujas objektas</a>';
        GEvent.addDomListener(a_link, 'click', function() {
            var point = that.map.fromContainerPixelToLatLng(that.clickedPixel);
            //AddItem(null, point);
            that.contextmenu.style.display = "none";
        });
        this.ul_container.appendChild(a_link);

        a_link = document.createElement("li");
        a_link.innerHTML = '<a href="javascript:void(0);">Naujas vaizdas</a>';
        GEvent.addDomListener(a_link, 'click', function() {
            //AddView();
            that.contextmenu.style.display = "none";
        });
        this.ul_container.appendChild(a_link);

    },
    _onSingleRightClick: function(pixel, tile) {
        this.clickedPixel = pixel;
        var x = pixel.x;
        var y = pixel.y;
        //Prevents the menu to go out of the map margins, in this case the expected menu size is 150x110
        if (x > this.map.getSize().width - 160) { x = this.map.getSize().width - 160 }
        if (y > this.map.getSize().height - 120) { y = this.map.getSize().height - 120 }
        var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x, y));
        pos.apply(this.contextmenu);
        this.contextmenu.style.display = "";
    },
    _onMapMove: function() {
        this.contextmenu.style.display = "none";
    },
    _onMapClick: function() {
        this.contextmenu.style.display = "none";
    }
}
SpatialTest.ContextMenu.registerClass('SpatialTest.ContextMenu');

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();



/*
ContextMenu.prototype.

ContextMenu.prototype.bind = function(method) {
    var self = this;
    var opt_args = [].slice.call(arguments, 1);
    return function() {
        var args = opt_args.concat([].slice.call(arguments));
        return method.apply(self, args);
    }
}

ContextMenu.prototype.*/
