var timeout_toggler = null;
var click = null;
$(document).ready(function () {
  $('.blocProduit .imgProductOrderable').bind('mouseover',function(){
    $(this).next().show();
  })

    $('.blocProduit .imgProductOrderable').bind('mouseout',function(){
    $(this).next().hide();
  })
  $('.selectStore').bind('click',function(){
      $.scrollTo('#choixMagasin',1000);
  });

	$('.fragment').hide();
var firstElm = $('#tabfragment1');
if(firstElm.length > 0)
			{
				$('#tabfragment1').show();
				$('.engagementTabs').css('height','auto');

			}
  $('.tabtoggler').each(function(i,el){
    $(this).click(function(){
      $('.tabs-selected').removeClass('tabs-selected');
      $(this).parent().addClass('tabs-selected');
      $('.fragment').hide();
      $('.push_tabs').hide();
      myid = parseInt($(this).attr('id').replace('tabtoggler',''));
      $('#tabfragment'+myid).fadeIn('slow');
      $('#push_tabs_'+myid).fadeIn('slow');
      clearTimeout(timeout_toggler);
      click = 1;
      return false;
    });
  });

	$('.blocProduit .button, .blocProduit .button_commandable').bind('mouseenter',function(){
  
			var found=$('#product_list li');
			var found_mise_a=$('.miseAvant');
			var found_promo=$('.promoContent');
			if(found.length > 0)
			{
				$('#product_list li').css('z-index',10);
				$(this).parents('.ajax_block_product').css('z-index',11);
			}
			if(found_mise_a.length > 0)
			{
				$('.miseAvant .blocProduit').css('z-index',10);
				$(this).parents('.blocProduit').css('z-index',11);
			}
			if(found_promo.length > 0)
			{
				$('.promoContent .blocProduit').css('z-index',10);
				$(this).parents('.blocProduit').css('z-index',11);
			}
      if($(this).hasClass('choisi'))
      {
        $(this).find('.infoBulle02').css('display','block');
      }else
      {
        $(this).find('.infoBulle01').css('display','block');
      }

      if($(this).hasClass('choisi_commandable'))
      {
        $(this).find('.infoBulle04').css('display','block');
      }else
      {
        $(this).find('.infoBulle03').css('display','block');
      }
		});

		$('.blocProduit  .button').bind('mouseleave',function(){
				$(this).find('.infoBulle01').css('display','none');
				$(this).find('.infoBulle02').css('display','none');
		});

  $('.blocProduit  .button_commandable').bind('mouseleave',function(){
				$(this).find('.infoBulle03').css('display','none');
				$(this).find('.infoBulle04').css('display','none');
		});

		$('.blocProduit  .button, .blocProduit  .button_commandable').click(function(){
    
    orderable =$(this).find('#is_orderable').val();
    var lieux = $('#lieux').val();
    if(lieux == null || lieux ==""){
      lieux = 'liste'
    }
    if(orderable != 0) {
      $(this).addClass('choisi_commandable');
      $(this).find('.infoBulle03').css('display','none');
      $(this).find('.infoBulle04').css('display','block');
      //tracking ajout produit list
      var Label=$(this).find('.idProduct').text()+'_'+$(this).find('.ProductName').text();
      _gaq.push(["_trackEvent","Panier","Ajout produit commandable [" + lieux + "]",Label,1]) ;
      _gaq.push(["_trackPageview","/VIRTUAL/ajout-produit-commandable/"+Label]);
	return false;
    }
    else {
      $(this).addClass('choisi');
      $(this).find('.infoBulle01').css('display','none');
      $(this).find('.infoBulle02').css('display','block');
      //tracking ajout produit list
      var Label=$(this).find('.idProduct').text()+'_'+$(this).find('.ProductName').text();
      _gaq.push(["_trackEvent","Panier","Ajout produit [" + lieux + "]",Label,1]) ;
      _gaq.push(["_trackPageview","/VIRTUAL/ajout-produit/"+Label]);
	return false;
    }
    
		});

		/*anim bouton fiche produit*/


		$('#add_to_cart .button').click(function(){
			$(this).parent().addClass('choisi');
			$(this).find('.infoBulle01').css('display','none');
			$(this).parent().find('.infoBulle02').css('display','block');
			//tracking ajout produit fiche
			var Label=$('#add_to_cart').find('.idProduct').text()+'_'+$('.ficheProduit').find('h2').text();
			_gaq.push(["_trackEvent","Panier","Ajout produit [fiche]",Label,parseInt($('#quantity_wanted').attr('value'))]) ;
			_gaq.push(["_trackPageview","/VIRTUAL/ajout-produit/"+Label]);

			return false;
		});

  $('#add_to_cart .button_commandable').click(function(){
			$(this).parent().addClass('choisi_commandable');
			$(this).find('.infoBulle03').css('display','none');
			$(this).parent().find('.infoBulle04').css('display','block');
			//tracking ajout produit fiche
			var Label=$('#add_to_cart').find('.idProduct').text()+'_'+$('.ficheProduit').find('h2').text();
			_gaq.push(["_trackEvent","Panier","Ajout produit [fiche]",Label,parseInt($('#quantity_wanted').attr('value'))]) ;
			_gaq.push(["_trackPageview","/VIRTUAL/ajout-produit/"+Label]);

			return false;
		});

		/*anim bouton fiche produit*/


		$('.notes_done  .recette_plus').click(function(){
			$(this).addClass('choisi');
      $(this).find('.infoBulle01').css('display','none');
			$(this).find('.infoBulle02').css('display','block');
			return false;
		});

	$('.displayForm').click(function(e){
		e.preventDefault();
		displayForm($(this).attr('rel'));
	});

	$('.annonce_row').bind('mouseenter',function(){
			$(this).addClass('annonce_hover');
	});
	$('.annonce_row').bind('mouseleave',function(){
		$(this).removeClass('annonce_hover');
	});

	$('ul.faq>li').find('a:first').toggle(function(e){
		e.preventDefault();
		$(this).addClass('active');
		$(this).parent().find('div:first').slideDown('normal');
	},function(){
		$(this).removeClass('active');
		$(this).parent().find('div:first').slideUp('normal');
	});

	//gestion hover sur bloc produit page idee saison
	$('.saisonElem').bind('mouseenter',function(){
			$(this).addClass('saison_hover');
	});
	$('.saisonElem').bind('mouseleave',function(){
		$(this).removeClass('saison_hover');
	});

	//favoris CMS
	$('#addToFavoris').click(function(e){
		e.preventDefault();

		jQuery.post($('#addToFavoris').attr('href'), {
			"id_cms": $('#addToFavoris').attr('rel'),
			"add_to_favoris": 1
        });

	});

	//over bouton
	$('input[type=submit]').bind('mouseenter',function(){
			$(this).addClass('btnHv');
	});
	$('input[type=submit]').bind('mouseleave',function(){
			$(this).removeClass('btnHv');
	});


	$('.blocProduit .listVisuel img').bind('mouseenter',function(){
			$(this).parents('.blocProduit').find('.bottom_block a').addClass('lienHov');

	});
	$('.blocProduit .listVisuel img').bind('mouseleave',function(){
			$(this).parents('.blocProduit').find('.bottom_block a').removeClass('lienHov');
	});

	//add lien carte gagnant
		$('.blockLienGangnat').bind('click',function(){
			var chemin = $(this).find('a.btn').attr('href');
			window.location.href=chemin;
	});

	//idee pratie detecter p vide
	$('.recetteElem .desc p').each(function(){
		if($.trim($(this).text())=='')
			$(this).css('display','none');
	})


	//Tracking lien sortant
	$('.forYrhealth a').bind('click',function(){
	 _gaq.push(["_trackEvent","Liens sortant","Clic","http://www.mangerbouger.fr"]) ;
	});
	$('.footerNav1 a').bind('click',function(){
	 _gaq.push(["_trackEvent","Liens sortant","Clic","http://emploi.supermarchesmatch.fr"]) ;
	});
  
	//Tracking Changer de magasin
	$('.mgsbloc .btn').bind('click',function(){
		 _gaq.push(["_trackEvent","Changer magasin","Clic",$(this).attr('title')]) ;
	});

//Tracking Impression delement
	$('.bloclistCourse .btn').bind('click',function(){
		_gaq.push(["_trackPageview","/VIRTUAL/impression/liste-courses"]);
	});
	$('#idees-pratiques .print').bind('click',function(){
		_gaq.push(["_trackPageview","/VIRTUAL/impression/dossier"]);
	});
	$('#idees-recettes .print').bind('click',function(){
		_gaq.push(["_trackPageview","/VIRTUAL/impression/recette"]);
	});
	$('#product .print').bind('click',function(){
		_gaq.push(["_trackPageview","/VIRTUAL/impression/fiche produit"]);
	});

 /* $('.linkBloc').click(function(){
    _gaq.push(["_trackPageview","/VIRTUAL/impression/fiche produit"]);
  })*/
  $('#Idees_recettes .recettes_blocleft .impr .droite .print').click(function(){
    _gaq.push(["_trackPageview","/VIRTUAL/impression/recette"]);
  })
  $('.ideePratique .pratiqueColLeft .impr .droite .print').click(function(){
    _gaq.push(["_trackPageview","/VIRTUAL/impression/dossier"]);
  })

  $('.btnModifInfo02').click(function(){
   // _gaq.push(["_trackPageview","/VIRTUAL/modification_compte_ok"]);
  })
  $('#flashContentPromo').click(function(){
    window.location.replace("?showProducts=1");
  })

  $('.contRayons .col_left .blocEdito .recetteFragment a').bind('click',function(){
     _gaq.push(["_trackEvent","merchandising","home","opé collector"]);
  });


  $("select").change(function () {
    if($(this).val()!='---'){
      $('#lien_choix_gps_2').attr('href',$(this).val());
    }
  });
});

var displayForm = function(display){
	var show = display.split('|');
	$('#'+show[0]).fadeOut('fast', function(){
		$('#'+show[1]).fadeIn('normal');
	});
};

function createCookie(name,value,days) {
                if (days) {
                               var date = new Date();
                               date.setTime(date.getTime()+(days*24*60*60*1000));
                               var expires = "; expires="+date.toGMTString();
                }
                else var expires = "";
                document.cookie = name+"="+value+expires+"; path=/";
};
function readCookie(name) {
                var nameEQ = name + "=";
                var ca = document.cookie.split(';');
                for(var i=0;i < ca.length;i++) {
                               var c = ca[i];
                               while (c.charAt(0)==' ') c = c.substring(1,c.length);
                               if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
                }
                return null;
}
function eraseCookie(name) {
                createCookie(name,"",-1);
}


function timetabtoggler()
{
  if(loopblockannonce == 'go'){
    var li;
    var id_encours;
    trouver=false;
    li= $('.tabs-selected a').attr('id');
    myid = parseInt(li.replace('tabtoggler',''));
    $('.tabs-selected').removeClass('tabs-selected');
    $('#tabfragment'+myid).fadeOut(200, function(){
    nbid=myid+1;
    boucle=0;
    while(!trouver && boucle < 3){
      id_encours=$('#tabtoggler'+nbid);
      if(id_encours.length !=0){
        id_encours.parent('li').addClass('tabs-selected');
        $('#tabfragment'+nbid).fadeIn(200);
        trouver=true;
      }else{
        nbid=1;
        boucle=boucle+1;//en cas d'erreur eventuel
      }
    }

    });
    if(click != 1){
        timeout_toggler = setTimeout('timetabtoggler()',5000);
    }
  }
}

