(function($) {
  $.fn.ContentSlider = function(options)
  {
    var defaults = {
      leftBtn : 'images/cs_leftImg.jpg',
      rightBtn : 'images/cs_rightImg.jpg',
      width : '900px',
      height : '400px',
      speed : 400,
      easing : 'linear',
      textResize : false,
      IE_h2 : '26px',
      IE_p : '11px'
    }
    var defaultWidth = defaults.width;
    var o = $.extend(defaults, options);
    var w = parseInt(o.width);
    var n = this.children('#wrapQuizz').children('#banQuizz').children('.step').length;
    var x = -1*w*n+w; // Minimum left value
    var p = parseInt(o.width)/parseInt(defaultWidth);
    var thisInstance = this.attr('id');
    var inuse = false; // Prevents colliding animations

    function moveSlider(d, b)
    {
      var l = parseInt(b.siblings('#wrapQuizz').children('#banQuizz').css('left'));
      if(isNaN(l)) {
        var l = 0;
      }
      var m = (d=='left') ? l-w : l+w;
      if(m<=0&&m>=x) {
        b
          .siblings('#wrapQuizz')
            .children('#banQuizz')
              .animate({ 'left':m+'px' }, o.speed, o.easing, function() {
                inuse=false;
              });

        if(b.attr('class')=='cs_leftBtn') {
          var thisBtn = $('#'+thisInstance+' .cs_leftBtn');
          var otherBtn = $('#'+thisInstance+' .cs_rightBtn');
        } else {
          var thisBtn = $('#'+thisInstance+' .cs_rightBtn');
          var otherBtn = $('#'+thisInstance+' .cs_leftBtn');
        }
        if(m==0||m==x) {
          thisBtn.animate({ 'opacity':'0' }, o.speed, o.easing, function() { thisBtn.hide(); });
        }
        if(otherBtn.css('opacity')=='0') {
          otherBtn.show().animate({ 'opacity':'1' }, { duration:o.speed, easing:o.easing });
        }
      }
    }

    function vCenterBtns(b)
    {
      // Safari and IE don't seem to like the CSS used to vertically center
      // the buttons, so we'll force it with this function
      var mid = parseInt(o.height)/2;
      b
        .find('.cs_leftBtn').css({ 'bottom':0+'px', 'padding':0 }).end()
        .find('.cs_rightBtn').css({ 'bottom':0+'px', 'padding':0 });
    }

    return this.each(function() {
      $(this)
        // Set the width and height of the div to the defined size
        .css({
          width:o.width,
          height:o.height
        })
        // Add the buttons to move left and right
        .prepend('<a href="#" class="cs_leftBtn">Précédent</a>')
        .append('<a href="#" class="cs_rightBtn">Suivant</a>')
        // Dig down to the article div elements
        .find('.step')
          // Set the width and height of the div to the defined size
          .css({
            width:o.width,
            height:o.height
          })
          .end()
        // Animate the entrance of the buttons
        .find('.cs_leftBtn')
          .css('opacity','0')
          .hide()
          .end()
        .find('.cs_rightBtn')
          .hide()
          .animate({ 'width':'show' });

      // Resize the font to match the bounding box
      if(o.textResize===true) {
        var h2FontSize = $(this).find('h2').css('font-size');
        var pFontSize = $(this).find('p').css('font-size');
        $.each(jQuery.browser, function(i) {
          if($.browser.msie) {
             h2FontSize = o.IE_h2;
             pFontSize = o.IE_p;
          }
        });
        $(this).find('h2').css({ 'font-size' : parseFloat(h2FontSize)*p+'px', 'margin-left' : '66%' });
        $(this).find('p').css({ 'font-size' : parseFloat(pFontSize)*p+'px', 'margin-left' : '66%' });
        $(this).find('.readmore').css({ 'font-size' : parseFloat(pFontSize)*p+'px', 'margin-left' : '66%' });
      }

      // Store a copy of the button in a variable to pass to moveSlider()
      var leftBtn = $(this).children('.cs_leftBtn');
      leftBtn.bind('click', function() {
        if(inuse===false) {
          inuse = true;
          moveSlider('right', leftBtn);
        }
        return false; // Keep the link from firing
      });

      // Store a copy of the button in a variable to pass to moveSlider()
      var rightBtn = $(this).children('.cs_rightBtn');
      rightBtn.bind('click', function() {
        if(inuse===false) {
          inuse=true;
          moveSlider('left', rightBtn);
        }
        return false; // Keep the link from firing
      });

      vCenterBtns($(this)); // This is a CSS fix function.
    });
  }
})(jQuery);
var ok = [0,1,0,0,0,1];
var reponsesOk = {question1:0,question2:1,question3:0,question4:0,question5:0,question6:1}
var i=0,bonnesReponses=0;
	$(function(){
		//
		$('#quizz .question').click(function(){
			var fieldVal = $(this).val();
			var field = $(this).parent().attr('id');
			$('.question','#'+field).hide();
			$('.question','#'+field).prev().hide();
			if (fieldVal == reponsesOk[field]) {
				$(this).parent().append('<p class="resultat vrai">Bonne réponse</p>');
			}else{

				$(this).parent().append('<p class="resultat faux">Mauvaise réponse</p>');					
			}
		});
		$('#quizz').ContentSlider({
			height:282,
			width:300
		});
		$('#formQuizz').submit(function(){
			$('input:radio:checked',this).each(function(){
				if ($(this).val() == ok[i]) {
					bonnesReponses++;
				//	$(this).parent().removeClass('false').addClass('true');
				}
				i++;				
			})
			$('div.step').remove();
			$('#quizz a').remove();
			
			if (bonnesReponses > 4)
				$('#quizz').html('<div id="resultatQuizz" style="padding:80px 0 0 30px;width:250px">Bravo vous avez une bonne connaissance de l\'ISR, pour participer aux événement de la Semaine 2011, voyez le <a href="http://semaine-isr.net/evenements">programme</a></div>');
			if (bonnesReponses < 4)
				$('#quizz').html('<div id="resultatQuizz" style="padding:80px 0 0 30px;width:250px">Pour parfaire votre connaissance de l\'ISR, participez aux <a href="http://semaine-isr.net/evenements">événements de la Semaine</a></div>');
	

			bonnesReponses = 0;
			i=0;
			return false;
		});
	});
