$(document).ready(function(){

    $("#teamform").submit(function() {
        if ($("#teamselect").val() > 0) {
              $.post(ajaxPath + "vote_team/", {id: $("#teamselect").val()}, function(response){
                    $("#teamlist").empty();
                    $("#teamlist").append(response);
                    $('#ex3b').jqm({
                          ajax: ajaxPath + "get_user_votes/?" + Math.random(),
                          target: 'div.jqmAlertContent',
                          overlay: 50
                    });
                    $('#ex3b').jqmShow();
                    renewChart();
              });
        }
        return false;
    });

    $("#teamselect").change(function(){
            $.get(ajaxPath + "get_team/" + $(this).val() + "?" + Math.random(), {}, function(response){
                    var team = $.evalJSON(response);
                    if (team['name']) {
                        $("#teamheader").text(team['name'].toUpperCase());
                    } else {
                        $("#teamheader").html('&nbsp;');
                    }
                    var imgpath = '';
                    if (team['name'].length > 0)
                        imgpath = team['name'].toLowerCase().split(' ').join('-') + '.jpg';
                    if (team['image'])
                        imgpath = team['image'].toLowerCase().split(' ').join('-') + '.jpg';
                    $("#teamimage").attr('src', sitePath + 'images/big/' + imgpath);
            });
    });

});

var renewChart = function(type) {
    var flashobj = swfobject.getObjectById("flashchart");
    if (flashobj) {
        flashobj.redrawChart(ajaxPath + "get_chart_data/teams?" + Math.random());
    }
}
