var g_oDateStart = null; // Date de début de sélection
var g_oDateEnd = null;   // Date de fin de sélection
var g_tabDiff = null;    // Durée de la sélection

var g_nCurrentAgenda;    // ID de l'agenda en cours de visualisation
var g_nCurrentUser;      // Utilisateur en cours
var g_bManagerUser;      // Droit de création sur un autre agenda

/**
 * Agenda à la semaine
 */
function cWeekAgenda(pnID, psAddURL, pbCanEdit, pnTimeStart, pnTimeEnd, pnHourStart, pnHourEnd, pbDisplayWeekend)
{
  this.m_nID = pnID;
  this.m_sAddURL = psAddURL;
  this.m_bCanEdit = pbCanEdit;
  this.m_bDisplayWeek = pbDisplayWeekend;
  
  // Disable text select
  if ($.browser.mozilla) $("#agenda_block" + this.m_nID).css({'MozUserSelect' : 'none'});
  if ($.browser.msie) $("#agenda_block" + this.m_nID).bind('selectstart.disableTextSelect', function() { return false; });
  if (!$.browser.msie && !$.browser.mozilla)$("#agenda_block" + this.m_nID).bind('mousedown.disableTextSelect', function() { return false; });
  
  // Table contenant les cellules de l'agenda
  oCells = $(".agenda_cell");

  // Effet de survol sur les cellules
  oCells.hover(function() {$(this).addClass("agenda_cell_over");}, function() {$(this).removeClass("agenda_cell_over");});
  
  this.m_aoWeekEvents = new Array();
  
  // Constantes: taille des cellules
  cWeekAgenda.CONST_CELL_DURATION = 30;
  cWeekAgenda.CONST_CELL_HEIGHT = 35;
  cWeekAgenda.CONST_CELL_HEADER_HEIGHT = 25;
  cWeekAgenda.CONST_HOUR_START = pnHourStart;
  cWeekAgenda.CONST_HOUR_END = pnHourEnd + 1;
  cWeekAgenda.CONST_COLUMN_WIDTH = 13;
  cWeekAgenda.CONST_NBDAY = 7;
  
  if(!this.m_bDisplayWeek)
  {
	// Affichage semaine de 5 jours
    cWeekAgenda.CONST_NBDAY = 5;
    cWeekAgenda.CONST_COLUMN_WIDTH = 18;
  }
  
  // Heure de début et de fin de l'agenda
  cWeekAgenda.m_nTimeStart = pnTimeStart;
  cWeekAgenda.m_nTimeEnd = pnTimeEnd;

  // Si on peut modifier l'agenda on ajoute les écouteur d'evenement
  if (pbCanEdit)
  {
    // on change le type de pointeur
    oCells.css('pointer', 'cursor');

    // on commence la selection sur le mousedown
    oCells.mousedown(function()
    {
      if(!g_bManagerUser && g_nCurrentAgenda != g_nCurrentUser && g_nCurrentAgenda != 0)
        return;
      
      var nTime = this.id.split('-')[1];
      
      $("table.agenda_weekagenda td.agenda_cell_selected").removeClass("agenda_cell_selected");
      g_oDateStart = new Date();
      g_oDateStart.setTime(parseInt(nTime) * 1000);
      $(this).addClass("agenda_cell_selected");

      $(document).mouseup(function()
      {
        g_oDateStart = null;
        g_oDateEnd = null;
        $("table.agenda_weekagenda td.agenda_cell_selected").removeClass("agenda_cell_selected");
      });
    });

    // Gestion du survol des cellules
    oCells.mouseover( function()
    {
      if (g_oDateStart != null)
      {
        $("#" + g_nCurrentAgenda + "-" + g_oDateStart.getTime() / 1000).addClass("agenda_cell_selected");
        
        var nTime = this.id.split('-')[1];
        
        var oDateEnd =  new Date();
        oDateEnd.setTime(parseInt(nTime) * 1000);
        
        if (oDateEnd.getTime() > g_oDateStart.getTime() || (oDateEnd.getDate() == g_oDateStart.getDate() && oDateEnd.getHours() * 60 + oDateEnd.getMinutes() >= g_oDateStart.getHours() * 60 + g_oDateStart.getMinutes()))
        {
          g_oDateEnd = oDateEnd;
          
          // Met à jour l'affichage
          $(".agenda_cell_selected").removeClass("agenda_cell_selected");
          
          oDateCurrent = g_oDateStart;
          while(oDateCurrent.getTime() <= g_oDateEnd.getTime())
          {
            switch(g_nCurrentAgenda)
            {
              case 0:
                for(nCpt = 0; nCpt < cWeekMultiAgenda.CONST_USERS_NB + 1; nCpt++)
                  if($("#" + g_nCurrentAgenda + nCpt + "-" + oDateCurrent.getTime() / 1000) != null)
                    $("#" + g_nCurrentAgenda + nCpt + "-" + oDateCurrent.getTime() / 1000).addClass("agenda_cell_selected");
                  
                  oDateCurrent = new Date(oDateCurrent.valueOf() + (cWeekMultiAgenda.CONST_CELL_DURATION * 60 * 2 * 1000));
                break;
              
              default:
                if($("#" + g_nCurrentAgenda + "-" + oDateCurrent.getTime() / 1000) != null)
                  $("#" + g_nCurrentAgenda + "-" + oDateCurrent.getTime() / 1000).addClass("agenda_cell_selected");
                
                oDateCurrent = new Date(oDateCurrent.valueOf() + (cWeekAgenda.CONST_CELL_DURATION * 60 * 1000));
                break;
            }
            
            
          }
          
          g_oDateEnd = oDateCurrent;
        }
      }
    });

    // Fin de sélection
  	oCells.mouseup(function()
    {
      if (g_oDateStart == null) return ;
      if (g_oDateEnd == null)
      {
        g_oDateEnd = new Date(g_oDateStart);
        g_oDateEnd.setMinutes(g_oDateEnd.getMinutes() + 30);
      }

      var sURL = psAddURL.replace("datestart", g_oDateStart.getTime() / 1000);
      sURL = sURL.replace("dateend", g_oDateEnd.getTime() / 1000)
      sURL = sURL.replace("loginpk", g_nCurrentAgenda);

      ExecuteAction(sURL, 600, $(window).height() - 20);
      $('#divaction').show();

      g_oDateStart = null;
      g_oDateEnd = null;
    });
  }
}

cWeekAgenda.prototype.Draw = function(refresh)
{
  // div contenant l'agenda
  oRoot = $("#agenda_block" + this.m_nID);

  // On déssine les séparateurs horizontaux
  for (nHour = cWeekAgenda.CONST_HOUR_START; nHour < cWeekAgenda.CONST_HOUR_END; nHour++)
  {
    for (nMinute = 0; nMinute < cWeekAgenda.CONST_CELL_HEIGHT * 2; nMinute += cWeekAgenda.CONST_CELL_HEIGHT)
    {
      oDiv = $(document.createElement("div"));

      oDiv.css("top", ((nHour - cWeekAgenda.CONST_HOUR_START) * 2 * cWeekAgenda.CONST_CELL_HEIGHT + cWeekAgenda.CONST_CELL_HEADER_HEIGHT + nMinute) + "px");
      if (nMinute != 0)
      {
        oDiv.addClass("agenda_hsep_halfhour");
        oDiv.css("right", "0px");
        oDiv.css("width", (cWeekAgenda.CONST_COLUMN_WIDTH * cWeekAgenda.CONST_NBDAY) + "%");
      }
      else
        oDiv.addClass("agenda_hsep_hour");

      oRoot.append(oDiv);
    }
  }

  // On déssine les séparateurs verticaux
  for (nCurrentDay = 1; nCurrentDay <= cWeekAgenda.CONST_NBDAY; nCurrentDay++)
  {
    oDiv = $(document.createElement("div"));
    oDiv.addClass("agenda_vsep");
    oDiv.css("right", (cWeekAgenda.CONST_COLUMN_WIDTH * nCurrentDay) + "%");
    oDiv.css("top", cWeekAgenda.CONST_CELL_HEADER_HEIGHT);
    oDiv.css("height", ((cWeekAgenda.CONST_HOUR_END - cWeekAgenda.CONST_HOUR_START) * 2 * cWeekAgenda.CONST_CELL_HEIGHT) + "px");
    oRoot.append(oDiv);
  }

  // à la création: on calcule les collisions de chaque évènements avec ses suivants
  if(!refresh)
  {
    for (nEventA=0; nEventA<this.m_aoWeekEvents.length; nEventA++)
    {
      for (nEventB = nEventA+1; nEventB < this.m_aoWeekEvents.length; nEventB++)
      {
        // Chaque évènements contrôle un conflit potentiel avec son prédecesseur
        // Chaque conflit détecté augment la position du 2ème
        this.m_aoWeekEvents[nEventA].ControlConflictDate(this.m_aoWeekEvents[nEventB]);
      }
    }
  }

  // on dessine les evenements
  jQuery.each(this.m_aoWeekEvents, function() { this.Draw(); });

  // On dessine les marqueurs pour la date en cour
  this.DrawCurrentDate();
}

cWeekAgenda.prototype.DrawCurrentDate = function()
{
  oCurrentDate = new Date();

  if (oCurrentDate.getTime() > cWeekAgenda.m_nTimeStart && oCurrentDate.getTime() < cWeekAgenda.m_nTimeEnd && oCurrentDate.getHours() >= cWeekAgenda.CONST_HOUR_START && oCurrentDate.getHours() < cWeekAgenda.CONST_HOUR_END)
  {
    oDiv = $(document.createElement("div"));

    oDiv.css("top", ((oCurrentDate.getHours() - cWeekAgenda.CONST_HOUR_START) * (2 * cWeekAgenda.CONST_CELL_HEIGHT) + (oCurrentDate.getMinutes() / cWeekAgenda.CONST_CELL_DURATION) * cWeekAgenda.CONST_CELL_HEIGHT + cWeekAgenda.CONST_CELL_HEADER_HEIGHT) + "px");
    oDiv.css("right", (Math.abs(oCurrentDate.getDay() - cWeekAgenda.CONST_NBDAY) * cWeekAgenda.CONST_COLUMN_WIDTH) + "%");
    oDiv.css("width", cWeekAgenda.CONST_COLUMN_WIDTH + "%");
    oDiv.css("position", "absolute");
    oDiv.css("z-index", "5");
    oDiv.addClass("agenda_currentdate");

    $("#agenda_block" + this.m_nID).append(oDiv);

    oDiv = $(document.createElement("div"));
    oCurrentDate = new Date();
    oDiv.css("top", ((oCurrentDate.getHours() - cWeekAgenda.CONST_HOUR_START) * (2 * cWeekAgenda.CONST_CELL_HEIGHT) + (oCurrentDate.getMinutes() / cWeekAgenda.CONST_CELL_DURATION) * cWeekAgenda.CONST_CELL_HEIGHT + cWeekAgenda.CONST_CELL_HEADER_HEIGHT - 3) + "px");
    oDiv.css("left", "0");
    oDiv.css("position", "absolute");
    oDiv.css("z-index", "5");
    oDiv.addClass("agenda_currentdate_arrow");

    $("#agenda_block" + this.m_nID).append(oDiv);
  }
}

cWeekAgenda.prototype.AddEvent = function(poEvent)
{
  this.m_aoWeekEvents.push(poEvent);
}

/**
 * Agenda multi-collaborateur à la semaine
 */
function cWeekMultiAgenda(pnID, psAddURL, pbCanEdit, pnTimeStart, pnTimeEnd, pnHourStart, pnHourEnd, pnUsers, pbDisplayWeekend)
{
  this.m_nID = pnID;
  this.m_sAddURL = psAddURL;
  this.m_bCanEdit = pbCanEdit;
  this.m_bDisplayWeek = pbDisplayWeekend;
  
  // Disable text select
  if ($.browser.mozilla) $("#agenda_block" + this.m_nID).css({'MozUserSelect' : 'none'});
  if ($.browser.msie) $("#agenda_block" + this.m_nID).bind('selectstart.disableTextSelect', function() { return false; });
  if (!$.browser.msie && !$.browser.mozilla)$("#agenda_block" + this.m_nID).bind('mousedown.disableTextSelect', function() { return false; });
  
  // Table contenant les cellules de l'agenda
  oCells = $(".agenda_cell");

  // Effet de survol sur les cellules
  oCells.hover(function() {$(this).addClass("agenda_cell_over");}, function() {$(this).removeClass("agenda_cell_over");});
  
  this.m_aoWeekEvents = new Array();
  
  // Constantes
  cWeekMultiAgenda.CONST_CELL_DURATION = 30;
  cWeekMultiAgenda.CONST_CELL_HEIGHT = 35;
  cWeekMultiAgenda.CONST_CELL_HEADER_HEIGHT = 25;
  cWeekMultiAgenda.CONST_CELL_DAY_SEPARATOR = 10;
  cWeekMultiAgenda.CONST_HOUR_START = pnHourStart;
  cWeekMultiAgenda.CONST_HOUR_END = pnHourEnd + 1;
  cWeekMultiAgenda.CONST_COLUMN_WIDTH = (100 / (pnHourEnd - pnHourStart + 1));
  cWeekMultiAgenda.CONST_USERS_NB = pnUsers;
  cWeekMultiAgenda.CONST_NBDAY = 7;
  if (!this.m_bDisplayWeek)
      cWeekMultiAgenda.CONST_NBDAY = 5;
  
  cWeekMultiAgenda.m_nTimeStart = pnTimeStart;
  cWeekMultiAgenda.m_nTimeEnd = pnTimeEnd;
  
  // Si on peut modifier l'agenda on ajoute les écouteur d'evenement
  if (pbCanEdit)
  {
    // on change le type de pointeur
    oCells.css('pointer', 'cursor');

    // on commence la selection sur le mousedown
    oCells.mousedown(function()
    {
      if(!g_bManagerUser && g_nCurrentAgenda != g_nCurrentUser && g_nCurrentAgenda != 0)
        return;
      
      var nTime = this.id.split('-')[1];
      
      $("table.agenda_weekagenda td.agenda_cell_selected").removeClass("agenda_cell_selected");
      g_oDateStart = new Date();
      g_oDateStart.setTime(parseInt(nTime) * 1000);
      $(this).addClass("agenda_cell_selected");

      $(document).mouseup(function()
      {
        g_oDateStart = null;
        g_oDateEnd = null;
        $("table.agenda_weekagenda td.agenda_cell_selected").removeClass("agenda_cell_selected");
      });
    });

    // Gestion du survol des cellules
    oCells.mouseover( function()
    {
      if (g_oDateStart != null)
      {
        $("#" + g_nCurrentAgenda + "-" + g_oDateStart.getTime() / 1000).addClass("agenda_cell_selected");
        
        var nTime = this.id.split('-')[1];
        
        var oDateEnd =  new Date();
        oDateEnd.setTime(parseInt(nTime) * 1000);
        
        if (oDateEnd.getTime() > g_oDateStart.getTime() || (oDateEnd.getDate() == g_oDateStart.getDate() && oDateEnd.getHours() * 60 + oDateEnd.getMinutes() >= g_oDateStart.getHours() * 60 + g_oDateStart.getMinutes()))
        {
          g_oDateEnd = oDateEnd;
          
          // Met à jour l'affichage
          $(".agenda_cell_selected").removeClass("agenda_cell_selected");
          
          oDateCurrent = g_oDateStart;
          while(oDateCurrent.getTime() <= g_oDateEnd.getTime())
          {
            switch(g_nCurrentAgenda)
            {
              case 0:
                for(nCpt = 0; nCpt < cWeekMultiAgenda.CONST_USERS_NB + 1; nCpt++)
                  if($("#" + g_nCurrentAgenda + nCpt + "-" + oDateCurrent.getTime() / 1000) != null)
                    $("#" + g_nCurrentAgenda + nCpt + "-" + oDateCurrent.getTime() / 1000).addClass("agenda_cell_selected");
                break;
              
              default:
                if($("#" + g_nCurrentAgenda + "-" + oDateCurrent.getTime() / 1000) != null)
                  $("#" + g_nCurrentAgenda + "-" + oDateCurrent.getTime() / 1000).addClass("agenda_cell_selected");
                break;
            }
            
            oDateCurrent = new Date(oDateCurrent.valueOf() + (cWeekAgenda.CONST_CELL_DURATION * 60 * 1000));
          }
        }
      }
    });

    // Fin de sélection
  	oCells.mouseup(function()
    {
      if (g_oDateStart == null) return ;
      if (g_oDateEnd == null)
      {
        g_oDateEnd = new Date(g_oDateStart);
        g_oDateEnd.setMinutes(g_oDateEnd.getMinutes() + 30);
      }

      var sURL = psAddURL.replace("datestart", g_oDateStart.getTime() / 1000);
      sURL = sURL.replace("dateend", g_oDateEnd.getTime() / 1000)
      sURL = sURL.replace("loginpk", g_nCurrentAgenda);

      ExecuteAction(sURL, 600, $(window).height() - 20);
	  $('#divaction').show();

      g_oDateStart = null;
      g_oDateEnd = null;
    });
  }
}

cWeekMultiAgenda.prototype.Draw = function(refresh)
{
  $('#agenda_block0 .agenda_hsep_hour').remove();
  $('#agenda_block0 .agenda_multi_hsep_halfhour').remove();
  $('#agenda_block0 .agenda_vsep').remove();
  
  // div contenant l'agenda
  oRoot = $("#agenda_block" + this.m_nID);
  
  // div contenant les événements
  oContainer = $('#agenda_container' + this.m_nID);
  
  var nFirstDate = (cWeekMultiAgenda.m_nTimeStart / 1000) + (cWeekMultiAgenda.CONST_HOUR_START * 3600);
  var nLength = (cWeekMultiAgenda.CONST_HOUR_END - cWeekMultiAgenda.CONST_HOUR_START);
  
  // On déssine les séparateurs horizontaux
  for (nCptDay = 0; nCptDay < cWeekMultiAgenda.CONST_NBDAY; nCptDay++)
  {
    for (nCptUser = 0; nCptUser < cWeekMultiAgenda.CONST_CELL_HEIGHT * cWeekMultiAgenda.CONST_USERS_NB; nCptUser += cWeekMultiAgenda.CONST_CELL_HEIGHT)
    {
      oDiv = $(document.createElement("div"));

      oDiv.css("top", (nCptDay * cWeekMultiAgenda.CONST_CELL_HEIGHT * cWeekMultiAgenda.CONST_USERS_NB + cWeekMultiAgenda.CONST_CELL_HEADER_HEIGHT + nCptUser + nCptDay * cWeekMultiAgenda.CONST_CELL_DAY_SEPARATOR * 2) + "px");
      if (nCptUser != 0)
      {
        oDiv.addClass("agenda_multi_hsep_halfhour");
        oDiv.css("right", "0px");
        oDiv.css("width", (nLength * cWeekMultiAgenda.CONST_COLUMN_WIDTH) + "px");
      }
      else
        oDiv.addClass("agenda_hsep_hour");

      oRoot.append(oDiv);
    }
  }
  
  // On déssine les séparateurs verticaux
  var i = nFirstDate;
  for (nCurrentDay = 1; nCurrentDay <= nLength; nCurrentDay++)
  {
    oDiv = $(document.createElement("div"));
    oDiv.addClass("agenda_vsep");
    oDiv.css("right", (cWeekMultiAgenda.CONST_COLUMN_WIDTH * nCurrentDay) + "%");
    oDiv.css("top", cWeekMultiAgenda.CONST_CELL_HEADER_HEIGHT);
    oDiv.css("height", ((cWeekMultiAgenda.CONST_NBDAY * cWeekMultiAgenda.CONST_USERS_NB * cWeekMultiAgenda.CONST_CELL_HEIGHT) + (cWeekMultiAgenda.CONST_NBDAY * cWeekMultiAgenda.CONST_CELL_DAY_SEPARATOR * 2)) + "px");
    oContainer.append(oDiv);
    
    i += 3600;
  }
  
  // à la création: on calcule les collisions de chaque évènements avec ses suivants
  if(!refresh)
  {
    for (nEventA=0; nEventA<this.m_aoWeekEvents.length; nEventA++)
    {
      for (nEventB = nEventA+1; nEventB < this.m_aoWeekEvents.length; nEventB++)
      {
        // Chaque évènements contrôle un conflit potentiel avec son prédecesseur
        // Chaque conflit détecté augment la position du 2ème
        this.m_aoWeekEvents[nEventA].ControlConflictDate(this.m_aoWeekEvents[nEventB]);
      }
    }
  }
  
  // on dessine les evenements
  jQuery.each(this.m_aoWeekEvents, function() { this.Draw(); });
}

cWeekMultiAgenda.prototype.AddEvent = function(poEvent)
{
  this.m_aoWeekEvents.push(poEvent);
}

/**
 * Agenda au mois
 */
function cMonthAgenda(pnID, psAddURL, pbCanEdit, pnTimeStart, pnTimeEnd, pnWeekNumber, pnUsers, pnHourStart, pnHourEnd, pbDisplayWeekend)
{
  this.m_nID = pnID;
  this.m_sAddURL = psAddURL;
  this.m_bCanEdit = pbCanEdit;
  this.m_bDisplayWeek = pbDisplayWeekend;
  
  // Disable text select
  if ($.browser.mozilla) $("#agenda_block" + this.m_nID).css({'MozUserSelect' : 'none'});
  if ($.browser.msie) $("#agenda_block" + this.m_nID).bind('selectstart.disableTextSelect', function() { return false; });
  if (!$.browser.msie && !$.browser.mozilla)$("#agenda_block" + this.m_nID).bind('mousedown.disableTextSelect', function() { return false; });
  
  // Table contenant les cellules de l'agenda
  oCells = $(".agenda_cell");

  // Effet de survol sur les cellules
  oCells.hover(function() {$(this).addClass("agenda_cell_over");}, function() {$(this).removeClass("agenda_cell_over");});
  
  this.m_aoWeekEvents = new Array();
  
  if(typeof(cMonthAgenda.CONST_MAX_USER) == 'undefined')
    cMonthAgenda.CONST_MAX_USER = 0;
  
  // Constantes: taille des cellules
  cMonthAgenda.CONST_NBDAY = 7;
  if(!this.m_bDisplayWeek)
    cMonthAgenda.CONST_NBDAY = 5;
  
  cMonthAgenda.CONST_CELL_DURATION = 1440; // 1 jour (en minutes)
  cMonthAgenda.CONST_CELL_HEIGHT = 50;
  cMonthAgenda.CONST_CELL_HEADER_HEIGHT = 25;
  cMonthAgenda.CONST_CELL_WEEK_SEPARATOR = 10;
  cMonthAgenda.CONST_COLUMN_WIDTH = (100 / (cMonthAgenda.CONST_NBDAY * 2)); // 100% / nb cases
  cMonthAgenda.CONST_WEEK_NUMBER = pnWeekNumber;
  cMonthAgenda.CONST_USERS_NB = pnUsers;
  if(cMonthAgenda.CONST_MAX_USER < cMonthAgenda.CONST_USERS_NB)
    cMonthAgenda.CONST_MAX_USER = cMonthAgenda.CONST_USERS_NB;
  
  // Constante heures
  cMonthAgenda.CONST_HOUR_MORNING_START = 8;
  cMonthAgenda.CONST_HOUR_MORNING_END = 12;
  cMonthAgenda.CONST_HOUR_EVENING_START = 14;
  cMonthAgenda.CONST_HOUR_EVENING_END = 19;
  
  // Heure de début et de fin de l'agenda
  cMonthAgenda.m_nTimeStart = pnTimeStart;
  cMonthAgenda.m_nTimeEnd = pnTimeEnd;
  cMonthAgenda.m_nHourStart = pnHourStart;
  cMonthAgenda.m_nHourEnd = pnHourEnd;
  
  // Si on peut modifier l'agenda on ajoute les écouteur d'evenement
  if (pbCanEdit)
  {
    // on change le type de pointeur
    oCells.css('pointer', 'cursor');

    // on commence la selection sur le mousedown
    oCells.mousedown(function()
    {
      if(!g_bManagerUser && g_nCurrentAgenda != g_nCurrentUser && g_nCurrentAgenda != 0)
        return;
      
      var nTime = this.id.split("-")[1];
      
      $("table.agenda_monthagenda td.agenda_cell_selected").removeClass("agenda_cell_selected");
      g_oDateStart = new Date();
      g_oDateStart.setTime(parseInt(nTime) * 1000);
      $(this).addClass("agenda_cell_selected");

      $(document).mouseup(function()
      {
        g_oDateStart = null;
        g_oDateEnd = null;
        $("table.agenda_monthagenda td.agenda_cell_selected").removeClass("agenda_cell_selected");
      });
    });

    // Gestion du survol des cellules
    oCells.mouseover( function()
    {
      if (g_oDateStart != null)
      {
        $("#" + g_nCurrentAgenda + "-" + g_oDateStart.getTime() / 1000).addClass("agenda_cell_selected");
        
        var nTime = this.id.split("-")[1];
        
        var oDateEnd =  new Date();
        oDateEnd.setTime(parseInt(nTime) * 1000);
        
        if (oDateEnd.getTime() > g_oDateStart.getTime() || (oDateEnd.getDate() == g_oDateStart.getDate() && oDateEnd.getHours() * 60 + oDateEnd.getMinutes() >= g_oDateStart.getHours() * 60 + g_oDateStart.getMinutes()))
        {
          g_oDateEnd = oDateEnd;
          
          // Met à jour l'affichage
          $(".agenda_cell_selected").removeClass("agenda_cell_selected");
          
          oDateCurrent = g_oDateStart;
          while(oDateCurrent.getTime() <= g_oDateEnd.getTime())
          {
            switch(g_nCurrentAgenda)
            {
              case 0:
                for(nCpt = 0; nCpt < cMonthAgenda.CONST_MAX_USER + 1; nCpt++)
                  if($("#" + g_nCurrentAgenda + nCpt + "-" + oDateCurrent.getTime() / 1000) != null)
                    $("#" + g_nCurrentAgenda + nCpt + "-" + oDateCurrent.getTime() / 1000).addClass("agenda_cell_selected");
                break;
              
              default:
                if($("#" + g_nCurrentAgenda + "-" + oDateCurrent.getTime() / 1000) != null)
                  $("#" + g_nCurrentAgenda + "-" + oDateCurrent.getTime() / 1000).addClass("agenda_cell_selected");
                break;
            }
            
            if(oDateCurrent.getHours() < cMonthAgenda.CONST_HOUR_MORNING_END || (oDateCurrent.getHours() == cMonthAgenda.CONST_HOUR_MORNING_END && oDateCurrent.getMinutes() <= 30))
              oDateCurrent = new Date(oDateCurrent.getFullYear(), oDateCurrent.getMonth(), oDateCurrent.getDate(), cMonthAgenda.CONST_HOUR_EVENING_START, 0, 0);
            else
            {
              nDay = oDateCurrent.getDate() + 1;
              if(getNbJour(oDateCurrent.getMonth(), oDateCurrent.getFullYear()) < nDay)
                nDay = 1;
              
              oDateCurrent = new Date(oDateCurrent.getFullYear(), oDateCurrent.getMonth(), oDateCurrent.getDate() + 1, cMonthAgenda.CONST_HOUR_MORNING_START, 0, 0);
            }
          }
        }
      }
    });
    
    // Fin de sélection d'une zone
  	oCells.mouseup(function()
    {
      if (g_oDateStart == null) return ;
      
      // Si la date de fin est vide, on a sélectionné une demie-journée
      if (g_oDateEnd == null)
      {
        // On se positionne à la fin de la demie_journée
        if(g_oDateStart.getHours() < 12)
          g_oDateEnd = new Date(g_oDateStart.getFullYear(), g_oDateStart.getMonth(), g_oDateStart.getDate(), cMonthAgenda.CONST_HOUR_MORNING_END, 0, 0);
        else
          g_oDateEnd = new Date(g_oDateStart.getFullYear(), g_oDateStart.getMonth(), g_oDateStart.getDate(), cMonthAgenda.CONST_HOUR_EVENING_END, 0, 0);
      }
      else
      {
        // Sinon on a choisi une plage plus longue, on se positionne sur la fin de la demie-journée de la date de fin
        if(g_oDateEnd.getHours() < 12)
        {
          g_oDateEnd.setHours(cMonthAgenda.CONST_HOUR_MORNING_END);
          g_oDateEnd.setMinutes(0);
        }
        else
        {
          g_oDateEnd.setHours(cMonthAgenda.CONST_HOUR_EVENING_END);
          g_oDateEnd.setMinutes(0);
        }
      }

      var sURL = psAddURL.replace("datestart", g_oDateStart.getTime() / 1000);
      sURL = sURL.replace("dateend", g_oDateEnd.getTime() / 1000);
      sURL = sURL.replace("loginpk", g_nCurrentAgenda);

      ExecuteAction(sURL, 600, $(window).height() - 20);
      $('#divaction').show();

      g_oDateStart = null;
      g_oDateEnd = null;
    });
  }
}

cMonthAgenda.prototype.Draw = function(refresh)
{
  // div contenant l'agenda
  oRoot = $("#agenda_block" + this.m_nID);

  // div contenant les événements
  oContainer = $('#agenda_container' + this.m_nID);
  
  // On déssine les séparateurs horizontaux
  for (nCptWeek = 0; nCptWeek < cMonthAgenda.CONST_WEEK_NUMBER; nCptWeek++)
  {
    for (nCptUser = 0; nCptUser < cMonthAgenda.CONST_CELL_HEIGHT * cMonthAgenda.CONST_USERS_NB; nCptUser += cMonthAgenda.CONST_CELL_HEIGHT)
    {
      oDiv = $(document.createElement("div"));

      oDiv.css("top", (nCptWeek * cMonthAgenda.CONST_CELL_HEIGHT * cMonthAgenda.CONST_USERS_NB + cMonthAgenda.CONST_CELL_HEADER_HEIGHT + nCptUser + nCptWeek * cMonthAgenda.CONST_CELL_WEEK_SEPARATOR * 2) + "px");
      if (nCptUser != 0)
      {
        oDiv.addClass("agenda_multi_hsep_halfhour");
        oDiv.css("right", "0px");
        oDiv.css("width", (cMonthAgenda.CONST_COLUMN_WIDTH * (cMonthAgenda.CONST_NBDAY * 2)) + "%");
      }
      else
        oDiv.addClass("agenda_hsep_hour");

      oRoot.append(oDiv);
    }
  }

  // On déssine les séparateurs verticaux
  for (nCurrentDay = 1; nCurrentDay <= cMonthAgenda.CONST_NBDAY; nCurrentDay++)
  {
    oDiv = $(document.createElement("div"));
    oDiv.addClass("agenda_vsep");
    oDiv.css("right", (100 / cMonthAgenda.CONST_NBDAY * nCurrentDay) + "%");
    oDiv.css("top", cMonthAgenda.CONST_CELL_HEADER_HEIGHT);
    oDiv.css("height", (cMonthAgenda.CONST_WEEK_NUMBER * cMonthAgenda.CONST_CELL_HEIGHT * cMonthAgenda.CONST_USERS_NB + (70 * 1.5)) + "px");
    
    oContainer.append(oDiv);
  }
  oRoot.append(oContainer);
  
  // à la création: on calcule les collisions de chaque évènements avec ses suivants
  if(!refresh)
  {
    for (nEventA=0; nEventA<this.m_aoWeekEvents.length; nEventA++)
    {
      for (nEventB = nEventA+1; nEventB < this.m_aoWeekEvents.length; nEventB++)
      {
        // Chaque évènements contrôle un conflit potentiel avec son prédecesseur
        // Chaque conflit détecté augment la position du 2ème
        this.m_aoWeekEvents[nEventA].ControlConflictDate(this.m_aoWeekEvents[nEventB]);
      }
    }
    
    for (nEventA=0; nEventA<this.m_aoWeekEvents.length; nEventA++)
      for (nEventB = nEventA+1; nEventB < this.m_aoWeekEvents.length; nEventB++)
        this.m_aoWeekEvents[nEventA].ControlConflictDate(this.m_aoWeekEvents[nEventB]);
  }
  
  // on dessine les evenements
  jQuery.each(this.m_aoWeekEvents, function() { this.Draw(); });
}

cMonthAgenda.prototype.AddEvent = function(poEvent)
{
  this.m_aoWeekEvents.push(poEvent);
}