String.prototype.format = function() {
    var formatted = this;
    for (var i = 0; i < arguments.length; i++) {
        var regexp = new RegExp('\\{'+i+'\\}', 'gi');
        formatted = formatted.replace(regexp, arguments[i]);
    }
    return formatted;
};

function rate_route(route_id, rating, csrf_token) {
    jQuery.post("/route/rate_route/", { id: route_id, rating:rating, 'csrfmiddlewaretoken': csrf_token },
        function(json) { if (json['success'])
                            document.getElementById("avg_rating_" + route_id).firstChild.textContent=json['result'];});
};
function rename_route(route_id, new_name, csrf_token) {
    jQuery.getJSON("/route/rename_route/", { id: route_id, new_name:new_name },
        function(json) { if (json['success']) document.getElementById("static_name_link_" + route_id).textContent=new_name;});
};

function delete_route(route_id, csrf_token) {
    var r=confirm("You are about to delete the route, are you sure?");
    if (r == false)
        return
    jQuery.getJSON("/route/delete_route/", { id: route_id },
        function(json) {
            if (json['success']) {
                row = document.getElementById("row_route_" + route_id);
                all_rows = row.parentElement.rows;
                for(var i = 0; i < all_rows.length; i++)
                {
                    if (row == all_rows[i])
                        row.parentElement.deleteRow(i);
                }
            }
        }
    );
    jQuery.modal.close();

    jQuery("table[route_list]").each(function() {
        route_list = jQuery(this)[0].route_list;
        route_list.load(route_list.start, route_list.count);
    });
};

function update_notes(route_id, notes, csrf_token) {
    var notes = notes.replace(/^\s*/, "").replace(/\s*$/, "");
    jQuery.post("/route/update_notes/", { id: route_id, notes:notes, 'csrfmiddlewaretoken': csrf_token },
        function(json) {
            // todo: change the route's notes
        }
    );
};


function toggle_public(route_id, csrf_token) {
    if (document.getElementById("public_" + route_id).checked) {
        jQuery.getJSON("/route/make_route_public/", { id: route_id},
            function(json) {});
    } else {
        jQuery.getJSON("/route/make_route_private/", { id: route_id},
            function(json) {});
    }
};

function distance_between(lat1, lon1, lat2, lon2) {
    var R = 6371; // km
    var degToRad = 0.0174532925;
    var dLat = (lat2-lat1) * degToRad;
    var dLon = (lon2-lon1) * degToRad;
    var lat1 = lat1 * degToRad;
    var lat2 = lat2 * degToRad;

    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    return d = R * c;
}

function convert_units(quantity, units) {
    // assumes quantities are all metric
    // also rounds to either 0 or 1 decimal, depending on the end unit

    switch(units) {
        case "meters":
            return Math.round(quantity * 10)/10;
        case "ft":
        case "feet":
            return Math.round(3.2808399 * quantity);
        case "miles":
            return Math.round((3.2808399 * quantity / 5280) * 10)/10;
        case "hours":
            hour = Math.floor(quantity/3600);
            min = Math.floor(quantity/60) % 60;
            sec = quantity % 60;

            if (min < 10) {
                min = '0' + min;
            }
            if (sec < 10) {
                sec = '0' + sec;
            }

            if (hour < 1) {
                return '{1}:{2}'.format(hour, min, sec);
            }
            return '{0}:{1}:{2}'.format(hour, min, sec);
    }
};

