(function($){$(function(){

    var GLOBALS = {};
    var G = GLOBALS;

    var init = function(){
        G.form = $('#form-poll'); 
        G.form.ajaxForm({success:submitVote, dataType: 'json'}); 
        G.next_poll_url = $('.next-poll').attr('href');
        $('.next-poll').live('click', showNextPoll); 
    };

    var submitVote = function(data){
        if(data['error']){
            showNextPoll();
        } else {
            showResults(data['pollResult']);
        }
        return false;
    };

    var showResults = function(results){
        $('#poll-container').html(results);
		setTitleWidth();
        init();
    };

    var showNextPoll = function(){
        $.get(G.next_poll_url,{}, function(data){
            $('#poll-container').html(data['nextPoll']);
            setTitleWidth();
			initializeDropDowns();
            init();
        },'json');
        return false;
    };

    init();

})}(jQuery))

