﻿//"GeoRSS.ashx"
function GeoTaggingMap(options) {
    if (typeof (options.onSelected) != "undefined")
        this.onSelected = options.onSelected;
    else
        this.onSelected = null;
        
    _mouseHandler = function(e,i) {
        var x = e.mapX;
        var y = e.mapY;
        var pixel = new VEPixel(x, y);
        var LL = i.map.PixelToLatLong(pixel);
        options.onSelected(LL.Latitude, LL.Longitude, true);
        return true;
    }
    _mouseHandlerPushPin = function(e, i) {
        if (e.eventName == "onmousedown") {
            i.dragShape = i.map.GetShapeByID(e.elementID);
            if(i.dragShape!=null)
                return true;
        } else if (e.eventName == "onmouseup") {
            if (i.dragShape != null) {
                var point = i.dragShape.GetPoints()[0]
                if (i.onSelected != null)
                    i.onSelected(point.Latitude, point.Longitude, true);
                this.dragShape = null;            
                return true;
            }            
        } else if (e.eventName == "onmousemove" && i.dragShape != null) {
            var x = e.mapX;
            var y = e.mapY;
            pixel = new VEPixel(x, y);
            var LL = i.map.PixelToLatLong(pixel);
            try {
                i.dragShape.SetPoints(LL);
            } catch (ex) { }
            return true; // prevent the default action
        }
    }
    _addLayer = function(map) {
        for (var i = 0; i < options.feed.length; i++) {
            if (options.feed[i].length == 0) continue;
            var l = new VEShapeLayer();
            l.index = i;
            l.isVE = false;
            var veLayerSpec = null;
            if (options.feed[i].indexOf("VE:") == 0) {
                //alert(options.feed[i].substr(3));
                l.isVE = true;
                veLayerSpec = new VEShapeSourceSpecification(VEDataType.VECollection, options.feed[i].substr(3), l);
            } else if (options.feed[i].indexOf("KML:") == 0) {
                alert(options.feed[i].substr(4));
                veLayerSpec = new VEShapeSourceSpecification(VEDataType.ImportXML, options.feed[i].substr(4), l);
            } else {
                veLayerSpec = new VEShapeSourceSpecification(VEDataType.GeoRSS, options.feed[i], l);
            }
            map.ImportShapeLayerData(veLayerSpec, _onFeedLoad);
        }
    }
    _onFeedLoad = function(layer) {
        layer.Hide();
        var numShapes = layer.GetShapeCount();
        for (var i = 0; i < numShapes; ++i) {
            var s = layer.GetShapeByIndex(i);
            if (layer.isVE) {
                s.HideIcon();
                if (s.GetType() == VEShapeType.Polygon) {
                    s.SetFillColor(new VEColor(0, 0, 0, 0));
                    //s.SetLineColor(new VEColor(0, 0, 0, 0));
                    //s.SetLineWidth(1);
                }
                continue;
            }
            if (typeof (options.pushPins) != "undefined" && options.pushPins.length > layer.index) {
                s.SetCustomIcon('<img src="' + options.pushPins[layer.index] + '"/>');
            }
        }
        layer.Show();
    }
    this.map = new VEMap('map_canvas');
    this.map.ShowMessageBox = false;
    if (typeof (options.dashboardSize) != "undefined")
        this.map.SetDashboardSize(VEDashboardSize.Small);
    if(typeof(options.feed)!="undefined")
        this.map.onLoadMap = function() { _addLayer(this) };
    this.map.LoadMap(new VELatLong(41, 2), 3, VEMapStyle.Hybrid);

    if (this.onSelected != null) {
        this.map.AttachEvent("ondoubleclick", function(i) { return function(e) { return _mouseHandler(e, i); }; } (this));
        this.map.AttachEvent("onmousedown", function(i) { return function(e) { return _mouseHandlerPushPin(e, i); }; } (this));
        this.map.AttachEvent("onmouseup", function(i) { return function(e) { return _mouseHandlerPushPin(e, i); }; } (this));
        this.map.AttachEvent("onmousemove", function(i) { return function(e) { return _mouseHandlerPushPin(e, i); }; } (this));
    }    

    this.AddPushPin = function(id, lat, lon) {
        //alert(id + "---" + lat + "---" + lon);
        if (typeof (this.shape) != "undefined")
            this.map.DeleteShape(this.shape);
        if (lat != "") {
            this.shape = new VEShape(VEShapeType.Pushpin, new VELatLong(lat, lon));
            //this.shape.SetTitle('My pushpin');
            //shape.SetDescription('This is shape number ' + pinid);
            //pinid++;
            this.map.AddShape(this.shape);
        }

    }
       
       
}   

   
