$(function()
{
  /* BROWSER DETECTION ------------------------------------------------------ */
  var userAgent = navigator.userAgent.toLowerCase();
  $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
  
  if($.browser.mozilla) { $('body').addClass('firefox'); }
  else if($.browser.chrome) { $('body').addClass('chrome'); }
  else if($.browser.safari) { $('body').addClass('safari'); }
  else if($.browser.opera) { $('body').addClass('opera'); }
  else if($.browser.msie && $.browser.version == 6.0) { $('body').addClass('ie6'); }
  else if($.browser.msie && $.browser.version == 7.0) { $('body').addClass('ie7'); }
  else if($.browser.msie && $.browser.version == 8.0) { $('body').addClass('ie8'); }  
  
  
  $('#submit').click(function()
  {
    $(this).parents('form:first').submit();
    return false;
  });
  
  //change le partenaire lors du click
  $(".dist_list a").click(function()
  {
    var href = $(this).attr("rel");
    //partenaire change quand on en selectionne un
    $.getJSON(href, function(data) {
      $("#text_part h3").text(data['title']);
      
      $("#desc_part").html(data['description']);
      $("#addresse").html(data['adresse']);
      
      $("#text_part .adresseweb a").text(data['url']);
      $("#text_part .adresseweb a").attr('href', data['url']);
      
      $("#telephone").text(data['tel']);
      $("#fax").text(data['fax']);
      $("#courriel").text(data['courriel']);
      $("#coordo_part .adresseweb").text(data['pers_ressource']);
      $("#img_part").attr("src", data['image']);

    });
    return false;
  });
  
  //prix et distinction + partenaire hover black n white
  $(".slide img, .dist_list img").hover
  (
    function () {
      var src = $(this).attr("src");
      var rel = $(this).attr("rel");
      $(this).attr("src", rel);
      $(this).attr("rel", src);
    }, 
    function () {
      var src = $(this).attr("src");
      var rel = $(this).attr("rel");
      $(this).attr("src", rel);
      $(this).attr("rel", src);
    }
  );

  //affichage / masquage des block nouvelles selon leur état initiale
  $(".news_desc_full").css("display", "none");
  $(".news_desc").css("display", "block");
  $(".poursuivre .refermer").css("opacity", "0");
  $(".poursuivre .refermer").css("display", "none");
  
  $(".poursuivre .ouvrir, .sujet h3, .positron").click(function()
  {
    var parent_li = $(this).parents(".item");
    var news_full = parent_li.find(".news_desc_full");
    var bouton  = parent_li.find(".poursuivre .ouvrir");
    var display = news_full.css("display");
    
    if(display == "none")
    {
      parent_li.find(".poursuivre .refermer").css("display", "block");
      parent_li.find(".poursuivre .ouvrir").css("display", "none");
      $(bouton).animate({
          opacity: 0
        }, 100, function() {
          $(news_full).slideDown('slow', function() {
            $(parent_li.find(".poursuivre .refermer")).animate({
              opacity: 1
            }, 100, function() {
            });
          });
      });
    }

    return false;
  });
  
  $(".poursuivre .refermer, .sujet h3, .positron").click(function()
  {
    var parent_li = $(this).parents(".item");
    var news_full = parent_li.find(".news_desc_full");
    var bouton  = parent_li.find(".poursuivre .refermer");
    var display = news_full.css("display");
    
    if(display == "block")
    {
      $(bouton).animate({
          opacity: 0
        }, 100, function() {
          
          parent_li.find(".poursuivre .refermer").css("display", "none");
          $(news_full).slideUp('slow', function() {
            parent_li.find(".poursuivre .ouvrir").css("display", "block");
            $(parent_li.find(".poursuivre .ouvrir")).animate({
              opacity: 1
            }, 100, function() {
            });
          });
      });
    }

    return false;
  });
  
  //iframe 2d inter nav
  /*
  $("#slide_step1 a").click(function()
  {
    var link = $(this).attr("rel");
    $("#step_2_img").css("display", "none");
    $("#iframe2d").attr("src", link);
    
    return false;
  });
  */
  
  //ajustement des blocks au départ
  $("#construction .info_construction").css("display", "none");
  $("#construction .ligne_construction").css("display", "block");
  
  $("#construction .active .info_construction").css("display", "block");
  $("#construction .active .ligne_construction").css("display", "none");
  
  //affichage / masquage des block selon leur état initiale
  $("#construction h3").click(function()
  {
    //get l'état
    $parent = $(this).parent("li");
    $active = $parent.hasClass("active");
    
    //si actif on cache
    if($active)
    {
      $($parent).find('.ligne_construction').css("display", "block");
      $($parent).find('.info_construction').slideUp('slow', function() {
      });
      $(this).parent("li").removeClass("active");
    }
    //sinon on affiche
    else
    {
      $($parent).find('.info_construction').slideDown('slow', function() {
      });
      $(this).parent("li").addClass("active");
      $($parent).find('.ligne_construction').css("display", "none");
    }
    return false;
  });
  
  //popup envoyer courriel a un ami
  $(".popup_courriel").colorbox({iframe:true, innerWidth:358, innerHeight:539, scrolling:false});
  //popup partenaires
  $.easy.popup({'closeText' : 'Fermer'});
  
  $('.colorbox').colorbox({iframe:true, innerWidth:358, innerHeight:300, scrolling: false});
  
  
  //aviaslider slideshow regulier
  $('#slideshow').aviaSlider({	blockSize: {height: 586, width:957},
    slideControlls: 'items',
    transition: 'slide',
    display: 'all',
    autorotationSpeed:4,
    transitionOrder: ['diagonaltop', 'diagonalbottom', 'topleft', 'bottomright', 'random']
  });
  
  //mini slideshowflash blanc
  $("#mm_detail #sous_slideshow #mini_slide ul li, #mm_detail #sous_slideshow #map img, #mm_detail.engrenage #step_1 ul li img").hover(
    function () {
      //animation afficher le block
      $(this).animate({
        opacity: 0
      }, 100, function() {
      });
      $(this).animate({
        opacity: 1
      }, 100, function() {
      });
    },
    function () {
    }
  );
  
  
  $("#mm #col1 .item").hover(
    function () {
      //animation afficher le block
      $(this).find(".info_modele img").animate({
        opacity: 0
      }, 200, function() {
      });
      $(this).find(".info_modele img").animate({
        opacity: 1
      }, 200, function() {
      });
    },
    function () {
    }
  );
 
  
  // lorsqu'on click sur une mini
  var image_big = $('#image_big img');
  
  $('#mini_slide_box a, #map .plan').click(function()
  {
    var href= $(this).attr('href');
    
    if(image_big.attr('src') != href)
    {
      image_big.fadeOut('fast', function()
      {
        image_big.attr('src', href);
        image_big.fadeIn('fast');
      });
    }
    
    return false;
  });
  
  //minislideshow nav
  $nb_block = $("#mini_slide_box ul li").size();
  $size_ul = $nb_block * 81;
  $("#mini_slide_box ul").css("width", $size_ul);
  
  $("#mini_slide #next, #mini_slide #prev").click(function()
  {
    if($nb_block >= 6)
    {
      $posLeft = $("#mini_slide_box ul").position().left;
      
      if($(this).attr("id") == "next")
      {
        $restant = $size_ul - 488 + $posLeft;
        if($posLeft > -$size_ul)
          if($restant < 81*6)
            $posLeft -= $restant+2;
          else
            $posLeft -= 81*6;
      }
      else
      {
        $restant = 0 + $posLeft;
        if($posLeft < 0)
          if($restant <= -$size_ul)
          {
            $posLeft -= $restant;
          }
          else
          {
            $posLeft += 81*6;
          }
      }
      
      //animation gauche ou droite
      if($("#mini_slide_box ul").position().left <= 0)
      {
        $("#mini_slide_box ul").animate({
          left: $posLeft
        }, 200, function() {
          if($("#mini_slide_box ul").position().left > 0)
            $("#mini_slide_box ul").css("left", 0);
        });
      }
    }
    
    return false;
  });
  
  //bouton recherche
  $("#bouton_recherche").click(function()
  {
    if($('#recherche_form').hasClass('done'))
    {
      $('#recherche_form').slideUp('slow', function() {
        $('#recherche_form').removeClass("done");
        $('#bouton_recherche').removeClass("active");
      });
    }
    else
    {
      $('#bouton_recherche').addClass("active");
      $('#recherche_form').slideDown('slow', function() {
        $('#recherche_form').addClass("done");
      });
    }
    return false;
  });
  
  //bouton close recherche
  $("#close_form").click(function()
  {
    $('#recherche_form').slideUp('slow', function() {
      $('#recherche_form').removeClass("done");
    });
    //$("#recherche_form").css("display", "none");
    return false;
  });
  
  //bouton slideshow + d'info
  $("#more_info").hover(
    
    function () {
      //timer pour eviter de repeter l'évènement
      clearTimeout($.data(this, 'timer'));
      
      //animation afficher le block
      $(".slide_txt").animate({
        opacity: 1,
        width: '700px'
      }, 500, function() {
        $("#more_info").height($(".slide_txt").height()+45)
        $("#more_info").width($(".slide_txt").width()+12)
      });
    },
    function () {
      //timer pour eviter de repeter l'évènement
      $.data(this, 'timer', setTimeout($.proxy(function()
      {
        $("#more_info").height(32);
        $("#more_info").width(32);
        
        //animation afficher le block
        $(".slide_txt").animate({
          opacity: 0,
          width: '0'
        }, 500, function() {
        });
      }, this), 500));
    }
  );
  
  //navigation 2d interactif bouton next
  $("#step_1 #next").click(function()
  {
    //crée un array contenant les src des images
    var array_img = new Array();
    $.each(".img_inter", function(index, val)
    {
      if($("#slide_step1 li").eq(index).find("img").attr("src"))
        array_img[index] = $("#slide_step1 li").eq(index).find("img").attr("src");
    });
    
    //change les positions pour un effet de next
    size_array = array_img.length;
    
    if(size_array >= 3)
    {
      for(var i in array_img)
      {
        j = parseInt(i) + 1;
        if(j >= size_array)
          j = 0;
          
        $("#slide_step1 li").eq(i).find("img").attr("src", array_img[j])
      }
    }
    return false;
  });
  
  //navigation 2d interactif bouton prev
  $("#step_1 #prev").click(function()
  {
    //crée un array contenant les src des images
    var array_img = new Array();
    $.each(".img_inter", function(index, val)
    {
      if($("#slide_step1 li").eq(index).find("img").attr("src"))
        array_img[index] = $("#slide_step1 li").eq(index).find("img").attr("src");
    });
    
    //change les positions pour un effet de next
    size_array = array_img.length;
    if(size_array >= 3)
    {
      for (i=size_array-1; i>=0; i--){
        j = parseInt(i) - 1;
        if(j < 0)
          j = size_array-1;
        
        $("#slide_step1 li").eq(i).find("img").attr("src", array_img[j])
      }
    }
    
    return false;
  });

  //gestion de la navigation des extraits
  $(".menu_block2 li").click(function()
  {
    $(this).parent("ul").children("li").children("a").removeClass("active");
    $(this).children("a").addClass("active");
    $(this).parent("ul").parent("li").children(".section").removeClass("active");
    $(this).parent("ul").parent("li").children(".section").eq($(this).index()).addClass("active");
    return false;
  });
  
  timer_extraits();
  
  //ajouter le timer ki slide les extraits
  function timer_extraits()
  {
    //sur chaque extrait
    $.each("#block2 li, #onglet_promo .slide", function(index, val) {
      //ajout d'un timer
      $("#block2>li, #onglet_promo .slide").eq(index).everyTime(8000, function() {
        //obtien la position en cours
        $pos = $(this).find(".menu_block2 li a.active").parent("li").index();
        //obtien le nb d'élément (toujours 3 mais bon)
        $count = $(this).find(".menu_block2 li").size();
        //retour a 0 si derniere element
        if($pos + 1 > $count-1)
          $pos = -1;
        
        //affiche et masque les élément en fonction de la position
        $(this).find("li a.active").removeClass("active");
        $(this).find("li a").eq($pos+1).addClass("active");
        $(this).find(".section.active").removeClass("active");
        $(this).find(".section").eq($pos+1).addClass("active");
      });
    }); 
  }
});
