function commonTemplate(item) {
    return "<option value='" + item.Value + "'>" + item.Text + "</option>"; 
}

function commonMatch(selectedValue) {
    return this.When == selectedValue; 
}

function newteam(f) {    
    var t = "/knvb-uitslagen/team/";

    $.ajax({
        url: f,
        cache: false,
        dataType: 'html',
        error: function(xml, status, error) {
            $("#results").empty();
            alert("Geen gegevens beschikbaar. Probeer het later opnieuw.");
        },
        success: function(html, textStatus) {
            $("#results").empty().append(html);
            top.location.hash = "#" + jQuery.trim($("#team option:selected").text());
            // adjust iframe height for new content loaded
            var h1 = $("#results").height() + $("#control").height();
            $("iframe", parent.document).height(h1 + 50);
            // check if competition menu only contains 1 row
            var count = $("#competitie option").length;
            if(count < 2) {
                $("#competitie").attr("disabled", "true");
            }
        }
    });
}

$(document).ready(function()
{
    $("#competitie").cascade("#team",{
        ajax: {url: 'data.js'},
        template: commonTemplate,
        match: commonMatch
    })
    .bind("loaded.cascade", function(e, target) {
        $(this).trigger("change");
    });
    $("#competitie").change(function() {
        newteam($(this).val());
    });
    // preset menu if anchor present
    var bookmark = top.location.hash.substring(1).split("-");
    // force update
    $("#team").val(bookmark[0]).trigger("change").focus();
});

