// Difference entre les dates
function setDateDifference(psDiff)
{
  g_tabDiff = psDiff.split(',');
}

// Définit l'agenda en cours de visualisation
function setCurrentAgenda(pnAgendaID)
{
  g_nCurrentAgenda = pnAgendaID;
}

// Définit l'utilisateur connecté
function setCurrentUser(pnLoginPK)
{
  g_nCurrentUser = pnLoginPK;
}

// Définit le role de manager
function setManagerUser(pbManager)
{
  g_bManagerUser = pbManager;
}

// Fermeture
function dateChange(psDate, psDestID, psLanguage)
{
  var FORM_PREFIX = 'cldisp';
  var nYear, nMonth, nDay;
  
  if(psLanguage != "us")
  {
    var tabDate = psDate.split('/');
    nYear = tabDate[2];
    nMonth = tabDate[1] - 1;
    nDay = tabDate[0];
  }
  else
  {
    var tabDate = psDate.split('-');
    nYear = tabDate[0];
    nMonth = tabDate[1] - 1;
    nDay = tabDate[2];
  }
  
  var dBegin = new Date(nYear, nMonth, nDay);
  
  if(g_tabDiff != null)
  {
    var nYear = parseInt(dBegin.getFullYear()) + parseInt(g_tabDiff[0]);
    var nMonth = parseInt(dBegin.getMonth()) + parseInt(g_tabDiff[1]);
    var nDay = parseInt(dBegin.getDate()) + parseInt(g_tabDiff[2]);

    var dDiff = new Date(nYear, nMonth, nDay);
    $('#' + FORM_PREFIX + psDestID).datepicker("setDate", dDiff);
    eval("setData" + FORM_PREFIX + psDestID + "()");
  }
}

// Rafraichit l'agenda
var tUpdate;
function updateAgenda(psUrl, psBlocID)
{
  clearTimeout(tUpdate);
  
  tUpdate = setTimeout(
    function() {
      updateAgendaCallback(psUrl, psBlocID);
    }, 1000
  );
}

function updateAgendaCallback(psUrlUpdate, psBlocID)
{
  setDisplay('divwait', true);
  
  // Utilisateurs sélectionné
  nCpt = 0;
  sUserId = '';
  $('#agenda_users_selector input[type=checkbox]:checked').each(
    function (index) {
      if(nCpt > 0) sUserId += '-';
      sUserId += $(this).attr('name').substr(3);
      nCpt++;
    }
  );
  
  // Envoie de la requête et mise à jour de l'affichage
  $.post(psUrlUpdate, {agpadd: sUserId}, function(data) {
    setDisplay('divwait', false);
    $('#' + psBlocID).html(data);
    
    if($('.htmlmaker_block_tabs').html() != null)
      $('.htmlmaker_block_tabs').tabs('');
  });
}

// Connaitre le nombre de jour dans un mois
function getNbJour(pnMonth, pnYear)
{
  var date = new  Date();
  var listeNbrJours = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  var nbJours = listeNbrJours[pnMonth];
  if (pnMonth == 1 && pnYear % 4 == 0) {
    nbJours++;
  }
  
  return nbJours;
}

// Affichage de la bulle de survol
var tBubble;
function getAjaxBubble(pEvent, psURL)
{
  clearTimeout(tBubble);
  
  // On passe par une fonction anonyme pour lancer la bulle
  // Sauf pour IE qui n'accepte pas le passage de paramètre
  if(!checkIt('msie'))
  {
    tBubble = setTimeout(
      function() {
        AjaxBubble(pEvent, psURL);
      }, 250
    );
  }
  else
    tBubble = setTimeout(AjaxBubble(pEvent, psURL), 250);
}

// Fermeture de la bulle de survol
function closeAjaxBubble()
{
  clearTimeout(tBubble);
  setDisplay('divbubble', false);
}

// Affiche les utilisateurs correspondant à la population
function showUserByPopulation(paUserPK)
{  
  $('div[id^=user]').hide();
  for(nCpt = 0; nCpt < paUserPK.length; nCpt++)  
    $('#user' + paUserPK[nCpt]).show();  
}

function checkIt(string)
{
	place = navigator.userAgent.toLowerCase().indexOf(string) + 1;
	thestring = string;
	return place;
}

// Gestion des onglets
(function($)
{
  $.fn.tabs = function(psParent)
  {
    return this.each(function()
    {
      var oSelf = $(this);
      $(' > li', this)
          .click(
            function()
            {
            $('> li.htmlmaker_tab_selected', oSelf)
              .removeClass('htmlmaker_tab_selected')
              .each(function()
              {
                $('#tabc_' + $(this).attr('id')).hide();
                $(' > ul > li ', this).removeClass('htmlmaker_tab_selected')
                .each(function()
                {
                  $('#tabc_' + $(this).attr('id')).hide();
                });
              });

            $(this).addClass('htmlmaker_tab_selected');
            $('#tabc_' + $(this).attr('id')).show();

            $(' > ul > li:first-child', this).click();

            if(!$(this).attr('id').match("^tabagenda"))
              window.location.hash = '#' + psParent + $(this).attr('id');

            return false;
            }
          )
          .each(
            function()
            {
              $(' > ul', this).tabs($(this).attr('id') + '/');
            }
          );

        var sAncre = location.hash.substr(1);
      if (sAncre != '')
      {
        var asTabsSelected = sAncre.split('/');
        for (var nCount = 0; nCount < asTabsSelected.length; nCount++)
        {
          $('#' + asTabsSelected[nCount]).click();
        }
      }
    });
  }
})(jQuery);