﻿/*
This JavaScript file defines functions/objects for interacting with the 
Asset Web virtual earth web map component. This includes things such
as registering custom mouse and key handler routines.
*/
/*
  Provides a JavaScript object wraper around a Virtual Earth map component.
*/

ASSETSWEB.AssetsMap = function() {
    var mapContainer = '';
    var vemap = null;
    var assetLayerID = "assetTilesLayer";
    var assetLayerURL = "tiles/%4.ashx";
    var distance = 2;
    var thiz = {
        init: function(container) {
            mapContainer = container;
            vemap = new VEMap(mapContainer);
            var opts = new VEMapOptions();
            opts.EnableBirdseye = false;
            vemap.SetDashboardSize(VEDashboardSize.Normal); //Needs to happend BEFORE call to LoadMap
            vemap.LoadMap(new VELatLong(39.95606977009003, -82.80120849609376), 7, 'r', false, VEMapMode.Mode2D, true, 0, opts);
            thiz.addAssetTileLayer();
            vemap.AttachEvent("onmousemove", thiz.updateLL);

        },
        resize: function() {
            var cH = Ext.lib.Dom.getViewHeight();
            var cW = Ext.lib.Dom.getViewWidth();
            var navWidth = $('#nav-panel').width();
            var cWMinusNav = (cW - navWidth);
            var mapDiv = $('#' + mapContainer)
            mapDiv.css({ "height": (cH - 62) + "px", "width": cWMinusNav + "px", "top": "0px", "left": "0px" });
            if (vemap) { vemap.Resize(mapDiv.width(), mapDiv.height()); }
            var centerOfMapW = (parseFloat(mapDiv.width() / 2) + parseFloat(navWidth));
            $("#divLatLongCoords").css({ "left": (centerOfMapW - 250) + "px", "width": "500px" });
        },
        updateLL: function(e) {
            try {
                var x = e.mapX;
                var y = e.mapY;
                var pixel = new VEPixel(x, y);
                var LL = vemap.PixelToLatLong(pixel);
                $("#cellLatitude").text(roundToPrecision(LL.Latitude, 6));
                $("#cellLongitude").text(roundToPrecision(LL.Longitude, 6));
            } catch (e) { }
        },
        getVEMap: function() {
            return vemap;
        },
        getContainerID: function() {
            return mapContainer;
        },
        enableAssetDetailClick: function() {
            vemap.AttachEvent("onclick", thiz.assetDetailClick);
        },
        disableAssetDetailClick: function() {
            vemap.DetachEvent("onclick", thiz.assetDetailClick);
        },
        assetDetailClick: function(e) {
            if (e.rightMouseButton) {
                var x = e.mapX;
                var y = e.mapY;
                var pixel = new VEPixel(x, y);
                var LL = vemap.PixelToLatLong(pixel);
                if (myPanel.isActiveBridgesSelected()) {
                    thiz.loadAssetActiveDetails(LL.Longitude, LL.Latitude);

                } else {
                    if (myChkBoxTree.isHistoricBridgesSelected()) {
                        thiz.loadAssetDetails(LL.Longitude, LL.Latitude);
                    }
                }
            }
        },
        loadAssetDetails: function(lon, lat) {
            //Create asset historic detail pop-up window
            ASSETSWEB.AssetsDetailDialog.show(lon, lat);
        },
        loadAssetActiveDetails: function(lon, lat) {
            //Create asset active detail pop-up window
            ASSETSWEB.AssetsActiveDetailDialog.show(lon, lat);

        },
        updateMap: function() {
            var isHistoricAndActiveSelected = ASSETSWEB.AssetsValidator.bridgeValidator();

            if (isHistoricAndActiveSelected) {
                return Ext.Msg.alert('Message', 'Please select either  active bridges or historic bridges.');

            }

            this.disableAssetDetailClick();

            Ext.MessageBox.show({
                msg: 'Please wait...',
                progressText: 'Loading Data...',
                width: 300,
                wait: true,
                waitConfig: { interval: 200 }
            });
            //Send xml request and process json response
            var queryXml = ASSETSWEB.AssetsQuery.toXml();

            $.post('ajax/UpdateAssetSelection.aspx', { assetQueryXml: queryXml }, function(resp) {
                if (resp) {
                    if (resp.message == true) {
                        // To remove images on the map
                        thiz.cleanup();
                        Ext.Msg.alert('Error!', 'Please select at least one bridge type.');

                    } else if (resp.success != true) {
                        Ext.Msg.alert('Error!', 'The server was unable to process your request at this time. <br /> We apologize for the inconvenience. ');

                    }
                    else {
                        if (resp.assetCount) {
                            Ext.Msg.alert('Message', resp.assetCount + ' assets were found. <br />Please right click on the map to view all of the bridges that are within a ' + distance + '  mile radius of the click point.');
                        }
                        else {
                            if (resp.activeAssetsCount) {
                                Ext.Msg.alert('Message', resp.activeAssetsCount + ' assets were found. <br />Please right click on the map to view all of the bridges that are within a 1/2  mile radius of the click point.');

                            } else {
                                Ext.Msg.alert('Status', '00 assets were found.');
                            }

                        }
                    }
                }
                thiz.reloadAssetTileLayer();


            }, "json");
            thiz.addBridgeImagesData();
            this.enableAssetDetailClick();

        },

        cleanup: function() {
            var successHandler = function(resp) {
                ASSETSWEB.AssetsMap.reloadAssetTileLayer();
            };

            var errorHandler = function(xhr) {
                Ext.Msg.alert('Error:' + xhr.status, 'The server was unable to process your request at this time. <br /> We apologize for the inconvenience. ');

            };
            $.ajax({
                //type: "POST",
                url: 'ajax/ClearAssetSelections.aspx',
                success: successHandler,
                error: errorHandler,
                dataType: "json"
            });
        },
        addAssetTileLayer: function() {

            var url = assetLayerURL + "?rnd=" + new Date().getTime()
            var tileSourceSpec = new VETileSourceSpecification(assetLayerID, url);
            var bounds = [vemap.GetMapView()];
            tileSourceSpec.Bounds = bounds;
            tileSourceSpec.NumServers = 1;
            vemap.AddTileLayer(tileSourceSpec, true);
        },
        reloadAssetTileLayer: function() {
            vemap.DeleteTileLayer(assetLayerID);
            thiz.addAssetTileLayer();
        },
        addBridgeImagesData: function() {

            $.ajax({
                
                url: 'bassimage/test.ashx'

            });
        }
    }
    return thiz;
} ();

                
