// Custom sorting plugin
(function($) {
  $.fn.sorted = function(customOptions) {
    var options = {
      reversed: false,
      by: function(a) { return a.text(); }
    };
    $.extend(options, customOptions);
    $data = $(this);
    arr = $data.get();
    arr.sort(function(a, b) {
      var valA = options.by($(a));
      var valB = options.by($(b));
      if (options.reversed) {
        return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;				
      } else {		
        return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;	
      }
    });
    return $(arr);
  };
})(jQuery);
 
// DOMContentLoaded
$(function() {

  // bind radiobuttons in the form
  var $filterType = $('#filter input[name="type"]');
  var $filterSort = $('#filter input[name="sort"]');

  // get the first collection
  var $applications = $('#applications');

  // clone applications to get a second collection
  var $data = $applications.clone();
 
  // attempt to call Quicksand on every form change
  $filterType.add($filterSort).click(function(e) {

		var afficheClient = $(this).val();
		
	    if ($($filterType+':checked').val() == 'all' || afficheClient == "client") {
	      var $filteredData = $data.find('li');
	    } else {
	      var $filteredData = $data.find('li[data-type~=' + $($filterType+":checked").val() + ']');
	    }
	
	    // if sorted by size
	    if ($('#filter input[name="sort"]:checked').val() == "date" || afficheClient != "client") { /*	ajouté la 2e condition	*/
	    
	      var $sortedData = $filteredData.sorted({
	        by: function(v) {
	          return $(v).find('span[data-type=date]').text().toLowerCase();
	        }
	      });
	      
	    } else {
	    	
	      // if sorted by name
	      var $sortedData = $filteredData.sorted({
	        by: function(v) {
				return $(v).find('span[data-type=client]').text().toLowerCase();
	        }
	      });
	      
	    }   
	
		
		    // finally, call quicksand
		    $applications.quicksand($sortedData, {
				  duration: 800,
				  enhancement: function() {
				 $('.pf_item3 li img').animate({'opacity' : 1}).hover(function() {
					$(this).animate({'opacity' : 0});
				}, function() {
					$(this).animate({'opacity' : 1});
				});
				},
				  easing: 'easeInOutQuad'
		    }, function() {
			
				
				/*	Fonction CallBack ajoutée par Francis Robert (francis@frobert.com)	*/
				if ($("ul.clientList").length == 0) {
					jQuery("ul.pf_nav").after("<ul class=\"clientList\"></ul>");
				}
		
				$("div.listeclients,legend.isTitle,ul.clientList").animate({opacity:0}).slideUp(300, function () {
					$(this).remove();
				});
				
				if (afficheClient == "client") { 
					//	Afficher les données additionnelles relatives aux clients
					var tempString = "";
					var previousClient = "";
					var previousApplications = $('#applications li').html();
					var myCounter = 0;
					
					$('#applications li').each(function(i) {
					
						/*	Si changement de client, on ajoute le titre du client devant l'item	*/
						if ($(this).find('span[data-type=client]').text() != previousClient) {
							myCounter = myCounter + 1;
							previousClient = $(this).find('span[data-type=client]').text();
							tempString = tempString + "<li><a href='#c"+myCounter+"'>" + previousClient + "</a></li>\r";
							$(this).css('clear','both').addClass('hasTitle');
						}
						
					});
					
					//	Ajouter le nom des clients ici en sélectionnant les clear:both
					$("ul.pf_nav").after("<div class='listeclients' style='display:block;clear:both;'>Liste des clients :</div><ul class='clientList' style='display:none;'>"+tempString+"</ul>");
					
					//	Générer la liste des clients au haut des vignettes
					myCounter = 0;
					$(".hasTitle").each(function(i) {
						myCounter = myCounter + 1;
						var myTitle = $(this).find('span[data-type=client]').text();
						$(this).before("<legend id='c"+myCounter+"' class='isTitle' style='clear:both;'>"+myTitle+"</legend>");
					});
					
					$("div.listeclients,legend.isTitle,ul.clientList").css("opacity",0).slideDown().animate({opacity:1});
					
				}
		
				/*	Fin de la Fonction CallBack ajoutée par Francis Robert (info@frobert.com)	*/
				
			});
	
	  });
	  
	//triggerQuicksand('identite-visuelle-promotion');
	if ($($filterType+':checked').val() != 'all') {
		triggerQuicksand('all');
	}
	function triggerQuicksand(afficheClient) {
		
	    if ($($filterType+':checked').val() == 'all' || afficheClient == "client") {
	      var $filteredData = $data.find('li');
	    } else {
	      var $filteredData = $data.find('li[data-type~=' + $($filterType+":checked").val() + ']');
	    }
	
	    // if sorted by size
	    if ($('#filter input[name="sort"]:checked').val() == "date" || afficheClient != "client") { /*	ajouté la 2e condition	*/
	    
	      var $sortedData = $filteredData.sorted({
	        by: function(v) {
	          return $(v).find('span[data-type=date]').text().toLowerCase();
	        }
	      });
	      
	    } else {
	    	
	      // if sorted by name
	      var $sortedData = $filteredData.sorted({
	        by: function(v) {
				return $(v).find('span[data-type=client]').text().toLowerCase();
	        }
	      });
	      
	    }   
	
		
		    // finally, call quicksand
		    $applications.quicksand($sortedData, {
				  duration: 800,
				  enhancement: function() {
				 $('.pf_item3 li img').animate({'opacity' : 1}).hover(function() {
					$(this).animate({'opacity' : 0});
				}, function() {
					$(this).animate({'opacity' : 1});
				});
				},
				  easing: 'easeInOutQuad'
		    }, function() {
			
				
				/*	Fonction CallBack ajoutée par Francis Robert (francis@frobert.com)	*/
				if ($("ul.clientList").length == 0) {
					jQuery("ul.pf_nav").after("<ul class=\"clientList\"></ul>");
				}
		
				$("div.listeclients,legend.isTitle,ul.clientList").animate({opacity:0}).slideUp(300, function () {
					$(this).remove();
				});
				
				if (afficheClient == "client") { 
					//	Afficher les données additionnelles relatives aux clients
					var tempString = "";
					var previousClient = "";
					var previousApplications = $('#applications li').html();
					var myCounter = 0;
					
					$('#applications li').each(function(i) {
					
						/*	Si changement de client, on ajoute le titre du client devant l'item	*/
						if ($(this).find('span[data-type=client]').text() != previousClient) {
							myCounter = myCounter + 1;
							previousClient = $(this).find('span[data-type=client]').text();
							tempString = tempString + "<li><a href='#c"+myCounter+"'>" + previousClient + "</a></li>\r";
							$(this).css('clear','both').addClass('hasTitle');
						}
						
					});
					
					//	Ajouter le nom des clients ici en sélectionnant les clear:both
					$("ul.pf_nav").after("<div class='listeclients' style='display:block;clear:both;'>Liste des clients :</div><ul class='clientList' style='display:none;'>"+tempString+"</ul>");
					
					//	Générer la liste des clients au haut des vignettes
					myCounter = 0;
					$(".hasTitle").each(function(i) {
						myCounter = myCounter + 1;
						var myTitle = $(this).find('span[data-type=client]').text();
						$(this).before("<legend id='c"+myCounter+"' class='isTitle' style='clear:both;'>"+myTitle+"</legend>");
					});
					
					$("div.listeclients,legend.isTitle,ul.clientList").css("opacity",0).slideDown().animate({opacity:1});
					
				}
		
				/*	Fin de la Fonction CallBack ajoutée par Francis Robert (info@frobert.com)	*/
				
			});		
		
	}
	
	//
	// Ajuste les liens vers les categories du bon portfolio
	//	
	jQuery('.widget_fr_categories_ole a').each(function() {
		
		$(this).attr('href', $(this).attr('href').replace('category/portfolio/', 'portfolio-2/?' ).slice(0,-1) );	
			
	});

});



