var domain = "http://"+window.location.hostname;
//domain+='/verboonen-le-site';
//domain+='/cms';

var searchVal ="";

$(window).load(function(){  
	 slider();
	 wow();
	  heightcols();
						
})
$(document).ready(function(){//on attend que la page soit charg�e
		
	 
	 filters();
	 search();
	 
	 alertShop();
	 orderForm(); 
	 quantity();
	 $('.fancy').fancybox({
	   transitionIn: 'elastic',
	   transitionOut: 'elastic'
	 });
	 $('.contactUs')
		  .fancybox
		  ({
			   'width':280,
			   'height':103,
			   'autoDimensions':false
			   
		  
		  });
	 $('.contactPromo')
		  .fancybox
		  ({
			   'width':280,
			   'height':103,
			   'autoDimensions':false
			   
		  
		  });
	 
		  
	
	 basketRemover();
	 

	 

});




function heightcols()
{
	 if($('#header_rubrique').height()<$('#menu').height())
	 {
		  $('#header_rubrique').height($('#menu').height());
	 }
}



function wow()
{
	 if ($('.wow').length)
	 {
		  $(".wow")
			   .wow({
					"effect":"inOut",
					'scale':2
					});
	 }
	 
	 if ($('.productImg').length)
	 {
		  if (!$('.listeFilter').length)
		  {
			   $('.productImg img').wow({'effect':'inOut'});	     
		  }
	 }
	 
}

function search()
{
	  //input de recherche
	 $('#recherche_barre')
		  .focus(
			   function(){
					searchVal = $(this).val();
					$(this).val('');
			   }
		  )
		  .blur(
			   function(){
					if($(this).val()==""){
						 $(this).val(searchVal);
					}
			   }
		  );
}


function slider(){
        $('#slideLogos').smoothSlide({
                width:516
                ,height:50
                ,multipage:false
                ,leftArrow:'<img src="'+domain+'/files/design/arrowLeft.png" alt="Gauche" />'
                ,rightArrow:'<img src="'+domain+'/files/design/arrow_right.png" alt="Droite" />'
        });
        
}                                                       


//-----------------------------------------------------------		ESHOP		-----------------------------------------------------------


function basketRemover()
{
	 $('a.BasketRemover')
		  .click
		  (
			   function()
			   {
					var productInfos = $(this).attr('rel').split('||');
					var datas = {'productId':productInfos[0],'quantity':productInfos[1],'shop_remove':'1'};
					$('body').append
					(
					'<form id="customDelete" action="" method="post"><input type="hidden" name="products[0][productId]" value="'+productInfos[0]+'" />'+
                        '<input type="hidden" name="shop_remove" value="1">'+
                        '<input type="hidden" name="products[0][quantity]" value="'+productInfos[1]+'">'+
                        '</form>'
						);
					$('#customDelete').submit();
					return false;
					
					
			   }
		  )
}





function orderForm()
{
	 if ($('.validFormElement').length)
	 {
		  
		  
		  if ($('input[name=adresses[]]').is(':checked'))
		  {
			   $('#rightColForm input,#rightColForm textarea,#rightColForm select').attr('disabled',true).css({'background':'#EFEFEF'});
		  }
		  
		  if ($('input[name=assujeti[]]').length && !$('input[name=assujeti[]]').is(':checked'))
		  {
			   $('#tva_num').attr('disabled',true).css({'background':'#EFEFEF'});
		  }
		  
		  
		  
		  $('input[name=adresses[]]')
			   .change
			   (
					function()
					{
						 if ($(this).is(':checked'))
						 {
							 $('#rightColForm input,#rightColForm textarea,#rightColForm select').attr('disabled',true).css({'background':'#EFEFEF'});
						 }
						 else
						 {
							  $('#rightColForm input,#rightColForm textarea,#rightColForm select').attr('disabled',false).css({'background':'#FFF'});
						 }
					}
			   )
		  $('input[name=assujeti[]]')
			   .change
			   (
					function()
					{
						 if ($(this).is(':checked'))
						 {
							 $('#tva_num').attr('disabled',false).css({'background':'#FFF'});
						 }
						 else
						 {
							  $('#tva_num').attr('disabled',true).css({'background':'#EFEFEF'});
						 }
					}
			   )
	 }
}


function alertShop()
{
	 if ($('#shopAlert').length)
	 {
		  absolutePlace($('#shopAlert'),3,2);
		  
		  if ($(window).height()<$(document).height())
		  {
			   $('#shopAlertBackground').height($(document).height());
		  }
		  $('#validerBack')
			   .click
			   (
					function()
					{
						 $('#shopAlertBackground').remove();
					}
			   )
	 }
}

function absolutePlace(objet,ratioTop,ratioLeft)
{
	 var top = ($(window).height()/ratioTop)-(objet.height()/ratioTop);
	 var left = ($(window).width()/ratioLeft)-(objet.width()/ratioLeft);
	 
	 objet.css({'left':left+'px','top':top+'px'});
	 
	 $(window)
		  .resize
		  (
			   function()
			   {
					var top = ($(window).height()/ratioTop)-(objet.height()/ratioTop);
					var left = ($(window).width()/ratioLeft)-(objet.width()/ratioLeft);
					
					objet.css({'left':left+'px','top':top+'px'});
			   }
		  )
}





function quantity(){
	 $('.less')
		  .click
		  (
			   function()
			   {
					if ($('#textQuantity').length)
					{
						var quantityInput =  $('#textQuantity');
					}
					else
					{
						 var quantityInput =  $(this).next('.textQuantity');
					}
					var qtty = parseInt(quantityInput.val());
					if (qtty>1)
					{
						 qtty--;
						 quantityInput.val(qtty);
					}
			   }
			   
		  )
		  
	 $('.more')
		  .click
		  (
			   function()
			   {
					if ($('#textQuantity').length)
					{
						var quantityInput =  $('#textQuantity');
					}
					else
					{
						 var quantityInput =  $(this).prev('.textQuantity');
					}
					
					var qtty = parseInt(quantityInput.val());
					qtty++;
					quantityInput.val(qtty);
			   }
			   
		  );
		  
	 $('#textQuantity').numeric();
	 $('.textQuantity').numeric();
	 
	 $('#textQuantity')
		  .change
		  (
			   function()
			   {
					var qtty = parseInt($('#textQuantity').val());
					if (qtty<1)
					{
						 qtty = 1;
						 $('#textQuantity').val(qtty);
					}
					
					
			   }
			   
		  );
		  
	  $('.textQuantity')
		  .change
		  (
			   function()
			   {
					var qtty = parseInt($('.textQuantity').val());
					if (qtty<1)
					{
						 qtty = 0;
						 $('.textQuantity').val(qtty);
					}
				
			   }
			   
		  );
		  
		  
		  
		  $('#buy').hover
		  (
			   function()
			   {
					$(this).animate({backgroundColor:'#72E4F1'},150);
			   }
			   ,function()
			   {
					$(this).animate({backgroundColor:'#62D4E1'},150);
			   }
		  );
}



//-----------------------------------------------------------		FILTERS E-SHOP		---------------------------------------------------------



function filters(){
	if ($('.selectOpener').length){
		$('.listeFilter').click(function(e){
			if (e.target.tagName!='LABEL' && e.target.tagName!='INPUT'){
				$('.listeFilter').css({'z-index':'1'});
				var wasOpen = false;
				if ($(this).height()>20){
					wasOpen = true;
				}
				$('.listeFilter').each(function(){
					if ($(this).height()>20){
						$(this).animate({'height':"20px"},200,function(){$(this).find('.selectOpener').attr('src',domain+'/files/design/selectOpener.png');});
					}
				});
				if (wasOpen == false){
					var height = $(this).find('li').length*20;
					$(this)
						 .animate(
							  {'height':height+"px"}
							  ,200
							  ,function(){
								   $(this).find('.selectOpener').attr('src',domain+'/files/design/selectCloser.png');
								   })
						 .css({'z-index':'10','position':'absolute'});
				}
			}
			
		});
	}
	davidCopperfield();
	
}



//fait disparaitre les produits en fonction des filtres. Comme pour David, il n'y a pas vraiment de magie (d'ailleurs ça n'existe pas la magie ! )
function davidCopperfield()
{
	$('.listeFilter input[type=checkbox]')
		  .click(
			   function()
			   {
					var temp = $(this).attr('id').split('_');
					var typeFilter = temp[0];
					var typeVal = temp[1];
					
					
					if (typeVal=="all")
					{
						 $('.'+typeFilter).attr('checked', false);
						 $(this).attr('checked', true);
					}
					else
					{
						 $('#'+typeFilter+'_all').attr('checked', false);
						 
						 if(!$(this).is(':checked'))
						 {
							  uncheckUnder($(this));
							  if (!$.find('.'+typeFilter+':checked').length)
							  {
								   $('#'+typeFilter+'_all').attr('checked', true);
							  }
						 }
						 else
						 {
							  checkUnder($(this));
						 }
						 
					}
					applyFilters();
					
			   }
			   
	 
		  );
	
}

function applyFilters()
{
	 $('.product').hide();
	 $('.show').removeClass('show');
	 
	 //Catégories
	 if ($('#cat_all:checked').length)
	 {
		  $('.product').show();
	 }
	 else
	 {
		  $('.cat:checked')
			   .each(
					function()
					{
						var showClass = $(this).attr('id');
						$('.'+showClass).show(); 
						 
					}
			   )
	 }
	 
	 
	 //Poids Max
	  if (!$('#pm_all:checked').length)
	 {
		  //alert('poidsMax');
		  $('.pm:checked')
			   .each
			   (
					function()
					{
						var showClass = $(this).attr('id');
						
						 $('.product:visible')
							  .each
							  (
								   function()
								   {
										if ($(this).find('.'+showClass).length)
										{
											 $(this).find('.'+showClass).addClass('firstStep');
										}
								   }
								   
							  );
					}
			   )
	 }else
	 {
		  $('.productInfos').addClass('firstStep');
	 }
	 
	
	 //Précision
	 if (!$('#pr_all:checked').length)
	 {
		  $('.pr:checked')
			   .each
			   (
					function()
					{
						var showClass = $(this).attr('id');
						
						 $('.product:visible')
							  .each(
								   function()
								   {
										if ($(this).find('.'+showClass+'.firstStep'))
										{
											 $(this).find('.'+showClass+'.firstStep').addClass('show');
										}
										else
										{
											 $(this).hide();
										}
										
								   }
								   
							  );
					}
			   )
	 }
	 else
	 {
		  $('.firstStep').addClass('show');
	 }
	 
	 $('.firstStep').removeClass('firstStep');
	 $('.productInfos').hide();
	 $('.show').show();
	 
	 $('.product:visible')
		  .each
		  (
			   function()
			   {
					if (!$(this).find('.show').length){
						 $(this).hide();
					}
			   }
			   
		  )
	 
}


//Décheck toutes les checkbox enfants de l'objet passé en param
function uncheckUnder(obj){
	 obj.parent('li').find('input[type=checkbox]').attr('checked', false);
}

function checkUnder(obj){
	 obj.parent('li').find('input[type=checkbox]').attr('checked', true);
}


