var map = null;
//-----------------------------------------------------
// Runs on Page Load
//-----------------------------------------------------            
 function GetMap()
 {
  
  // Hide the driving directions
  HideMappingItems('none');
  document.getElementById('ShowMsg').style.display = 'none';
  
  // Hide the content for search
   document.getElementById('MainContentNV').style.display = 'none';
    
  map = new VEMap('myMap');
  map.LoadMap();
  map.AttachEvent('onclick', ChangeMapStyle);
  
 }
//-----------------------------------------------------
//----------------------------------------------------- 
function DisableItems(turnOff)
{

var color;

if (turnOff==true)
{
 color ='gray';
}
else
{
 color ='black';
}

document.getElementById('BtnLocation').disabled = turnOff; 
document.getElementById('BtnLocation').style.color = color; 

document.getElementById('BtnSpellLOC').disabled = turnOff; 
document.getElementById('BtnSpellLOC').style.color = color; 

document.getElementById('BtnRoute').disabled = turnOff; 
document.getElementById('BtnRoute').style.color = color; 

document.getElementById('btnDirections').disabled = turnOff; 
document.getElementById('btnDirections').style.color = color; 

document.getElementById('btnSpellDir').disabled = turnOff; 
document.getElementById('btnSpellDir').style.color = color; 

document.getElementById('BtnMap').disabled = turnOff; 
document.getElementById('BtnMap').style.color = color; 


}
//-----------------------------------------------------
//-----------------------------------------------------        
 function GetMapRoute(RouteType)         
 {       
 
 HideMappingItems('none');
  
 var vFrom = trim(document.getElementById('TxtFrom').value);
 var vTo = trim(document.getElementById('TxtTo').value);
 
 if (vFrom == '')
 {
  alert('Enter a From Location');
  return;
 }
			 
 if (vTo == '')
 {
  alert('Enter a To Location');
  return;
 }


   //Show Processing Message	
//  document.getElementById('ShowMsg').style.display = 'inline'; 
  DisableItems(true); 
  
   //Make the page refresh so the message shows up
  alert('Route drawing may take from 1 to 25 seconds to complete. \n\n Click OK to proceed');
  
  map.SetMapStyle(VEMapStyle.Road);
  if (RouteType == 'route')
   {
     map.GetRoute(vFrom, vTo);    
   }
   else
   {
    map.GetRoute(vFrom, vTo, null, null, onGotRoute); 
   }

   
  // Hide Message
  //document.getElementById('ShowMsg').style.display = 'none';
  DisableItems(false); 
       
 }
//--------------------------------------------------
//--------------------------------------------------
//function GetDirNewWindow(vFrom, vTo)
//{
//  GetMap();
//  map.GetRoute(vFrom, vTo, null, null, onGotRoute); 
//}
//--------------------------------------------------

//--------------------------------------------------
function onGotRoute(route)         
{  

       
  //var routeinfo="Route info <br>";     
  var routeinfo="";          
  routeinfo+="<b>Total distance: ";           
  routeinfo+=   route.Itinerary.Distance+" ";            
  routeinfo+=   route.Itinerary.DistanceUnit+"</b><br>";                        

  var steps="";            
  var len = route.Itinerary.Segments.length;
  var leadingZero;
     
  DisableItems(true);   
  
   for(var i = 0; i<len ;i++)               
   {
     // Allign the 1) and 10) don't show step 0 this is the starting point
   if (i<10)
   {
   
	    // not zero put a leading zero on the step
	    if (i>0)
	    {
	      steps+= "0" + i + ")  " + route.Itinerary.Segments[i].Instruction+" -- ("  ;  
	    }
	    else
	    {
	      // it is zero START Message
	      steps+= '<b>' + route.Itinerary.Segments[i].Instruction+" -- ("  ; 
	    }
    }
   else
   {
     steps+= i + ")  " + route.Itinerary.Segments[i].Instruction+" -- ("  ;   
   }   
   
     steps+=route.Itinerary.Segments[i].Distance+") ";                  
     steps+=route.Itinerary.DistanceUnit + "<hr>";    
     
     // Break after START Message 
     if (i==0)
     {
       steps+="</b>";
     }      
   }            
   
   document.getElementById('DivDIR').innerHTML = '';
   routeinfo+=steps +"<div align='center'> <b>SURFThru.com</b><br>Use mouse wheel to zoom in on the map below</div>";    
   document.getElementById('DivDIR').innerHTML = routeinfo;

  DisableItems(false);
  
  // Show the driving directions table
  HideMappingItems('inline');
  
}

//-----------------------------------------------------
// Show/hide the table on top of the map with driving
// directions
//----------------------------------------------------- 
function HideMappingItems(HideStatus)
{
  //Hide Status = 'none' or 'inline'
  document.getElementById('DirectionsTable').style.display = HideStatus; 
  
  //Clear the text box
  if (HideStatus=='none')
  {
    document.getElementById('DivDIR').innerhtml = ' '; 
  }
  
}
//-----------------------------------------------------
//-----------------------------------------------------
function FindLoc()         
{         

  HideMappingItems('none');
  
  var loc = trim(document.getElementById('txtLocation').value);
  
  if (loc == '')
  {
   alert('Enter a location');
   return;
  }
  
  DisableItems(true);
 
 map.SetBirdseyeScene('3695548');   
  map.FindLocation(loc);  
  
  DisableItems(false);

}	
//-----------------------------------------------------
//-----------------------------------------------------	
function ChangeMapStyle()         
{            
 
  var s=map.GetMapStyle();   
  if (s==VEMapStyle.Hybrid)
  {               
   map.SetMapStyle(VEMapStyle.Road);            
  }            
  else            
  {               
   map.SetMapStyle(VEMapStyle.Hybrid);            
  }         

}
//-----------------------------------------------------
// Removes leading whitespaces
//-----------------------------------------------------
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}
//-----------------------------------------------------
// Removes ending whitespaces
//-----------------------------------------------------
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}
//-----------------------------------------------------
// Removes leading and ending whitespaces
//-----------------------------------------------------
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}
function PrintPage() {
window.print();  
}

