﻿function clsMapCtrl()
{
    this.lasso_x = -1;
    this.lasso_y = -1;
    this.lasso_status = 0;
}

var vtMap_mapview = null;                     //The Virtual Map variable
var ctrlMap = new clsMapCtrl();               //The Virtual Earth Map ctrl

clsMapCtrl.prototype.initMap = function() {

    var options = new VEMapOptions();
    options.EnableBirdseye = false;
    options.EnableDashboardLabels = true;

    this.divMap_map = document.getElementById("divMap_map");
    this.divMap_map.style.width = '100%';
    this.divMap_map.style.height = '100%';

    var height = $('#graph_maptime_Pane').height();
    var width = $('#graph_maptime_Pane').width();

    height -= (ctrlTime.height + 12);
    $('#divMap_map').height(height);
    $('#divMap_map').width(width);

    if (vtMap_mapview != null) {
        vtMap_mapview.Resize(width, height);
    }
    else {
        vtMap_mapview = new VEMap('divMap_map');
        vtMap_mapview.LoadMap();
        vtMap_mapview.SetMapStyle(VEMapStyle.Hybrid);
        vtMap_mapview.AttachEvent("onclick", this.OnClicked);
        vtMap_mapview.AttachEvent("ondoubleclick", this.OnDblClicked);
        vtMap_mapview.AttachEvent("onmouseout", this.OnMouseOut);
        vtMap_mapview.AttachEvent("onmouseover", this.OnMouseOver);
        vtMap_mapview.AttachEvent("onmousedown", this.user_mouseDown);
        vtMap_mapview.AttachEvent("onmouseup", this.user_mouseUp);
        vtMap_mapview.AttachEvent("onmousemove", this.user_mouseMove);
        document.getElementById("MSVE_obliqueNotification").style.visibility = "hidden";            //Prevent the annoying stuff in the map like notification.
    }
}

clsMapCtrl.prototype.Resize = function()
{
    if(vtMap_mapview != null)
    {
        var ltlg = vtMap_mapview.GetCenter();
        var zoom = vtMap_mapview.GetZoomLevel();
        //vtMap_mapview.style.height = this.divMap_map.elDiv.offsetHeight;
        this.initMap();
        this.RenderLocations();
        vtMap_mapview.SetCenter(ltlg);
        vtMap_mapview.SetCenter(ltlg);
        vtMap_mapview.SetZoomLevel(zoom);
    }
}

clsMapCtrl.prototype.render_Map = function()
{
    var options = new VEMapOptions();
    options.EnableBirdseye = false;
    options.EnableDashboardLabels = true;

    vtMap_mapview = new VEMap('divMap_map');
    vtMap_mapview.LoadMap();
    
    vtMap_mapview.SetMapStyle(VEMapStyle.Hybrid);
    vtMap_mapview.AttachEvent("onclick", this.OnClicked);
    vtMap_mapview.AttachEvent("ondoubleclick",this.OnDblClicked);
    vtMap_mapview.AttachEvent("onmouseout", this.OnMouseOut);
    vtMap_mapview.AttachEvent("onmouseover", this.OnMouseOver);
    vtMap_mapview.AttachEvent("onmousedown", this.user_mouseDown);
    vtMap_mapview.AttachEvent("onmouseup", this.user_mouseUp);
    vtMap_mapview.AttachEvent("onmousemove", this.user_mouseMove);
    document.getElementById("MSVE_obliqueNotification").style.visibility = "hidden";            //Prevent the annoying stuff in the map like notification.
}

clsMapCtrl.prototype.RenderLocations = function() {
    vtMap_mapview.DeleteAllShapes();
    var curDate = new Date();
    var strToday = ctrlMap.GetCurrentMonth() + '/' + ctrlMap.GetCurrentDay() + '/' + ctrlMap.GetCurrentYear();
    var strToday_wo0 = (curDate.getMonth() + 1) + '/' + curDate.getDate() + '/' + curDate.getFullYear();
    var tempStr = "";
    for (var i = 0; i < arrLocations.length; i++) {
        if (arrLocations[i].shape != null) {
            //delete arrLocations[i].shape;           //for managing memory 
            arrLocations[i].shape = null;
        }

        arrLocations[i].shape = new VEShape(VEShapeType.Pushpin, new VELatLong(arrLocations[i].lat, arrLocations[i].lon));
        var desc_ = 'Site: ' + arrLocations[i].siteName;
        if (dataManager.dataMode == "data") {
            arrLocations[i].shape.SetTitle("location" + arrLocations[i].id);
            if (strToday == arrLocations[i].endDate) {
                desc_ += '<br>Deployed:' + arrLocations[i].startDate + ' - Current';
            }
            else {
                desc_ += '<br>Deployed:' + arrLocations[i].startDate + ' - ' + arrLocations[i].endDate;
            }
        }
        else {
            arrLocations[i].shape.SetTitle("Box" + arrLocations[i].id);
            desc_ += '<br>-History-<br>' + arrLocations[i].sensorTypes.replace(strToday_wo0, 'Current');
        }

        arrLocations[i].shape.SetDescription(desc_);

        if (arrLocations[i].holdData == "full") {
            if (arrLocations[i].selected)
                arrLocations[i].shape.SetCustomIcon("<img id='IconLoc" + arrLocations[i].id + "' src='Image/Map/full_selected.png' border='0'>");
            else
                arrLocations[i].shape.SetCustomIcon("<img id='IconLoc" + arrLocations[i].id + "' src='Image/Map/full_unselected.png' border='0'>");
        }
        else if (arrLocations[i].holdData == "part") {
            if (arrLocations[i].selected)
                arrLocations[i].shape.SetCustomIcon("<img id='IconLoc" + arrLocations[i].id + "' src='Image/Map/part_selected.png' border='0'>");
            else
                arrLocations[i].shape.SetCustomIcon("<img id='IconLoc" + arrLocations[i].id + "' src='Image/Map/part_unselected.png' border='0'>");
        }
        else if (arrLocations[i].holdData == "none") {
            arrLocations[i].shape.SetCustomIcon("<img id='IconLoc" + arrLocations[i].id + "' src='Image/Map/none_unselected.png' border='0'>");
        }
        vtMap_mapview.AddShape(arrLocations[i].shape);
    };
    document.getElementById("MSVE_obliqueNotification").style.visibility = "hidden";            //Prevent the annoying stuff in the map like notification.
}

clsMapCtrl.prototype.RenderSites = function()
{   
    alert('RenderSites ');
    for(var i=0;i< arrSites.length ; i++)
    {
        if(arrSites[i].shape != null)
        {
            //delete arrLocations[i].shape;           //for managing memory 
            arrSites[i].shape = null;
        }
        
        arrSites[i].shape = new VEShape(VEShapeType.Pushpin, new VELatLong(arrSites[i].lat,arrSites[i].lon));
        arrSites[i].shape.SetTitle("Site" + arrSites[i].id);
        arrSites[i].shape.SetDescription('');
        arrSites[i].shape.SetCustomIcon("<img id='IconSit" + arrSites[i].id + "' src='Image/Map/Site/site" + arrSites[i].id + ".png' border='0'>");
        vtMap_mapview.AddShape(arrSites[i].shape);
    }        
}

clsMapCtrl.prototype.SetCenterOfSites = function() 
{
    var latTot = 0.0;
    var lonTot = 0.0;
    
    var minLat = 99999.0;
    var maxLat = -99999.0;
    var minLon = 99999.0;
    var maxLon = -99999.0;
    
    
    for(var i=0;i< arrSites.length ; i++)
    {
        if(arrSites[i].id != 5)
        {
            latTot += arrSites[i].lat;
            lonTot += arrSites[i].lon;
        }
    }
    latTot /= arrSites.length-1;
    lonTot /= arrSites.length-1;
    vtMap_mapview.SetCenterAndZoom(new VELatLong(latTot, lonTot),9);
    document.getElementById("MSVE_obliqueNotification").style.visibility = "hidden";            //Prevent the annoying stuff in the map like notification.
}

clsMapCtrl.prototype.OnClick_location = function(locationid) 
{
   for(var i=0;i<arrLocations.length;i++)
    {
        if(arrLocations[i].id == locationid)
        {
            ctrlDesc.LocationInfo(arrLocations[i].id);
            vtMap_mapview.SetCenterAndZoom(new VELatLong(arrLocations[i].lat, arrLocations[i].lon), 20);
            break;
        }
    }
}

clsMapCtrl.prototype.FocusOnLastLocation = function()
{
    var id = -1;
    for(var i=0;i<arrLocations.length;i++)
    {
        if(arrLocations[i].selected == true)
        {
            for(var j=0;j<arrSites.length;j++)
            {
                if(arrSites[j] != null && arrSites[j].selected == true && arrSites[j].id == arrLocations[i].siteID)
                {
                    id = arrLocations[i].id;
                }
            }
        }
    }
    if(id != -1)
    {
        ctrlMap.OnClick_location(id)
    }
}

clsMapCtrl.prototype.OnClick_site = function(siteid) 
{
    //Iconblur    
    for(var i=0;i<arrSites.length;i++)
    {
        if(arrSites[i].id == siteid)
        {
            //Gotothe position
            ctrlDesc.SiteInfo(arrLocations[i].id);
            vtMap_mapview.SetCenterAndZoom(new VELatLong(arrSites[i].lat, arrSites[i].lon), 18);
            break;
        }
    }
}

clsMapCtrl.prototype.OnClicked = function(e)
{
    if(e.elementID == null)
        return;
    var shape = vtMap_mapview.GetShapeByID(e.elementID);
    for(var i=0;i<arrLocations.length;i++)
    {
        if(shape != null && arrLocations[i].shape.GetID() == shape.GetID())
        {
            var img = document.getElementById("IconLoc" + arrLocations[i].id);
            if(img != null)
            {
                if(arrLocations[i].selected)
                {
                    arrLocations[i].selected = false;
                    if(arrLocations[i].holdData == "full")
                        img.src = 'Image/Map/full_unselected.png';
                    else if(arrLocations[i].holdData == "part")
                        img.src = 'Image/Map/part_unselected.png';
                }
                else
                {
                    arrLocations[i].selected = true;
                    if(arrLocations[i].holdData == "full")
                        img.src = 'Image/Map/full_selected.png';
                    else if(arrLocations[i].holdData == "part")
                        img.src = 'Image/Map/part_selected.png';
                    else if(arrLocations[i].holdData == "none")
                    {
                        ctrlDesc.Notice("Location" + arrLocations[i].id + " dose not have any data in the period you selected");   
                        return;
                    }
                        
                }
                //ctrlDesc.LocationInfo(arrLocations[i].id);
                //ctrlSiteLocation.clicked_location_onMap(arrLocations[i].id);
                break;
            }
        }
    }
    ctrlSiteLocation.refreshAll();
    ctrlGraph.CheckReady();
}

clsMapCtrl.prototype.OnDblClicked = function(e)
{
}
/*
clsMapCtrl.prototype.Resize = function()
{
    //alert(this.divMap_map.offsetWidth + " " + this.divMap_map.offsetHeight);
    var ltlg = vtMap_mapview.GetCenter();
    var zoom = vtMap_mapview.GetZoomLevel();
    //vtMap_mapview.style.height = this.divMap_map.elDiv.offsetHeight;
    this.render_Map();
    this.RenderLocations();
    vtMap_mapview.SetCenter(ltlg);
    vtMap_mapview.SetCenter(ltlg);
    vtMap_mapview.SetZoomLevel(zoom);
}
*/
clsMapCtrl.prototype.OnMouseOver = function(e)
{
    if(e.elementID != null)
    {
        var shape = vtMap_mapview.GetShapeByID(e.elementID);
        if(shape != null)
        {
            for(var i=0;i<arrLocations.length;i++)
            {
                if(shape != null && arrLocations[i].shape.GetID() == shape.GetID())
                {
                    var img = document.getElementById("IconLoc" + arrLocations[i].id);
                    if(img != null)
                    {
                        if(arrLocations[i].holdData == "full")
                        {
                            img.src = 'Image/Map/full_mouseover.png';
                            ctrlSiteLocation.blur_location(arrLocations[i].id);
                        }
                        else if(arrLocations[i].holdData == "part")
                        {
                            img.src = 'Image/Map/part_mouseover.png';
                            ctrlSiteLocation.blur_location(arrLocations[i].id);
                        }
                        else if(arrLocations[i].holdData == "none")   
                        {
                            img.src = 'Image/Map/none_mouseover.png';
                        }
                        
                        ctrlDesc.LocationInfo(arrLocations[i].id);
                    }
                    else
                    {
                        alert("IMG NULL:" + "IconLoc" + arrLocations[i].id);
                    }
                    //return true;
                    return; //Popup notice enable
                }
            }
        }
    }
}

clsMapCtrl.prototype.OnMouseOut = function(e)
{
    if(e.elementID == null)
        return;
    var shape = vtMap_mapview.GetShapeByID(e.elementID);
    if(shape == null)
        return;
    for(var i=0;i<arrLocations.length;i++)
    {
        if(arrLocations[i].shape.GetID() == shape.GetID())
        {
            var img = document.getElementById("IconLoc" + arrLocations[i].id);
            if(img != null)
            {
                if(arrLocations[i].holdData == "full")   
                {
                    if(arrLocations[i].selected)
                        img.src = 'Image/Map/full_selected.png';
                    else   
                        img.src = 'Image/Map/full_unselected.png';
                }
                else if(arrLocations[i].holdData == "part")   
                {
                    if(arrLocations[i].selected)
                        img.src = 'Image/Map/part_selected.png';
                    else   
                        img.src = 'Image/Map/part_unselected.png';
                }
                else if(arrLocations[i].holdData == "none")   
                {
                        img.src = 'Image/Map/none_unselected.png';
                        return;
                }
                ctrlSiteLocation.nonblur_location(arrLocations[i].id);
            }
            break;
        }
    }
}

clsMapCtrl.prototype.user_mouseDown = function(e)
{   
    if(e.ctrlKey == true && ctrlMap.lasso_status == 0)
    {
        ctrlMap.lasso_x = e.mapX;
        ctrlMap.lasso_y = e.mapY;
        ctrlMap.lasso_status = 1;
        return false;
    }
    
    if(ctrlMap.lasso_status == 2)
    {
        var pixel_start = null;
        var pixel_end = null;
        
        pixel_start = new VEPixel((ctrlMap.lasso_x < e.mapX) ? ctrlMap.lasso_x : e.mapX, (ctrlMap.lasso_y < e.mapY) ? ctrlMap.lasso_y : e.mapY);
        pixel_end = new VEPixel((ctrlMap.lasso_x > e.mapX) ? ctrlMap.lasso_x : e.mapX, (ctrlMap.lasso_y > e.mapY) ? ctrlMap.lasso_y : e.mapY);

        
        var latlon_start = vtMap_mapview.PixelToLatLong(pixel_start);
        var latlon_end = vtMap_mapview.PixelToLatLong(pixel_end);
        
        for(var i = 0; i< arrLocations.length; i++)
        {
            var img = document.getElementById("IconLoc" + arrLocations[i].id);
            if(img != null)
            {
                if(arrLocations[i].lat <= latlon_start.Latitude && arrLocations[i].lat >= latlon_end.Latitude &&
                    arrLocations[i].lon >= latlon_start.Longitude && arrLocations[i].lon <= latlon_end.Longitude)
                    {
                        arrLocations[i].selected = true;
                    }
                    else
                    {
                        arrLocations[i].selected = false;
                    }
            }
        }
        ctrlSiteLocation.refreshAll();
        ctrlMap.lasso_status = 0;
        return false;
    }
}

clsMapCtrl.prototype.user_mouseUp = function(e)
{   
    if(ctrlMap.lasso_status == 2)
    {
        return true;
    }
    return false;
}

clsMapCtrl.prototype.user_mouseMove = function(e)
{   
    if(ctrlMap.lasso_status == 1 || ctrlMap.lasso_status == 2)
    {
        var pixel_start = null;
        var pixel_end = null;
        
        pixel_start = new VEPixel((ctrlMap.lasso_x < e.mapX) ? ctrlMap.lasso_x : e.mapX, (ctrlMap.lasso_y < e.mapY) ? ctrlMap.lasso_y : e.mapY);
        pixel_end = new VEPixel((ctrlMap.lasso_x > e.mapX) ? ctrlMap.lasso_x : e.mapX, (ctrlMap.lasso_y > e.mapY) ? ctrlMap.lasso_y : e.mapY);

        
        var latlon_start = vtMap_mapview.PixelToLatLong(pixel_start);
        var latlon_end = vtMap_mapview.PixelToLatLong(pixel_end);
        
        for(var i = 0; i< arrLocations.length; i++)
        {
            var img = document.getElementById("IconLoc" + arrLocations[i].id);
            if(img != null)
            {
                if(arrLocations[i].lat <= latlon_start.Latitude && arrLocations[i].lat >= latlon_end.Latitude &&
                    arrLocations[i].lon >= latlon_start.Longitude && arrLocations[i].lon <= latlon_end.Longitude)
                    {
                        if(arrLocations[i].holdData == "full")
                            img.src = 'Image/Map/full_selected.png';
                        else if(arrLocations[i].holdData == "part")
                            img.src = 'Image/Map/part_selected.png';
                    }
                    else
                    {
                        if(arrLocations[i].holdData == "full")
                            img.src = 'Image/Map/full_unselected.png';
                        else if(arrLocations[i].holdData == "part")
                            img.src = 'Image/Map/part_unselected.png';
                    }
            }
        }
        
        ctrlMap.lasso_status = 2;
        return false;
    }
}

clsMapCtrl.prototype.GetCurrentMonth = function() {
    var curDate = new Date();
    var curMon = curDate.getMonth() + 1;

    var strMon;
    if (curMon < 10) {
        strMon = '0' + curMon;
    }
    else {
        strMon = curMon.toString();
    }

    return strMon;
}

clsMapCtrl.prototype.GetCurrentDay = function() {
    var curDate = new Date();
    var curDay = curDate.getDate();

    var strDay;

    if (curDay < 10) {
        strDay = '0' + curDay;
    }
    else {
        strDay = curDay.toString();
    }

    return strDay;
}

clsMapCtrl.prototype.GetCurrentYear = function() {
    var curDate = new Date();
    var curYear = curDate.getFullYear();
    return curYear;
}

