﻿var map = null;
var CurrentLocation = null;
var currSearchResult = null;

      
function GetMap()
{
    //var data = $get(hidenLoadedPointName).value.split(",");
    var data = document.getElementById(hidenLoadedPointName).value.split(",");

    var zoomLevel;//map zoom level on first load
    
    if(data.length >= 2)
    {
        CurrentLocation = new VELatLong(data[0], data[1]);
        zoomLevel = 10;
    }
    else
    {
        //Set China by default
        CurrentLocation = new VELatLong(36.553994230276416, 104.05597174085247);
        zoomLevel = 3;
    }

    map = new VEMap('myMap');
    map.LoadMap(CurrentLocation, zoomLevel, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
    AddPin(CurrentLocation);
    if(data[0] == "0" && data[1] == "0")
    {
        document.getElementById("tdmap").style.display = "none";
    }
}

function AddPin(locationDefinition)
{
    try
    {
        //TODO: initiate with real data
        var pin = new VEPushpin(
                                   0, 
                                   locationDefinition, 
                                   pushPinImage, 
                                   null, 
                                   null
                                   );
        map.AddPushpin(pin); 
    }
    catch(err)
    {
        alert(err);
    }
                
}

function DeletePin()
{
    try
    {
        map.DeletePushpin(0);
    }
    catch(Error)
    {
        alert(err);
    }
}

///////////////////////////////////////////////////////
//Add subscribe to find result
///////////////////////////////////////////////////////
function PanLatLong(c)
{
    //not implemented(fro future development...)
     var lat =  document.getElementById('txtMapLat').value;
     var lon =  document.getElementById('txtMapLon').value;
     map.PanToLatLong(new VELatLong(lat, lon));
}
      

function Find(cityControl)
{
     var textToSearch = "";
     document.getElementById('resultDiv').innerHTML= "";
     if (cityControl != null && cityControl != "")
     {
        textToSearch = cityControl.value;
     }
     
     if (textToSearch != "")
     {
        document.getElementById("tdmap").style.display = "block";
        map.Find(null, textToSearch, null, null, 0, 5, true, true, true, true, callback);
     }
     else //no value for serach
     {
        $get(hidenLoadedPointName).value = "0,0";
        document.getElementById("tdmap").style.display = "none";
     }
}

function callback(a,b,c,d,e) {

     if (c != null && c.length >= 1)
     {
        var results="";
        currSearchResult = null;
        
        for (x=0; x<c.length; x++)
        {
           currSearchResult = c;
           CurrentLocation = c[0].LatLong; 
           $get(hidenLoadedPointName).value= c[0].LatLong.Latitude + "," + c[0].LatLong.Longitude
           
           results+="<a href='javascript:UpdateAndPresentCurrentLocation(" + x +");'>"+c[x].Name+"</a><br>";
        }
        document.getElementById('resultDiv').innerHTML=results;
     }
}

function UpdateAndPresentCurrentLocation(dataIndex)
{
    if(currSearchResult != null)
    {
        var tmp = currSearchResult[dataIndex];
        
        try
        {
            CurrentLocation = tmp.LatLong;
            map.Find(null,tmp.Name);
            $get(hidenLoadedPointName).value= tmp.LatLong.Latitude + "," + tmp.LatLong.Longitude
        }
        catch(err)
        {
            alert(err);
        }
    }
}
///////////////////////////////////////////////////////
