var art_vote = function(art_id,vote) {
   var opt = {
      method: 'post',
   	  postBody: 'aid=' + art_id + '&v=' + vote + '&r=' + Math.random(),
   	  onSuccess: function(t) {
         var response = t.responseText.split(':');
         alert(response[1]);
//         $('message_txt').innerHTML = response[1];
//         $('processing').hide();
//         $('message').show();
   	  },

   	  onLoading: function() {
//         $('poll_form').hide();
//         $('processing').show();
   	  },

   	  on404: function(t) {
         alert('Error 404: location "' + t.statusText + '" was not found.');
   	  },

   	  onFailure: function(t) {
         alert('Error ' + t.status + ' -- ' + t.statusText);
   	  }
   }
   new Ajax.Request('article_vote.php', opt);
}

var poll_vote = function(po_id,poll_id) {
   var opt = {
      method: 'post',
   	  postBody: 'poid=' + po_id + '&poll_id='+poll_id+'&r=' + Math.random(),
   	  onSuccess: function(t) {
         var response = t.responseText.split(':');
         //alert(response[1]);
         get_poll_results(poll_id);
//         $('message_txt').innerHTML = response[1];
//         $('processing').hide();
//         $('message').show();
   	  },

   	  onLoading: function() {
//         $('poll_form').hide();
//         $('processing').show();
   	  },

   	  on404: function(t) {
         alert('Error 404: location "' + t.statusText + '" was not found.');
   	  },

   	  onFailure: function(t) {
         alert('Error ' + t.status + ' -- ' + t.statusText);
   	  }
   }
   new Ajax.Request('poll_vote.php', opt);
}

var get_poll_results = function(poll_id) {
   var opt = {
      method: 'get',
	  parameters: 'po_id=' + poll_id,
	  onSuccess: function(request) {
	     var json = evalResponse(request);
         if ( json.clone )
         {
//            $('tr_city').hide();
         }
         else
 	     {
            show_results(json);
 	     }
	  },
	  onLoading: function() {
    	 //cleanOption($('form[location_id]'));
         //$('form[location_id]').disabled = true;
	     //loadingLocationOption($('form[location_id]'));
	  },
	  on404: function(t) {
		 alert('Error 404: location "' + t.statusText + '" was not found.');
	  },
	  onFailure: function(t) {
		 alert('Error ' + t.status + ' -- ' + t.statusText);
	  }
   }
   new Ajax.Request('ajax_poll_results.php', opt);
}

var evalResponse = function(request) {
    try {
        return eval('('+request.responseText+')');
    } catch (e) {}
}

var show_results = function(results) {
   $('poll_options').hide();
   var html_cont = '';
   var total = 0;
   results.each(function(item, index) {
                       html_cont = item.poll_option;
                       html_cont+= '<br/>';
                       html_cont+= '<span class="progressBar percentImage1" id="element'+item.poll_option_id+'">'+item.percent+'</span>';
                       html_cont+= '<br/>';
                       new Insertion.Bottom('poll_results', html_cont);
                       total+= eval(item.votes);
                }
   );

   new Insertion.Bottom('poll_results', '<br/>Votos: '+total);
   initProgressBarHandler();
}