﻿var CarMaps = {Maps: []}
CarMaps.InitMap = function(div,id,w,h){
    if (CarCurrent.timer==null) CarCurrent.Update();
    this.Maps[id] = new MapsClass(div,id,w,h);
}
CarMaps.UpdateData = function(Json)
{
    for (var i=0; i<Json.length; i++) {
        if (this.Maps[Json[i].Maps]!=null) {
           this.Maps[Json[i].Maps].UpdateMarkers(Json[i].Maps,Json[i].Markers);
        }
    }
}

CarMaps.OnOpenMap = function(MapId){
    if (this.Maps[MapId]!=null){ 
        return true;}
        else 
        {return false;}
}

CarMaps.SelectCar = function(MapId,CarId){
    if (this.Maps[MapId]!=null) {
        for (var i=0; i<this.Maps[MapId].Markers.length; i++) {
            if (this.Maps[MapId].Markers[i].CarId==CarId) {
            	this.Maps[MapId].Size=this.Maps[MapId].Map.getZoom();
                this.Maps[MapId].Map.setCenter(this.Maps[MapId].Markers[i].Marker.getLatLng(),this.Maps[MapId].Size);
                if (this.Maps[MapId].Selectcar>-1) this.Maps[MapId].Markers[this.Maps[MapId].Selectcar].OnSelect(false);
                this.Maps[MapId].Selectcar=i;
                this.Maps[MapId].Markers[i].OnSelect(true);
                this.Maps[MapId].Mm.refresh();
            }
        }
    }
}

CarMaps.AddLinesG = function(MapId,CarId,points,ColorLine,Name){
    if (this.Maps[MapId]!=null) this.Maps[MapId].AddLines(CarId,points,ColorLine,Name);  
}

CarMaps.RemoveLinesG = function(MapId,CarId){
     if (this.Maps[MapId]!=null) this.Maps[MapId].RemoveLine(CarId);
}

MapsClass = function(div,id,w,h) {
    this.DivObj = div;
    this.id=id;
    this.Size=13;
    this.Map= new GMap2(div,{size:new GSize(w,h)});
    this.Map.setCenter(new GLatLng(54.717531, 20.514135),this.Size);
    this.Map.addControl(new GSmallMapControl());
    this.Map.addControl(new GMapTypeControl());
    this.Mm = new GMarkerManager(this.Map, {borderPadding:1,trackMarkers:true});
    this.Markers = [];
    this.Polylines = [];
    this.ColorLine='0000FF';
    this.Enable=true;
    this.CenterMarker=1;
    this.IsMove=false;
    this.Selectcar=-1;
}
MapsClass.prototype.EventMarkers = function(Marker) {
	GEvent.bind(Marker.Marker, 'click', Marker, this.onMarkerClick);
}
	
MapsClass.prototype.onMarkerClick = function() {
   this.Marker.openInfoWindowHtml(this.html);
}

MapsClass.prototype.AddLines = function(CarId,points,ColorLine,Name){
    if (this.Polylines[CarId]!=null) {
        this.Map.removeOverlay(this.Polylines[CarId].Line);
        this.Map.removeOverlay(this.Polylines[CarId].Mark.Marker);
    }
    this.Polylines[CarId] =new LinesClass(CarId,points,ColorLine,Name);
    this.EventMarkers(this.Polylines[CarId].Mark);
    this.Map.setCenter(points[points.length-1],this.Size);
    this.Map.addOverlay(this.Polylines[CarId].Line);
    this.Map.addOverlay(this.Polylines[CarId].Mark.Marker);
//    var a= this.Polylines[CarId].Line.getVertex(3);
}

MapsClass.prototype.RemoveLine = function(CarId){
    this.Map.removeOverlay(this.Polylines[CarId].Line);
    this.Map.removeOverlay(this.Polylines[CarId].Mark.Marker);
}

MapsClass.prototype.SetMarker = function(CarId,time){
    var np= AllCarLines.CarLines[CarId].DtPoint.length;
    for (var i=0; i< np; i++)
        if (AllCarLines.CarLines[CarId].DtPoint[i]!=null) 
           if (AllCarLines.CarLines[CarId].DtPoint[i].Time>time) break;
   	try{
    		if (i<np ) {
   
    			this.Polylines[CarId].Mark.html=  'Ник:    '+AllCarLines.CarLines[CarId].Name+'<br>'+
                                          'Время:  '+(new Date(AllCarLines.CarLines[CarId].DtPoint[i].Time)).format('Y.m.d h:i:s')+'<br>'+
                                          'Скорость:  '+AllCarLines.CarLines[CarId].DtPoint[i].Speed+'<br>'+
                                          'От старта:'+this.Polylines[CarId].Distance(AllCarLines.CarLines[CarId].DtPoint[i].Time)+ ' Км.';
                                      //    +'<br>'+'Описание:'+ Jsons.html;
    			this.Polylines[CarId].Mark.Marker.setPoint(AllCarLines.CarLines[CarId].DtPoint[i]);
    		}
    		else this.Polylines[CarId].Mark.Marker.setPoint(AllCarLines.CarLines[CarId].DtPoint[np-1]);
    	} 
    	catch (err){e=err}
}

MapsClass.prototype.UpdateMarkers = function(mapid,Jsons) {
    for (var i=0; i<Jsons.length; i++) {
        var point = new GLatLng(Jsons[i].lat, Jsons[i].lng);
        if (this.Markers[i]!=null){
         this.Markers[i].html= 'Ник:    '+Jsons[i].title+'<br>'+
                      'Время:  '+Jsons[i].time+'<br>'+
                      'Скорость:  '+Jsons[i].Speed+'<br>'+
                      'Долгота:' +Jsons[i].lat+'<br>'+
                      'Широта: ' +Jsons[i].lng+'<br>'+
                      'Описание:'+ Jsons[i].html;
         this.Markers[i].Marker.setPoint(point);
        }else{
            this.Markers[i] = new MarkersClass(point,Jsons[i]);
            CarMaps.Maps[mapid].EventMarkers(this.Markers[i]);;
            
            this.Mm.addMarker(this.Markers[i].Marker,0);
        }
    }
    this.Mm.refresh();
    if (this.IsMove) this.Map.setCenter(this.Markers[this.CenterMarker].Marker.getLatLng());
}

MapsClass.prototype.RemoveMarkers = function(id){
    this.Mm.removeMarker(this.Markers[id]);
    this.Markers.splice(id, 1);
}

MapsClass.prototype.FindCar = function(CarId){
    for (var i=0; i<this.Markers.length; i++) {
        if (this.Markers[i].CarId==CarId) return this.Markers[i].Marker;
    }
    return null;
}

MarkersClass = function (point,Jsons){
    this.arrowIcon = new GIcon();
    if (Jsons.hasOwnProperty('img')) {
        this.img=Jsons.img;
        this.imgsel=Jsons.img;
    }else { 
        this.img= 'img/Dodge48.png';
        this.imgsel='img/Dodge48sel.png';
    }    
    this.arrowIcon.iconSize = new GSize(40,40);
    this.arrowIcon.shadowSize = new GSize(1,1);
    this.arrowIcon.iconAnchor = new GPoint(20,20);
    this.arrowIcon.image = this.img;
    this.arrowIcon.infoWindowAnchor = new GPoint(20,20);
    this.CarId=Jsons.CarId;
    this.html= 'Ник:    '+Jsons.title+'<br>'+
               'Время:  '+Jsons.time+'<br>'+
               'Скорость:  '+Jsons.Speed+'<br>'+
               'Описание:'+ Jsons.html;
    this.title=Jsons.title;
    this.time=Jsons.time;
    this.Marker=new GMarker(point,{ icon: this.arrowIcon,title: this.title});    
}

MarkersClass.prototype.OnSelect = function(ok) {
    if (ok) this.arrowIcon.image = this.imgsel;
    else this.arrowIcon.image = this.img;
}

LinesClass = function (CarId,points,color,Name){
    this.CarId=CarId;
    this.Name=Name;
    this.Line= new GPolyline(points,color, 2,1); 
    this.Json={CarId: CarId,html: 'Старт',title: Name,time: (new Date(points[0].Time)).format('Y.m.d h:i:s'),img:'img/prism.png', Speed : points[0].Speed};
    for (var i=0; i<points.length; i++){
        if (points[i]!=null) break;}
    this.Mark= new MarkersClass(points[i],this.Json);    
}

LinesClass.prototype.Distance = function(time) {
  var dist = 0;
  for (var i=1; i < this.Line.getVertexCount(); i++) {
    var t=this.Line.h[i];
    if (t.Time>time) break;
    dist += this.Line.getVertex(i).distanceFrom(this.Line.getVertex(i-1));
  }
  return (dist/1000).toFixed(3);
}



