jQuery(document).ready(function($) {

  var hide_spot=true;

  // general //
  function load_fiche(id) {
    $('.minifiche').hide();
    $('#fiche-'+id).show();
  }

  function plotParcours() {
    
    
    //console.log('plotparcours');
  
    $('.boutique_location span').remove();
    $('.guide_shopping').each(function(index) {
      var id=$(this).attr('boutique');
      $('#boutique-'+id).html($('#boutique-'+id).html()+'<span class="numParcours">'+(index+1)+'</span>');
    });
  }

  function hide_plan() {
    $('.boutique_location').css('background', 'none');
    $('.boutique_infobulle').hide();
  }

  function hide_all() {
    $('.bloc_boutique').hide();
    $('.bloc_thematique').hide();
//    $('.blocRechercheBoutiques').hide();
    $('.minifiche').hide();
    hide_plan();
  }

  function show_boutique(id) {
    hide_all();
    $('.bloc_boutique').show();
    $('#fiche-'+id).show();
    $('#infobulle-'+id).show();
  }

  function spot(id) {
    $('#boutique-'+id).css('background', 'url(/themes/esprit-voisin/images/ici.gif) no-repeat');
  }

  function reOrder() {
    var data={};
    $('.listSortable li').each(function(index) {
      data['list'+index]=$(this).attr('boutique');
    });
    data['cpt']=$('.listSortable li').length;
    $.post($('.listSortable:first').attr('link'), data, newBasket);
  }
  
  function newBasket(data) {
  

    if (data.length>0) {
    
        $('#overlayLightbox,#parcours_dialog').remove();
        
        $('#parcours_shopping').html(data);
        
        var move;
        
        move = $('#overlayLightbox,#parcours_dialog').detach();
        
        move.appendTo('body');

        //console.log('move lightbox');
        
              
        initParcours();

        $('.view_parcours').click(function () {
            $('.contentOnglet').hide();
            $('.tab_parcours_shopping').show(); 
      });
      
    }
  }

  function initParcours() {
  
    //console.log('initParcours() ok');
    
    if ( $('.tinyScrollBar').length ) {
      $('.tinyScrollBar').tinyscrollbar();
    }
    
    if ( $('.listSortable').length ) {
      $('.listSortable').sortable({
       update: function(event, ui) { reordonneList(); },
       axis: 'y'
      });
      $('.listSortable').disableSelection();
      function reordonneList() {
        reOrder();
      }
      $('.listSortable li').each(function(index) {
        $(this).find('a.guide_shopping').prepend('<span>'+(index+1)+'. </span>');
        var id=$(this).attr('boutique');
        $('#fiche-'+id).find('.add_to_basket').remove();
      });
      // fix bug focus drag & drop sur ie < 9
      if ($.browser.msie && $.browser.version.substr(0,1) < 9) {
        $('.listSortable a').mouseup(function() {
          this.blur();
        });
      }
      
      $('.btImprParcours').click(function (e) {
          window.print();
          this.blur();
          e.preventDefault();
      });
      
      $(".openLightCommBt").click(function (e) {
          
          $('#openLightComm').click();
          this.blur();
          e.preventDefault();
      });
      
    }
    
    $('.remove_parcours').click(function(e) {
      var id=$(this).attr('boutique');
      $.get( $(this).attr('href'), {id: id}, newBasket);
      this.blur();
      e.preventDefault();
    });
    
    plotParcours();
  }
  // fin general //

  // Recherche par nom
  $('#rechercheParNom').change(function () {
    $('ul.contentOnglets:first').addClass('blocRechercheBoutiquesResultat');
    hide_all();
    var id=$(this).val();
    $('.bloc_boutique').show();
    spot(id);
    $('#fiche-'+id).show();
    $('#rechercheParThematique').val('');
  });

  // Recherche par thematique
  $('#rechercheParThematique').change(function () {
    $('ul.contentOnglets:first').addClass('blocRechercheBoutiquesResultat');
    hide_all();
    var id=$(this).val();
    $('#thematique-'+id).show();
    $('#rechercheParNom').val('');
  });

  // Click sur plan
  $('.boutique_location').click(function() {
    $('ul.contentOnglets:first').addClass('blocRechercheBoutiquesResultat');
    var id=$(this).attr('boutique');
    show_boutique(id);
    $('.tab_parcours_shopping').hide();
    $('ul.contentOnglets > li:first').show();
  }); 

  // RollOver thematique
  $('.boutique_par_thematique').mouseenter(function() {
    hide_plan();
    var id=$(this).attr('boutique');
    spot(id);
  }).mouseleave(function() {
    if (hide_spot) {
      hide_plan()
    }
    hide_spot=true;
  });; 

  // Click thematique
  $('.boutique_par_thematique').click(function() {
    $('ul.contentOnglets:first').addClass('blocRechercheBoutiquesResultat');
    hide_all();
    var id=$(this).attr('boutique');
    $('#rechercheParNom').val(id);
    $('.bloc_boutique').show();
    $('#fiche-'+id).show();
    hide_spot=false;
    spot(id);
    return false;
  });

  // RollOver sur plan
  $('.boutique_location').mouseenter(function() {
    hide_plan();
    var id=$(this).attr('boutique');
    spot(id);
    $('#infobulle-'+id).show();
  }).mouseleave(function() {
    hide_plan()
  });

  $('.add_to_basket').click(function (e) {
    var id=$(this).attr('boutique');
    $.get( $(this).attr('href'), {id: id}, newBasket);
    e.preventDefault();
  });

  $('.add_to_basket_boutique').click(function (e) {
      var id=$(this).attr('boutique');
      $.get( $(this).attr('href'), {id: id}, function(data) {
      $('body').append(data);
      
      $('.view_parcours').click(function () {
        $('.contentOnglet').hide();
        $('.tab_parcours_shopping').show();
      });
      
      $('.add_to_basket-'+id).remove();
      //console.log('removed .add_to_basket-'+id);
      });
      
      e.preventDefault();
  });

  $('.add_to_basket_form').click(function (e) {
    var id=$('#rechercheParNomParcours').val();
    $.get( $(this).attr('href'), {id: id}, newBasket);
    e.preventDefault();
  });

  $('.save_comment').click(function (e) {
    var comment=$('#comment').val();
    $.get($(this).attr('href'), {comment: comment}, function(data) {
      $.fancybox.close();
      //console.log('save_comment : fermer lightbox');
    });
    e.preventDefault();
  });


  initParcours();
  /*IE Fix*/
  $('.tab_parcours_shopping').hide();
});

