var url_base = "/reco/www/index.php/global/global/";
var url_search_user = url_base + "searchUser";

// Test de la case "Afficher les utilisateurs en attente"
function waiting() {
	if($("#message input").is(":checked") == true)
    	var attente = 1;
    else
    	var attente = 0;
    	
    return attente;
}

// Affichage des "box" / utilisateurs
function user_box() {
	$(".user-content").corner("round tl bottom 10px");
	$(".user-header-award").corner("round br 10px");
	$(".user-header-pseudo").corner("round tl 10px");
	$(".user-header-randp").corner("round tr 10px");
	$(".user-wrap:last").css("padding-bottom", "0px");
	
	$("a.link-recos").data("flag", 1);
	
	// Gestion de l'affichage des recos
	$("a.link-recos").click(function () {
    	if($(this).data("flag") == 1) {
    		$($(this).attr("user")).load($(this).attr("url"), function() {
	    		$(".list-recos span[title]").each(function() {
    		  		tip($(this));
	    		});
    		});
    		$(this).removeData("flag");
		 }
    });
    
    $("a.link-recos").toggle(
    	function(){    	
    		$($(this).attr("user")).show();
    		$($(this)).html("Recommandations -") } , 
    	function(){ 
    	   	$($(this).attr("user")).hide();
    		$($(this)).html("Recommandations +") } 
    );
}

// Affichage de l'info-bulle dans les recommandations
function tip(obj) {
	if(obj.attr("type")=='motif')
		var color = '#800000';
	else
		var color = '#4D86BB';
		
	obj.qtip({
  	  content: { text: false },
  	  style: { 
      width: 220,
      padding: 2,
      background: color,
      color: '#FFF',
      border: {
      	width: 2,
      	color: '#000'
      },
      fontSize: '9pt',
      fontWeight: 'bold',
      textAlign: 'center',
 	  }
   });
}

$(document).ready(function() {
  $("div.content").hide();
  $("a.tab:first").addClass("actif");
  $("div.content:first").show();
  $("#header").corner("round bottom 10px");
  $("#footer").corner("round top 10px");
  
  // Gestion des onglets
  $("a.tab").click(function () {
    $("a.tab").removeClass("actif");
    $("a.letter").removeClass("actif");
    $(this).addClass("actif");
    $("div.content").hide();
    
    $($(this).attr("href")).show();
	
    return false;
  });
  
  // Gestion des lettres de l'alphabet
  $("a.letter").click(function () {
  	$("a.tab").removeClass("actif");
    $("a.letter").removeClass("actif");
    $(this).addClass("actif");
    $("div.content").hide();
    $("div#content-list").empty().show();
	
    // Chargement de la liste des utilisateurs
	$("div#content-list").load( $(this).attr("url") + "&attente=" + waiting() , function(){
		user_box();
	});
	
	return false;
  });
  
  $("#search-input").keyup(function()  {
  	var search = $(this).val();
  	if(search.length > 0) {
  		$("#autocomplete").load($(this).attr("url") + "?search=" + search + "&attente=" + waiting());
		$("#autocomplete").show();
  	}
  	else {
  		$("#autocomplete").empty().hide();
  	}
  });
  
  $("#ul-ac li").live("mouseover", function() {
			if($(this).children().next().html()!="..." && $(this).children().children().html()!="Fermer")
				$(this).addClass("li-over");
  });

  $("#ul-ac li").live("mouseout", function() {
		if($(this).children().next().html()!="..." && $(this).children().children().html()!="Fermer")
			$(this).removeClass("li-over");
  });
  
  $("#ul-ac li").live("click",
	function() {
		if($(this).children().next().html()!="..." && $(this).children().children().html()!="Fermer") {
			$("div.content").hide();
			$("div#content-list").empty().show();
			$("div#content-list").load( $("#ul-ac").attr("url") + "?pseudo=" + $(this).children().next().html() , function(){
				$("#autocomplete").empty().hide();
			 	$("#search-input").val("");
				user_box();				
			});
			
			return false;
		}
		if($(this).children().children().html()=="Fermer") {
			$("#autocomplete").empty().hide();
		 	$("#search-input").val("");
		}
  });
  
  $("#search-user").live("click", function() {
  	$(this).qtip("destroy");
  	$("div.content").hide();
    $("div#content-list").empty().show();
  	$("div#content-list").load( url_search_user + "?pseudo=" + $(this).html() , function(){
		user_box();
	});
  });
  
  $(".topic-ref").attr("target", "_blank");
});
