var DaterangeControl = Class.create({
    input:null,
    view:null,
    fromdate:null,
    todate:null,
    quickselect: null,
    drop:null,

    initialize: function(element)
    {
        this.input = $(element);
        this.input.daterange = this;
        // :BUG: hoannd008 Builder not return an prototype element. Solution: use $
        this.view = $(Builder.node('div', {className: 'daterange'}));
        this.view.update(
            'from <input class="datepicker fromdate"> to <input class="datepicker todate">\n\
            <div>or \n\
                <select class="quickselect">\n\
                    <option value="null">Quick select</option>\n\
                    <option value="0">Today</option>\n\
                    <option value="-1">Past 1 day</option>\n\
                    <option value="-2">Past 2 days</option>\n\
                    <option value="-3">Past 3 days</option>\n\
                    <option value="-7">Past 7 days</option>\n\
                    <option value="-30">Past 30 days</option>\n\
                    <option value="-60">Past 60 days</option>\n\
                    <option value="-90">Past 90 days</option>\n\
                </select>\n\
            </div>\n\
            <div class="action"><span class="action_item apply">Apply</span> <span class="action_item cancel">Cancel</span></div>'
        );
        this.drop = new DropdownControl(this.input, this.view);
        this.observeEvent();
    },

    observeEvent: function()
    {
        var control = this;
        this.fromdate = this.view.down('.fromdate');
        this.todate = this.view.down('.todate');
        this.quickselect = this.view.down('.quickselect');

        this.view.find('.datepicker').invoke('observe', 'click',function(){
            new CalendarDateSelect(this);
        });

        this.view.down('.action_item.apply').observe('click', function(){
            control.apply();
        });
        this.view.down('.action_item.cancel').observe('click', function(){
			control.drop.hide();
        });

        this.quickselect.observe('change', function(){
            control.setQuickSelect(this.value);
        });
    },

    setQuickSelect: function(offset)
    {
        if (offset == 'null') return;
        offset = parseInt(offset);
        var today = new Date();
        this.todate.value = this.formatOffsetDate(today, 1);
        this.fromdate.value = this.formatOffsetDate(today, offset);
    },

    offsetDay: function(date, offset)
    {
        var newDate = new Date();
        newDate.setDate(date.getDate()+offset);
        return newDate;
    },

    formatOffsetDate: function(date, offset)
    {
        var offsetDate  = this.offsetDay(date, offset);
        return this.formatDate(offsetDate);
    },

    apply: function()
    {
        this.input.value = this.getRangeValue();
        this.drop.hide();
        if (this.applyCallback)
            this.applyCallback();
    },

    getRangeValue: function()
    {
        var fromdate = this.fromdate.value;
        var todate = this.todate.value;
        if (todate && !fromdate)
            return "<= '" + todate + "'";
        else if (fromdate && !todate)
            return ">= '" + fromdate + "'";
        else if (fromdate && todate)
            return "BETWEEN '" + fromdate + "' AND '" + todate + "'";
        else
            return null;
    },

    formatDate: function(d)
    {
        var month = d.getMonth()+1;
        if (month<10) month = '0'+month;
        return d.getFullYear()+'-'+month+'-'+d.getDate();
    }
});