﻿// JScript File for default.aspx
var map = null;
var pinID = 0;
var selectedTypes = "";
var controlId = "";


//initial call to create map
function GetMap()
{
     
  map = new VEMap('MapDiv');
  
  MapControl.Features.ScaleBarKilometers = false;
  
  controlId = document.getElementById('hidModuleId').value;
  
  var centerLat = document.getElementById('dnn_ctr' + controlId + '_AIPOIMap_hidCenterLat').value;
  var centerLong = document.getElementById('dnn_ctr' + controlId + '_AIPOIMap_hidCenterLong').value;
  var centerZoomLevel = document.getElementById('dnn_ctr' + controlId + '_AIPOIMap_hidZoomLevel').value;
  
 
  
  if (centerLat == '' || centerLong == '') {
        map.LoadMap();    
  }
        
  else {
  map.LoadMap(new VELatLong(centerLat, centerLong), centerZoomLevel ,'r' ,false);
  }
  
  
  map.AttachEvent("onchangeview", DoAjaxQuery);
  
  GetSelectedTypes('DeleteThis');
  
  //DoAjaxQuery();
}   

function GetSelectedTypes(idVal)
{
    selectedTypes = "";
    var frm = document.forms[0];
   
    
   
   for (i=0; i < frm.length; i++)
   {
        if (idVal.indexOf ('DeleteThis') != -1)
        {
             
            if(frm.elements[i].checked == true)             
			{    
			    if(frm.elements[i].name == 'DeleteThis') {
			    selectedTypes += frm.elements[i].value + ','; 
			   }       
				  
			}
        }
   } 
   

   var txtBigTaskTitle = document.getElementById('dnn_ctr' + controlId + '_AIPOIMap_hidPOITypes');
    txtBigTaskTitle.value = selectedTypes;
   
    
   __doPostBack('dnn_ctr' + controlId + '_AIPOIMap_lnkRefreshBigTasks','');
   
   if (selectedTypes != '') {
   DoAjaxQuery();
   }
   else {
   map.Clear();
   }
}

function CenterOnSelected(selLat, selLong)
{
    var selZoom = map.GetZoomLevel();
    map.SetCenterAndZoom(new VELatLong(selLat, selLong), selZoom);
   DoAjaxQuery(); 
}

//show a loading label
function ShowLoading()
{
  var el = document.createElement("div"); 
  el.setAttribute('id',"VELoading");
  //Now we should know the width and height of the VE map or else go and get it.
  var curr_width = 600;
  var curr_height = 600;
  el.style.top = ((curr_height - 25) / 2) + "px";
  el.style.left = ((curr_width - 105) / 2) + "px";
  el.style.border = "1px solid gray";
  el.style.font = "12px arial";
  el.style.background = "White";
  el.style.padding = "2px";
  el.style.verticalAlign = "middle";
  el.innerHTML = "<img src='/DesktopModules/AIMapping/images/spinner.gif' /> Please Wait. Loading data....";  
  map.AddControl(el);
}

//remove loading label
function HideLoading()
{
  var el = document.getElementById("VELoading");
  el.parentNode.removeChild(el);
}


//helper function for AJAX
function GetXmlHttp()
{
  var x = null;
  try
  {
    x = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e)
  {
    try
    {
      x = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e)
    {
      x = null;
    }      
  }
  if (!x && typeof XMLHttpRequest != "undefined")
  {
    x = new XMLHttpRequest();      
  }
  return x;
}

//call the AJAX query.
function DoAjaxQuery()
{
    var mapstyle = map.GetMapStyle();
    
   if (mapstyle == "o") {
      //Build the url to call the server
      //var be = map.GetBirdseyeScene();
      map.Clear();
      var ptl = document.getElementById("poiTypeList").style.visibility = 'hidden';
      
      
      
  //var url = "/DesktopModules/AIMapping/GetData.aspx?";
    //var pixel = new VEPixel(0, 0);
//  url += "tl=" + map.PixelToLatLong(pixel);
    //url += "tl=" + be.PixelToLatLong(0,0, map.GetZoomLevel());
    
  
  //Now we should know the width and height of the VE map or else go and get it.
   //var curr_width = 600;
   //var curr_height = 600;

  //url += "&w=" + curr_width;
  //url += "&h=" + curr_height;
   
   //var pixel2 = new VEPixel(curr_width, curr_height);
   
  //url += "&br=" + map.PixelToLatLong(pixel2);
  //url += "&br=" + be.PixelToLatLong(curr_width, curr_height, map.GetZoomLevel());
  //alert("HERE");
  //url += "&z=" + map.GetZoomLevel();
  //url += "&pt=" + selectedTypes;
  
  //alert(url);
   }
   else
   {
   var ptl = document.getElementById("poiTypeList").style.visibility = 'visible';
     //Build the url to call the server
  var url = "/DesktopModules/AIMapping/GetData.aspx?";
  var pixel = new VEPixel(0, 0);
  url += "tl=" + map.PixelToLatLong(pixel);
  
  //Now we should know the width and height of the VE map or else go and get it.
   var curr_width = 600;
   var curr_height = 600;

  url += "&w=" + curr_width;
  url += "&h=" + curr_height;
   
   var pixel2 = new VEPixel(curr_width, curr_height);
   
  url += "&br=" + map.PixelToLatLong(pixel2);
  url += "&z=" + map.GetZoomLevel();
  url += "&pt=" + selectedTypes;
  
  //put up a loading label
  ShowLoading();

  //Start by getting the appropriate XMLHTTP object for the browser
  var xmlhttp = GetXmlHttp();
  
  

  //If we have a valid xmlhttp object
  if (xmlhttp)
  {

    xmlhttp.open("GET", url, true); // varAsync = true;
    
   //Set the callback.  This function is called when we 
    xmlhttp.onreadystatechange = function()
    {
      if (xmlhttp.readyState == 4)  //4 is a success
      {
        //Server code creates javascript "on the fly" for us to
        //execute using eval()
        
        var result = xmlhttp.responseText;
        //alert(result);
        eval(result);
      }
    }
    xmlhttp.send(null); 
    
  }
   }

  
  
  
}
