
// Mettre à jour un élément du dom aprés un certain temps
function refreshElement(timeout, element, url) {
   jQuery(element).fadeTo(timeout, 1, function(){
       jQuery.ajax({
           type: "GET",
           url: url,
           dataType: "html",
           success: function(data){
               var div = "<div>"+data+"</div>";
               jQuery(div).prependTo(element).hide().fadeIn(1000);
           }
       }); 
   });
}

// Afficher le loading
function showLoading(element) {
    var loading= jQuery(element).children(loading);
    jQuery(element).html('<div class="loading">' + jQuery(loading).html() + '</div>');
    jQuery(loading).removeClass('hiddendiv');
}

// facebook share
function fbs_click() {
    u=location.href;t=document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}
function applyTabStyle(elem){
             var contentblocks = $(elem).closest('.content-tabs').siblings('.content-blocks-tabs');
            $(elem).closest('.content-tabs').find('a').removeClass('activeTab');
            $(elem).addClass('activeTab');
            $(contentblocks).find('div').removeClass('activeBlock');
            $(contentblocks).find($(elem).attr('href')).addClass('activeBlock');
}

function loadAjaxTab(elem){
     var url = $(elem).attr('customhref');
                var div = $(elem).attr('href');

                $(elem).addClass('loaded');
                $.ajax({
                    type: "GET",
                    url: url,
                    dataType: "html",
                    success: function(data){
                        $(div).html(data);
                    }
                });
}

// jQuery
jQuery(document).ready(function(){

 if($("#propose_blog_description").length) {
   $('#propose_blog_description').maxlength({
    events: [], // Array of events to be triggerd
    maxCharacters: 400, // Characters limit
    status: true, // True to show status indicator bewlow the element
    statusClass: "status", // The class on the status div
    statusText: "caractère(s) restants", // The status text
    showAlert: false, // True to show a regular alert message
    slider: true // True Use counter slider
  });
 }

    // Gestionnaire de tabulations
    // Chargement du contenu ajax du tab "mes articles"
    if($("#userFeedsTabLink").length) {
        loadAjaxTab($("#userFeedsTabLink"));
    }
  
    // Selectionner tout dans le formulaire de recherche
    jQuery('#search_input').live('click', function(){
        var value = jQuery(this).attr('value');
        if (value == "Rechercher sur bloginy") {
            jQuery(this).attr('value', '');
        }
    });
    
    // Supprimer un tag
    jQuery('.deleteTag').live('click', function(){
        jQuery(this).closest('.newtag').remove();
        return false;
    });

    // Gestion des tabulations
    jQuery('.content-tabs a').live('click', function(){
        if (!$(this).hasClass('activeTab')) {
            if ($(this).hasClass('ajax') && !$(this).hasClass('loaded')) {
               loadAjaxTab(this);
            }
            applyTabStyle(this);
        }
        return false;
    });
    
    // Gestion des liens en Ajax
    jQuery('.ajaxLink').live('click', function() {
      var confirmation = true;
      if ($(this).attr('confirmation'))
      {
        confirmation = confirm($(this).attr('confirmation'));
      }
      
      if (confirmation)
      {
        var url = $(this).attr('href');
        var update = $(this).attr('update');
        $.ajax({
          type: "GET",
          url: url,
          dataType: "html",
          success: function(data){
              $(update).html(data);
          }
        });
      }
      
      return false;
    });
    
});

