$(function(){
	var loader=$('#pollloader');
	var pollcontainer=$('#pollcontainer');
    
	loader.fadeIn();
    
	//Load the poll form
	$.post(URL+'/ajax.php', 'go=getvote', function(xml, status){
    
		pollcontainer.slideDown('slow').html($(xml).text());
		animateResults(pollcontainer);
        loader.fadeOut('slow');
		pollcontainer.find('input#viewresult').live('click' , function(){
			//if user wants to see result
			loader.fadeIn();
			$.post(URL+'/ajax.php', {go:'getvote', showresult:'1'}, function(xml,status){
				pollcontainer.fadeOut(1000, function(){
					$(this).slideDown('slow').html($(xml).text());
					animateResults(this);
				});
				loader.fadeOut();
			});
			//prevent default behavior
			return false;
		}).end()
		.find('#pollform').live('submit' , function(){
			var selected_val=$(this).find('input[name=vote]:checked').val();
            
			if(selected_val!=''){
			 
				//post data only if a value is selected
				loader.fadeIn();
				$.post(URL+'/ajax.php',  $(this).serialize(), function(xml, status){
					$('#pollinfo').slideUp('slow').fadeOut(100, function(){
						$(this).slideDown('slow').html($(xml).text());
						animateResults(this);
						loader.fadeOut();
					});
				});
			}
			//prevent form default behavior
			return false;
		})  ;
		loader.fadeOut();
      
	});

	function animateResults(data){
		$(data).find('.pollbar').hide().end().fadeIn('slow', function(){
		$(this).find('.pollbar').each(function(){
			var bar_width=$(this).css('width');
            console.log("Bar width %s" , bar_width);
		$(this).css('width', '0').fadeIn('slow').animate({ width: bar_width }, 1000); ;
			});
						});
	}
	
    
    
});

function get_poll_form()
    {
        $('#pollcontainer').slideUp('slow').html();
        $('#pollloader').fadeIn();
        $.post(URL+'/ajax.php', 'go=getvote' , function(xml , status){
            $('#pollcontainer').slideDown('slow').html($(xml).text());
            });
        $('#pollloader').fadeOut('slow' , function()($(this).hide('slow')));
    }


