function Calendar(settings) {
    this.setting;
    this.calendarTable;
    this.monday = 7;
    this.showDate = new Date();
    this.lastDayOfMonth;
    this.firstOfMount;
    this.countDaysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    this.nameOfMonth = new Array('Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec','Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień');
    this.reservations = new Array();
    this.offers = new Array();
    
    this.setUp = function(settings) {
        if(settings['showDate']) {
            this.showDate = this.datePHP2DateJS(settings['showDate']);
        }
        this.calendarTable = $("#" + settings['id']);
        this.setLastDayOfMonth();
        this.setFirstDayOfMonth();
        this.insertMountNameAndYear();

    }

    this.nextMonth = function() {
        this.showDate.setMonth(this.showDate.getMonth()+1);
        this.insertMountNameAndYear();
        this.setFirstDayOfMonth();
        this.setLastDayOfMonth();
        this.showCalendar();
        this.insertReservations();
        this.insertOffers();
    }

    this.prevMonth = function() {
        this.showDate.setMonth(this.showDate.getMonth()-1);
        this.insertMountNameAndYear();
        this.setFirstDayOfMonth();
        this.setLastDayOfMonth();
        this.showCalendar();
        this.insertReservations();
        this.insertOffers();
    }

    this.setFirstDayOfMonth = function() {
        var firstDay = new Date(this.showDate.getFullYear(), this.showDate.getMonth(), 1);
        if(firstDay.getDay()==0){
            this.firstOfMount = this.monday;
        } else{
            this.firstOfMount = firstDay.getDay();
        }
    }

    this.setLastDayOfMonth = function() {
        this.lastDayOfMonth = this.countDaysInMonth[this.showDate.getMonth()];
        this.setLeapYear();
        
    }
    this.setLeapYear = function() {
        if(this.showDate.getMonth()==1 && this.showDate.getFullYear()%4==0) {
            this.lastDayOfMonth = 29;
        }
    }
    this.insertMountNameAndYear = function() {
        $(this.calendarTable).find('.headDate').html(this.nameOfMonth[this.showDate.getMonth()] + " " + this.showDate.getFullYear());
    }
    this.datePHP2DateJS = function(datePHP){
        if(datePHP instanceof Date) {
            return datePHP;
        }
        return new Date(datePHP.substr(0,4),datePHP.substr(5,2)-1,datePHP.substr(8,2));
    }
    
    this.showCalendar = function() {
        var numberOfDay = 1;
        for(var cellNumber=1; cellNumber<=42; cellNumber++){
            if(this.firstOfMount<=cellNumber && this.lastDayOfMonth>=numberOfDay){
                this.getDayCell(cellNumber).html('<div>' + numberOfDay + '</div>');
                this.getDayCell(cellNumber).addClass('border');
                numberOfDay++;
            }else{
                this.getDayCell(cellNumber).html("");
                this.getDayCell(cellNumber).removeClass('border');
            }
        }
    }

    this.setOffers = function(offers) {
        this.offers = offers;
    }

    this.insertOffers = function() {
        for(var i=0; i<this.offers.length; ++i) {
            this.offers[i]['dateFrom'] = this.datePHP2DateJS(this.offers[i]['dateFrom']);
            this.offers[i]['dateTo'] = this.datePHP2DateJS(this.offers[i]['dateTo']);
            this.insertOffer(this.offers[i]);
        }
    }

    this.insertOffer = function(offer) {
        var dateTo = offer['dateTo'];
        var dayInOffer = offer['dateFrom'];
        while(dayInOffer<=dateTo){
            if(dayInOffer.getMonth()==this.showDate.getMonth() && dayInOffer.getFullYear()==this.showDate.getFullYear()) {
                this.appendOfferToCell(dayInOffer, offer);
            }
            dayInOffer = new Date(dayInOffer.getFullYear(),dayInOffer.getMonth(),dayInOffer.getDate()+1);
        }
    }

    this.appendOfferToCell = function(dayInOffer, offer) {
        if(this.lastDayOfMonth >= dayInOffer.getDate()) {
            if(offer['type'] == 1) {
                this.getDayCell(this.getNumberDayCell(dayInOffer.getDate())).append('<div class="lastMinute">' + offer['percent'] + '%</div>');
            } else if(offer['type'] == 2) {
                this.getDayCell(this.getNumberDayCell(dayInOffer.getDate())).append('<div class="higherPrice">+' + offer['percent'] + '%</div>');
            } else if(offer['type'] == 3) {
                this.getDayCell(this.getNumberDayCell(dayInOffer.getDate())).append('<div class="promotion">P</div>');
            }
        }
    }

    this.setReservations = function(reservations) {
        this.reservations = reservations;
    }

    this.insertReservations = function() {
        for(var i=0; i<this.reservations.length; ++i) {
            this.reservations[i]['dateFrom'] = this.datePHP2DateJS(this.reservations[i]['dateFrom']);
            this.reservations[i]['dateTo'] = this.datePHP2DateJS(this.reservations[i]['dateTo']);
            this.insertReservation(this.reservations[i]);
        }
    }

    this.insertReservation = function(reservation) {
        var dateTo = reservation['dateTo'];
        var dayInReservation = reservation['dateFrom'];
        while(dayInReservation<=dateTo){
            if(dayInReservation.getMonth()==this.showDate.getMonth() && dayInReservation.getFullYear()==this.showDate.getFullYear()) {
                this.appendReservationToCell(dayInReservation, reservation);
            }
            dayInReservation = new Date(dayInReservation.getFullYear(),dayInReservation.getMonth(),dayInReservation.getDate()+1);
        }
    }

    this.appendReservationToCell = function(dayInReservation, reservation) {
        if(this.equalsDate(reservation['dateFrom'], dayInReservation)) {
            this.getDayCell(this.getNumberDayCell(dayInReservation.getDate())).append('<img src="/img/reservation_' + reservation['type'] + '_half.gif" style="float: right; margin: 0 6px 0 6px;" alt="rezerwacja"/>');
        } else if(this.equalsDate(reservation['dateTo'], dayInReservation)) {
            this.getDayCell(this.getNumberDayCell(dayInReservation.getDate())).append('<img src="/img/reservation_' + reservation['type'] + '_half.gif" style="float: left; margin: 0 6px 0 6px;" alt="rezerwacja"/>');
        } else {
            this.getDayCell(this.getNumberDayCell(dayInReservation.getDate())).append('<img src="/img/reservation_' + reservation['type'] + '.gif" style="margin: 0 6px 0 6px;" alt="rezerwacja"/>');
        }
    }
    
    this.equalsDate = function(date1, date2) {
        if(date1.getFullYear() != date2.getFullYear()) {
            return false;
        }
        if(date1.getMonth() != date2.getMonth()) {
            return false;
        }
        if(date1.getDate() != date2.getDate()) {
            return false;
        }
        return true
    }

    this.getNumberDayCell = function(day) {
        return day + this.firstOfMount - 1;
    }

    this.getDayCell = function(cellNumber){
        return $(this.calendarTable).find('.cell_' + cellNumber);
    }

    this.install = function () {
        this.setUp(settings);
        this.showCalendar();
        this.insertReservations();
        this.insertOffers();
    }

//    specialOffer(dzis[ind_d],pierwszy,ostatni,id_ap,o_special);
}

/*
    var DATE dzis - pierwszy dzien aktualnego miesiaca
*/

function specialOffer(dzis,pierwszy,ostatni,id_ap,o_special)
{
    var next_month = new Date(dzis.getFullYear(),dzis.getMonth()+1,1);
    for(var i=0; i<o_special.length; i++)
    {
        dzis_temp = dzis;
        while(dzis_temp<next_month)
        {
            if(dzis_temp>=o_special[i][0] && dzis_temp<=o_special[i][1])
            {
                getElemRefs("kal_dzien"+(pierwszy-1+dzis_temp.getDate())+"_"+id_ap).innerHTML += "<br /><b style=\"color: #CC9900;\">+" + o_special[i][2] + "%</b>";
            }
            dzis_temp = new Date(dzis_temp.getFullYear(),dzis_temp.getMonth(),dzis_temp.getDate()+1);
        }
    }
}

function policzCeneDnia(cdor,cdor_dod,cdz){
    var policz = 0;
    var ile_dor = getElemRefs('pilosc_osob').value;
    var ile_dz = getElemRefs('pilosc_dzieci').value;
    if(ile_dz=='') ile_dz=0;
    var ile_dor_dod = 0;
  
    if(ile_dor >2){
        ile_dor_dod = ile_dor-2;
        ile_dor = 2;
    }
    return cdor+ile_dor_dod*cdor_dod+ile_dz*cdz;
}

function policzCene(){
    var przy = getElemRefs('pprzyjazd').value;
    var wyj = getElemRefs('pwyjazd').value;
    var idz = getElemRefs('pilosc_dzieci').value;
    var idor = getElemRefs('pilosc_osob').value;
    if(przy=='' || wyj=='' || idor==''){
        alert('Musza być podane pola: Ilość dorosłych, Przyjazd, Wyjazd'); return;
    }
    if(isNaN(idz) || isNaN(idor)) {
        alert("Ilość osób i Ilość dzieci muszą być liczbami całkowitymi"); return false;
    }
    var cena = 0;
    var data_w = DatePHP2DateJS(getElemRefs('pprzyjazd').value);
    var data_wk = DatePHP2DateJS(getElemRefs('pwyjazd').value);
  
    //jesli przyjazd i wyjazd liczymy jako 1 dzen to tylko <
    /*
    promocja = Array(
                0 => od,
                1 => do,
                2 => cena 2 pierwszych doroslych,
                3 => cena koljnego doroslego,
                4 => cena dziecka
               )
    promocja_mniej = Array( //promocja mnie niż n (var n) dni
                0 => od,
                1 => do,
                2 => cena 2 pierwszych doroslych,
                3 => cena koljnego doroslego,
                4 => cena dziecka
               )
    ceny = Array(
                0 => cena 2 pierwszych doroslych,
                1 => cena koljnego doroslego,
                2 => cena dziecka
               )
  */

    //gdy ilosc dni rezerwacji jest mniejsza od n - zdeklarowana zmiena w bazie, tabeli poromojce
    if(n>0 && new Date(data_w.getFullYear(),data_w.getMonth(),data_w.getDate()+n)>data_wk)
    {
        while(data_w<data_wk){
            if(data_w>=promocja_mniej[0] && promocja_mniej[1]>data_w){
                cena_dnia = policzCeneDnia(promocja_mniej[2],promocja_mniej[3],promocja_mniej[4]);
            }else{
                cena_dnia = policzCeneDnia(ceny[0],ceny[1],ceny[2]);
            }
        
            //gdy w danym terminie jest wieksza cena (ofeta specialna)
            for(var j=0; j<special_offer.length; j++)
            {
                if(data_w>=special_offer[j][0] && special_offer[j][1]>data_w){
                    cena_dnia = cena_dnia*(special_offer[j][2]/100 + 1);
                    cena_dnia = Math.floor(cena_dnia);
                }
            }
            cena += cena_dnia;
            data_w = new Date(data_w.getFullYear(),data_w.getMonth(),data_w.getDate()+1);
        }
    }
    //gdy ilosc dni jest wieksza rowna n lub nie ma promocji
    else
    {
        while(data_w<data_wk){
            if(data_w>=promocja[0] && promocja[1]>data_w){
                cena_dnia = policzCeneDnia(promocja[2],promocja[3],promocja[4]);
            }else{
                cena_dnia = policzCeneDnia(ceny[0],ceny[1],ceny[2]);
            }
        
            //gdy w danym terminie jest wieksza cena (ofeta specialna)
            for(var j=0; j<special_offer.length; j++)
            {
                if(data_w>=special_offer[j][0] && special_offer[j][1]>data_w){
                    cena_dnia = cena_dnia*(special_offer[j][2]/100 + 1);
                    cena_dnia = Math.floor(cena_dnia);
                }
            }
            cena += cena_dnia;
            data_w = new Date(data_w.getFullYear(),data_w.getMonth(),data_w.getDate()+1);
        }
    }
    getElemRefs('cena').value = cena;
}
