$(document).ready(function()
{
  //toogle faq
  $(".faq .toggle_container, .toogle_condition .toggle_container").hide();
  $(".faq .trigger, .toogle_condition .title").toggle(function(){
    $(this).addClass("active");
    }, function () {
      $(this).removeClass("active");
    });
    $(".faq .trigger, .toogle_condition .title").click(function(){
      $(this).next(".faq .toggle_container, .toogle_condition .toggle_container").slideToggle("slow");
    });

  //toogle secteurs
  $(".secteur ul").filter(function() { if($(this).find('.familleactive').size() > 0) return true; return false; }).show();
  $(".secteur span").click(function()
  {
    $(".secteur span").not($(this)).removeClass('secteuractif');
    $(this).toggleClass('secteuractif');
    $('.secteur ul').not($(this).parent().find("ul")).slideUp('slow');
    $(this).parent().find("ul").slideToggle("slow");
  });
  
  // Fournisseurs
	var $secteurs = $('#content_secteurs');
	if ($secteurs.length > 0) {
  		$secteurs
			.find('.fournisseur')
			.tipsy({gravity: $.fn.tipsy.autoNS})
			.each(function() {
				var $this = $(this);
				$this.fancybox({
					autoDimensions: false,
					titleShow: false,
					width: 990,
					padding: 0,
					margin: 0,
					height: '100%',
					href: '#conditions_commerciales_'+$this.data('id'),
					showCloseButton: false,
					onComplete: function() {
						$thisCC = $('#conditions_commerciales_'+$this.data('id'));
						$thisCC.find('span').find('a').each(function() {
							var $thisA = $(this);
							$thisA.css('top', ( 50 - $thisA.height() ) / 2 + 'px');							
						});
						/*var maxHeight = Math.max($thisCC.find('.conditions_commerciales_left').height(), $thisCC.find('.conditions_commerciales_middle').height(),$thisCC.find('.conditions_commerciales_right').height()-71);
						$thisCC.find('.conditions_commerciales_left').height(maxHeight);
						$thisCC.find('.conditions_commerciales_middle').height(maxHeight);
						$thisCC.find('.conditions_commerciales_right').height(maxHeight+71);*/
					}
				});
			});
		$('.conditions_commerciales_header').find('.close').click(function() {
			$.fancybox.close();
		});
		
		setTimeout(function() {
			$('.fournisseur').find('img').each(function() {
				var $this = $(this);
				$this.css({
					top: ( 85 - $this.height() ) / 2 + 'px',
					opacity: 1,
					filter: 'alpha(opacity=100)'
				});
			});
		}, 1000)
	}
	
	// Formulaire de demande d'avis
	var $bottomContact = $('#bottom_contact'),
	    $btTop = $('#bt_top')
	$btTop.click(function(e) {
    e.preventDefault();
    if (!$btTop.data('hasAnswer'))
      $bottomContact.slideToggle();
	});
	
	if (typeof easytoog_admin !== 'undefined') {
	  $bottomContact.show();
	} else {
	  $bottomContact.hide();
	}
	
	$bottomContact.find('form').submit(function(e) {
	  e.preventDefault();
    if (typeof easytoog_admin !== 'undefined') {
      alert('Seulement en mode publié.');
    } else {
      var $this = $(this),
          $textearea = $this.find('textarea');
      if (!$textearea.val()) {
        alert('Veuillez entrer votre question dans le formulaire.');
      } else {
        $.post($this.attr('action'), { message: $textearea.val() }, function() {
          alert('Votre message a bien été envoyé.')
          $btTop.data('hasAnswer', true);
          $bottomContact.slideUp();
        })
      }
    }
	});
	
	
  
	// Filtres
	var $filtres = $('#rightbar_secteurs').find('.content');
	if ($filtres.length > 0) {
		var top = $filtres.offset().top - parseFloat($filtres.css('margin-top').replace(/auto/, 0));
		$(window).scroll(function(event){
			var y = $(this).scrollTop() + 25;
			if (y >= top) {
				$filtres.addClass('fixed');
			}
			else {
				$filtres.removeClass('fixed');
			}
		});
	}
	
	$('#filter_famille, #filter_marche').change(function(e) {
  		var $target		= $(this),
			familleId	= $('#filter_famille').val(),
  			marcheId	= $('#filter_marche').val();
		if (familleId == 'none' && marcheId == 'none') {
			$secteurs.find('.fournisseur').show();
		} else {
			$secteurs.find('.fournisseur').each(function() {
				var $this		= $(this),
					familles	= $this.data('famille').split(':'),
					marche		= $this.data('marche'),
					hasFamille	= false,
					hasMarche	= false;
					
				if (marche == marcheId || marcheId == 'none')
					hasMarche = true;
				if (hasMarche && familleId != 'none') {
					$.each(familles, function(i, val) {
						if (familleId == val) {
							hasFamille = true;
							return false;
						}
					});
				} else if (hasMarche && familleId == 'none') {
					hasFamille = true;
				}
				if (hasFamille) {
					$this.show();
				} else {
					$this.hide();
				}
			});
		}
		
		// on cache les secteurs vide
		$secteurs.find('ul').each(function() {
			var $this = $(this);
			if ($this.height() < 50) {
				$this.hide();					
			} else {
				$this.show();
			}
		});
		
		// Si aucun secteur
		if ($('#list_secteurs').height() == 0) {
			var type	= $target.attr('id').split('filter_')[1];
			alert('Il n\'y a aucun fournisseur pour la famille "'+$('#filter_famille').find('option[value="'+familleId+'"]').text()+'" et pour le marché "'+$('#filter_marche').find('option[value="'+marcheId+'"]').text()+'". Annulation du dernier filtre.');
			if (type == 'famille') {
				$('#filter_famille')
					.val('none')
					.change();
			} else {
				$('#filter_marche').
					val('none')
					.change();
			}
		}
	});
	
});
