var currentQuestion=1;
var isOk=false;
$(document).ready(function(){
	if($.browser.msie==true && $.browser.version==6){
		pngfix();
	}
	$('div.question').show()
	updateHeight();
	$('div.question:not(#question2)').hide()
	
	$('div.bonnereponse, div.question').hide();
	$('div#question1').show();
	$('#navbar div.bg').fadeTo(0,0.62);
	$('div#navbar ul').css('width',($('div#navbar ul').width()+12)+'px');
	$('div#navbar ul').css('float','none');
	
	init_question();

	init_analytics_link();
});

function init_analytics_link(){
	//pageTracker._trackPageview('/question1-reponseA');
	var cpt=1;
	var a_lettre = ['A','B','C','D'];
	$('div.question').each(function(){
		var cpt_reponse=0;
		$(this).find('a[id^=reponse-]').each(function(){
			$(this).data('count',cpt_reponse);
			$(this).data('questionid',cpt);
			$(this).bind('mouseup',function(){
				pageTracker._trackPageview('/question'+$(this).data('questionid')+'-reponse'+a_lettre[$(this).data('count')]); 
			});
			cpt_reponse++;
		});
		cpt++;
	});
	
	$('a.plusinfo').click(function(){
		pageTracker._trackPageview('/plus-info-protection');
	});
}

function updateHeight(){
	//alert('updateHeight');
	$('div.question li a span').each(function(){
		myheight = $(this).height()
		$(this).css('height',myheight+'px');
		mymargin = Math.round(myheight/2);
		$(this).css('margin-top','-'+mymargin+'px');
	})
}

function init_question(){
	$('ul.reponses a').bind('click',vote);
	$('ul.reponses a').bind('mouseover',function(){
		$(this).stop(true).animate({ color: "#125e7b" }, 700);
		if(!$.browser.msie){
			$(this).css('cursor','pointer');
		}else{
			$(this).css('cursor','hand');
		}
		
	});
	$('ul.reponses a').bind('mouseout',function(){
		$(this).stop(true).animate({ color: "#1c97c6" }, 700);
		$(this).css('cursor','default');
	});
	
	
	$('div.bonnereponse a.next').bind('click',function(){
		pageTracker._trackPageview('/question2');
		$('html,body').animate({ scrollTop: 0 }, 500);
		$('div#question'+currentQuestion+' div.bonnereponse').slideUp('fast', function(){
			$('div#question'+currentQuestion+' ul.reponses').fadeOut('slow',function(){
				//switch la question
				$('div#question'+currentQuestion).hide();
				currentQuestion++;
				$('div#question'+currentQuestion+' ul.reponses').hide();
				//$('div#question'+currentQuestion+' a.next').remove();
				$('div#question'+currentQuestion).show();
				$('div#question'+currentQuestion+' ul.reponses').fadeIn('slow');
			})	
		});
		
		return false;
	})
}

function vote(){
	$.ajax({
		type: "POST",
  		url: "update-answer.php",
  		data: "info="+$(this).attr('id')
	})
	
	isOk=false;
	if($(this).attr('rel')=='good'){
		isOk = true;
	}
	
	//on affiche la bonne explication
	var myid = $(this).attr('id').split('-')[2];
	if(currentQuestion==1){
		$('div#question'+currentQuestion+' div.bonnereponse p[rel^=explication-]').hide();
		$('div#question'+currentQuestion+' div.bonnereponse p[rel=explication-'+myid+']').show();
	}else{
		$('div#question'+currentQuestion+' div.bonnereponse p[rel^=explication-]').show();
	}
	
	
	$('p.duproprio').show();
	
	//switch les class
	if(isOk){
		classname = 'good';
	}else{
		classname = 'wrong';
	}
	$(this).addClass('click');
	$('div#question'+currentQuestion+' a:not(.click)').addClass('notclicked', 500);
	$(this).addClass(classname, 500, function(){
		//show good answer
		if(!isOk){
			$('div#question'+currentQuestion+' ul.reponses a[rel=good]').animate({opacity:1},500).switchClass('notclicked','good',1000,showVote);
		}else{
			showVote();
		}
	});

	$('div#question'+currentQuestion+' ul.reponses a').unbind('click').unbind('mouseout').unbind('mouseover').bind('click',function(){return false;})
	
	return false;
}

function showVote(){
	if($.browser.msie==true){
		$('div#question'+currentQuestion+' a strong').show(0);
		showExplication();
	}else{
		$('div#question'+currentQuestion+' a strong').fadeIn('slow',showExplication);
	}
}

function showExplication(){
	//updateHeight();
	if(isOk){
		$('div#question'+currentQuestion+' div.bonnereponse h2.good').show();
		$('div#question'+currentQuestion+' div.bonnereponse h2.wrong').hide();
	}else{
		$('div#question'+currentQuestion+' div.bonnereponse h2.good').hide();
		$('div#question'+currentQuestion+' div.bonnereponse h2.wrong').show();
	}

	$('div#question'+currentQuestion+' div.bonnereponse').slideDown('normal');
}
