$(document).ready(function(){
  
  /*************************************************/
  /*  Query current language                       */
  /*************************************************/
  lang = $('html').attr('lang');
  
  /*************************************************/
  /*  Flash animation Home and Developer           */
  /*************************************************/
  $('#stage').flash({swf: '/assets/flash/animation_'+lang+'.swf', height: 272, width: 727, bgcolor: '#FFFFFF', wmode: 'transparent'});
  $('#stage-dn').flash({swf: '/assets/flash/animation_dn_'+lang+'.swf', height: 272, width: 727, bgcolor: '#FFFFFF', wmode: 'transparent'});
  
  /*************************************************/
  /*  Results list navigation                      */
  /*************************************************/
  if ($("#results-navigation-wrapper").length)
  {
    results_count = $("#results-navigation li").length;
    results_current = 0;
  
    results_widths_array = new Array();
    results_widths_array.push(0);
    results_total_width = 0;
    i = 0;
    $('#results-navigation-wrapper ul li').each(function(){
      results_widths_array.push($(this).width() + results_widths_array[i]);
      results_total_width += $(this).width();
      i++;
    });
    results_left_limit = $('#results-navigation-wrapper').width() - results_total_width;
  
    if ($("#results-navigation ul").width() > 650)
    {
      // $("#results-navigation ul").width(616);
    }
    $("#results-navigation .prev").click(function(e){
      e.stopPropagation();
      showResult(results_current-1);
      return false;
    });
    $("#results-navigation .next").click(function(e){
      e.stopPropagation();
      showResult(results_current+1);
      return false;
    });

    licount = 0;
    $("#results-navigation li a").each(function(){
      this.result_position = licount;
      $(this).click(function(e){
        e.stopPropagation();
        showResult(this.result_position);
        return false;
      })
      licount++;
    });
    $("#results-navigation li:last a").get(0).result_position = licount-2;
  
    function showResult(result_position)
    {
      if (result_position<0 || result_position>(results_count-2))
      {
        return false;
      }
      // Graphical modifications
      $(".current-first").removeClass("current-first");
      $(".current-last").removeClass("current-last");
      $("#result-button-"+result_position).addClass("current-first");
      $("#result-button-"+(result_position+1)).addClass("current-last");
    
      if (results_total_width>606){
        // Change navigation position
        navigation_new_position = -results_widths_array[result_position];
        if (navigation_new_position > 606-results_total_width){
          $("#results-navigation-wrapper ul").animate({left: navigation_new_position}, 500)
        } else {
          $("#results-navigation-wrapper ul").animate({left: 606-results_total_width}, 500)
        }
      }
    
      // Change list position
      results_current = result_position;
      $("#results").animate({left: (-result_position)*336}, 500)
    }
  }
  
  
  /*************************************************/
  /*  Navigation                                   */
  /*************************************************/
  $(".navigation").each(function(){
    $(this).find("h4 a").click(function(e){
      e.stopPropagation();
      
      bloc_nav = $(this).parent().parent();
      opening_element = $(bloc_nav).find("ul:first");
      if (!opening_element.hasClass("opened"))
      {
        opening_element.slideToggle("normal");
      }
      $(".navigation ul.main").each(function(){
        // if ($(this).hasClass("opened") && !$(this).hasClass("current"))
        if ($(this).hasClass("opened"))
        {
          $(this).slideToggle("normal").removeClass("opened");
        }
      })
      opening_element.addClass("opened");
      
      if ($(bloc_nav).attr("id")!="nav-espace-developpeur")
      {
        return false;
      }
    });
  });
  
  
  /*************************************************/
  /*  Newsletter button                            */
  /*************************************************/
  saved_email = $("#newsletter-subscription #newsletter_email").get(0).value;

  $("#newsletter-subscription .title").click(function(e){
    e.stopPropagation();
    if (this.opened == true)
    {
      $(this).parent().animate({width: "110px"}, 500, null );
      this.opened = false;
    } else {
      $(this).parent().animate({width: "260px"}, 500);
      this.opened = true;
    }
  })
  $("#newsletter-subscription").submit(function(e){
    $.post($(this).attr('action'), $('#newsletter-subscription').serializeArray(), function(data){
      
      if (data=="false") {
        $('#newsletter-subscription').addClass("error");
      } else {
        $('#newsletter-subscription').removeClass("error");
        $("#newsletter-subscription .title").click();
        $("body").append(data);
        tb_show("","#TB_inline?height=409&width=465&inlineId=newsletter-success-wrapper","");
      }
    }, "html");
    return false;
  });
  
  $("#newsletter-subscription #newsletter_email")
  .focus(function(e){
    e.stopPropagation();
    if (this.value == saved_email)
    {
      this.value = '';
    }
  })
  .blur(function(e){
    e.stopPropagation();
    if(this.value == '') { this.value = saved_email; }
    $(this).parent.find('.email').animate({width: '0.2em'}, 500, function(){$(this).css('display', 'none')});
  });


  /*************************************************/
  /*  Language button                              */
  /*************************************************/
  $("#language-switch").hover(
    function () {
      $("#language-switch-list").toggle();
    }, 
    function () {
      $("#language-switch-list").toggle();
    }
  );
  
  
  /*************************************************/
  /*  News ticker                                  */
  /*************************************************/
  newsposition = 1;
  newscount = $("#news-list li").length;
  newswidth = 209;
  $("#news-block .prev a").click(function(e){
    e.stopPropagation();
    newsposition > 1 ? newsposition-- : newsposition = newscount;
    $("#news-list").animate({ left: newswidth-(newsposition*newswidth) }, 500);
    return(false);
  })
  $("#news-block .next a").click(function(e){
    e.stopPropagation();
    newsposition < newscount ? newsposition++ : newsposition = 1;
    $("#news-list").animate({ left: newswidth-(newsposition*newswidth) }, 500);
    return(false);
  })
  
  
  /*************************************************/
  /*  Quizz form                                   */
  /*************************************************/
  initQuizzStep1 = function() {
    $("#form-quizz :radio").uniform();

    $('#form-quizz :radio').click(function() {
      $('#profiling-tool').load($('#form-quizz').attr('action'), { client_type: this.value }, initQuizzStep2);
    });
  }
  initQuizzStep1();
  
  initQuizzStep2 = function() {
    xt_click(this,'F','','site_'+lang+'::aide_au_choix_step2');
    
    $("#form-quizz :checkbox").uniform();
    $('#form-quizz').submit(function(e){
      atleastone = false;
      $("#form-quizz :checkbox").each(function(){
        if (this.checked) { atleastone=true; }
      });
      if (!atleastone) { return false; }

      $('#profiling-tool').load($(this).attr('action'), $('#form-quizz').serializeArray(), initQuizzStep3);
      return false;
    });
  }
  
  initQuizzStep3 = function() {
    xt_click(this,'F','','site_'+lang+'::aide_au_choix_step3');
    
    $("#form-quizz :checkbox").uniform();
    $('#form-quizz').submit(function(e){
      atleastone = false;
      $("#form-quizz :checkbox").each(function(){
        if (this.checked) {
          atleastone=true;
        }
      });
      if (!atleastone) { return false; }
    });
  }
});


submitContact = function () {
  $('#form-contact').submit(function(e)
  {
    e.stopPropagation();
    $('#contact-form').load($(this).attr('action'), $(this).serializeArray());
    return false;
  });
}