// These are now specified in the basket_block.html template
// var basket_actions_url = "/basket_actions_page/";
// var basket_block_id = "#BasketBlock";

function get_class_value(elem, key) {
    classes = $(elem).attr("class");
    parts = classes.split(" ");
    
    for (i in parts) {
        if (parts[i].indexOf(key) == 0)
        {
            // got the class, now grab the value
            return parts[i].replace(key + "_", "");
        }
    }
    return "";
}

function update_block_visibility() {
    var athlete_list = $(basket_block_id + " .athlete_list");
    var speaker_list = $(basket_block_id + " .speaker_list");
    
    var athlete_size = $("li", athlete_list).not('.ph').size()
    var speaker_size = $("li", speaker_list).not('.ph').size()
    
    if (athlete_size > 0) {
        athlete_list.show();
    } else {
        athlete_list.hide();
    }
    
    if (speaker_size > 0) {
        speaker_list.show();
    } else {
        speaker_list.hide();
    }
    
    if (athlete_size > 0 || speaker_size > 0) {
        $(basket_block_id).show();
    } else {
        $(basket_block_id).hide();
    }
}

function insert_to_basket(data) {
    var ul = $($(basket_block_id + " ." + data.type + "_list ul:first").get(0));
    
    var new_elem = $("<li>-&nbsp;<span class=\"value name\">" + data.name + "</span><a class=\"action_remove type_" + data.type + " id_" + data.id + "\" href=\"#\">Remove</a></li>");
    
    var inserted = false;
    $("li", ul).each(function() {
        if (inserted) return;
        var val = $("span.value.name", this).html();
        var cmp = (val >= data.name);

        if (cmp == true) {
            new_elem.insertBefore(this);
            inserted = true;
        }
    });
    
    if (inserted == false) {
        ul.append(new_elem);
    }
}

function add_to_basket(elem) {
    var type = get_class_value(elem, "type");
    var id = get_class_value(elem, "id");
    
    var url = basket_actions_url;
    if (type == "athlete") {
        url += "add_athlete/?ids=";
    } else {
        url += "add_speaker/?ids=";
    }
    url += id;
    
    $.getJSON(url, function(data) {
        if (data.status == "add_athlete" || data.status == "add_speaker") {
            for (i in data.objs) {
                insert_to_basket(data.objs[i]);
            }
            update_block_visibility();
        } else {
            //alert("An error occurred while adding this athlete, please try again.");
        }
    });
    
    return false;
}

function remove_from_basket(elem) {
    var type = get_class_value(elem, "type");
    var id = get_class_value(elem, "id");
    
    var url = basket_actions_url + "remove_" + type + "/?ids=" + id;
    
    $.getJSON(url, function(data) {
        if (data.status == "remove_" + type) {
            // remove the li from the list
            $(elem).parent().remove();
            
            update_block_visibility();
        }
    });
    
    return false;
}

function clear_all_by_type(type)
{
    var ids = "";
    $(basket_block_id + " ." + type + "_list li a.action_remove").each(function() {
        if (ids.length > 0) ids += ",";
        ids += get_class_value(this, "id");
    });
    if (ids.length < 1) return;
    
    var url = basket_actions_url + "remove_" + type + "/?ids=" + ids;
    
    $.getJSON(url, function(data) {
        if (data.status == "remove_" + type) {
            // remove the li from the list
            $(basket_block_id + " ." + type + "_list li").remove();
            update_block_visibility();
        }
    });
}

function clear_all()
{
    clear_all_by_type("athlete");
    clear_all_by_type("speaker");
    return false;
}

$(document).ready(function() {
    $("a.action_add").click(function() {
        return add_to_basket(this);
    });
    
    $(basket_block_id + " a.action_remove").live("click", function() {
        return remove_from_basket(this);
    });
    
    $(basket_block_id + " input.submit.clear_all").click(function() {
        return clear_all();
    });
    
    $(basket_block_id + " input.submit.query").click(function() {
        window.location.replace(submit_query_url);
    });
    
    update_block_visibility();
});





