var mapa_actiu = "arribar";

function executaModeTransport() {
    
    if (mapa_actiu == 'arribar') {
     
        draw_directions_arribar(request);        
                
    } else {
        
        draw_directions(request);                
    }
            
    $('#' + $.mobile.activePage.attr('id') + ' .popup_opcions_mapa').popup('close');
            
}

function aPeu() {
    
    request.travelMode = google.maps.DirectionsTravelMode.WALKING;
    executaModeTransport();
    activaBotoMode('transport_a_peu');    
    
}

function enCotxe() {
    
    request.travelMode = google.maps.DirectionsTravelMode.DRIVING;
    executaModeTransport();
    activaBotoMode('transport_en_cotxe');
        
}

function enTPublic() {
    
    request.travelMode = google.maps.DirectionsTravelMode.TRANSIT;
    executaModeTransport();
    activaBotoMode('transport_public');    
    
}

//Marquem com a seleccionat el botó corresponent al mode de transport

function activaBotoMode(div_boto) {
    
    /*$('.transport_a_peu').parent().parent().parent().parent().removeClass('ui-btn-active');
    $('.transport_en_cotxe').parent().parent().parent().parent().removeClass('ui-btn-active');
    $('.transport_public').parent().parent().parent().parent().removeClass('ui-btn-active');
    
    $('.' + div_boto).parent().parent().parent().parent().addClass('ui-btn-active');*/
    
    $('.popup_opcions_mapa a').removeClass('ui-btn-active');
    
    $('.' + div_boto).parent().addClass('ui-btn-active');
        
}

function veureRuta() {
    
    if ($('#map_canvas').css('display') == 'block') {
     
        $('#map_canvas').hide()
        $('#map_canvas_ruta').show();
     
    } else {
        
        $('#map_canvas').show()
        $('#map_canvas_ruta').hide();
        
        bounds.extend(origen);
        map.fitBounds(bounds);
        
    }
    
    $('#' + $.mobile.activePage.attr('id') + ' .popup_opcions_mapa').popup('close');
}

function veureRutaArribar() {
    
    if ($('#map_canvas_arribar').css('display') == 'block') {
     
        $('#map_canvas_arribar').hide()
        $('#map_canvas_arribar_ruta').show();
     
    } else {
        
        $('#map_canvas_arribar').show()
        $('#map_canvas_arribar_ruta').hide();
        
        bounds.extend(origen);
        map.fitBounds(bounds);        
        
    }
    
    $('#' + $.mobile.activePage.attr('id') + ' .popup_opcions_mapa').popup('close');
}

function veureRutaPoi() {
    
    if ($('#poi_map_canvas').css('display') == 'block') {
     
        $('#poi_map_canvas').hide()
        $('#poi_map_canvas_ruta').show();
     
    } else {
        
        $('#poi_map_canvas').show()
        $('#poi_map_canvas_ruta').hide();
        
        bounds.extend(origen);
        map.fitBounds(bounds);
        
    }
    
    $('#' + $.mobile.activePage.attr('id') + ' .popup_opcions_mapa').popup('close');
}

function veureRutaBotiga() {
    
    if ($('#map_canvas').css('display') == 'block') {
     
        $('#map_canvas').hide()
        $('#map_canvas_ruta').show();
     
    } else {
        
        $('#map_canvas').show()
        $('#map_canvas_ruta').hide();
        
        bounds.extend(origen);
        map.fitBounds(bounds);
        
    }
    
    $('#' + $.mobile.activePage.attr('id') + ' .popup_opcions_mapa').popup('close');
}

function veureRutaOfertes() {
    
    if ($('#map_canvas_oferta').css('display') == 'block') {
     
        $('#map_canvas_oferta').hide()
        $('#map_canvas_oferta_ruta').show();
     
    } else {
        
        $('#map_canvas_oferta').show()
        $('#map_canvas_oferta_ruta').hide();
        
        bounds.extend(origen);
        map.fitBounds(bounds);
        
    }
    
    $('#' + $.mobile.activePage.attr('id') + ' .popup_opcions_mapa').popup('close');
}

//Funció per calcular la distància entre dos punts

function distance(lat1, lon1, lat2, lon2, unit) {
    var radlat1 = Math.PI * lat1/180
    var radlat2 = Math.PI * lat2/180
    var radlon1 = Math.PI * lon1/180
    var radlon2 = Math.PI * lon2/180
    var theta = lon1-lon2
    var radtheta = Math.PI * theta/180
    var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
    dist = Math.acos(dist)
    dist = dist * 180/Math.PI
    dist = dist * 60 * 1.1515
    if (unit=="K") {
        dist = dist * 1.609344
    }
    if (unit=="N") {
        dist = dist * 0.8684
    }
    return dist
}