/**
 * Removes duplicates in the array 'a'
 * @author Johan Känngård, http://johankanngard.net/
 */
function unique(a) {
	tmp = new Array(0);
	for(i=0;i<a.length;i++){
		if(!contains(tmp, a[i])){
			tmp.length+=1;
			tmp[tmp.length-1]=a[i];
		}
	}
	return tmp;
}
 
/**
 * Returns true if 's' is contained in the array 'a'
 * @author Johan Känngård, http://johankanngard.net/
 */
function contains(a, e) {
	for(j=0;j<a.length;j++)if(a[j]==e)return true;
	return false;
}
 
 
 
function removeItems(array, item) {
	var i = 0;
	while (i < array.length) {
		if (array[i] == item) {
			array.splice(i, 1);
		} else {
			i++;
		}
	}
	return array;
}
 
 
$(document).ready(function () {
	
	
	
	
	// everything hinges on creating a string of class names, 
	// so i'll create a variable to hold that first
	
	var stringOfClassNames = '';
	
	// grab the class name of each list item to build that string
	$('.filterThis > li').each( function (i) {
		var thisClassString = $(this).attr('class');
		stringOfClassNames = stringOfClassNames +' '+ thisClassString
	});
	
	// now i have a space-delimited string of all class names stored
	// in the stringOfClassNames variable.	
	// Trim spaces from the ends of that string:
	stringOfClassNames = jQuery.trim(stringOfClassNames);
	
	// i can't really do anything with it until it's an array, so
	// convert that string to an array.
	var arrayClasses = stringOfClassNames.split(' ');
	
	
	// now for the isolating the filter that is common to all.
	// must do before extracting only the unique classes
	// one way to approach: count the number of times classes occur, and
	// if any occur the same number of times as how many list items i have,
	// assume that class is common to all list items, and remove it from
	// the filter list. duplicate class on same item = problem, but 
	// i'm not thinking about that right now.
	// i've also chosen sort the pre-unique'd array
	// instead of sorting the unique'd array.  i think i have to for the count.
	var arrayClasses = arrayClasses.sort();
	totalNumberOfItemsToFilter = $('.filterThis > li').length;
	
	
	// borrowed from http://stackoverflow.com/questions/348021/counting-results-in-an-array
	// for counting up items.  do i even need this?
	var result = new Object();
	for (var filterClass in arrayClasses) {
		if (result[arrayClasses[filterClass]] === undefined) {
			result[arrayClasses[filterClass]] = 1;
		} else {
			result[arrayClasses[filterClass]]++;
		}
	}
	var resultsToRemoveFromFilters = new Array();
	for (var item in result) {
		if (result[item] == totalNumberOfItemsToFilter) {
			resultsToRemoveFromFilters.push(item);
		}
	}
	
	
	
 
	// pull out only unique values from that array.  Otherwise
	// i'll end up with duplicate filter checkboxes.
	arrayUniqueClasses = (unique(arrayClasses));
 
 
	// and now remove classes that appear in every result from my 'unique
	// classes' array
	for (x in resultsToRemoveFromFilters) {
		arrayUniqueClasses = removeItems(arrayUniqueClasses,resultsToRemoveFromFilters[x]);
	}
	/*$('.filterThis').before('<p><strong>Classes excluded from filters because they are common to all elements:<\/strong> '+resultsToRemoveFromFilters+'<\/p>'); */
 
 
 
	// we only want to create filters if there are multiple classes. check
	// length of that array to see if it worth going forward.  I need at least
	// two, so my value has to be greater than 1.
	if (arrayUniqueClasses.length > 1) {
 
		// it must be worth it, because everything else is
		// within the true side of this if statement.	
		// so since we're going to have some filters, 
		// lets give them a place to live
/*		$('<ul class="filters"><\/ul>').insertBefore('.filterThis');
		
		 then build the filter checkboxes based on all the class names
		$.each(arrayUniqueClasses, function() {
			$('<li><input id="dynamicFilterInput" type="checkbox" checked="checked" value="'+this+'" id="filterID'+this+'" /><label for="filterID'+this+'">'+this+'<\/label><\/li>').appendTo('ul.filters');
		}); 
		
		// lets throw in the 'show all' checkbox for giggles
		$('<li><input type="checkbox" checked="checked" value="filterAll" id="filterIDall" /><label for="filterIDall">Show all<\/label><\/li>').appendTo('ul.filters');
 */
		// now lets give those filters something to do
		$('.filter input').click(function(){
											clickfilter($(this));
											});
		
		// Matthieu : J'ai transféré cette fonction dans custom.checkbox pour qu'elle se déclanche seulement 
		// Quand le span est ajouté.
		//
		//$('.filter span.checkbox').click(function(){
		//									clickfilter($(this).parent('li').children('input'));
		//									});
		 
		
	}
	
	prepareDayFilter();
});

  function clickfilter(myCheckbox) {
			var value= myCheckbox.val();
			
			if ((value == 'filterAll') && (myCheckbox.is(':checked'))) { 
			
				$('.filter input').attr('checked','checked'); 
				$('.filterThis li').addClass('ok_to_show');
				show_conferences();
				
				
			} else if((value == 'filterAll') && !(myCheckbox.is(':checked'))){
			
				$('.filter input').removeAttr('checked');
				$('.filterThis li').removeClass('ok_to_show');
				show_conferences();
			
  			} else { 
				
				stringValue = '.filterThis > li.'+value;
				stringValueOpposite = '.filterThis > li:not(.'+value+')';
				if (myCheckbox.is(':checked')) { //QUAND JE CLICK POUR COCHÉ UNE CATEGORIE SAUF LE ALL
					classesOfItemToShow = '';
					
						$(stringValue).addClass('ok_to_show');
						classesOfItemToShow = classesOfItemToShow + ' ' + myCheckbox.attr('class');
						
						show_conferences();
					
					// trim spaces, then turn to an array, then
					// exclude non-unique classes
					classesOfItemToShow = jQuery.trim(classesOfItemToShow);
					classesOfItemToShow = classesOfItemToShow.split(' ');
					classesOfItemToShow = (unique(classesOfItemToShow));
					if (classesOfItemToShow.length > 1) {
						$.each(classesOfItemToShow, function() {
							$('.filter input[value='+this+']').attr('checked','true');
						});	
					}
					if ($('#dynamicFilterInput').not(':checked').length == 0) {
						//$('#filterIDall').attr('checked','true');
					};
				} else { // QUAND JE CLICK
					
					$('.filter input#filterIDall').removeAttr('checked');
					Custom.clear();
					
					// all my new stuff goes here to tackle that 'webdev'
					// and 'resources' class issue.
					OtherClassesAssociatedWithTheItemToBeRemoved = '';
					Oca = OtherClassesAssociatedWithTheItemToBeRemoved;
					$(stringValue).each(function(i) {
						Oca = Oca + ' ' + myCheckbox.attr('class');
					});
					
					// trim spaces, then turn to an array, then
					// exclude non-unique classes
					Oca = jQuery.trim(Oca);
					Oca = Oca.split(' ');
					Oca = (unique(Oca));
					if (Oca.length > 1) {
						$.each(Oca, function() {
							classToCompare = this; 
							if (!($('.'+classToCompare).is(stringValueOpposite))) {
								// uncheck the checkbox that classToCompare represents
								$('.filter input[value='+classToCompare+']').removeAttr('checked');
							}
						});	
					}
					
					
					
					////////////////////////////////////////
					//
					// Pareil que plus haut,
					// Il faudrait créer une fonction pour remplace ce bout de code
					// et faire l'effet smooth identique à celui du day 1 day 2
					//
					////////////////////////////////////////
					
					$(stringValue).removeClass('ok_to_show');
					show_conferences();
				}
			}
		}
	
	function show_conferences(){
		
		if($('.conference_item_li.visible').length > 0){
			$('.conference_item_li.visible').fadeOut(400,function(){
					 $(this).removeClass('visible');
					 $('.conference_item_li.ok_to_show.ok_to_show_day_filter').fadeIn(400,function(){$(this).addClass('visible');}); //POURQUOI 2X-PARENT SI L'ARG EST LE conference_item_li
				});	
		}else{
			$('.conference_item_li.ok_to_show.ok_to_show_day_filter').fadeIn(400,function(){$(this).addClass('visible');}); //POURQUOI 2X-PARENT SI L'ARG EST LE conference_item_li
			}
		};
	
	function prepareDayFilter(){
		
		$('.conference_item').parent().parent().addClass('conference_item_li');
		$('.conference_item').parent().parent().addClass('ok_to_show');
		$('.conference_item').parent().parent().addClass('ok_to_show_day_filter');
		$('.conference_item').parent().parent().addClass('visible');
		$('#conference_list #session_menu .menu:eq(2)').addClass('current');							
		
		
		// day 1
		$('#conference_list #session_menu .menu:eq(0) a').click(function(){
			$('#conference_list #session_menu .menu').removeClass('current');		
			$(this).parent().addClass('current');
			dayFilter('.conference_item.day-01');
			return false;
			});
		
		// day 2
		$('#conference_list #session_menu .menu:eq(1) a').click(function(){
			$('#conference_list #session_menu .menu').removeClass('current');		
			$(this).parent().addClass('current');	
			dayFilter('.conference_item.day-02');
			return false;
			});
		
		// all
		$('#conference_list #session_menu .menu:eq(2) a').click(function(){
			$('#conference_list #session_menu .menu').removeClass('current');		
			$(this).parent().addClass('current');	
						
			dayFilter('.conference_item_li .conference_item');
			
			return false;
			});
		
		}
	
	function dayFilter(confItemClass){
		
		$('.conference_item_li').removeClass('ok_to_show_day_filter');
		$(confItemClass).parent().parent().addClass('ok_to_show_day_filter'); //POURQUOI 2X-PARENT SI L'ARG EST LE conference_item_li
		
		$('.conference_item_li.visible').fadeOut(400,function(){
				 $(this).removeClass('visible');
				 $(confItemClass).parent().parent('.ok_to_show').fadeIn(400,function(){$(this).addClass('visible');}); //POURQUOI 2X-PARENT SI L'ARG EST LE conference_item_li
			});
		
		} 
