﻿function clsSmallMapCtrl()
{
    this.arrLocations = new Array();
    this.toggle = true;
}

var ctrlSmallMap = new clsSmallMapCtrl();

var vtMap_graphview = null;                     //The Virtual Map ctrl will be 
clsSmallMapCtrl.prototype.initMap = function()
{
    this.divMap= document.getElementById("divSmallMap");
}


clsSmallMapCtrl.prototype.ClearMap = function()
{
    if(vtMap_graphview != null)
        vtMap_graphview.Clear();
}

clsSmallMapCtrl.prototype.render_Map = function()
{
    if(vtMap_graphview != null)vtMap_graphview.Dispose();
    var options = new VEMapOptions();
    options.EnableBirdseye = false;
    options.EnableDashboardLabels = true;

    vtMap_graphview = new VEMap('divSmallMap');
    vtMap_graphview.LoadMap();
    vtMap_graphview.HideDashboard();
    
    vtMap_graphview.SetMapStyle(VEMapStyle.Hybrid);
    
    //vtMap_graphview.AttachEvent("ondoubleclick",this.OnDblClicked);
    
    vtMap_graphview.AttachEvent("onmousedown", ctrlSmallMap.OnClicked);
    vtMap_graphview.AttachEvent("onmouseout", ctrlSmallMap.OnMouseOut);
    vtMap_graphview.AttachEvent("onmouseover", ctrlSmallMap.OnMouseOver);
    ctrlSmallMap.RenderLocations();
    ctrlSmallMap.DefaultMap();
}

clsSmallMapCtrl.prototype.toggle_Map = function()
{
    var options = {};
    if(this.toggle == true)
    {
	    $("#divSmallMap").hide('clip',options,500);
	    this.toggle = false;
    }
    else
    {
        $("#divSmallMap").show('clip',options,500);
	    this.toggle = true;
    }
    ctrlLayout.resizeEastMap();
}

clsSmallMapCtrl.prototype.DefaultMap = function()
{   
    var graph_ = null;
    for(var j=0; j<ctrlGraph.arrQuickGraphs.length;j++)
    {
        
        if(ctrlGraph.arrDrawGraphs[0] == ctrlGraph.arrQuickGraphs[j].id )
        {
            graph_ = ctrlGraph.arrQuickGraphs[j];
            break;
        }
    }
    if(graph_ == null)
    {
        for(j=0; j<arrGraphs.length;j++)
        {
            if(ctrlGraph.arrDrawGraphs[0] == arrGraphs[j].id)
            {
                graph_ = arrGraphs[j];
                break;
            }
        }
    }
    graph_.shapeLayer.Show();
    for(var w=0;w<arrLocations.length;w++)
    {
        if(arrLocations[w].id == graph_.arrLocations[0].id)
        {
            vtMap_graphview.SetCenterAndZoom(new VELatLong(arrLocations[w].lat, arrLocations[w].lon), 18);
            return true;
        }
    }
}

clsSmallMapCtrl.prototype.RenderLocations = function()
{   
    if(vtMap_graphview != null)
    {
        vtMap_graphview.DeleteAllShapeLayers();
        vtMap_graphview.ClearInfoBoxStyles();
    }
    var graph_ = null;
    for(var i=0; i<ctrlGraph.arrDrawGraphs.length;i++)
    {
        graph_ = null;
        /*
        for(var j=0; j<ctrlGraph.arrQuickGraphs.length;j++)
        {
            if(ctrlGraph.arrDrawGraphs[i] == ctrlGraph.arrQuickGraphs[j].id )
            {
                graph_ = ctrlGraph.arrQuickGraphs[j];
                break;
            }
        }
        if(graph_ == null)
        {
            for(j=0; j<arrGraphs.length;j++)
            {
                if(ctrlGraph.arrDrawGraphs[i] == arrGraphs[j].id)
                {
                    graph_ = arrGraphs[j];
                    break;
                }
            }
        }
        */

        graph_ = ctrlGraph.GetGraphByID(ctrlGraph.arrDrawGraphs[i]);
        
        var tempStr = "";
        if(graph_ != null)
        {
            graph_.shapeLayer = new VEShapeLayer();
            vtMap_graphview.AddShapeLayer(graph_.shapeLayer);
        
            for(j=0;j<graph_.arrLocations.length;j++)
            {
                for(k=0;k<arrLocations.length;k++)
                {
                    if(graph_.arrLocations[j].id == arrLocations[k].id)
                    {
                        graph_.arrLocations[j].shape = new VEShape(VEShapeType.Pushpin, new VELatLong(arrLocations[k].lat,arrLocations[k].lon));
                        graph_.arrLocations[j].shape.SetTitle("location" + graph_.arrLocations[j].id);
                        var desc_ = "<div style='width:40px;height:70px;'>" + arrLocations[k].siteName;
                        desc_ += "<br />" + arrLocations[k].startDate + "<br />" + arrLocations[k].endDate + "</div>";
                        graph_.arrLocations[j].shape.SetDescription(desc_);
                        graph_.arrLocations[j].siteID = arrLocations[k].siteID;
                        if(arrLocations[k].holdData == "full")
                        {
                            graph_.arrLocations[j].holdData  = "full";
                            graph_.arrLocations[j].shape.SetCustomIcon("<img id='IconLcg" + graph_.id + "-" + graph_.arrLocations[j].id + "' src='Image/Map/full_unselected.png' border='0'>");    
                        }
                        else if(arrLocations[k].holdData == "part")
                        {
                            graph_.arrLocations[j].holdData  = "part";
                            graph_.arrLocations[j].shape.SetCustomIcon("<img id='IconLcg" + graph_.id + "-" + graph_.arrLocations[j].id + "' src='Image/Map/part_unselected.png' border='0'>");    
                        }
                        graph_.shapeLayer.AddShape(graph_.arrLocations[j].shape);
                    }
                }
            }
        }
    }
}

clsSmallMapCtrl.prototype.OnClick_location = function(graphid,locationid) 
{
    for(var w=0;w<arrLocations.length;w++)
    {
        if(arrLocations[w].id == locationid)
        {
            vtMap_graphview.SetCenterAndZoom(new VELatLong(arrLocations[w].lat, arrLocations[w].lon), 18);
            break;
        }
    }
    
    var graph_ = null;
    for(var i=0; i<ctrlGraph.arrDrawGraphs.length;i++)
    {
        graph_ = ctrlGraph.GetGraphByID(ctrlGraph.arrDrawGraphs[i]);
        graph_.shapeLayer.Hide();
    }
    
    graph_ = ctrlGraph.GetGraphByID(graphid);
    graph_.shapeLayer.Show();
    for(j=0;j<graph_.arrLocations.length; j++)
    {
        var img = document.getElementById("IconLcg" + graph_.id + "-" + graph_.arrLocations[j].id);
        
        if(graph_.arrLocations[j].id == locationid)
        {
            if(img != null)
            {
                if(graph_.arrLocations[j].holdData == "full")
                {
                    img.src = 'Image/Map/full_selected.png';
                    img.src = 'Image/Map/full_selected.png';
                    img.src = 'Image/Map/full_selected.png';
                }
                else if(graph_.arrLocations[j].holdData == "part")
                {
                    img.src = 'Image/Map/part_selected.png';
                    img.src = 'Image/Map/part_selected.png';
                    img.src = 'Image/Map/part_selected.png';
                }
                graph_.arrLocations[j].selected = true;
            }
            //alert(graph_.arrLocations[j].id + " " + locationid);   
            ctrlDesc.LocationInfo(locationid);
        }
        else
        {
            if(img != null)
            {   
                if(graph_.arrLocations[j].holdData == "full")
                {
                    img.src = 'Image/Map/full_unselected.png';
                    img.src = 'Image/Map/full_unselected.png';
                    img.src = 'Image/Map/full_unselected.png';
                }
                else if(graph_.arrLocations[j].holdData == "part")
                {
                    img.src = 'Image/Map/part_unselected.png';
                    img.src = 'Image/Map/part_unselected.png';
                    img.src = 'Image/Map/part_unselected.png';
                }
                
                graph_.arrLocations[j].selected = false;
            }
        }
    }
}

clsSmallMapCtrl.prototype.OnClicked = function(e)
{
    var nodenum = null;
    var flag_ = false;
    var graph_ = null;
    
    if(e.elementID != null)
    {
        var shape = vtMap_graphview.GetShapeByID(e.elementID);
    
        if(shape != null)
        {
            for(var i=0; i<ctrlGraph.arrDrawGraphs.length;i++)
            {
                nodenum = shape.GetTitle();
                nodenum = nodenum.substr(8,nodenum.length);
                flag_ = false;
                
                graph_ = ctrlGraph.GetGraphByID(ctrlGraph.arrDrawGraphs[i]);
                if(graph_ == null || graph_ == undefined)
                    continue;
                    
                for(var k=0; k < graph_.arrLocations.length; k++)
                {
                    if(nodenum == graph_.arrLocations[k].id)
                    {
                        if(graph_.rendered == true)
                        {
                            ctrlGraph.userClickedTreeItem(graph_.id, nodenum);
                            break;
                        }
                    }
                }
            }
        }
    }
    return true;
}

clsSmallMapCtrl.prototype.OnDblClicked = function(e)
{
}


clsSmallMapCtrl.prototype.OnMouseOver = function(e)
{
    if(e.elementID != null)
    {
        var shape = vtMap_graphview.GetShapeByID(e.elementID);
    
        if(shape != null)
        {   
            for(var i=0;i<ctrlGraph.arrQuickGraphs.length;i++)
            {
                for(var j=0;j<ctrlGraph.arrQuickGraphs[i].arrLocations.length;j++)
                {
                    if(ctrlGraph.arrQuickGraphs[i].arrLocations[j].shape.GetID() == shape.GetID())
                    {
                        var img = document.getElementById("IconLcg" + ctrlGraph.arrQuickGraphs[i].id + "-" + ctrlGraph.arrQuickGraphs[i].arrLocations[j].id);
                        if(img != null)
                        {
                            if(ctrlGraph.arrQuickGraphs[i].arrLocations[j].holdData == "full")
                            {
                                img.src = 'Image/Map/full_mouseover.png';
                            }
                            else if(ctrlGraph.arrQuickGraphs[i].arrLocations[j].holdData == "part")
                            {
                                img.src = 'Image/Map/part_mouseover.png';
                            }
                            //ctrlDesc.LocationInfo(ctrlGraph.arrQuickGraphs[i].arrLocations[j].id);
                        }
                        //return true;
                        return; //Popup notice enable
                    }
                }    
            }
                
            for(i=0;i<arrGraphs.length;i++)
            {
                for(j=0;j<arrGraphs[i].arrLocations.length;j++)
                {
                    if(arrGraphs[i].arrLocations[j].shape.GetID() == shape.GetID())
                    {
                        
                        img = document.getElementById("IconLcg" + arrGraphs[i].id + "-" + arrGraphs[i].arrLocations[j].id);
                        if(img != null)
                        {
                            if(arrGraphs[i].arrLocations[j].holdData == "full")
                            {
                                img.src = 'Image/Map/full_mouseover.png';
                            }
                            else if(arrGraphs[i].arrLocations[j].holdData == "part")
                            {
                                img.src = 'Image/Map/part_mouseover.png';
                            }
                            var divEL = document.getElementById('divSmallInfoBox');
                            divEL.style.visibility = 'visible';
                            divEL.style.top = e.clientY;
                            divEL.style.left = e.clientX;
                            divEL.innerHTML = 'Sensing Location: ' + ctrlGraph.arrGraphs[i].arrLocations[j].id;
                        }
                        return true;
                        //return; //Popup notice enable
                    }
                }    
            }
        }
    }
    return true;
}

clsSmallMapCtrl.prototype.OnMouseOut = function(e)
{
    var divEL = document.getElementById('divSmallInfoBox');
    divEL.style.visibility = 'hidden';
    
    if(e.elementID != null)
    {
        var shape = vtMap_graphview.GetShapeByID(e.elementID);
    
        if(shape != null)
        {   
            for(var i=0;i<ctrlGraph.arrQuickGraphs.length;i++)
            {
                for(var j=0;j<ctrlGraph.arrQuickGraphs[i].arrLocations.length;j++)
                {
                    if(ctrlGraph.arrQuickGraphs[i].arrLocations[j].shape.GetID() == shape.GetID())
                    {
                        
                        var img = document.getElementById("IconLcg" + ctrlGraph.arrQuickGraphs[i].id + "-" + ctrlGraph.arrQuickGraphs[i].arrLocations[j].id);
                        if(img != null)
                        {
                            if(ctrlGraph.arrQuickGraphs[i].arrLocations[j].selected)
                            {
                                if(ctrlGraph.arrQuickGraphs[i].arrLocations[j].holdData == "full")
                                {
                                    img.src = 'Image/Map/full_selected.png';
                                }
                                else if(ctrlGraph.arrQuickGraphs[i].arrLocations[j].holdData == "part")
                                {
                                    img.src = 'Image/Map/part_selected.png';
                                }
                                
                            }
                            else
                            {
                                if(ctrlGraph.arrQuickGraphs[i].arrLocations[j].holdData == "full")
                                {
                                    img.src = 'Image/Map/full_unselected.png';
                                }
                                else if(ctrlGraph.arrQuickGraphs[i].arrLocations[j].holdData == "part")
                                {
                                    img.src = 'Image/Map/part_unselected.png';
                                }
                                
                            }
                        }
                        return true;
                        //return; //Popup notice enable
                    }
                }    
            }
                
            for(i=0;i<arrGraphs.length;i++)
            {
                for(j=0;j<arrGraphs[i].arrLocations.length;j++)
                {
                    if(arrGraphs[i].arrLocations[j].shape.GetID() == shape.GetID())
                    {
                        img = document.getElementById("IconLcg" + arrGraphs[i].id + "-" + arrGraphs[i].arrLocations[j].id);
                        if(img != null)
                        {
                            if(arrGraphs[i].arrLocations[j].selected)
                            {
                                if(arrGraphs[i].arrLocations[j].holdData == "full")
                                {
                                    img.src = 'Image/Map/full_selected.png';
                                }
                                else if(arrGraphs[i].arrLocations[j].holdData == "part")
                                {
                                    img.src = 'Image/Map/part_selected.png';
                                }
                                
                            }
                            else
                            {
                                if(arrGraphs[i].arrLocations[j].holdData == "full")
                                {
                                    img.src = 'Image/Map/full_unselected.png';
                                }
                                else if(arrGraphs[i].arrLocations[j].holdData == "part")
                                {
                                    img.src = 'Image/Map/part_unselected.png';
                                }
                                
                            }
                        }
                        return true;
                        //return; //Popup notice enable
                    }
                }    
            }
        }
    }
    return true;
}

clsSmallMapCtrl.prototype.HideMap_graph_Panel = function()
{
    this.divMap.style.visibility = "hidden";
}

clsSmallMapCtrl.prototype.clicked_Map = function(e)
{
    var posx = 0; 
    var posy = 0; 
    if (!e) 
        e = window.event; 
    if (e.pageX || e.pageY)
    { 
        posx = e.pageX; 
        posy = e.pageY; 
    } 
    else if (e.clientX || e.clientY)
    { 
        posx = e.clientX + document.body.scrollLeft 
        + document.documentElement.scrollLeft; 
        posy = e.clientY + document.body.scrollTop 
        + document.documentElement.scrollTop; 
    }
    this.divMap.style.top = posy + 'px';
    this.divMap.style.left = posx + 'px';
    this.divMap.style.visibility = 'visible';
}

