﻿// ZoomToScale.js
/// <reference assembly="System.Web.Extensions" name="MicrosoftAjax.js"/>

Type.registerNamespace('ADF.Samples');

ADF.Samples.zoomScaleClass = function (element) {
    ADF.Samples.zoomScaleClass.initializeBase(this,[element]);
    this.DPI = 96;
    this.MapScale = 1.0;
    this.UPP = 1.0;
    this.DegreeFactor = 40030174.00/360.00; 
    this.FeetFactor = 12;
    this.MeterFactor = 39.37007874;
    this.MapLevels = 1;
    this._zoomToScaleLastScale = 0;
    this._zoomToScalePanel = null;
    this._dropdown = null;
    this._textbox = null;
    
}

ADF.Samples.zoomScaleClass.prototype = {
    initialize : function() {
        ADF.Samples.zoomScaleClass.callBaseMethod(this, 'initialize');
        var id = this.get_id();
        this._zoomToScalePanel = $get(this.get_id());
        if (screen.deviceXDPI)
            this.DPI = screen.deviceXDPI;
        if (this._zoomToScalePanel!=null) {
            if (this._map.get_layers().get_levels() !== null) this.MapLevels = this._map.get_layers().get_levels().length;
            this._dropdown =  $get(id + "_ScaleDdl");
            this._textbox =  $get(id + "_ScaleTxt");
            
            $addHandler(this._textbox, "keypress", Function.createDelegate(this, this._zoomToScaleTextChange));
            $addHandler(this._dropdown, "change", Function.createDelegate(this, this._zoomToScaleSelectChange));
                
             
            this._updateZoomToScaleElements() ;     
       }
            if (this._map!=null) {
            this._map.add_extentChanged(Function.createDelegate(this, this._updateZoomToScaleElements));
        }
    },
    get_map : function() {
        /// <value name="map" type="Object">Map control associated with the GoToLocation functionality</value>
		return this._map;
	},
	set_map : function(value) {
		this._map = value;
	},
    get_scales : function() {
        /// <value name="scales" type="Object">Set of scale values for a cached resource</value>
		return this._scales;
	},
	set_scales : function(value) {
		this._scales = value;
	},
    get_units : function() {
        /// <value name="units" type="String">Name of map unit type</value>
		return this._units;
	},
	set_units : function(value) {
		this._units = value;
	},
	
	_updateZoomToScaleElements: function() {
	    /// <summary>
	    /// Updates the display
	    /// </summary>
	    var m = this.get_map();
        var mapId = m.get_id();
        var mapwidth = parseInt(m.get_element().clientWidth);
        var unitsperpixel = m.get_extent().get_width()/ mapwidth;
        var scale = this._getMapScale(unitsperpixel);
        
        if (true) {
            if (this._textbox!=null)
                this._textbox.value = scale;
            if(this._dropdown!=null)
            {
            
                var Options = this._dropdown.options;
                var Found = false;
        
                for (var i=0; i<Options.length; i++) {
                    if (Options[i].value == scale) {
                        Found = true;
                        this._dropdown.selectedIndex = i;
                    }
                }
        
                if (!Found) {
                    if (Options.length == 10) {
		                Options[Options.length-1] = new Option('1:' + scale, scale);
		                Options[Options.length] = new Option('(Other)', 'Other');
		            } else {
		                Options[Options.length-2] = new Option('1:' + scale, scale);		        
		            }
		            this._dropdown.selectedIndex = Options.length-2;         
                }
            
            }
            
          //this._zoomToScaleLastScale = scale;
        }	
    },
    
    _getMapScale : function(unitsperpixel) {
	    /// <summary>
	    /// Gets current map scale
	    /// </summary>
	    /// <param name="unitsperpixel" type="Number">Number of units per pixel</param>
	    var unitsperinch = unitsperpixel * this.DPI; 
	    var factor = 1; 
	    switch(this._units) { 
	        case "Feet": factor = this.FeetFactor; break;
	        case "Meters": factor = this.MeterFactor; break; 
	        //<----- case "Inches": factor = 1; break; 
	        case "DecimalDegrees": 
	        default: factor = this.DegreeFactor * this.MeterFactor; break;
	    } 
	    return Math.round(factor * unitsperinch);
    },
    
    _zoomToScaleSelectChange : function() {
	    /// <summary>
	    /// Handler for dropdown selection - used if cached resource
	    /// </summary>
	      
	       
	        var m = this.get_map();
            var scale = this._dropdown.options[this._dropdown.selectedIndex].value;
            
         if (scale == 'Other') {
            this._dropdown.style.display = "none";
            this._textbox.style.display = "inline";
            this._textbox.focus();
        } else {
            var mapwidth = parseInt(m.get_element().clientWidth);
            pixelsize = this._getUnitsPerPixel(scale);
            var unitsperpixel = m.get_extent().get_width()/ mapwidth;
			var factor = unitsperpixel / pixelsize;

			m.zoom(factor);
        }
        
    },
    
    _zoomToScaleTextChange : function (e) {
	    /// <summary>
	    /// Handler for Zoom button clicked - used if dynamic resource
	    /// </summary>
	    var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	    if (keyCode == 13) {
            if (this._textbox!=null) {
                var scale = this._textbox.value;
                if (scale.indexOf(",")!=-1) {
                 scale = scale.replace(/","/g, "");
                }
                if (isNaN(scale) || scale<=0)
                    alert("Angi en gyldig målestokk");
                else {
                    pixelsize = this._getUnitsPerPixel(scale);
                    var m = this.get_map();
                    var mapwidth = parseInt(m.get_element().clientWidth);
                    var unitsperpixel = m.get_extent().get_width()/ mapwidth;
			        var factor = unitsperpixel / pixelsize;
			        m.zoom (factor);
			        this._dropdown.style.display = "inline";
                    this._textbox.style.display = "none";
                }
            }
            return false;
        } else {
            return true;
        }
        
    },
 
    _getUnitsPerPixel : function(scale) {
	    /// <summary>
	    /// Get units per pixel based on scale
	    /// </summary>
	    /// <param name="scale" type="Number">Scale to be used</param>
       var metersperpixel = 0.0254 / this.DPI;
        var unitsperpixel = metersperpixel * scale;
        switch(this._units) {
            case "Feet": unitsperpixel *= 3.28; break;
            case "DecimalDegrees": unitsperpixel /= (40030174.00/360.00); break;
        }
        return unitsperpixel;
    }
    
}
ADF.Samples.zoomScaleClass.registerClass('ADF.Samples.zoomScaleClass', Sys.UI.Control);

if (typeof(Sys) !== "undefined") { Sys.Application.notifyScriptLoaded(); }


