﻿
function AptConfig(thisId, dateLink1, dateLink2, selMonth1Id, selMonth2Id, selDay1Id, selDay2Id, selYear1Id, selYear2Id, nightsId, apartment_prices, price_label_id, container1Id, container2Id) {

    var obj = {
        thisId: null,
        link1: null,
        link2: null,

        selMonth1: null,
        selDay1: null,
        selYear1: null,

        selMonth2: null,
        selDay2: null,
        selYear2: null,
        nights: null,
        apartment_prices: null,
        price_label: null,

        cal1: null,
        cal2: null,


        init: function() {

            this.today = new Date();
            this.thisId = thisId;
            this.link1 = document.getElementById(dateLink1);
            this.link2 = document.getElementById(dateLink2);

            this.selMonth1 = document.getElementById(selMonth1Id);
            this.selDay1 = document.getElementById(selDay1Id);
            this.selYear1 = document.getElementById(selYear1Id);

            this.selMonth2 = document.getElementById(selMonth2Id);
            this.selDay2 = document.getElementById(selDay2Id);
            this.selYear2 = document.getElementById(selYear2Id);

            this.nights = document.getElementById(nightsId);
            this.apartment_prices = apartment_prices;
            this.price_label = document.getElementById(price_label_id);

            //Load  selected values in cal
            var thisMonth2 = this.selMonth2.selectedIndex;
            var thisDay2 = this.selDay2.selectedIndex;
            var thisYear2 = this.selYear2.value;

            var thisMonth1 = this.selMonth1.selectedIndex;
            var thisDay1 = this.selDay1.selectedIndex;
            var thisYear1 = this.selYear1.value;

            this.cal1 = new YAHOO.widget.Calendar2up("YAHOO.example.calendar.cal1", container1Id, (thisMonth1 + 1) + "/" + thisYear1, (thisMonth1 + 1) + "/" + (thisDay1 + 1) + "/" + thisYear1);
            this.cal1.title = "Select your desired arrival date:";
            this.cal1.setChildFunction("onSelect", function() { window[thisId].setDate1(); });
            this.cal1.render();

            this.cal2 = new YAHOO.widget.Calendar2up("YAHOO.example.calendar.cal2", container2Id, (thisMonth2 + 1) + "/" + thisYear2, (thisMonth2 + 1) + "/" + (thisDay2 + 1) + "/" + thisYear2);
            this.cal2.title = "Select your desired departure date:";
            this.cal2.setChildFunction("onSelect", function() { window[thisId].setDate2(); });
            this.cal2.render();

        },
        showCalendar1: function() {
            this.cal2.hide();
            var pos = YAHOO.util.Dom.getXY(this.link1);
            this.cal1.outerContainer.style.display = 'block';
            YAHOO.util.Dom.setXY(this.cal1.outerContainer, [pos[0], pos[1] + this.link1.offsetHeight + 1]);

        },

        showCalendar2: function() {
            this.cal1.hide();
            var pos = YAHOO.util.Dom.getXY(this.link2);
            this.cal2.outerContainer.style.display = 'block';
            YAHOO.util.Dom.setXY(this.cal2.outerContainer, [pos[0], pos[1] + this.link2.offsetHeight + 1]);
        },
        setDate1: function() {
            var date1 = this.cal1.getSelectedDates()[0];
            this.selMonth1.selectedIndex = date1.getMonth();
            this.selDay1.selectedIndex = date1.getDate() - 1;
            this.selYear1.value = date1.getFullYear();
            this.cal1.hide();
            this.UpdateNights();
        },
        setDate2: function() {
            var date2 = this.cal2.getSelectedDates()[0];
            this.selMonth2.selectedIndex = date2.getMonth();
            this.selDay2.selectedIndex = date2.getDate() - 1;
            this.selYear2.value = date2.getFullYear();
            this.cal2.hide();
            this.UpdateNights();
        },
        changeDate1: function() {
            var month = this.selMonth1.selectedIndex;
            var day = this.selDay1.selectedIndex + 1;
            var year = this.selYear1.value;
            this.cal1.select((month + 1) + "/" + day + "/" + year);
            this.cal1.setMonth(month);
            this.cal1.render();
            this.UpdateNights();
        },
        changeDate2: function() {
            var month = this.selMonth2.selectedIndex;
            var day = this.selDay2.selectedIndex + 1;
            var year = this.selYear2.value;

            this.cal2.select((month + 1) + "/" + day + "/" + year);
            this.cal2.setMonth(month);
            this.cal2.render();

            this.UpdateNights();
        },

        UpdateNights: function() {

            //Load  selected values in cal
            var thisMonth2 = this.selMonth2.selectedIndex;
            var thisDay2 = this.selDay2.selectedIndex;
            var thisYear2 = this.selYear2.value;

            var thisMonth1 = this.selMonth1.selectedIndex;
            var thisDay1 = this.selDay1.selectedIndex;
            var thisYear1 = this.selYear1.value;

            var start = Date.UTC(thisYear1, thisMonth1, (thisDay1 + 1));
            var end = Date.UTC(thisYear2, thisMonth2, (thisDay2 + 1));

            //Get 1 day in milliseconds
            var one_day = 1000 * 60 * 60 * 24
            var diff = Math.ceil((end - start) / one_day);
            if (diff < 0)
                diff = 0;

            this.nights.value = diff;

        },
        UpdateAptPrices: function(ctl) {
            this.price_label.innerHTML = this.apartment_prices[ctl.options.selectedIndex];
        }
    }

    //obj.init();
    return obj;
}

YAHOO.namespace("example.calendar");
//YAHOO.util.Event.addListener(window, "load", config.init);



            
