function Calendrier (params) {
  Composant.call(this, params);

}

Calendrier.prototype = new Composant();

Calendrier.prototype.afficheHoraireDatePicker = function (idEvt, jour, mois, annee ) {

    var ctx = this;
    $("#calendarLoader").show();

    $("#contenuCalendarHeures").slideUp("fast", function() {
        $.ajax({
            data: {
                px                : 11,
                idEvt             : idEvt,
                jour              : jour,
                mois              : mois,
                annee             : annee
            },
            type:'get',
            async: false,
            dataType: "json",
            success: function(data, textStatus) {

                if(phptojs.empty(data.liste)) {
                    $("#dateCalendarRep").empty();
                    $("#listeCalendarRep").empty();
                    $.alerts.okButton = "Retour";

                    jAlert(globalsTexts["calendrier"][0], globalsTexts["calendrier"][1]);
                }
                else if( phptojs.count(data.liste) == 1 && data.liste[0].dispo ) {
                    if(data.isCompChoixDateTarifActif == 1) {
                       ctx.initAjaxDateLink(data.ids);

                    }
                    else {
			             Rediriger( data.liste[0].lien );
                    }
                }
                
                    $("#dateCalendarRep").html(data.debut);

                    if(typeof(window['recapAchat' + this.numero]) != "undefined") {
                        window['recapAchat' + this.numero].majDate(data.debut);
                    }

                    var liste           = "";
                    var countLines      = 0;
                    var countRows       = 1;
                    var checkIndispoAll = true;
                    var inc             = 0;
                    $.each(data.liste, function() {
                        if(countLines == 0) {
                            liste += "<div class='calendarHeureRows' id='calendarHeureRow" + countRows + "'>";
                        }
                        if(this.dispo == true) {
                            if(data.isCompChoixDateTarifActif == 1) {
                                // rajouter ici appel ajax au click sur l' 'horaire
                                if($('.compTarifDisponible').length > 0) {
                                    $('.compTarifDisponible').parents('form:first').remove();
                                }
                                liste += "<div class='calendarHeureLines'><a id='choixDateTarif_" + data.ids[inc] +"' class='choixDateTarif'>" + this.horaire + "</a></div>";
                                checkIndispoAll = false ;
                            }
                            else {
                                liste += "<div class='calendarHeureLines'><a href='" + this.lien + "'>" + this.horaire + "</a></div>";
                                checkIndispoAll = false ;
                            }
                        }
                        else {
                            liste += "<div class='calendarHeureLines'><a class='indispo'>" + this.horaire + "</a></div>";
                        }
                        countLines++;
                        inc++;

                        if(countLines == maxLines) {
                            liste += "</div>";
                            countLines=0;
                            countRows++;
                        }
                    });

                    if(checkIndispoAll == true) {
                        $.alerts.okButton = "Retour";
                        jAlert(globalsTexts["calendrier"][2] + "<br>" + globalsTexts["calendrier"][3], globalsTexts["calendrier"][1]);
                    }

                    $("#listeCalendarRep").html(liste);
                    $('.calendarHeureLines .choixDateTarif').click(function() {
                        $(".calendarHeureLines").each(function(){ $(this).removeClass("selected"); });
                        $(this).parent().addClass("selected");
                        var idRep = $(this).attr("id").split('_');
                        ctx.initAjaxDateLink(idRep[1]);
                    });
                    $("#calendarLoader").hide();

                    $("#contenuCalendarHeures").slideDown("fast");
                
            }
        });
    });
};

Calendrier.prototype.initAjaxDateLink = function (repId){
       $.ajax({
               data: {
                   px:              31,
                   action:          "choixDateTarif",
                   repId:           parseInt(repId)
               },
               type: 'get',
               dataType: 'json',
               cache: false,
               success: function (data){
                       var $dataContent = $('.compChoixDateTarif').find('.dataContentComposants:first');
                       if($dataContent.find('form:first').length == 1){
                               $dataContent.find('form:first').remove();
                       }

                       $dataContent.find('.dataContentComposants:first').append(data.htmlContainer);
               }
       });
}

Calendrier.prototype.getDateFormat = function () {
  format = '';
  switch(langueId){
    case langFR:
      format = 'dddd, dd/mm/yy';
    break;
    case langEN:
      format = 'dddd, mm/dd/yy'
    break;
    default:
      format = '';
  }
  return format;
};


Calendrier.prototype.replaceDateFormat = function () {
    Date.defaultFormat = Date.format;
    Date.format = this.getDateFormat();
};

Calendrier.prototype.resetDateFormat = function () {
    Date.format = Date.defaultFormat;
};

Calendrier.prototype.init = function (){
    var ctx = this;
    // ajouter la traduction sur les infos bulles du calendrier
    $.dpText = {
        TEXT_PREV_YEAR      :   globalsTexts["dpText"]["TEXT_PREV_YEAR"],
        TEXT_PREV_MONTH     :   globalsTexts["dpText"]["TEXT_PREV_MONTH"],
        TEXT_NEXT_YEAR      :   globalsTexts["dpText"]["TEXT_NEXT_YEAR"],
        TEXT_NEXT_MONTH     :   globalsTexts["dpText"]["TEXT_NEXT_MONTH"],
        TEXT_CLOSE          :   globalsTexts["dpText"]["TEXT_CLOSE"],
        TEXT_CHOOSE_DATE    :   globalsTexts["dpText"]["TEXT_CHOOSE_DATE"]
    }

    var $domDP = null;
    if(isChoixDateTarif){
        $domDP = $("#datepicker");
        $domDP.attr("readonly","readonly")
              .attr("placeholder",globalsTexts["calendrier"][17])
              .focus(function(){
                $(this).select();
              });
    }else{
        $("#contenuCalendar").addClass("datepicker");
        $domDP = $("#contenuCalendar");
    }

    $domDP.datePicker({
        inline:     isInline,
        startDate:  startDate,
        endDate:    endDate,
        month :     month,
        year :      year,
        clickInput: true,
        createButton: (isChoixDateTarif ? false : true),
        renderCallback: function($td, thisDate, month, year) {
            var dateSelected = thisDate.format("yyyy-mm-dd");
            if(month != thisDate.getMonth()) {
                $td.empty();
                $td.addClass("disabled");
            }
            if(dateSelected == dateArrive && !checkLoad) {
                $td.addClass("border");
                $("#contenuCalendar .dp-popup").addClass("load");
                ctx.afficheHoraireDatePicker(evenementId, thisDate.getDate(), thisDate.getMonth() + 1, thisDate.getFullYear() );
                checkLoad = true;
                $("#contenuCalendar .dp-popup").removeClass("load");
            }

            if(!phptojs.isset(listeTimeStamps[dateSelected])) {
                            $td.addClass("disabled");
            }

            if(phptojs.isset(listeTimeStampsIndispos[dateSelected])) {
                $td.addClass("indispo");
                if(phptojs.isset(codeEtatTimestampsIndispos[dateSelected])) {
                    var codeEtat = codeEtatTimestampsIndispos[dateSelected];
                    $td.addClass("etat_"+codeEtat);

                    if($td.hasClass("current-month") && phptojs.isset(globalsTexts["calendrierCodeEtat"][codeEtat]) && globalsTexts["calendrierCodeEtat"][codeEtat].length > 0){
                        $td.attr("title", globalsTexts["calendrierCodeEtat"][codeEtat]);
                    }
                }
            }

                if(displayTooltip && $td.attr("title") && $td.attr("title").length > 0
                && (phptojs.isset(listeTimeStampsIndispos[dateSelected]))){
                    jQueryLastClosure($td).tooltip({
                        position: "top right",
                        offset:[-15,-35]
                    });
                }
        }
    })
    .bind(
    "dateSelected",
    function(e, selectedDate, $td) {
            $("#contenuCalendar .jCalendar td").removeClass("border");
            $td.addClass("border");
            ctx.afficheHoraireDatePicker(evenementId, selectedDate.getDate(), selectedDate.getMonth() + 1, selectedDate.getFullYear());
    })
    //Les deux bind ci-dessous permettent de surcharger le format de date par defaut utilise
    //passe de dd/mm/yy au format Nom du jour, dd/mm/yy
    //Surcharge donc du Date.format & gestion du "dddd" dans Date.asString (les formats n y sont pas tous par defaut)
    .bind(
        "dpDisplayed",
        function () {
            ctx.replaceDateFormat();
        }
    ).bind(
        "dpClosed",
        function () {
            ctx.resetDateFormat();
        }
    );

    if(day) {
        ctx.afficheHoraireDatePicker(evenementId, day, month+1, year);
        $("#contenuCalendar").hide();
        checkLoad = true;
    }
};

