$(function(){
  Ctrl.init($(document));
});

var Ctrl = new function()
{
  this.mycarousel_initCallback = function (carousel) {
		jQuery('.jcarousel-control a').bind('click', function() {
			jQuery('.jcarousel-control a').removeClass('active');
			carousel.scroll(jQuery.jcarousel.intval($('img',$(this)).attr('alt')));
			$(this).addClass('active');
			return false;
		});

		jQuery('.mycarousel-next').bind('click', function() {
			carousel.next();
			return false;
		});

		jQuery('.mycarousel-prev').bind('click', function() {
			carousel.prev();
			return false;
		});
  };
  this.mycarousel_itemLoadCallback = function (a, b) {
		//alert(a+"/"+b);
		dbg($(b).attr('class'));
  };
  this.init = function($dom)
  {
	  
	  if($.browser.msie && $.browser.version < 9)
	  {
		 $("#menuRightConseil #conseilPerso select").each(function(){
			 var $select = $(this);
			 $(this).mouseover(function(){
		          $(this).css("width", "auto");
		      });
			 $(this).blur(function(){
		          $(this).css("width", "170px");
		      });
			 $(this).change(function(){
		          $(this).css("width", "170px");
		      });
		 });
	  }

	  
    $(".slide").msAccordion();
    $('a[rel=facebox]').facebox();
    $('.facebox .close').click(function(){
      $.facebox.close();
    });
    
    if(!($.browser.msie && $.browser.version < 7))
    {
	    $('.bt_facebox_commande').hover(
	    	function(){
		      $(this).addClass('hover');
		    },
		    function(){
		      $(this).removeClass('hover');
		    }
		  );
    }
	if ($('div.slide_wrapper').length)
    {
	  if($.browser.msie && $.browser.version < 7)
	  {
		  $('div.slide_wrapper').addClass('slideForIeLabo');
	  }
      $('ul.slideShowLabo').cycle({
        speed: 1000,
        timeout: 4000,
        fx:  'scrollLeft',
        pagerEvent: 'click',
        pause: 1,
        prev:  '.slideShowLaboBtLeft',
        next:  '.slideShowLaboBtRight',
        pager: '#navSlideLabo',
        pagerAnchorBuilder: function(idx, element){
    	  $metadata = $(element).metadata();
          return "<li class='li_bt"+(idx+1)+" fleft clearfix'><a style='z-index:1000;' class='bt"+(idx+1)+" ifixpng clearfix' href='#'><img style='z-index:1000; position:relative;' width='160' height='64' src="+$metadata.image+" class='ifixpng fleft' /><img style='z-index:1000; position:relative;' src="+$metadata.imageBis+" width='103' height='47'  class='fright mgt10 mgr10 ifixpng' /></a></li>";
        }
      });
      if($.browser.msie && $.browser.version < 7)
      {
//    	  $('.slide_wrapper .slideShowLaboNav li').each(function(){
//    		  $(this).bind('click', function(){
//    			  $('a', $(this)).click();
//    		  });
//    	  });
    	  
    	  $('.slide_wrapper .slideShowLaboNav li.li_bt3').width('341');
    	  $('.slide_wrapper .slideShowLaboNav li a.bt3').width('331');
    	  
    	  $('.slide_wrapper .slideShowLaboNav li a.bt2').width('273');
    	  $(".slideShowLaboBtLeft").hover(
  		    function(){
  		      $(this).addClass('hoverBtLeft');
  		    },
  		    function(){
  		      $(this).removeClass('hoverBtLeft');
  		    }
  		  );
      	  $(".slideShowLaboBtRight").hover(
      		function(){
      	      $(this).addClass('hoverBtRight');
      	    },
      	    function(){
      	      $(this).removeClass('hoverBtRight');
      	    }
      	  );
      }
      //$('.slide_wrapper .slideShowLaboNav').ifixpng($imageIfixpng);
    }
	
    $(".tabs_me").tabs();
    
    if($('body').hasClass('layoutHome'))
    {
    	if($('.interstitiel_home').length)
    	{
    		$.facebox.settings.classBody = 'interstitielHome';
    		$('#facebox').width($('.imageInterstitielHidden').width()+20);
//    		$.facebox.loading();
    		$metadata = $('.interstitiel_home').metadata();
    		$.facebox("\
				<div class='contenu_facebox'> \
					<span class='titre_interstitiel'>"+$metadata.titre+"</span> \
					<a class='clearfix' href='"+$metadata.url+"'><img alt='"+$metadata.titre+"' src="+$metadata.image+" /></a> \
				</div>"
			);
    	}
    }
    $(".layoutHome .conseil .submit").hover(
      function(){
        $(this).addClass('submitHover');
      },
      function(){
        $(this).removeClass('submitHover');
      }
    );
    $(".bt_recherche").hover(
      function(){
        $(this).addClass('bt_recherche_hover');
      },
      function(){
        $(this).removeClass('bt_recherche_hover');
      }
    );
    $(".submitSearch").hover(
      function(){
        $(this).addClass('submitSearchHover');
      },
      function(){
        $(this).removeClass('submitSearchHover');
      }
      );
    $(".submit").hover(
      function(){
        $(this).addClass('submitHover');
      },
      function(){
        $(this).removeClass('submitHover');
      }
    );

    $(".footerCommande .submit").hover(
          function(){
            $(this).attr('src','/extension/ezvitarmonyl/design/ezwebin/images/content/bgVisualiserOver.jpg');
          },
          function(){
            $(this).attr('src','/extension/ezvitarmonyl/design/ezwebin/images/content/bgVisualiser.png');
          }
    );
    
    
    if($('.liste_produit').length)
    {
    	Ctrl.hoverForListe($dom);
    }
    
    $('.zoomProduit').each(function(){
    	$(this).bind('click',function(){
        	$.facebox.loading();
    		$metadata = $(this).metadata();
    		dbg($metadata);
    		$.facebox("\
    			<div class='contenu_facebox'> \
    				<img alt='"+$metadata.titre+"' src="+$metadata.image+" /> \
    				<span class='titre_produit'>"+$metadata.titre+"</span> \
    			</div>"
    		);
    		$('.facebox .close').click(function(){
    			$.facebox.close();
    		});
        });
    });
    
    $('.smooth_span span').css('opacity',0);  
    $('.smooth_span').hover(function(){
      $('span',this).css('display','block').clearQueue().animate({
        opacity: 1
      });
    },function(){
      $('span',this).clearQueue().animate({
        opacity: 0
      });
    });
    
   
    
    $('.input_legend').each(function(){
      var input = $('input[name='+$(this).attr('for')+']');
      var $this = $(this);
      if(input.attr('value').length > 0)
        $(this).css('display','none');
      
      $(this).click(function(){
        $(this).css('display','none');
      });
      input.blur(function(){
        if($(this).attr('value').length == 0)
          $this.css('display','block');
      });
    });

	$('.ajouterProduit').click(function() {
		id=$(this).attr('id');
		var $boutonAddProdMetadata=$(this).metadata();
		id= id.replace('ajouterProduit-','');
		$.ez( 'panier::ezjscore_ajouterPanier', {id_produit:id}, function( data )
		{ 
			if ( data.error_text ){
				$( '#panier' ).html( data.error_text );
			}else{
				$( '#panier' ).html( data.content.panier_design );
				$.facebox.settings.classBody = 'popupAddCart';
				$.facebox.loading();
				$.facebox("\
						<div class='contenu_facebox ajout_panier_facebox clearfix'> \
							<span class='titre_facebox'>Cet article a bien été ajouté à votre panier</span> \
							<div class='statique clearfix'> \
								<span class='prix'>Prix :</span><span class='pri'>"+$boutonAddProdMetadata.prix+" €</span> \
							</div> \
							<div class='btn1'> \
								<a class='link ifixpng go_to_cart' href="+$boutonAddProdMetadata.url+">Je souhaite voir mon panier</a> \
							</div> \
							<div class='btn2'> \
								<a class='link ifixpng close' href='#'>Je continue mes achats</a> \
							</div> \
						</div>"
					);
				$('.contenu_facebox .close').click(function(){
			      $.facebox.close();
			    });
//				jQuery.facebox('Le produit a été ajouté au panier') ;
				initPanierEvent();
				if(!$('body').hasClass('withPanier'))
					$('body').addClass('withPanier');
			}
		});
	});
	
	$('#besoin').change(function() {
		id = $(this).val();
		$('#selectConseil select').hide();
		$('#selectConseil li').hide();
		$('#selectConseil-'+id).show();
		$('#selectConseil-'+id).parent().show();
	});
	$('#selectConseil-'+$('#besoin').val()).show();
	$('#selectConseil-'+$('#besoin').val()).parent().show();
	
	$('#besoin').change(function() {
		id = $(this).val();
		$('#selectPointVenteContent select').hide();
		$('#selectPointVenteContent li').hide();
		$('#selectPointVente-'+id).show();
		$('#selectPointVente-'+id).parent().show();
	});
	$('#selectPointVente-'+$('#besoin').val()).show();
	$('#selectPointVente-'+$('#besoin').val()).parent().show();

	if($.browser.msie && $.browser.version < 7)
		$('.bouton_theme_questions').addClass('bouton_for_ie');
		
	$('.bouton_theme_questions').each(function(){
		$(this).hover(
	      function(){
	        $(this).addClass('hover');
	      },
	      function(){
	        $(this).removeClass('hover');
	      }
	    );
		$(this).bind('click', function(){
			$('.bouton_theme_questions').removeClass('current_bt');
			var id = $(this).attr('id');
			id = id.replace('node_','');
			$(this).addClass('current_bt');
			$.ez( 'questionsreponses::getListe', {node_id: id}, function( data )
			{ 
				if ( data.error_text )
				{
					$('.bouton_theme_questions').removeClass('current_bt');
					$( '#liste_questions' ).html( data.error_text );
				}
				else
				{
					$( '#liste_questions' ).html( data.content.design );
				}
			});
			
		});
	});
	
	$("#form_formQuestionMarine").submit(function(){ 
		var field_info = {
			civilite:$("#form_formQuestionMarine input[name=civilite]:checked").val(),
			field_mail:	$("#field_mail").val(),
			field_message:$("#field_message").val(),
			field_sujet:$("#field_sujet").val(),
			field_nom:$("#field_nom").val(),
			field_prenom:$("#field_prenom").val(),
			field_telephone:$("#field_telephone").val(),
			field_newsletter:$("#field_newsletter:checked").val()
		}
		
		var height_content = ($('#formQuestionMarine').height())/2;
		var loadingMsg = "<div style='height:"+height_content+"px; padding-top:"+height_content+"px;' class='produitSearch text_align_center'><img src='/extension/ezvitarmonyl/design/ezwebin/images/loader.gif' /></div>";
		$( '#formQuestionMarine' ).html( loadingMsg );
		
		$.ez( 'contact::submit_marine', field_info, function( data )
		{ 
			if ( data.error_text )
				$( '#formQuestionMarine' ).html( data.error_text );
			else
				$( '#formQuestionMarine' ).html( data.content.design );
		});
	}); 
	$('.ifixpng').ifixpng($imageIfixpng);
	initPanierEvent();
	initFormulaireCommandeEvent();
	initOpener();
	
	
	
	if($( '#liste_produits' ).length)
	{
		Ctrl.initListeForCategorieListe($( '#liste_produits' ));
	}
	if($( '#liste_produits' ).length)
	{
		Ctrl.initListeForGammeListe($( '#liste_produits' ));
	}
	if($( '#liste_conseils' ).length)
	{
		Ctrl.initListeForConseilsListe($( '#liste_conseils' ));
	}
	if($( '#liste_dossiers' ).length)
	{
		Ctrl.initListeForDossierListe($( '#liste_dossiers' ));
	}
	
	if($.browser.msie && $.browser.version < 7)
        $("#facebox").bgiframe();
  };
  
  this.hoverForListe = function($dom)
  {
	  $('.liste_produit li', $dom).each(function(){
  		$(this).hover(
  				function(){
  					$(this).addClass('contentHover');
  				},
		        function(){
  					$(this).removeClass('contentHover');
		        }
		   );
	  });
  };
  
  
  this.initListeForCategorieListe = function($listeProduit)
  {
	  $('.bouton_categorie_liste_produits', $listeProduit).each(function(){
			$(this).bind('click', function(){
				$metadata = $(this).metadata();
				categorie_id = $metadata.categorie_id;
				page = $metadata.page;
				var height_content = ($('#liste_produit_content', $listeProduit).height())/2;
				var loadingMsg = "<div style='height:"+height_content+"px; padding-top:"+height_content+"px;' class='produitSearch text_align_center'><img src='/extension/ezvitarmonyl/design/ezwebin/images/loader.gif' /></div>";
				$('#liste_produit_content',$listeProduit).html( loadingMsg );
					
				$.ez( 'produitsliste::getCategorieListe', {categorie_id: categorie_id, page:page }, function( data )
				{ 
					if ( data.error_text )
					{
						$listeProduit.html( data.error_text );
					}
					else
					{
						$listeProduit.html( data.content.design );
					}
					Ctrl.initListeForCategorieListe($listeProduit);
					Ctrl.hoverForListe($listeProduit);
				});
				return false;
			});
		});
  };
  
  this.initListeForGammeListe = function($listeProduit)
  {
	  $('.bouton_gamme_liste_produits', $listeProduit).each(function(){
			$(this).bind('click', function(){
				$metadata = $(this).metadata();
				gamme_id = $metadata.gamme_id;
				page = $metadata.page;
				
				var height_content = ($('#liste_produit_content', $listeProduit).height())/2;
				var loadingMsg = "<div style='height:"+height_content+"px; padding-top:"+height_content+"px;' class='produitSearch text_align_center'><img style='border:0 none' src='/extension/ezvitarmonyl/design/ezwebin/images/loader.gif' /></div>";
				$( '#liste_produit_content', $listeProduit ).html( loadingMsg );
					
				$.ez( 'produitsliste::getGammeListe', {gamme_id: gamme_id, page:page }, function( data )
				{ 
					if ( data.error_text )
					{
						$listeProduit.html( data.error_text );
					}
					else
					{
						$listeProduit.html( data.content.design );
					}
					Ctrl.initListeForGammeListe($listeProduit);
					Ctrl.hoverForListe($listeProduit);
				});
				return false;
			});
		});
  };
  this.initListeForConseilsListe = function($listeConseil)
  {
	  $('.bouton_conseils_liste',$listeConseil).each(function(){
			$(this).bind('click', function(){
				$metadata = $(this).metadata();
				categorie_id = $metadata.categorie_id;
				page = $metadata.page;
				
				var height_content = ($('#liste_conseils_content',$listeConseil).height())/2;
				var loadingMsg = "<div style='height:"+height_content+"px; padding-top:"+height_content+"px;' class='produitSearch text_align_center'><img style='border:0 none' src='/extension/ezvitarmonyl/design/ezwebin/images/loader.gif' /></div>";
				$( '#liste_conseils_content',$listeConseil ).html( loadingMsg );
					
				$.ez( 'conseilsliste::getListe', {categorie_id: categorie_id, page:page }, function( data )
				{ 
					if ( data.error_text )
					{
						$listeConseil.html( data.error_text );
					}
					else
					{
						$listeConseil.html( data.content.design );
					}
					Ctrl.initListeForConseilsListe($listeConseil);
				});
				return false;
			});
		});
  };
  this.initListeForDossierListe = function($listeDossier)
  {
		
		
		$('.bouton_dossiers_liste', $listeDossier).each(function(){
			$(this).bind('click', function(){
				$metadata = $(this).metadata();
				categorie_id = $metadata.categorie_id;
				page = $metadata.page;
				
				var height_content = ($('#liste_dossiers_content', $listeDossier).height())/2;
				var loadingMsg = "<div style='height:"+height_content+"px; padding-top:"+height_content+"px;' class='produitSearch text_align_center'><img style='border:0 none' src='/extension/ezvitarmonyl/design/ezwebin/images/loader.gif' /></div>";
				$( '#liste_dossiers_content', $listeDossier).html( loadingMsg );
					
				$.ez( 'dossiersliste::getListe', {categorie_id: categorie_id, page:page }, function( data )
				{ 
					if ( data.error_text )
					{
						$listeDossier.html( data.error_text );
					}
					else
					{
						$listeDossier.html( data.content.design );
					}
					Ctrl.initListeForDossierListe($listeDossier);
				});
				return false;
			});
		});
	};
  
};

function dbg()
{
  try {
    console.debug(arguments);
  }
  catch(e)
  {
    for(var i in arguments)
    {
      if (i<5) alert(arguments[i]);
    }
  }
};

function initOpener(){
	$('.open .opener').unbind();
	$('.open .opener').click(function(){
		$(this).parents('.open').children('.openable').slideToggle(300);
		return false;
	});
	$('.open .openable.closed').hide();
}

/*
function initPanierEvent(){
	$('#viderPanier').unbind();
	$('#viderPanier').click(function() {
		$.ez( 'panier::ezjscore_viderPanier', {}, function( data )
		{ 
			if ( data.error_text ){
				$( '#panier' ).html( data.error_text );
			}else{
				$( '#panier' ).html( data.content.panier_design );
				$('body').removeClass('withPanier');
				initPanierEvent();
			}
		});
	});
}*/
