/* Minification failed. Returning unminified contents.
(1,1): run-time error CSS1019: Unexpected token, found '/'
(1,2): run-time error CSS1019: Unexpected token, found '/'
(1,3): run-time error CSS1019: Unexpected token, found '/'
(3,1): run-time error CSS1031: Expected selector, found '('
(3,1): run-time error CSS1025: Expected comma or open brace, found '('
(73,52): run-time error CSS1002: Unterminated string: 't been initialized yet
 */
/// Requires dojo

(function ($) {

    dojo.require("dijit.layout.BorderContainer");
    dojo.require("dijit.layout.ContentPane");
    dojo.require("esri.map");
    dojo.require("esri.dijit.Popup");

    var methods = {
        init: function (options) {
            var $this = $(this);
            dojo.ready(function () {
                init($this, options);
            });

        },
        set: function (hdop) {
            var $this = $(this);
            return setSignalStrength($this, hdop);
        },
        resize: function () {
            var $this = $(this);
            resize($this);
            return false;
        }
    };

    function init($this, options) {
        
        // Create some defaults, extending them with any options that were provided
        options = $.extend({
            'lat': 0,
            'lon': 0,
            'hdop': 1,
            'mapServer': 'http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer'
        }, options);

        var map = new esri.Map($this.attr('id'), {
            slider: true,
            logo: false,
            nav: false,
            showAttribution: false,
            infoWindow: null,
            sliderStyle: 'small'
        });

        var basemap = new esri.layers.ArcGISTiledMapServiceLayer(options.mapServer);
        map.addLayer(basemap);

        dojo.connect(map, "onLoad", function () {
            var location = new esri.geometry.Point(options.lon, options.lat, new esri.SpatialReference({ "wkid": 4326 }));

            // Project the point
            var pts = new Array();
            pts.push(location);
            var gsvc = new esri.tasks.GeometryService("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer");
            
            var params = new esri.tasks.ProjectParameters();
            params.geometries = pts;
            params.outSR = map.spatialReference;
            params.transformation = { "wkid": 1670 };
            params.transformForward = false;

            gsvc.project(params, function (projected) {
                setSignalStrength($this, options.hdop, projected[0]);
                map.centerAt(projected[0]);
            });
        });

        var data = $this.data('signalStrength');

        // If the plugin hasn't been initialized yet
        if (!data) {
            $this.data('signalStrength', {
                target: $this,
                options: options,
                map: map
            });
        }
    }
    
    function resize($this) {
        var data = $this.data('signalStrength');

        var map = data.map;
        map.resize(true);
    }

    function buildCircle(spatialReference, pt, radius) {
        var circle, ring, pts, angle;

        circle = new esri.geometry.Polygon(spatialReference);
        ring = []; // point that make up the circle
        pts = 1000; // number of points on the circle
        angle = 360 / pts; // used to compute points on the circle
        for (var i = 1; i <= pts; i++) {
            // convert angle to raidans
            var radians = i * angle * Math.PI / 180;
            // add point to the circle
            ring.push([pt.x + radius * Math.cos(radians), pt.y + radius * Math.sin(radians)]);
        }
        ring.push(ring[0]); // start point needs to == end point
        circle.addRing(ring);
        return circle;
    }

    function setSignalStrength($this, hdop, pos) {

        var data = $this.data('signalStrength');
        var map = data.map;

        var zoomLevel = 14;
        var signalRadius = hdop * 5; // in metres
        if (hdop <= 35) {
            // Minimum of HDOP 35
            signalRadius = 35 * 5;
        }
        
        if (hdop <= 35) {
            zoomLevel = 14;
        } else if (hdop <= 50) { // 25m
            // Medium
            zoomLevel = 13;
        } else {
            // Very weak
            zoomLevel = 12;
        }

        if (!pos) {
            pos = map.extent.getCenter();
        }

        map.graphics.clear();

        var circle = buildCircle(map.spatialReference, pos, signalRadius);

        var sym = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID,
			new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,
            new dojo.Color([218, 24, 58]), 1), new dojo.Color([218, 24, 58, 0.25]));
        map.graphics.add(new esri.Graphic(circle, sym));

        map.setLevel(zoomLevel);
    }

    $.fn.signalStrengthMap = function (method) {

        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.signalStrengthMap');
        }
    };
})(jQuery);
