var m_nMap=1;
var g_mapsource="google";
var m_bmaptypechange=false;
var m_a;
var g_locs; 
var m_nStartHistory=0;
var m_oMapc;
var m_oMapcQuery;
var m_bShowTraceLine=true;
var m_bShowTracePt=false;
var m_btraffic=false;

function load(){ 
 initmapstatus();
 //inittree(); 
  if (g_mapsource=="google") load_google();
}

function maploaded(){   

      m_bmaptypechange=true;
      //getTargetList();
}

function getoff(y,x){
    return {offy:0,offx:0};
  	if (g_latlon_correct==false) return {offy:0,offx:0};
    if (m_nMap!=1) return {offy:0,offx:0};
  	var off = a.o(x*1000000,y*1000000); 
  	if(off == null)
    {
    	off={offy:0,offx:0};
    }
    return off;
  }
  
  function getfake(y,x){
    return {y:y,x:x};
  	if (g_latlon_correct==false) return {y:y,x:x};
    if (m_nMap!=1) return {y:y,x:x};
  	var off = m_a.o(x*1000000,y*1000000);
  	var fake={y:y,x:x}; 
  	if(off != null)
    {
    	fake={y:(y+off.offy),x:(x+off.offx)};
    }
    return fake;
  }

  function getarea(y,x){
  	var off = m_a.o(x*1000000,y*1000000);
  	var area=""; 
  	if(off != null)
    {
    	area=off.area;
    }
    return area;
  }

function viewTarget(index){
  if (g_mapsource=="google") viewTarget_google(index);
}

function setzoom(zoom){
  if (g_mapsource=="google") setzoom_google(zoom);
}

function zoomin(){
  if (g_mapsource=="google") zoomin_google();
}

function zoomout(){
  if (g_mapsource=="google") zoomout_google();
}

function pan(){
}

function setcenter(lat,lon){
  if (g_mapsource=="google") setcenter_google(lat,lon);
}


function setTipCenter(strtip){
   if (g_mapsource=="google")  setTipInfo_google(m_dCenterLat,m_dCenterLon,strtip,1);       
     
}


function setmaptype(type){
 if (g_mapsource=="google"){
   if (type==1 || type==2){
     setmaptype_google(type);
     return;
   }
 }
}

function fastfocuspoint(lat,lon){
    var arr;
    if (lat.length<6 || lon.length<7) return false;   
    arr=lat.split(" ");
    if (arr.length!=2) return false;   
    var dlat=arr[0]+"."+((Number(arr[1])/60).toFixed(6)+"").substr(2);
    arr=lon.split(" ");
    if (arr.length!=2) return false;   
    var dlon=arr[0]+"."+((Number(arr[1])/60).toFixed(6)+"").substr(2);
    focusalarm(dlat,dlon);
}

function focusalarm(lat,lon){
   if (g_mapsource=="google") focusalarm_google(lat,lon);
   setcenter(lat,lon);
}

function focushistory(lat,lon){
   if (g_mapsource=="google") focushistory_google(lat,lon);
   setcenter(lat,lon);
}


function createMarker(lat,lon,heading,focus,alarm,title,strtip,strid) {
   var index;
   if (g_mapsource=="google") index=createMarker_google(lat,lon,heading,focus,alarm,title,strtip,strid);
   return index;
}

function setMarker(index,lat,lon,heading,focus,alarm,title,strtip,showtail,strid){
   if (g_mapsource=="google") setMarker_google(index,lat,lon,heading,focus,alarm,title,strtip,showtail,strid);
}

function addDrawByPos(lon1,lat1,lon2,lat2)
{
   if (g_mapsource=="google") addDrawByPos_google(lon1,lat1,lon2,lat2);
   setcenter(Number(lat1),Number(lon1));
}

function AddPolyline(pset,pcount){
   var index;   
   if (g_mapsource=="google") index=AddPolyline_google(pset,pcount);
   return index;
}

function deletePolyline(index){
   if (g_mapsource=="google") deletePolyline_google(index);
}

function initplot(){
   if (g_mapsource=="google") initplot_google();
}

function plotPoint(){
   if (g_mapsource=="google") plotPoint_google();
} 

function plotPointMove(){
   if (g_mapsource=="google") plotPointMove_google();
} 

function deleteMarker(index){
   if (g_mapsource=="google") deleteMarker_google(index);
   deleteLable(index);
}

function deleteLable(index){
   if (g_mapsource=="google") deleteLable_google(index);
}


function clearalarmquery(){
  if (g_mapsource=="google") clearalarmquery_google();
}

function clearhistoryquery(){
  if (g_mapsource=="google") clearhistoryquery_google();
}

function measurelen(){
  if (g_mapsource=="google") measurelen_google();
}

function resetmap(){
  if (g_mapsource=="google") resetmap_google();
}

function reloadMap(){
    resetmap();
}

function cleartraceLinesActive(){
  if (g_mapsource=="google") cleartraceLinesActive_google();
}

function drawrect(){
  if (g_mapsource=="google") drawrect_google();
}

function mapresize(){
  if (g_mapsource=="google") mapresize_google();
}

function getAddressBylatlngForCenter(lat,lng){
  if (g_mapsource=="google") getAddressBylatlngForCenter_google(lat,lng);
}

function getAddressBylatlng(lat,lng){
  if (g_mapsource=="google") getAddressBylatlng_google(lat,lng);
}

function toggleTraffic(){
  if (g_mapsource=="google")  toggleTraffic_google();
}

 
function getRuleAngleByHeading(heading){
 if (heading<30 || heading>=330)  return 0;
 if (heading>=30 && heading<60)  return 45;
 if (heading>=60 && heading<120) return 90;
 if (heading>=120 && heading<150) return 135;
 if (heading>=150 && heading<210) return 180;
 if (heading>=210 && heading<240) return 225;
 if (heading>=240 && heading<300) return 270;
 if (heading>=300 && heading<330) return 315;
}


//connect id and marker to opt it easyly;
function MarkerTarget(p_strID,p_oMarker){
this.m_strID=p_strID;
this.m_Marker=p_oMarker;
}

function initmapstatus(){

var czoom=GetCookie("czoom");
var ccenterx=GetCookie("ccenterx");
var ccentery=GetCookie("ccentery");
if (czoom!="" && ccenterx!="" && ccentery!=""){
   m_nCurscale=Number(czoom);
   m_dCenterLon=Number(ccenterx);
   m_dCenterLat=Number(ccentery);
}


}

function savemapstatus(){

SetCookie("czoom",m_nCurscale);
SetCookie("ccenterx",m_dCenterLon);
SetCookie("ccentery",m_dCenterLat);

}

function unloadmap(){
if (g_mapsource=="google") GUnload();
}

function gettargettip(oTarget){
       if(oTarget==null) return "";
       var strtip="<table width=100% cellspacing=0 cellpadding=0>";
       strtip=strtip+"<tr><td align=left>"+getStr(s_Caption_Group)+":"+oTarget.m_type+"</td></tr>";
       strtip=strtip+"<tr><td align=left>"+getStr(s_Caption_Time)+":"+UTC2DateStr(oTarget.m_lastPoint.time)+"</td></tr>";
       strtip=strtip+"<tr><td align=left><table width=100%  cellspacing=0 cellpadding=0><tr><td width=50%>"+getStr(s_Caption_Latitude)+":"+(oTarget.m_lastPoint.dY-oTarget.m_lastPoint.ddY).toFixed(6)+"</td><td width=50%>"+getStr(s_Caption_Longitude)+":"+(oTarget.m_lastPoint.dX-oTarget.m_lastPoint.ddX).toFixed(6)+"</td></tr></table></td></tr>";
       if(g_tp=="TP2"){
          strtip=strtip+"<tr><td align=left><table  width=100%    cellspacing=0 cellpadding=0><tr><td width=50%>"+getStr(s_Caption_Speed)+":"+(oTarget.m_lastPoint.speed*3.6/1.852).toFixed(1)+"("+getStr(s_Caption_SeaSpeedUnit)+")</td><td width=50%>"+getStr(s_Caption_Heading)+":"+oTarget.m_lastPoint.heading.toFixed(1)+"("+getStr(s_Caption_HeadingTo)+")</td></tr></table></td></tr>";
       }else{
          strtip=strtip+"<tr><td align=left><table  width=100%    cellspacing=0 cellpadding=0><tr><td width=50%>"+getStr(s_Caption_Speed)+":"+(oTarget.m_lastPoint.speed*3.6).toFixed(1)+"("+getStr(s_Caption_KMH)+")</td><td width=50%>"+getStr(s_Caption_Heading)+":"+oTarget.m_lastPoint.heading.toFixed(1)+"("+getStr(s_Caption_HeadingTo)+")</td></tr></table></td></tr>";
       }
       strtip=strtip+"</table>";
       return strtip;          
}
