

function mapaClass(nD){
    this.markers = [];
    this.additionalMarkers = [];
    this.additionalPMarkers = [];
    this.additionalSMarkers = [];
    this.nextMarkers = [];
    this.lines = [];
    this.nextLines = [];
    this.nazwaDiva = nD;
    this.icons = [];
  
    this.bounds = {minLat:90,
                   maxLat:-90,
                   minLng:180,
                   maxLng:-180};
    
    
    var mapOptions = {zoom: 10,
            center: new google.maps.LatLng(49.2,20),
            mapTypeId: google.maps.MapTypeId.TERRAIN,
            mapTypeControl: true
        };
        
    this.infoWindow = new google.maps.InfoWindow({content:"empty"});    
        
        //alert(this.nazwaDiva);
    this.mapa = new google.maps.Map(document.getElementById(this.nazwaDiva), mapOptions);
}


    mapaClass.prototype.setBoundsLimit = function(key,val){
        if(key == 'Lat'){
            this.bounds['minLat'] = Math.min(this.bounds['minLat'],val);
            this.bounds['maxLat'] = Math.max(this.bounds['maxLat'],val);
        }else if(key == 'Lng'){
            this.bounds['minLng'] = Math.min(this.bounds['minLng'],val);
            this.bounds['maxLng'] = Math.max(this.bounds['maxLng'],val);
        }
    }
    
    mapaClass.prototype.setBounds = function(){
        if(this.markers.length > 0){
            var boundsMin = new google.maps.LatLng(this.bounds['minLat'], this.bounds['minLng']);
            var boundsMax = new google.maps.LatLng(this.bounds['maxLat'], this.bounds['maxLng']);
            var bds = new google.maps.LatLngBounds(boundsMin,boundsMax);
            this.mapa.fitBounds(bds);
        }
    }
    
    //funkcja którą wywołujemy jak usuwamy markery
    mapaClass.prototype.boundsLimitCalculate = function(){
        this.bounds = {minLat:90,maxLat:-90,minLng:180,maxLng:-180};
        
        if(this.markers.length >0){
            for(var i=0;i<this.markers.length;i++){
                this.setBoundsLimit('Lat', this.markers[i].position.lat());
                this.setBoundsLimit('Lng', this.markers[i].position.lng());
            }
            this.setBounds();
        }else{
            this.mapa.setCenter(new google.maps.LatLng(49.2,20));
            this.mapa.setZoom(10);
        }
    }
    
    mapaClass.prototype.addMarker = function(Lat,Lng,title,tekst,id,options,type){
        if(options.icon != '') options.icon = this.icons[options.icon];
        options.position = new google.maps.LatLng(Lat,Lng);
        options.title = title;
        options.map = this.mapa
        options.txt = tekst;
        //options = {position: new google.maps.LatLng(Lat,Lng),title: title,map: this.mapa, txt:tekst};
        var markerZ = new google.maps.Marker(options);
        google.maps.event.addListener(markerZ,"click",this.setInfoWindow(this.infoWindow));  
        this.setBoundsLimit('Lat', markerZ.position.lat());
        this.setBoundsLimit('Lng', markerZ.position.lng());
        
        if(type == null || type == 1) this.markers[id] = markerZ;
        else if(type == 2) this.additionalPMarkers[id] = markerZ;
        else if(type == 3) this.additionalSMarkers[id] = markerZ;
        else if(type == 4) this.nextMarkers.push(markerZ);
    }

    mapaClass.prototype.aMV = function(Lat,Lng,title,tekst,id,options,type){
        if(options.icon != '') options.icon = this.icons[options.icon];
        options.position = new google.maps.LatLng(Lat,Lng);
        options.title = title;
        options.map = this.mapa
        options.txt = tekst;
        //options = {position: new google.maps.LatLng(Lat,Lng),title: title,map: this.mapa, txt:tekst};
        var markerZ = new google.maps.Marker(options);
        markerZ.id = id;
        google.maps.event.addListener(markerZ,"click",this.setVisitedMarker(this));
        
        this.setBoundsLimit('Lat', markerZ.position.lat());
        this.setBoundsLimit('Lng', markerZ.position.lng());
        
        if(type == null || type == 1) this.markers[id] = markerZ;
        else if(type == 2) this.additionalPMarkers[id] = markerZ;
        else if(type == 3) this.additionalSMarkers[id] = markerZ;
        else if(type == 4) this.nextMarkers.push(markerZ);
    }
 
   mapaClass.prototype.setVisitedMarker = function(xD){
       return function(){
            if(this.icon.name == "r"){
              this.setIcon(xD.icons.z);  
              document.getElementById("fVP[" + this.id + "]").value = "1";
            }else{
              this.setIcon(xD.icons.r);  
              document.getElementById("fVP[" + this.id + "]").value = "0";
            } 
       }
   }
 
 
   mapaClass.prototype.deleteMarker = function(id,type) {
        if(type == null || type == 1) this.markers[id].setMap(null);
        else if(type == 2) this.additionalPMarkers[id].setMap(null);
        else if(type == 3) this.additionalSMarkers[id].setMap(null);
        else if(type == 4) this.nextMarkers[id].setMap(null); 
   }

   mapaClass.prototype.setInfoWindow = function(extraData){
       return function(){
            extraData.setContent(this.txt);  
            extraData.open(this.map,this);
       }
   }

    //mapaClass.prototype.addLine = function(sLat,sLng,fLat,fLng,color,opacity,weight,id){
    mapaClass.prototype.addLine = function(array,color,opacity,weight,id,type){
        var p = [];
        for(i=0;i<array.length;i++){
            p.push(new google.maps.LatLng(array[i].Lat,array[i].Lng));
        }
        var path = new google.maps.Polyline(
        {
            path: p,
            strokeColor: color,
            strokeOpacity: opacity,
            strokeWeight: weight
        });
        path.setMap(this.mapa);
        if(type == null || type == 1) this.lines[id] = path;
        else if(type == 2) this.nextLines.push(path);
   }

    mapaClass.prototype.outsideClick = function(id){
         google.maps.event.trigger(this.markers[id],'click');
    }
    
    mapaClass.prototype.addIcon = function(icon){
        this.icons[icon.name] = new google.maps.MarkerImage(icon.fileUrl,new google.maps.Size(icon.sizeWidth,icon.sizeHeight),new google.maps.Point(icon.startPoint1,icon.startPoint2),new google.maps.Point(icon.cPoint1,icon.cPoint2));
        this.icons[icon.name].name = icon.name;
    }
    
    
    
    
    function elevationService(elevId){
        this.elevId = elevId;
        
        this.path = [];
        this.elevator = new google.maps.ElevationService();
    }
    
    elevationService.prototype.addLine = function(array){
        for(i=0;i<array.length;i++){
            this.path.push(new google.maps.LatLng(array[i].Lat,array[i].Lng));
        }
   }
    
   elevationService.prototype.drawPath =  function drawPath() {
            // Create a PathElevationRequest object using this array.
            // Ask for 256 samples along that path.
           var pathRequest = {
            'path': this.path,
            'samples': 256
            };
            
            
            // Initiate the path request.
            this.elevator.getElevationAlongPath(pathRequest, this.plotElevation({elevId: this.elevId}));
           
    }

    



          // Takes an array of ElevationResult objects, draws the path on the map
          // and plots the elevation profile on a Visualization API ColumnChart.
    elevationService.prototype.plotElevation = function(extraData) {
                return function(results, status){
                var dane;
                if (status == google.maps.ElevationStatus.OK) {
                document.getElementById('elev' + extraData.elevId).style.display = 'block';
                elevations = results;
                                

              // Extract the data from which to populate the chart.
              // Because the samples are equidistant, the 'Sample'
              // column here does double duty as distance along the
              // X axis.

                dane = new google.visualization.DataTable();
                dane.addColumn('string', 'Trasa');
                dane.addColumn('number', 'Wysoko\u015bć');
                for (var i = 0; i < results.length; i++) {
                    dane.addRow(['', elevations[i].elevation]);
                }

            // Draw the chart using the data within its DIV.
            // Create a new chart in the elev DIV.
                new google.visualization.LineChart(document.getElementById('elev' + extraData.elevId))
                .draw(dane, {
                    width: 720,
                    height: 200,
                    legend: 'none',
                    colors: ['#aabbaa'],
                    titleY: 'Wysoko\u015bć (m)',
                    curveType: "function"
                    });
                    //elevId = elevId + 1;'.$break;
                    //if(elevId < ile+1) drawPath(elevId,pathArr[elevId]);'.$break;
                    //}
                    } 
                }
            
   }

