(function($) {
    $.fn.fadeIn = function(speed, callback) {
        return this.animate({opacity: 'show'}, speed, function() {
                if ( $.browser.msie )
                {
                        this.style.removeAttribute('filter');
                }
                if ( $.isFunction(callback) )
                {
                        callback.call(this);
                }
        });
    };

    $.fn.fadeOut = function(speed, callback) {
        return this.animate({opacity: 'hide'}, speed, function() {
                if ( $.browser.msie )
                {
                        this.style.removeAttribute('filter');
                }
                if ( $.isFunction(callback) )
                {
                        callback.call(this);
                }
        });
    };

    $.fn.fadeTo = function(speed, to, callback) {
        return this.animate({opacity: to}, speed, function() {
                if ( to == 1 && $.browser.msie )
                {
                        this.style.removeAttribute('filter');
                }
                if ( $.isFunction(callback) )
                {
                        callback.call(this);
                }
        });
    };
})(jQuery);


$(document).ready(function(){
  
  $("#poll").submit(formProcess); 
  
   if ($("#poll-results").length > 0 ) {
     animateResults();
   }
  
});

function formProcess(event){
  event.preventDefault();
  
  var id = $("input[@name='poll']:checked").attr("value");
  
  if (id)
  {
  
	  id = id.replace("opt",'');
	  
	  $("#poll-container").fadeOut("slow",function(){
		
		$(this).empty();
		
		$.ajax({
		  url: 'index.php?vote='+id,
		  success: function(data) {
			
			$("#poll-container").append(data).fadeIn("slow",function(){
				animateResults();});
			}
			
		});

	  });
	  
  }
}

function animateResults(){
  $(".bar-container div").each(function(){
      var percentage = $(this).parent().next().find('strong').text();
	  
	  //alert(percentage);
	  
	  //percentage = 50;
	  
	  percentage = parseInt(percentage)*1.5;
	  
      $(this).css({width: "0%"}).animate({
				width: percentage}, 'slow');
  });
}

