var simplepoll = {
    
    check: function (el_id, poll_id) {
          $.post('simplepoll.php?action=check', {id: poll_id }, function (resp)  {
            if( resp=='voted' ) {
                simplepoll.data(el_id, poll_id);
            }else{
                $(el_id).find('.question').slideDown('fast');
                $(el_id).find('.statistics').slideUp('fast');
            }
        });
        
    },
    
    vote: function(el_id, poll_id ) {
        
        if( $(el_id).find('input[name="poll"]:checked').val() ) {
            $.post('simplepoll.php?action=vote', {vote: $(el_id).find('input[name="poll"]:checked').val(), id: poll_id }, function (resp)  {
                simplepoll.data(el_id, poll_id);                
            });
        }
        return false;
    },
    
    data: function (el_id, poll_id) {
	$(el_id).find('.question').slideUp('fast');
	$(el_id).find('.statistics').slideDown('fast');

	var s = window.location+'&';
	if( s.indexOf('show_stats')<=0 ) {
		$(el_id).find('.statistics').html('Tack f&ouml;r din medverkan');
		return;
	}

        $.post('simplepoll.php?action=data', {id: poll_id }, function (resp)  {
                $(el_id).find('.question').slideUp('fast');
                $(el_id).find('.statistics').slideDown('fast');

                if( resp.data ) { 
                    for( i in resp.data ) {
                        var percent = parseInt((resp.data[i]/resp.total)*100);
                        $(el_id).find('.statistics').find('.poll_result[value="'+i+'"]').text(percent+'%');
                    }
			$(el_id).find('.statistics').append(resp.total+' svar');
                }
   	         
        
        }, 'json');
        
        
        
    }
    
    
    
    
}