var $j = jQuery.noConflict();
            	function openUrl( url )
            	{
            		document.location.href = url;
            	}

	$j(function(){
		if($j.browser.msie && $j.browser.version < 9){
			$j('#spotTv #videoHtml5').remove();
		}else{
			var rejouerVideo = $j('#spotTv #playerBio .link');
			$j('#spotTv').append(rejouerVideo);
			$j('#spotTv #playerBio').remove();
			
			$j('#spotTv .link #rejouervideo').click(function(e){
				var videoHtml5 = document.getElementById('videoHtml5');
				videoHtml5.currentTime = 0;
				//videoHtml5.play();
			});
		}
	});

function closePopin() {
    if(($j.browser.msie && $j.browser.version > 8) || !$j.browser.msie ){
    var videoHtml5 = document.getElementById('videoHtml5');
	videoHtml5.pause();
	videoHtml5.currentTime = 0;
	}else{
	jQuery('#playerBio').html('<div id="videoPlayerBio"></div><p class="link"><a href="javascript:void(0);" id="rejouervideo">Rejouer le spot TV</a></p>');	
	}
}

function openSpot()
{
	
	if(($j.browser.msie && $j.browser.version > 8) || !$j.browser.msie ){
		var videoHtml5 = document.getElementById('videoHtml5');
		videoHtml5.play();
	}else{
		startVideo('/videos/spot-tv-bio.flv');
	}
	jQuery('#spotTv').customlightbox();
}



function openPage(rayon,marque,slugrayon)
{
	$j('#tableau_detail_content').load('/bio/new-donnees-detaillees', {'rayon' : rayon,'marque':marque,'slugrayon':slugrayon},function(response, status, xhr){
	jQuery('#tableau_detail').customlightbox();
	});
    return false;
}            	


function openPageFamille(idFamille, marque)
{
    $j('#tableau_detail_content').load('/bio/new-donnees-detaillees-famille', { 'idFamille' : idFamille, 'marque':marque },function(response, status, xhr){
	jQuery('#tableau_detail').customlightbox();
	});
    return false;
}


function startVideo($file){
    if ($j('#videoPlayerBio').size() > 0){
        var flashvars = {'file': $file};
        var params = {'bgcolor ': '#000000'};
        var attributes = {};
        swfobject.embedSWF("/flash/Player.swf", "videoPlayerBio", "500", "299", "8.0.0","expressInstall.swf", flashvars, params, attributes);
    }
}

$j(document).ready(function(){
    jQuery.ajax({cache : false});

    // Lancement du player Video
    startVideo('/videos/spot-tv-bio.flv');
    
    $j('#rejouervideo').click(function(e){
        e.preventDefault();
        startVideo('/videos/spot-tv-bio.flv');
    });
    
    $j('#spotTVBio').click(function(e){
        e.preventDefault();
        startVideo('/videos/spot-tv-bio.flv')
    });
  
    
    //Gestion des roll over sur page produit
    
    $j('img.imgInfoPrixMoyen').hover
    (
        function()
        {
           $j(this).next('div.layerInfoPrixMoyen').show();
        },
        function()
        {
            $j(this).next('div.layerInfoPrixMoyen').hide();
        }
     );
     
     $j('img.imgProduitMdd').hover
    (
        function()
        {
           $j(this).next('div.layerInfoProduitMdd').show();
        },
        function()
        {
            $j(this).next('div.layerInfoProduitMdd').hide();
        }
     );    
       
   
  //submit d'ajout au panier
  $j('a.addToCart').click(function()
  {

  		$j.post($j(this).parent('form').attr('action'), $j(this).parent('form').serialize(),
		   function(data){
		 		//$j('#panier').load('/bio/epicerie-salee/conserves #monPanier', function() {
		 		$j('#panier').empty();
		 		$j("#contentRight #Map").after($j(data));
		   });
			$j(this).children('img').attr('src', '/imgs/btn_ajouter_panier_on.png'); // pour changer l'image d'ajout au panier
  });

	   $j('a#deleteCart').live('click', function()
	   {
				$j('div.ajouterPanier img').each(function(i){
					if($j(this).attr('src') == '/imgs/btn_ajouter_panier_on.png')
					{
						$j(this).attr('src', '/imgs/btn_ajouter_panier.png');
					}
				});
				//on suprime l'icone on de la page produit;
				jQuery('.monPanierRight a').each(function()
				{
					url=jQuery(this).attr('href');
					urlsplit=url.split("/");
					gencodeToDelete=urlsplit[urlsplit.length-2];
					jQuery('#form'+gencodeToDelete+' input:image').attr('src','/imgs/btn_ajouter_panier.png');
					
				});
				
				
			
	   });
  
  // checkboxes dans les select multiples pour liste express
  //$j("#select01").dropdownchecklist();
   

 		//conserve l'icone 'on' au rafraichissement de la page (pour le panier)
		$j('input[id^=quantite_]').each(function(i){
			var obj = $j(this);
			var idInput = $j(this).attr('id');
			var gencodeInput = new RegExp(idInput.substring(9), "");
			
			$j('div#monPanier div.monPanierRight a.cartAjaxify').each(function(j){
					//console.log($j(this));
				if(gencodeInput.test($j(this).attr('href')))
				{
					obj.closest('form').find('a.addToCart img').attr('src', '/imgs/btn_ajouter_panier_on.png');
				}
			});
		});
		
		// change l'icone quand on supprime un produit dans le panier (suppression d'un produit au détail)
		// $j('div.monPanierRight a.cartAjaxify').click(function(){
		// 			var hrefProduit = $j(this).attr('href');
		// 			console.log(hrefProduit);
		// 			// var gencodeToDelete = hrefProduit.substring(12);
		// 			// 			console.log(gencodeToDelete, 'blabla');
		// 		});
		
		
		
    
    $j('.imgbasic').hover(
        function(){
            $j(this).attr('src',  $j(this).attr('src').replace('off.jpg','roll.jpg') );
            $j(this).attr('src',  $j(this).attr('src').replace('on.jpg','roll.jpg') );
            
        },
        function(){
            switchExtension = $j(this).closest('li').hasClass('active') ? 'on' : 'off';
            $j(this).attr('src',  $j(this).attr('src').replace('roll.jpg', switchExtension + '.jpg') );
        }
    );
  
    /*******************************************************************/    
    //Hide All the .help layer at starting
    $j('.help').hide();
    
    
    $j('.windowopen').click(function(e){
        e.preventDefault();
        window.open( $j(this).attr('href'),'', 'location=no,status=no,toolbar=no,menubar=no,scrollbars=yes,resizable=no,width=530,height=500');
    })
    
    // Add click Behavior on Open / Close Help Button
    $j('.openHelp').click(function(e){
        e.preventDefault();
        $j('.help').hide();
        idHelp = $j(this).attr('href').replace('#','');
        //alert(idHelp);
        $j('#' + idHelp).show();
    });
    
    $j('.closeHelp').click(function(e){
        e.preventDefault();
        $j(this).closest('.help').fadeOut()
    });
    
    $j('.plusMoinsProduitFamille, #plusMoinsProduit').show();
    
    $j('.qtyMore').click(function(e){
       inputField = $j(this).closest('div.qtyContainer').find('input.qty');
       newVal = parseInt(inputField.val()) +1 < 1000 ? parseInt(inputField.val()) +1 : 999;
       inputField.val(newVal);      
    });
    
    $j('.qtyLess').click(function(e){
       inputField = $j(this).closest('div.qtyContainer').find('input.qty');
       newVal = parseInt(inputField.val()) - 1 > 1 ? parseInt(inputField.val()) - 1 : 1;
       inputField.val(newVal);
       
    });
    
    $j('.updateCart').click(function(e){
        e.preventDefault();
        /*$j('#panier').load( , , function(){
            ajaxifyCart();} 
        );*/
        var url = $j(this).closest('form').attr('action');
        var formdata = $j(this).closest('form').serialize( );
        $j.ajax({
            type: "GET",
            url: url,
            data : formdata,
            dataType: "html",
            cache: false,
            success: function(data) { 
                $j('#panier').html(data);
                ajaxifyCart();
                //alert('c bon');
            }
        });
        
        
        
    });
    
   ajaxifyCart();
   
   
   /********************************
    * Methodo et FAQ
    *******************************/
    $j('.FaqLev').hide();
    if (document.location.hash != ''){
        //console.log( $j( document.location.hash ).parents() );
        $j( document.location.hash ).parents().show()
    }
    
    $j('.opener').toggle(
        function(){$j(this).siblings('.FaqLev').slideDown('normal');},
        function(){$j(this).siblings('.FaqLev').slideUp('normal');}
    );
    
    
    /*****************************
     * Comparateur Flash
     ****************************/
    //  if ($j('#charteGraphique').size() > 0){
    //     var flashvars = {'xmlUrl': '/swf/xmlComp.xml'};
    //     var params = {'wmode ': '#transparent','quality':'high'};
    //     var attributes = {};
    //     swfobject.embedSWF("/swf/chart.swf", "charteGraphique", "528", "420", "8.0.0","expressInstall.swf", flashvars, params, attributes);
    // }

		
		$j('#tableauhistorique tr td.clickable').click(function(){
			$j(this).children('span.on').hide();
			$j(this).children('span.off').show();
			$j(this).css('background', '#eead00');
			$j(this).css('color', 'white');
			
			var tdid = $j(this).attr('id');
			var numberofid = tdid.substring(5);
			
			$j('div.info_synthese').hide();
			$j('div#infos_' + numberofid).show();
		});
		
		
    /*****************************
     * Synthese Rayon
     ****************************/
    $j('#syntheseComparaisonContainer').hide();
    
   /*
    $j('#layerMethodo').hide();
    $j('.methodobio').hover(
        function(e){$j('#layerMethodo').show();},
        function(e){$j('#layerMethodo').hide();}
    );
    */
   
    $j('#lev2').children('ul').children('li').children('ul').hide();
    $j('#lev2').children('ul').children('li').hover(
        function(e){$j(this).children('ul').show(); $j(this).addClass('hovered');},
        function(e){$j(this).children('ul').hide(); $j(this).removeClass('hovered');}
    );
    
    if( $j('#lev2').children('ul').children('li.first').children('a').children('img').attr('src') == '/imgs/btn_retour_accueil_bio.gif')
    {
        $j('#lev2').children('ul').children('li.first').hover(
            function(e){$j(this).children('a').children('img').attr('src', '/imgs/btn_retour_accueil_bio_on.gif');},
            function(e){$j(this).children('a').children('img').attr('src', '/imgs/btn_retour_accueil_bio.gif');}
        );
    }
    
    
    /*******************************************************************/
    //Hide All the #detailReleves layer at starting
    $j('#detailReleves').hide();
    
    // Add toggle behavior on detaildeReleve button
    $j('#lnkDetailReleves').toggle(
        function(){
            $j(this).attr('src',  $j(this).attr('src').replace('on.gif','off.gif')  );
            $j('#detailReleves').show();
        },
        
        function(){
            $j(this).attr('src',  $j(this).attr('src').replace('off.gif','on.gif')  );
            $j('#detailReleves').hide();
        }
    );
    /*******************************************************************/
    // Add click Behavior on help Button for the Releve Page
    $j('#linkAidePrixMoyen,#linkAideNbrMagasins,#linkAideMagasins,#linkAideReleve').click(function(e){
        e.preventDefault();
        $j('.help').not('#' + this.id.replace('linkA','a')).hide();
        $j('#' + this.id.replace('linkA','a')).fadeIn('slow');
    });
	
	
    
});

function opencomparaisonRayon(){
    $j('#syntheseComparaisonContainer').fadeIn();
}


//uniquement pour le flash
function showHelpComparaison(id){
    $j('#' + id).show();
}

function ajaxifyCart(){
    $j('.cartAjaxify').click(function(e){
        e.preventDefault();
        var url = $j(this).attr('href');
        //$j('#panier').load(  ,'', function(){} );
        $j.ajax({
            type: "GET",
            url: url,
            dataType: "html",
            cache: false,
            success: function(data) { 
                $j('#panier').html(data);
                ajaxifyCart();
                //alert('c bon');
            }
        });
				
		 urlsplit=url.split("/");
        lastSplit=urlsplit[urlsplit.length-1];

        // retablit l'icone d'origine a la suppression d'un produit
        if(lastSplit == 0) // on verifie qu'il s'agit bien de l'icone de suppression et pas des +/-
        {
        	var gencodeToDelete = urlsplit[urlsplit.length-2].toString();
        	$j('div#highlightFamille form').each(function(j){
        		if($j(this).attr('id').indexOf(gencodeToDelete)!=-1)
        		{

        			$j(this).find('a.addToCart img').attr('src', '/imgs/btn_ajouter_panier.png');
        		}
        	});
		}
		// retablit l'icone d'origine a la suppression d'un produit
		//console.log('#form'+gencodeToDelete+' input:image');
		jQuery('#form'+gencodeToDelete+' input:image').attr('src','/imgs/btn_ajouter_panier.png');
        
        
        
        
        
        
				
    });
    $j('#lienComparateur').attr('href', $j('#lienComparateur').attr('href'));
}

			/**
			 *	positionnement d'un objet fixe en rapport de la bordure gauche de la page
			 *
			 *	@argument
			 *		idDuConteneur:(string) indiquer le conteneur de l'objet
			 *		idObjetAPositionner:(string) indiquer l'objet � positionner
			 *		saisieUtilisateur:(integer) donner la position gauche en rapport � idDuConteneur
			 */
			function positionFixeBordDePageRedimension(idDuConteneur,idObjetAPositionner,saisieUtilisateur){
				positionFixeBordDePagedeBase(idDuConteneur,idObjetAPositionner,saisieUtilisateur);
				$j(window).resize(function(){
					positionFixeBordDePagedeBase(idDuConteneur,idObjetAPositionner,saisieUtilisateur);
				});
				function positionFixeBordDePagedeBase(idDuConteneur,idObjetAPositionner,saisieUtilisateur){
					var distanceConteneurBordGaucheDeLaPage = $j(idDuConteneur).offset().left;
					$j(idObjetAPositionner).css({"left":distanceConteneurBordGaucheDeLaPage+saisieUtilisateur+"px","position":"fixed"});
				}
			}
$j(function(){
	
	
	
   if ($j('div#content').length > 0) 
		positionFixeBordDePageRedimension("#content","#contentRight",793);

		//anim prix page d'accueil
		
		if($j.browser.msie && $j.browser.version < 9){
			$j('.bgdRond').show();
		}
			var classEnseignePlusCher = 'div[class^=enseignePlusChere]';
			var vitesseAnimPageAccueil = 1000;
			var nbeBlocPrixEnseigne = $j(classEnseignePlusCher).length;
			var vitessefinalPageAccueil = vitesseAnimPageAccueil / nbeBlocPrixEnseigne;
			var compteurAnimPageAccueil = 0;
			
			function animPageAccueil(){
				$j(classEnseignePlusCher).eq(compteurAnimPageAccueil).fadeIn(vitessefinalPageAccueil,function(){});
				if(compteurAnimPageAccueil < nbeBlocPrixEnseigne){
					compteurAnimPageAccueil += 1;
				}else{
					clearInterval(tempsAnimPageAccueil);
					$j('#enseigneMoinsChere').fadeIn(vitesseAnimPageAccueil / 2,function(){});
				}
				
				
			}
			var tempsAnimPageAccueil = setInterval(animPageAccueil,vitessefinalPageAccueil);
	
});

