﻿// JScript 檔
var map = null;
var latlngArray=null;
var ThisCarDeviceId=null;
var AutoScaleFlagBool=null;
function MapSetCenter(y, x, speed, deviceid, direction, carnumber, carjob , AutoScaleBool)
{  
    if(!isDefined(map)) 
    {
        map = new IMap(document.getElementById("map"));
        map.setCenter(new ILatLng(y,x),7);
        map.addControl(new IZoomControl());
        map.addControl(new IScaleControl());
        PPG_STATUS_CONTROL = new StatusControl();
        map.addControl(PPG_STATUS_CONTROL);
        
	// ICON Image 1 Start //
        var point1 = new ILatLng(25.327605, 51.464875);
        var icon1 = new IIcon();
        icon1.image = 'img/qatar.png';
        icon1.iconSize = new ISize(80, 65);  
        icon1.iconAnchor = new IPoint(41, 10);
        
        var landmark1 = new IMarker(point1, icon1);

	//listen click event		
	IEvent.addListener(landmark1,'click',function(){
		window.open('http://www.qatarnavigator.com', 'Qatarnavigator',
		config = 'height=400,width=600,menubar=1,resizable=1,scrollbars=yes')
});
	//add on the map
	map.addOverlay(landmark1);

	// ICON Image 1 End //

	// ICON Silk Start //
	var point2 = new ILatLng(25.321358, 51.489188);
	var icon2= new IIcon();
	icon2.image = 'img/silk.png';
	icon2.iconSize = new ISize(50, 40);
	icon2.iconAnchor = new IPoint(41, 10);

	var landmark2 = new IMarker(point2, icon2);
	map.addOverlay(landmark2);
	// ICON Silk END //
	//ICON Qtel //
	
	var qtelpnt = new ILatLng(25.323388, 51.537243);
	var qtelicon = new IIcon();
	qtelicon.image = 'img/Qtel.png';
	qtelicon.iconSize = new ISize(60, 45);
	qtelicon.iconAnchor = new IPoint(41, 10);
	var qtellndmrk = new IMarker(qtelpnt, qtelicon);
	IEvent.addListener(qtellndmrk, 'click', function() {
	    window.open('http://219.84.151.30/icons/qtel.htm', 'Qatarnavigator',
		config = 'height=325,width=500')
	});
	//add on the map
	map.addOverlay(qtellndmrk);


	// ICON qtel 1 End //

		
//        Gmap = new IMap(document.getElementById("map")); //set center and zoom
//        Gmap.setCenter(new ILatLng(y,x), 8); 
//        Gmap.addControl(new SmallZoomControl());
//       
//        var ppgicon = new IIcon();
//        ppgicon.image = 'img/caravl.png';
////        ppgicon.shadow = 'http://192.168.100.157/mapstation/img/shadow.png';
//        ppgicon.iconSize = new ISize(24, 24);
////        ppgicon.shadowSize = new ISize(115, 58);
//        ppgicon.iconAnchor = new IPoint(41, 10);
//        ppgicon.infoWindowAnchor = new IPoint(0, 115);
//        ppgicon.transparent = null;
//        ppgicon.imageMap = null;
//        
//        DESTINATION_MARKER = new IMarker(new ILatLng(y,x), ppgicon);
//    
//        Gmap.addOverlay(DESTINATION_MARKER);
//        


//         IEvent.addDomListener(document.getElementById("map"), 'contextmenu', function (e)
//             {
//                  var x=e.clientX;
//                  var y=e.clientY;
//                  var r=confirm("設為目的地?");
//                  if(r)
//                  {
//                    var scrPoint = new IPoint(x,y);
//                    var il= Gmap.fromContainerPixelToLatLng(scrPoint);
////                    var txtX=document.getElementById("txtX");
////                    var txtY=document.getElementById("txtY");
////                    txtX.value=il.lng();
////                    txtY.value=il.lat();
//           
////                   DESTINATION_MARKER = new IMarker(new ILatLng(il.lat(),il.lng()), ppgicon);
////    
////                   Gmap.addOverlay(DESTINATION_MARKER);
//                   
//                    DESTINATION_MARKER.setLatLng(new ILatLng(il.lat(),il.lng()));
//                  }
//             }
//         );
    }
    else 
    {   
        
        var ppgicon = new IIcon();
        
        if(22.5 < direction && direction < 67.5)
        {
           strimg='img/car_45.png';
        }
        else if (67.5 < direction && direction < 112.5)
        {
          strimg= 'img/car_90.png';
        }
        else if (112.5 < direction && direction < 157.5)
        {
          strimg= 'img/car_135.png';
        }
        else if (157.5 < direction  && direction < 202.5)
        {
          strimg= 'img/car_180.png';
        }
         else if (202.5 < direction && direction < 247.5)
        {
          strimg= 'img/car_225.png';
        }
         else if (247.5 < direction && direction < 292.5)
        {
          strimg= 'img/car_270.png';
        }
        else if (292.5 < direction && direction < 337.5)
        {
           strimg= 'img/car_315.png';
        }
        else
        {
           strimg='img/car_0.png';
        }
        
        ppgicon.image = strimg;
        ppgicon.iconSize = new ISize(24, 24);
        ppgicon.iconAnchor = new IPoint(12, 12);
        ppgicon.infoWindowAnchor = new IPoint(0, 24);
        ppgicon.transparent = null;
        ppgicon.imageMap = null;
        
        var latlng=new ILatLng(y,x);
         if(!isDefined(latlngArray))
         {
           latlngArray=new Array();
         } 
        
          latlngArray.push(latlng);
          
          if (speed<80)
          {
            speed="<span style='color:#0000FF'>"+ speed +"</span>";
          }
          else if (speed<100)
          {
            speed="<span style='color:#009900'>"+ speed +"</span>";
          }
          else if (speed<120)
          {
            speed="<span style='color:#000000'>"+ speed +"</span>";
          }
          else
          {
            speed="<span style='color:#FF0000'>"+ speed +"</span>";
          }
        
            DESTINATION_MARKER = new IMarker(latlng, ppgicon);
            DESTINATION_MARKER.x = x;
            DESTINATION_MARKER.y = y;
            DESTINATION_MARKER.speed = speed;
            DESTINATION_MARKER.deviceid=deviceid;
            DESTINATION_MARKER.setCarNo = carnumber;
            
            if(isDefined(ThisCarDeviceId)) 
            if (ThisCarDeviceId==deviceid)
            {
             PPG_STATUS_CONTROL.setLatitude(y);
             PPG_STATUS_CONTROL.setLongitude(x);
             PPG_STATUS_CONTROL.setSpeed(speed);
             PPG_STATUS_CONTROL.setCarNo(carnumber);
             PPG_STATUS_CONTROL.setJob(carjob);
            }
            
            IEvent.addListener(DESTINATION_MARKER, 'click', function() {
            PPG_STATUS_CONTROL.setLatitude(y);
            PPG_STATUS_CONTROL.setLongitude(x);
            PPG_STATUS_CONTROL.setSpeed(speed);
            PPG_STATUS_CONTROL.setCarNo(carnumber);
           
            
            ThisCarDeviceId=deviceid;
            
        });
        
        map.addOverlay(DESTINATION_MARKER);
       // map.setCenter(new ILatLng(y,x));

//    var p1, p2, p3; //ILatLng
        AutoScaleFlagBool=AutoScaleBool.toLowerCase();
       // alert(AutoScaleFlagBool);
        if (AutoScaleFlagBool=="true")
        {
          AutoScale(x,y);
        }
       
    
    //map.setZoom(level);   
   }
}
function AutoScale(x,y)
{
    if (latlngArray.length==1)
        {
           map.setCenter(new ILatLng(y,x));
        }
        else
        {
            var bound = new ILatLngBound();
            for (var i=0; i<latlngArray.length ; i++)
            {
              bound.extend(latlngArray[i]);
            }
            var level = map.getCurrentMapType().getBoundZoomLevel(bound, map.getSize());
            
            var ne = bound.getNorthEast();
            var sw = bound.getSouthWest();
           
            map.setCenter(new ILatLng((ne.lat() + sw.lat()) / 2, (ne.lng() + sw.lng()) / 2),level);
        }
}
function CarStatus(CarNumber)
{
  PPG_STATUS_CONTROL.setCarNo(CarNumber);
}
function ClearCar()
{
  map.clearOverlays();
  if(isDefined(latlngArray))
  {
    latlngArray.length = 0;
  }
}

function SetMap(map_path)
{
  PPG_BASE_TILE_URL =map_path;
}

function PrintMap()
{
  var Fdiv=$get("tabs0");
  var PrintBtn=$get("Printbtn");
  var ExportBtn=$get("btnExport");
  var Header=$get("header");
  var Bottom=$get("bottom");
  
   Fdiv.style.display="None";
   PrintBtn.style.display="None";
   ExportBtn.style.display="None";
   Header.style.display="None";
   Bottom.style.display="None";
   
   window.print();
   
   Fdiv.style.display="block";
   PrintBtn.style.display="block";
   ExportBtn.style.display="block";
   Header.style.display="block";
   Bottom.style.display="block";
}
function gotoqatar(x, y) {
    map.setCenter(new ILatLng(x, y), 13);
    var qtelicon = new IIcon();
    qtelicon.image = 'img/Qtel.png';
    qtelicon.iconSize = new ISize(60, 45);
    qtelicon.iconAnchor = new IPoint(41, 10);
    var qtellndmrk = new IMarker(new ILatLng(x, y), qtelicon);
   
    //add on the map
    map.addOverlay(qtellndmrk);
  

}




