CalendarController = Class.create();
CalendarController.prototype =
{
    initialize: function(formElementName,wl,departureDateContainer,numNightsContainer)
    {
        this.formElementName = formElementName;
        this.weekList = wl;
        this.departureDateContainer = departureDateContainer;
        this.numNightsContainer = numNightsContainer;
    },
    init: function()
    {
        this.formElement = $(this.formElementName);

        this.weekCtrl = this.formElement["_isoWeek"];
        this.weekCtrl.onchange = this.onChangeWeek.bind(this,this.weekCtrl);
        this.isoWeekField = this.formElement["isoWeek"];

        // OK, find our elements to control
        this.periodCtrl = this.formElement["_period"];
        this.periodCtrl.onchange = this.onChangePeriod.bind(this,this.periodCtrl);
        this.periodIdField = this.formElement["periodId"];
        this.modeField = this.formElement["mode"];

        this.departureDateCtrl = this.formElement["_dates"];
        this.departureDateCtrl.onchange = this.onChangeDepartureDate.bind(this,this.departureDateCtrl);
        this.departureDateField = this.formElement["departuredate"];

        this.numNightsCtrl = this.formElement["_numNights"];
        this.numNightsField = this.formElement["numNights"];

        // Optional fields
        if(this.formElement["beds"])
        {
            this.bedsCtrl = this.formElement["beds"];
            this.bedsField = this.formElement["fBeds"];
        }

        if(this.formElement["fType"])
            this.typeMaskField = this.formElement["fType"];

        if(this.formElement["fSite"])
            this.siteMaskField = this.formElement["fSite"];

    },
    updatePeriod: function(week)
    {
        // OK, lets fill the periods
        this.periodCtrl.length = 0;
        if(week.periods[1])
            this.periodCtrl.options[this.periodCtrl.length] = new Option(this.translate('period1'),1);

        if(week.periods[2])
            this.periodCtrl.options[this.periodCtrl.length] = new Option(this.translate('period2'),2);

        if(week.periods[3])
            this.periodCtrl.options[this.periodCtrl.length] = new Option(this.translate('period3'),3);

        if(week.periods[4])
            this.periodCtrl.options[this.periodCtrl.length] = new Option(this.translate('period4'),4);
    },
    updateDepartureDate: function(week)
    {
        this.departureDateCtrl.length = 0;

        for(i = 1; i <= 7; i++)
        {
            var day = week.periods[4]['days'][i];
            if(day)
            {
                this.departureDateCtrl.options[this.departureDateCtrl.length] = new Option(this.translate('day' + i) + ' (' + day['date'] + ')',i);
            }
        }
    },
    updateNumNights: function(week)
    {
        var cdd = this.getCurrentDepartureDay();

        this.numNightsCtrl.length = 0;
        var day = week.periods[4]['days'][cdd];
        for(i = 1; i <= day['num']; i++)
                this.numNightsCtrl.options[this.numNightsCtrl.length] = new Option(i,i);
    },
    getCurrentWeek: function()
    {
        var dateExpression = this._getSelection(this.weekCtrl);
        var args = dateExpression.split(':');

        return this.weekList[args[3]];
    },
    getCurrentPeriodId: function()
    {
        return this._getSelection(this.periodCtrl);
    },
    getCurrentDepartureDay: function()
    {
        return this._getSelection(this.departureDateCtrl);
    },
    getCurrentDepartureDate: function()
    {
        var w = this.getCurrentWeek();
        var pid = this.getCurrentPeriodId();
        var day = this.getCurrentDepartureDay();

        if(pid == 4)
        {
            // Days, we should have a current date
            return w.periods[4]['days'][day]['date'];
        }
        return null;
    },
    getCurrentNumNights: function()
    {
        return this._getSelection(this.numNightsCtrl);
    },
    getCurrentMode: function()
    {
        if(this.getCurrentPeriodId() == 4)
            return "days";
        else
            return "period";
    },
    getCurrentBeds: function()
    {
        if(this.bedsCtrl)
            return this._getSelection(this.bedsCtrl);
        else
            return 0;
    },
    getCurrentTypeMask: function()
    {
        if(!this.formElement['type0'])
            return 0xFFFFFFFF;

        i = 0;
        tMask = 0;
        while(true)
        {
            c = this.formElement['type'+i];

            if(c == null)
                break;

            if(c.checked)
                tMask = tMask | c.value;
            i++;
        }
        return tMask;
    },
    getCurrentSiteMask: function()
    {
        if(!this.formElement['site0'])
            return 0xFFFFFFFF;

        i = 0;
        tMask = 0;
        while(true)
        {
            c = this.formElement['site'+i];

            if(c == null)
                break;

            if(c.checked)
                tMask = tMask | c.value;
            i++;
        }
        return tMask;
    },
    setWeek: function(dateExpression)
    {
        var args = dateExpression.split(':');
        var wid = args[3];

        // OK, find specified selection in ctrl
        for(i = 0; i < this.weekCtrl.length; i++)
        {
            var o = this.weekCtrl[i];
            args = o.value.split(':');

            if(args[3] == wid)
            {
                this.weekCtrl.selectedIndex = i;
                break;
            }
        }
        this.onChangeWeek();
    },
    onChangeWeek: function()
    {
        var w = this.getCurrentWeek();

        // Save current selections
        var pid = this.getCurrentPeriodId();
        var cdd = this.getCurrentDepartureDay();
        var cnn = this.getCurrentNumNights();

        // Update periods
        this.updatePeriod(w);
        this._selectItem(this.periodCtrl,pid);
        pid = this.getCurrentPeriodId();

        if(this.getCurrentMode() == "period")
        {
            this.setPeriodMode();
        }
        else
        {
            this.setDaysMode();

            // Init departure
            this.updateDepartureDate(w);
            this._selectItem(this.departureDateCtrl,cdd);

            // init num nights
            this.updateNumNights(w);
            this._selectItem(this.numNightsCtrl,cnn);
        }
    },
    onChangePeriod: function()
    {
        var w = this.getCurrentWeek();

        // Save current selections
        var cdd = this.getCurrentDepartureDay();
        var cnn = this.getCurrentNumNights();

        if(this.getCurrentMode() == "period")
        {
            this.setPeriodMode();
        }
        else
        {
            this.setDaysMode();

            // Init departure
            this.updateDepartureDate(w);
            this._selectItem(this.departureDateCtrl,cdd);

            // init num nights
            this.updateNumNights(w);
            this._selectItem(this.numNightsCtrl,cnn);
        }
    },
    onChangeDepartureDate: function()
    {
        var w = this.getCurrentWeek();

        // Save current selections
        var cnn = this.getCurrentNumNights();

        // init num nights
        this.updateNumNights(w);
        this._selectItem(this.numNightsCtrl,cnn);
    },
    onTypeChanged: function()
    {
        // Not used
    },
    onSiteChanged: function()
    {
        // Not used
    },
    onBedsChanged: function()
    {
        // Not used
    },
    setDaysMode: function()
    {
        this.departureDateContainer.show();
        this.numNightsContainer.show();
    },
    setPeriodMode: function()
    {
        this.departureDateContainer.hide();
        this.numNightsContainer.hide();
    },
    _selectItem: function(ctrl,value)
    {
        for(i = 0; i < ctrl.length;i++)
        {
            if(ctrl[i].value == value)
            {
                ctrl.selectedIndex = i;
                return;
            }
        }

        // If not found, select first
        ctrl.selectedIndex = 0;
    },
    _getSelection: function(ctrl)
    {
        if(ctrl.selectedIndex >= 0 && ctrl.length > 0)
            return ctrl[ctrl.selectedIndex].value;
        else
            return -1;
    },
    translate: function(key)
    {
        return key;
    },
    submit: function()
    {
        var w = this.getCurrentWeek();

        // OK, copy selected values
        this.isoWeekField.value = w.periodid + ':' + w.start + ':' + w.end + ':' + w.id;
        var form = 'IsoWeek:' + this.isoWeekField.value + '\n';

        this.periodIdField.value = this.getCurrentPeriodId();
        form += 'PeriodId:' + this.periodIdField.value + '\n';

        this.modeField.value = this.getCurrentMode();
        form += 'mode:' + this.modeField.value + '\n';

        if(this.modeField.value == "days")
        {
            this.departureDateField.value = this.getCurrentDepartureDate();
            this.numNightsField.value = this.getCurrentNumNights();

            form += 'DepartureDate:' + this.departureDateField.value + '\n';
            form += 'Nights:' + this.numNightsField.value + '\n';
        }
        if(this.bedsField && this.bedsCtrl)
        {
            form += 'Beds:' + this.bedsField.value + '\n';
            this.bedsField.value = this.getCurrentBeds();
        }

        if(this.typeMaskField)
        {
            this.typeMaskField.value = this.getCurrentTypeMask();
            form += 'TypeMask:' + this.typeMaskField.value + '\n';
        }
        
        if(this.siteMaskField)
        {
            this.siteMaskField.value = this.getCurrentSiteMask();
            form += 'siteMask:' + this.siteMaskField.value + '\n';
        }


//        alert(form);
        this.formElement.submit();
    }
};
