﻿function MapController(map, minZoomLevel, maxZoomLevel ) {

    this._vemap = map;
    this._maxZoomLevel = maxZoomLevel;
    this._minZoomLevel = minZoomLevel;

    this._slider = $(document).find(".MMdragcursor").draggable({
        axis: 'y',
        containment: '.MMzoomfactors',
        stop: createRef(this, this._onScroll),
        drag: createRef(this, this._onScroll)
    });


    $(document).find(".MMzoomin a").click(createRef(this, function() {
        if(this._vemap.GetZoomLevel() < this._maxZoomLevel){
            this._vemap.SetZoomLevel(this._vemap.GetZoomLevel() + 1);
        }
    }));

    $(document).find(".MMzoomout a").click(createRef(this, function() {
        if(this._vemap.GetZoomLevel() > this._minZoomLevel){
            this._vemap.SetZoomLevel(this._vemap.GetZoomLevel() - 1);
        }
    }));

    $(document).find(".MMNorth a").click(createRef(this, function() { this._vemap.Pan(0, -100); }));
    $(document).find(".MMSouth a").click(createRef(this, function() { this._vemap.Pan(0, 100); }));
    $(document).find(".MMWest a").click(createRef(this, function() { this._vemap.Pan(-100, 0); }));
    $(document).find(".MMEast a").click(createRef(this, function() { this._vemap.Pan(100, 0); }));

    $(document).find(".MMreset a").click(createRef(this, function() { this._vemap.SetCenterAndZoom(networkMap._mapCenter, 1); }));

    Microsoft.Maps.Events.addHandler(this._vemap, "onendzoom", createRef(this, this._onEndZoom));

}

MapController.prototype = {

    _onEndZoom: function(e) {

        var zoomLevel = this._vemap.GetZoomLevel();

        if (zoomLevel <= this._maxZoomLevel && zoomLevel >= this._minZoomLevel) {

            var offset = (15 - Math.round(scale(this._minZoomLevel, this._maxZoomLevel, 0, 15, zoomLevel))) - 24;

            $(this._slider).css('top', offset);
        }

    },

    _onScroll: function(e, ui) {

        var zoomLevel = Math.round(scale(0, 15, this._minZoomLevel, this._maxZoomLevel, (15 - (ui.position.top + 24))));

        var currentZoomLevel = this._vemap.GetZoomLevel();

        if (zoomLevel != currentZoomLevel &&
            zoomLevel <= this._maxZoomLevel && 
            zoomLevel >= this._minZoomLevel) {
            this._vemap.SetZoomLevel(zoomLevel);
        }
    }

};

