var athlete_sport_ul;
var athlete_position_ul;
var athlete_country_ul;

var speaker_sport_ul;
var speaker_position_ul;
var speaker_country_ul;

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_athlete_sports_input() {
    str_ids = "";
    $("li.sport_select input:checked", athlete_sport_ul).each(function() {
        if (str_ids.length > 0) str_ids += ",";
        str_ids += $(this).val();
    });
    $("#q_sports").val(str_ids);
}

function update_speaker_sports_input() {
    str_ids = "";
    $("li.sport_select input:checked", speaker_sport_ul).each(function() {
        if (str_ids.length > 0) str_ids += ",";
        str_ids += $(this).val();
    });
    $("#q_sports").val(str_ids);
}

function update_speaker_expertise_input() {
    str_ids = "";
    $("li.select_expertise input:checked", speaker_expertise_ul).each(function() {
        if (str_ids.length > 0) str_ids += ",";
        str_ids += $(this).val();
    });
    $("#q_expertise").val(str_ids);
}

function update_athlete_positions_input() {
    str_ids = "";
    $("li.select_position input:checked").each(function() {
        if (str_ids.length > 0) str_ids += ",";
        str_ids += $(this).val();
    });
    $("#q_positions").val(str_ids);
}

function see_if_all_positions_are_checked() {
    $("li.select_sport input", athlete_position_ul).each(function() {
        var sport_id = get_class_value($(this).parent(), "sport_id");
        var checked_count = $("li.select_position.sport_id_" + sport_id + " input:checked").size();
        var count = $("li.select_position.sport_id_" + sport_id + " input").size();
        if (checked_count == count) {
            $(this).attr("checked", "checked");
        } else {
            $(this).removeAttr("checked");
        }
    });
}

function update_athlete_countries_input() {
    str_ids = "";
    $("li.select_country input:checked", athlete_country_ul).each(function() {
        if (str_ids.length > 0) str_ids += ",";
        str_ids += $(this).val();
    });
    $("#q_countries").val(str_ids);
}

function update_speaker_countries_input() {
    str_ids = "";
    $("li.select_country input:checked", speaker_country_ul).each(function() {
        if (str_ids.length > 0) str_ids += ",";
        str_ids += $(this).val();
    });
    $("#q_countries").val(str_ids);
}

function update_athlete_provinces_input() {
    str_ids = "";
    $("li.select_province input:checked", athlete_country_ul).each(function() {
        if (str_ids.length > 0) str_ids += ",";
        str_ids += $(this).val();
    });
    $("#q_provinces").val(str_ids);
}

function update_visible_positions()
{
    var sport_ids = [];
    $("li.sport_select input:checked", athlete_sport_ul).each(function() {
        var sport_id = get_class_value($(this).parent(), "sport_id");
        sport_ids[sport_ids.length] = sport_id;
    });
    
    $("li", athlete_position_ul).each(function() {
        var sport_id = get_class_value(this, "sport_id");
        var found = false;
        for (i in sport_ids) {
            if (sport_ids[i] == sport_id) {
                found = true;
                break;
            }
        }
        if (found) {
            $(this).show();
        } else {
            $(this).find("input").removeAttr("checked");
            $(this).hide();
        }
    });
}

$(document).ready(function() {
      //
     // ATHLETES
    //
    
    athlete_sport_ul = $("#athleteSelect .sport_ul");
    athlete_position_ul = $("#athleteSelect .position_ul");
    athlete_country_ul = $("#athleteSelect .country_ul");
    
    $("li.sport_select input", athlete_sport_ul).click(function() {
        update_athlete_sports_input();
        update_visible_positions();
        update_athlete_positions_input();
    });
    
    $("li.select_position input", athlete_position_ul).click(function() {
        update_athlete_positions_input();
        see_if_all_positions_are_checked();
    });
    
    $("li.select_sport input", athlete_position_ul).click(function() {
        var sport_id = get_class_value($(this).parent(), "sport_id");
        if (this.checked) {
            $("li.select_position.sport_id_" + sport_id + " input", athlete_position_ul).each(function() {
                $(this).attr("checked", "checked");
            });
        } else {
            $("li.select_position.sport_id_" + sport_id + " input", athlete_position_ul).each(function() {
                $(this).removeAttr("checked");
            });
        }
        update_athlete_positions_input();
    });
    
    $("li.select_country input", athlete_country_ul).click(function() {
        update_athlete_countries_input();
    });
    
    $("li.select_province input", athlete_country_ul).click(function() {
        update_athlete_provinces_input();
    });
    
    
    if (search_type == "athlete") {
        see_if_all_positions_are_checked();
        update_visible_positions();
    }
    
    
      //
     // SPEAKERS
    //
    speaker_sport_ul = $("#speakerSelect .sport_ul");
    speaker_expertise_ul = $("#speakerSelect .expertise_ul");
    speaker_country_ul = $("#speakerSelect .country_ul");
    
    $("li.sport_select input", speaker_sport_ul).click(function() {
        update_speaker_sports_input();
    });
    
    $("li.select_expertise input", speaker_expertise_ul).click(function() {
        update_speaker_expertise_input();
    });
    
    $("li.select_country input", speaker_country_ul).click(function() {
        update_speaker_countries_input();
    });
    
    $("#q_name").focus(function() {
        if ($(this).val() == "Athlete Name" || $(this).val() == "Speaker Name") $(this).val("");
    }).blur(function() {
        if (search_type == "speaker") {
            if ($(this).val() == "") $(this).val("Speaker Name");
        } else {
            if ($(this).val() == "") $(this).val("Athlete Name");
        }
    });
    $("#tabWrapper form").submit(function() {
        if ($("#q_name").val() == "Athlete Name" || $("#q_name").val() == "Speaker Name") $("#q_name").val("");
    });

    
});
