/////////////////
// 1 VALEURS
/////////////////
var timeout; // Nécéssaire au delay de la livesearch

/////////////////
// 2 FONCTIONS
/////////////////

function myLiveSearch() {

	$("#mytagline").html("Un peu de patience, MyFédé <b>recherche</b> les articles…");
	$("#results").fadeOut();

	// Get selected query
	var query = $('#my_queryresults li.current').html();
	$('#my_queryresults li').remove();
	$('input.my_query').attr('value',query);
	
	// Explode / Remove non-ascii chars /  Glue ("key-word")
	
	query = query.replace("é","e");
	query = query.split(" ");
	query = query.join("-");
		
	$("#results").load("/tag/"+query+" .posts", function(response, status, xhr) {
	  
	  if (status == "error") {
		$("#mytagline").html("Désolé, <b>MyFédé ne trouve pas encore d'articles</b> pour ta section. Pour le moment …");
	  }
	  if (status == "success") {
	  	$("#results").fadeIn();
		$("#mytagline").html("Voilà! <b>MyFédé te propose les articles suivants</b> pour ta section.");
		getReady();
	  }
	});
	
	
}

function myLiveSearchLi() {

	$('#my_queryresults li').click(function() {
		$('#my_queryresults li.current').removeClass('current');
		$(this).addClass('current');
		myLiveSearch();
	});
}


////////////////////////////////
// 			RECHERCHE 		  //
////////////////////////////////

function searchKeyup() {

	var query =  $("#search-input").attr('value');	
	
		
		    window.clearTimeout(timeout);
		    timeout = window.setTimeout(function(){
	
				if(query.length>3) 	{
							
					$("#search-tagline").html("Un peu de patience, <b>recherche</b> en cours…");
					$("#search-results").fadeOut();
							
					
					// Explode / Remove non-ascii chars /  Glue ("key-word")			
					query = query.split(" ");
					query = query.join("+");
									
					$("#search-results").load("/?s="+query+" .posts", function(response, status, xhr) {
								  
						if (status == "error") {
							$("#search-tagline").html("Désolé, <b>aucun résultat</b> pour cette recherche.");
						}
						if (status == "success") {
						
							$("#search-tagline").html("<b>Voilà!</b> Les résultats suivants sont disponibles.");
							$("#search-results").fadeIn();

							getReady();
						}
								
					});
						
				}
				
				else {
					$("#search-tagline").html("Tape <b>4 caractères</b> ou plus pour lancer la recherche.");
				}
	
		    },1000);
}


////////////////////////////////
// 3 DOCUMENT READY - ONLY ONCE
////////////////////////////////

$(document).ready(function() {

	
	// 1 - SCROLL TO ANCHOR ANIMATION
	////////////////////////////////////////
	
	$('a[href*=#]').click(function() {
	    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
	    && location.hostname == this.hostname) {
	      var $target = $(this.hash);
	      $target = $target.length && $target
	      || $('[name=' + this.hash.slice(1) +']');
	      if ($target.length) {
	        var targetOffset = $target.offset().top;
	        $('html,body')
	        .animate({scrollTop: targetOffset}, 1000);
	       return false;
	      }
	    }
	  });
		

	// 2 - .LIST ARTICLE / MANSORY LAYOUT (ISOTOPE) ON .POSTS
	//////////////////////////////////////////
	$('.posts').isotope({ layoutMode : 'masonry' });

	// 3 - .LIST ARTICLE / SHOW EXTRACT ON HOVER
	//////////////////////////////////////////	
	$(".list article:not(.third)").hoverIntent(function () {
	        $(this).children(".extract").show("fade");        
	},function () {
	        $(this).children(".extract").hide("fade");        
	});
	
	// 4 - .LIST ARTICLE / SET HEADLINE HEIGHT
	//////////////////////////////////////////	
	$(".list article:not(.third) .headline").each(function() {
		var height = $(this).parent().innerHeight();
		$(this).css("height",height);
		$(this).parent().children(".extract").css("height",height);
	});
	
	// 5 - FANCYBOX
	//////////////////////////////////////////	
	
	var fb_opts = { 'overlayShow' : true, 'centerOnScroll' : true, 'showCloseButton' : true, 'showNavArrows' : false, 'titleShow' : true, 'titlePosition' : 'over', 'titleFromAlt' : true, 'transitionIn' : 'elastic', 'transitionOut' : 'elastic', 'opacity' : false,'scrolling' : 'no' };
	
	var fb_IMG_selector = 'a[href$=".jpg"]:not(.nofancybox),a[href$=".JPG"]:not(.nofancybox),a[href$=".gif"]:not(.nofancybox),a[href$=".GIF"]:not(.nofancybox),a[href$=".png"]:not(.nofancybox),a[href$=".PNG"]:not(.nofancybox)';
	
	$(fb_IMG_selector).addClass('fancybox');
	
    $("a.fancybox").fancybox(fb_opts);
	
	// 7 - POST ARTICLE IMG - APPEND A SHADOW
	//////////////////////////////////////////	
	$(".post article img").not(".slickr-flickr-gallery img").after('<div class="shadow"></div>');


	// 8 - MYFEDE
	//////////////////////////////////////////	

	$('.my_query').keyup(function(event) {
	
		var query =  $(this).attr('value');
		
		// ENTER
		if (event.keyCode == 13) { 	
			myLiveSearch();
		}
	
		// DOWN
		if (event.keyCode == 40) {
			$('#my_queryresults li.current')
				.removeClass('current')
				.next()
				.addClass('current');  
		}
	
		// UP
		else if (event.keyCode == 38) {
			$('#my_queryresults li.current')
				.removeClass('current')
				.prev()
				.addClass('current');  
		}
		
		// AUTRE
		// Autre touche, on refait la recherche
		else if (event.keyCode > 64) {
			$.ajax({
				url: '/my-livesearch.php?q='+query,
				 success: function(data) 
				  {	
				  	$('#my_queryresults').html(data);
				  	myLiveSearchLi();
				  }
			});
		}
		
	});
	
		
	// 9 - RECHERCHE
	//////////////////////////////////////////	

	$("#search-input, ").focus(function () {	
		var query =  $(this).attr('value');	
		if(query=='Tape ta recherche ici...') $(this).attr('value','');	

	});

	$("#search-input").focusout(function () {	
		var query =  $(this).attr('value');	
		if(query=='') $(this).attr('value','Tape ta recherche ici...');	

	});

	
	$("#search-input").keyup(function () {
		searchKeyup();
	});
	
	// 10 - TRI DE LISTES - CERCLES
	//////////////////////////////////////////	
	$("#qscercles").focus(function () {	
		var query =  $(this).attr('value');	
		if(query=='Nom du cercle...') $(this).attr('value','');	

	});

	$("#qscercles").focusout(function () {	
		var query =  $(this).attr('value');	
		if(query=='') $(this).attr('value','Nom du cercle...');	

	});
	
	$("input.quicksearch").quicksearch('.quicksearchsorting li');
	



////////////////////////////////
// 			MYFEDE			  //
////////////////////////////////




	// HOVER LINES
	//////////////
	
	$("#lines li:not(.prev)").hoverIntent(function () {
	        $(this).animate({'margin-left':'+=5px'},{easing:"easeInOutBack"});        
	},function () {
	        $(this).animate({'margin-left':'-=5px'},{easing:"easeInOutBack"});                
	});
	
	$("#lines li.prev").hoverIntent(function () {
	        $(this).animate({'margin-right':'+=5px'},{easing:"easeInOutBack"});        
	},function () {
	        $(this).animate({'margin-right':'-=5px'},{easing:"easeInOutBack"});                
	});

	// SMARTFIXED LOGO
	//////////////////
	
	  	$(window).scroll(function (event) {
		    
		   	var y = $(this).scrollTop();
		   		    
			if (y >= 120 ) {
				$('#logo').addClass('fixed');
			}	
	
			else {
				$('#logo').removeClass('fixed');
			}		
		});
	
     
	// HOVER LOGO
	//////////////
	
	$("#logo").hoverIntent(function () {
	        $("#logo:not(.fixed)").animate({"margin-top":"-100px"});        
	        $("#logo.fixed").animate({"margin-top":"+40px"});        
	},function () {
	        $(this).animate({"margin-top":"-315px"});        
	
	});     
	    
	     
	// HOVER MENU
	//////////////
	
	$("#menu li").hoverIntent(function () {
	        $(this).children("ul").slideDown();
	},function () {
	        $(this).children("ul").slideUp();
	
	});     


});

