// JavaScript Document
function galeryHomepage() {
	
	$('.galery_menu a').click(function(){
		var _this = $(this);
		var frameIndex = _this.index('.galery_menu a');
		if( !_this.parent().is('.active') ) {
			$('.galery_content li.active').fadeOut(500,function(){
				$(this).removeClass('active');												
				$('.galery_content li:eq('+frameIndex+')').fadeIn(500,function(){
					$(this).addClass('active')
					$('.galery_menu li').removeClass('active');
					_this.parent().addClass('active');
				});
			});
			}
		return false;
	});
	setTimeout(function(){autoSlide()},5000);
	
}
function autoSlide() {
	if( ($.browser.msie)&&($.browser.version <= 7) ){
		if($('.galery_menu .active').next().size()>0) {
			$('.galery_menu .active').next().find('a').trigger('click');
		} else {
			$('.galery_menu li:first a').trigger('click');
		}
	} else {
		if(!$('.galery').is('.galery:hover')) {
			if($('.galery_menu .active').next().size()>0) {
				$('.galery_menu .active').next().find('a').trigger('click');
			} else {
				$('.galery_menu li:first a').trigger('click');
			}
		}
	}
	setTimeout(function(){autoSlide()},5000);
}

function asideAccordion() {
	$('#content .aside li > a + div').hide();
	$('#content .aside li:first > a + div').show();
	$('#content .aside li > a').append($('<span>').html('+'));
	$('#content .aside li:first > a span').html('-');
	
	$('#content .aside li > a').click(function(){
		if(!$(this).next('div').is('#content .aside li > a + div:visible')) {
			$('#content .aside li > a span').html('+');
			$(this).find('span').html('-');
			$('#content .aside li > a + div:visible').slideUp(500);
			$(this).next('div').slideDown(500);
		}
		return false;
	});
}

function helpBox(){
	$('.encart_conseil').mouseenter(function(){
		if( ($.browser.msie) && ($.browser.version <= 7 ) ) {
			$(this).find('div').show();
		} else {
			$(this).find('div').fadeIn(200);
		}
	});
	$('.encart_conseil').mouseleave(function(){
		if( ($.browser.msie) && ($.browser.version <= 7 ) ) {
			$(this).find('div').hide();
		} else {
			$(this).find('div').fadeOut(200);
		}
	});
}

function navigation() {
	$('.navigation ul li').mouseenter(function(){
		if(($(this).find('div.sous_menu').size() > 0)&&($('.menu_fixe').size() == 0)) {
			$(this).find('div.sous_menu').fadeIn(200);
		}
	});
	$('.navigation ul li').mouseleave(function(){
		if($('.menu_fixe').size() == 0) {
			$(this).find('div.sous_menu').fadeOut(200);
		}
	});
	
	$('.navigation > ul > li > a').click(function(){
		if($(this).closest('li').find('div.sous_menu').size() > 0) {
			if(!$(this).closest('li').is('.menu_fixe')) {
			
				$('div.sous_menu').fadeOut(200);
				$('.navigation > ul > li').removeClass('menu_fixe');
				$(this).closest('li').addClass('menu_fixe');
				$(this).closest('li').find('div.sous_menu').stop(true,true).fadeIn(200);
			}
			return false;
		}
		
	});
}

function slideshow() {
	$('.slideshow li').mouseenter(function(){
		$(this).css('border-color','#CCFF64')
	});
	$('.slideshow li').mouseleave(function(){
		$(this).css('border-color','#fff')
	});	
}

function pageListe(){
	
	jQuery.preLoadImages("/uploads/images/background/bg_erreur.png");
	$('.liste_produit').data('requete_en_cours',false);
	
	/* FONCTION DE TRI DES PRODUITS */
	if ($('#liste_tri').val() != "") {
		var divCritere = $('<div>').addClass('filtre_critere');
		var labelCritere = $('<label>').attr('for','filtre_critere').html('Filtrer par :').appendTo(divCritere)
		var filtreCritere = $('<select>').attr('id','filtre_critere').appendTo(divCritere);
		var voir_tout = $('<option>').attr('value','').html('Tous les produits').appendTo(filtreCritere);
		
		criteresTri = $('#liste_tri').val().split(',');
		for(i=0;i<criteresTri.length;i++){
			if(criteresTri[i] != ""){
				$('<option>').attr('value',criteresTri[i]).html(criteresTri[i]).appendTo(filtreCritere)
			}
		}
		divCritere.insertAfter($('h2'))
		
		$('#filtre_critere').change(function(){
			_this = $(this);
			if($('.visuel_filter span:contains("'+_this.val()+'")').size()  > 0 ) {
				if( ($.browser.msie) && ($.browser.version <= 7 ) ) {
					$('.liste_produit li:visible').hide();
					$('.visuel_filter span:contains("'+_this.val()+'")').closest('li').show();									
				} else {
					$('.liste_produit li:visible').fadeOut(200,function(){
						$('.visuel_filter span:contains("'+_this.val()+'")').closest('li').fadeIn(200);									
					});
				}
			} else {
				if( ($.browser.msie) && ($.browser.version <= 7 ) ) {
					$('.liste_produit li:visible').hide();
					$('.liste_produit li').show();
				} else {
					$('.liste_produit li:visible').fadeOut(200,function(){
						$('.liste_produit li').fadeIn(200);	
					});
				}
			}
		});
		
		$('.visuel_filter span').click(function(){
			$('#filtre_critere option[value="'+$(this).text()+'"]').attr('selected',true)
			$('#filtre_critere').trigger('change');	
		});
	}
	
	/* SEULEMENT DES NOMBRE POUR LE CHAMP INVITE */
	$('.item_devis input[id^=nb_]').onlyNumber();
	
	
	/* AJOUT AU PANIER VIA QUANTITE */
	$('.item_devis input').keyup(function(event){
		if( event.keyCode  == 13 ){
			$('.ajouter_au_devis').trigger('click')	
		}
	});
	/* FONCTION DE MISE AU PANIER */
	$('.ajouter_au_devis').click(function(){
		
		if( $('.liste_produit').data('requete_en_cours') ) {
			return false;
		}
		
		_this = $(this);
		
		var qtyArticleField = $(this).closest('li').find('.item_devis input[id^=nb_]');
		if (qtyArticleField.val() != "") {
			if( ($.browser.msie) && ($.browser.version <= 7 ) ) {
			} else {
				_this.css('opacity',0.5);
			}
			$('.liste_produit').data('requete_en_cours',true);
		
			var popup = $('<div>').addClass('popup');
			var layer = $('<div>').addClass('layer');
			$.ajax({
				url: "/index.php?page=div-panier",
				data: {
					title_article: $(this).closest('li').find('.item_title').html(),
					alias_article: qtyArticleField.attr('id').split('nb_invite_')[1],
					qty_article: qtyArticleField.val()
				},
				type: "POST",
				success: function(data){
					popup.html(data).css('top',$(document).scrollTop()+($(window).height()/2)).hide();
					layer.height($(document).height()).hide();

					if( ($.browser.msie) && ($.browser.version <= 7 ) ) {
						layer.show();
						popup.show();
						$('body').append(layer).append(popup);
					} else {
						layer.fadeIn(500);
						popup.fadeIn(500);
						$('body').css('overflow','hidden').append(layer).append(popup);
					}
					$('.retour_liste').click(function(){
						if( ($.browser.msie) && ($.browser.version <= 7 ) ) {
							$('.layer,.popup').hide();
							$(this).remove();
						} else {
							$('.layer,.popup').fadeOut(500,function(){
								$(this).remove();
								$('body').css('overflow','auto');
							});
						}
						return false;
					});
					$('.goto_devis').click(function(){
						document.location = "/demande-de-devis/devis-detaille.html";
						return false;
					});
					
					$('.liste_produit').data('requete_en_cours',false);
					if( ($.browser.msie) && ($.browser.version <= 7 ) ) {
					} else {
						_this.css('opacity',1);
					}
				}
			});
		} else {
			_erreur = $('<div>').addClass('erreur_quantite').html("Veuillez d'abord renseigner la quantit&eacute; souhait&eacute;e").hide();	
			_this.closest('li').find('.item_devis').prepend(_erreur);
			if( ($.browser.msie) && ($.browser.version <= 7 ) ) {
				$('.erreur_quantite').show()
				setTimeout(function(){$('.erreur_quantite').hide().remove();},3000);
			} else {
				$('.erreur_quantite').fadeIn(500,function(){
					$(this).delay(2000).fadeOut(500,function(){
						$(this).remove();
					});
				});
			}
			
			
		}
		
		return false;								  
	});
}

function devisPerso(){
	$('.effacer_form a').click(function(){
		$('.custom_form :text').val('');
		$('.custom_form :checkbox').attr('checked',false);
		return false;
	});
	$('a[rel=supprimer_article]').click(function(){
		
		_this = $(this);
		
		$.ajax({
			url: "/index.php?page=remove-article",
			data: {
				articleToRemove: $(this).closest('li').find('.quantite_devis input').attr('id').split('qty_')[1]
			},
			type: "POST",
			success: function(data){
				_this.closest('li').fadeOut(200,function(){
					$(this).remove();
				});
				
				_total = $('.total_devis strong:eq(1)');
				oldTotal = parseFloat(_total.html().split(' ')[0]).toFixed(2).toString().replace('.','');
				newTotal = parseFloat(_this.closest('li').find('.total_article_devis').html().split(' ')[0]).toFixed(2).toString().replace('.','');
				newTotal = parseInt(oldTotal)-parseInt(newTotal);
				if (newTotal <= 0) {
					document.location = "/demande-de-devis/devis-express.html";
					return false;
				}
				newTotal = newTotal.toString();
				newTotal = newTotal.substring(0,newTotal.length-2)+'.'+newTotal.substring(newTotal.length-2,newTotal.length);
				_total.html(newTotal+' &euro;* TTC');
				_this.closest('li').find('.total_article_devis').html(data+' &euro;* TTC');
			}
		});
		
		
		return false;
	});
	
	
	$('a[rel^=retrait_]').click(function(){
		
		var _this = $(this);
		var currentQty = $(this).closest('li').find('.quantite_devis input').val();
		
		if( parseInt(currentQty)-1 == 0 ) {
			$(this).closest('li').find('a[rel=supprimer_article]').trigger('click')
			return false;
		}
		
		$.ajax({
			url: "/index.php?page=update-article-quantite",
			data: {
				articleToUpdate: $(this).closest('li').find('.quantite_devis input').attr('id').split('qty_')[1],
				qtyToSet: -1,
				prixQty: $(this).closest('li').find('[id^=prix_]').val()
			},
			type: "POST",
			success: function(data){
				_total = $('.total_devis strong:eq(1)');
				oldTotal = parseFloat(_total.html().split(' ')[0]).toFixed(2).toString().replace('.','');
				newTotal = parseFloat(_this.closest('li').find('[id^=prix_]').val()).toFixed(2).toString().replace('.','');
				newTotal = parseInt(oldTotal)-parseInt(newTotal);
				newTotal = newTotal.toString();
				newTotal = newTotal.substring(0,newTotal.length-2)+'.'+newTotal.substring(newTotal.length-2,newTotal.length);
				_total.html(newTotal+' &euro;* TTC');
				
				currentQty = _this.closest('li').find('.quantite_devis input').val();
				
				_this.closest('li').find('.quantite_devis input').val(parseInt(currentQty)-1);
				_this.closest('li').find('.total_article_devis').html(data+' &euro;* TTC');
			}
		});
		
		return false;
	});
	$('a[rel^=ajout_]').click(function(){
		
		var _this = $(this);
		var currentQty = $(this).closest('li').find('.quantite_devis input').val();
		
		$.ajax({
			url: "/index.php?page=update-article-quantite",
			data: {
				articleToUpdate: $(this).closest('li').find('.quantite_devis input').attr('id').split('qty_')[1],
				qtyToSet: 1,
				prixQty: $(this).closest('li').find('[id^=prix_]').val()
			},
			type: "POST",
			success: function(data){
				_total = $('.total_devis strong:eq(1)');
				oldTotal = parseFloat(_total.html().split(' ')[0]).toFixed(2).toString().replace('.','');
				newTotal = parseFloat(_this.closest('li').find('[id^=prix_]').val()).toFixed(2).toString().replace('.','');
				newTotal = parseInt(oldTotal)+parseInt(newTotal);
				newTotal = newTotal.toString();
				newTotal = newTotal.substring(0,newTotal.length-2)+'.'+newTotal.substring(newTotal.length-2,newTotal.length);
				_total.html(newTotal+' &euro;* TTC');
				
				currentQty = _this.closest('li').find('.quantite_devis input').val();
				
				_this.closest('li').find('.quantite_devis input').val(parseInt(currentQty)+1);
				_this.closest('li').find('.total_article_devis').html(data+' &euro;* TTC');
			}
		});

		return false;
	});

	
}

(function($){
	$.fn.onlyNumber = function(callBack_fn){
		var defaults = {
			callBack:function(){}
		};
		var options = {
			callBack:callBack_fn
		};
		var opts = $.extend(defaults,options);
		
		function only_number(){
			$(this).keyup(function(event){
				newVal = "";
				listChar=$(this).val().split('');
				for(i=0; i<listChar.length; i++){
					currentChar = listChar[i].charCodeAt('0');
					if( (currentChar>=46) && (currentChar<=57) ){
						newVal += String.fromCharCode(currentChar);
					}
				}
				$(this).val(newVal);
				opts.callBack();
			 });
		}
		$(this).each(only_number);
		return $(this);
	};
})(jQuery);

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)







