/*
* Copyright (C) 2009 Joel Sutherland.
* Liscenced under the MIT liscense
*/

(function($) {
	$.fn.filterable = function(settings) {
		settings = $.extend({
			useHash: true,
			animationSpeed: 500,
			show: { width: 'show', opacity: 'show' },
			hide: { width: 'hide', opacity: 'hide' },
			useTags: true,
			tagSelector: '#filtrage_liste_test option',
			selectedTagClass: 'current',
			allTag: 'all'
		}, settings);
		
		return $(this).each(function(){
									// passe pas alert(settings['useHash']);
		
			/* FILTER: select a tag and filter */
			$(this).bind("filter", function( e, tagToShow ){
				if(settings.useTags){
					$(settings.tagSelector).removeClass(settings.selectedTagClass);
					$(settings.tagSelector + '[href=' + tagToShow + ']').addClass(settings.selectedTagClass);
					//alert('passe 1'+tagToShow);
				}
				$(this).trigger("filterportfolio", [ tagToShow.substr(1) ]);
				//alert('passe 1'+ tagToShow);
			});
			
		
			/* FILTERPORTFOLIO: pass in a class to show, all others will be hidden */
			$(this).bind("filterportfolio", function( e, classToShow ){
				if(classToShow == settings.allTag){
					$(this).trigger("show");
				}else{
					$(this).trigger("show", [ '.' + classToShow ] );
					$(this).trigger("hide", [ ':not(.' + classToShow + ')' ] );
				}
				if(settings.useHash){
					location.hash = '#' + classToShow;
				}
				//alert('passe 2'+ classToShow);
			});
			
			
			/* SHOW: show a single class*/
			$(this).bind("show", function( e, selectorToShow ){
				$(this).children(selectorToShow).animate(settings.show, settings.animationSpeed);
			});
			
			/* SHOW: hide a single class*/
			$(this).bind("hide", function( e, selectorToHide ){
				$(this).children(selectorToHide).animate(settings.hide, settings.animationSpeed);	
			});
			
			/* ============ Check URL Hash ====================*/
			if(settings.useHash){
				if(location.hash != '')
					$(this).trigger("filter", [ location.hash ]);
				else
					$(this).trigger("filter", [ '#' + settings.allTag ]);
					//alert('okf');
					
				//	alert ('check'+location.hash+this);
			}
			
			/* ============ Setup Tags ====================*/
			if(settings.useTags){
				$("select[name='filtrage_liste_test']").change(function(){
				//$(settings.tagSelector).change(function(){
				//$(settings.tagSelector).click(function(){
														 
														 
														// $("select[name='filtrage_liste_test'] option:selected").val()
					//$('#ma_liste').trigger("filter", [ $(this).attr('href') ]);
					
						// if ($("#ma_liste").is(":hidden")) {
							// $('#cal').trigger("filter", [ $(this).attr('href') ]);
		//$('#cal').trigger("filter", [ $("select[name='filtrage_liste_test'] option:selected").val() ]);
		//alert('hidden');
	// }else{
		// $('#ma_liste').trigger("filter", [ $(this).attr('href') ]);
		
		// ici ca passe dans les 2 cas
		//alert(settings.useTags);
		
					$('#calendrier').trigger("filter", [ $("select[name='filtrage_liste_test'] option:selected").val() ]);
					$('#ma_liste').trigger("filter", [ $("select[name='filtrage_liste_test'] option:selected").val() ]);
					//alert('pashidden');
//	 }
					//alert('ok'+[ $(this).attr('href') ]);
					
					//$('#testr').trigger("filter", [ $(this).attr('href') ]);
					
					$(settings.tagSelector).removeClass('current');
					$(this).addClass('current');
					//alert('ok2'+[ $(this).attr('href') ]);
				});
			}
		});
	}
})(jQuery);


$(document).ready(function(){
	$('#ma_liste').filterable();
	$('#calendrier').filterable();

	/*$('#testr').filterable();*/

});
