function init() {

}

var taxi = new Object();

taxi.LocationPicker = function(type, wrapper, input, list, hidden_address, hidden_location, hidden_postcode, bounds, callback, focus) {
    this.chosen = false;
    this.type = type;
    this.wrapper = wrapper;
    this.input = input;
    this.list = list;
    this.hidden_address = hidden_address;
    this.hidden_location = hidden_location;
    this.hidden_postcode = hidden_postcode;
    this.bounds = bounds;
    this.callback = callback;
    this.last_query = '';
    this.last_input_value = '';
    var picker = this;
    this.input.val('');
    this.input.attr('autocomplete', 'off');
    if (focus)
        this.input.focus();
};
taxi.LocationPicker.prototype.search = function(force) {
    this.lookup(this.input.val());
};
taxi.LocationPicker.prototype.lookup = function(text) {
    if (text == this.last_query)
        return;
    this.clear_list();
    if (text) {
        var picker = this;
        var result_count = 0;
        $.getJSON('/locations.json', {q: text, type: this.type}, function(data, status){
            results_status = '';
            if (data && status == 'success') {
                results_status = data.status;
                if (results_status == 'ok') {
                    result_count = data.results.length;
                    $.each(data.results, function(index, item){
                        var li = $(document.createElement('li'));
                        var a = $(document.createElement('a'));
                        a.text(item.address);
                        a.attr('href', '#');
                        a.data('address', item.address); 
                        a.data('full_address', item.address);
                        a.data('location', item.location); 
                        a.data('postcode', item.postcode); 
                        a.click(function(e){
                            e.preventDefault();
                            picker.clear_chosen();
                            li.addClass('chosen');
                            picker.set_chosen_address(item);
                        });
                        li.append(a);
                        picker.list.append(li);
                    });
                }
            }
            if (picker.callback)
                picker.callback.trigger('picklist-changed', [picker, result_count, results_status]);
        });
        this.last_query = text;
    }
};
taxi.LocationPicker.prototype.set_chosen_address = function(item) {
    this.hidden_address.val(item.address);
    this.hidden_location.val(item.location.lat + ',' + item.location.lng);
    this.hidden_postcode.val(item.postcode);
    $(this).data('chosen_item', item);
    $(this).data('chosen_address', item.address);
    this.chosen = true;
    if (this.callback)
        this.callback.trigger('chosen-changed', [this]);
};
taxi.LocationPicker.prototype.clear_list = function() {
    this.list.empty();
};
taxi.LocationPicker.prototype.clear_chosen = function() {
    this.list.children().each(function(index, item){
        $(item).removeClass('chosen');
    });
    this.chosen = false;
};

taxi.getQuote = function(from_location, to_location, pickup_address, pickup_postcode, dropoff_address, dropoff_postcode, result, callback) {
    distance = 0
    duration = 0
    $.each(result.routes[0].legs, function(index, leg){
        distance += leg.distance.value;
        duration += leg.duration.value;
    });
    $.getJSON('/fare.json', {
        from_location: from_location.toUrlValue(),
        to_location: to_location.toUrlValue(),
        pickup_address: pickup_address,
        pickup_postcode: pickup_postcode,
        dropoff_address: dropoff_address,
        dropoff_postcode: dropoff_postcode,
        distance: distance, 
        duration: duration
    }, function(data, textStatus){
        if (textStatus == 'success')
            callback(data);
    });
};

taxi.makeLatLng = function(location) {
    return new google.maps.LatLng(location.lat, location.lng);
};

taxi.BackText = function(textbox, text) {
    textbox = $(textbox);
    textbox.bind('focus', function(e){
        if (textbox.hasClass('back_text')) {
            textbox.removeClass('back_text');
            textbox.val('');
        }
    });
    textbox.bind('blur', function(e){
        if (textbox.val().length == '' || textbox.val() == text) {
            textbox.addClass('back_text');
            textbox.val(text);
        }
    });
    var form = textbox.parents('form');
    if (form)
        $(form).bind('submit', function(e){
            if (textbox.val() == text)
                textbox.val('');
        });
    textbox.trigger('blur');
}

taxi.BackImage = function(field) {
    field = $(field);
    field.bind('keyup', function(e){
        field.toggleClass('empty', field.val() == '');
    });
}

$(document).ready(init);

