var t;
var img_actuel = 0;

function launchSlideshow()
{
	
	
	if(img_actuel < 4){
		img_new = img_actuel+1;
	}
	else{
		img_new = 1;
	}
	
	desc = $("#galerie_mini li:nth-child("+img_new+") a").attr("title");
	lien = $("#galerie_mini li:nth-child("+img_new+") a").attr("href");
	$("li.on").removeClass("on");
	
	$("#galerie dd a").attr("href",lien);
	
	$("#galerie_mini li:nth-child(" + img_new +") a").parent().toggleClass("on");
	$("#photo dt").html(desc);
	 
	$("img.img"+img_new).css("display","block");
	$("img.img"+img_actuel).css("display","none");
	img_actuel = img_new;

	t=setTimeout("launchSlideshow()",3000);
}



$(document).ready(function(){
  
	$("img."+img_actuel).css("display","block");
	contenu = $("#galerie_mini li a:first").attr("title");
	$("#photo dt").html(contenu);
	
	launchSlideshow();
	
    $("#galerie_mini a").click(function () {
	 clearTimeout(t);
     img_temp = $(this).attr("class");
	 img_new = img_temp.substring(3,4);
	 
	 if(img_actuel != img_new){
		  
		  desc = $(this).attr("title");
		  lien = $(this).attr("href");
		  $("li.on").removeClass("on");
		  
		  $(this).parent().toggleClass("on");
		  $("#photo dt").html(desc);
		  $("#galerie dd a").attr("href",lien);
		  $("img.img"+img_new).css("display","block");
		  $("img.img"+img_actuel).css("display","none");
		  img_actuel = img_new;
	  }
	  return false;	  
    });
	
	// POLL
	
		var loader=$('#loader');
		var pollcontainer=$('#pollcontainer');
		loader.fadeIn();
		//Load the poll form
		$.get('poll.php', '', function(data, status){
			pollcontainer.html(data);
			animateResults(pollcontainer);
			pollcontainer.find('#viewresult').click(function(){
				//if user wants to see result
				loader.fadeIn();
				$.get('poll.php', 'result=1', function(data,status){
					pollcontainer.fadeOut(1000, function(){
						$(this).html(data);
						animateResults(this);
					});
					loader.fadeOut();
				});
				//prevent default behavior
				return false;
			}).end()
			.find('#pollform').submit(function(){
				var selected_val=$(this).find('input[name=poll]:checked').val();
				if(selected_val!=''){
					//post data only if a value is selected
					loader.fadeIn();
					$.post('poll.php', $(this).serialize(), function(data, status){
						$('#formcontainer').fadeOut(100, function(){
							$(this).html(data);
							animateResults(this);
							loader.fadeOut();
						});
					});
				}
				//prevent form default behavior
				return false;
			});
			loader.fadeOut();
		});
		
		function animateResults(data){
			$(data).find('.bar').hide().end().fadeIn('slow', function(){
								$(this).find('.bar').each(function(){
									var bar_width=$(this).css('width');
									$(this).css('width', '0').animate({ width: bar_width }, 1000);
								});
							});
		}
	
	

  });
