var DatePicker = Class.create();
DatePicker.prototype = {
    initialize: function(id, props)
    {
        this.id = id;
        this.props = props;
        //this.Text = $(this.id + 'Text');
        this.PickButton = $(this.id + 'PickButton');
        //var PickDate = this.PickDate.bind(this);
        //this.PickButton.onclick = function(evt) {PickDate(evt); return false;};
        //this.PickButton.onclick = this.PickDate.bindAsEventListener(this)
        //Event.observe(this.PickButton, 'click', this.PickDate.bind(this), false);
        
        $(this.id + 'Day').onfocus = this.focus.bindAsEventListener(this);
        //Event.observe($(this.id + 'Day'), 'keyUp', function(){alert('foo');}, false);
        $(this.id + 'Month').onfocus = this.focus.bindAsEventListener(this);
        $(this.id + 'Year').onfocus = this.focus.bindAsEventListener(this);
        $(this.id + 'Separator1').onfocus = this.focus.bindAsEventListener(this);
        $(this.id + 'Separator2').onfocus = this.focus.bindAsEventListener(this);
        
        $(this.id + 'Day').onkeyup = this.keyUp.bindAsEventListener(this);
        $(this.id + 'Month').onkeyup = this.keyUp.bindAsEventListener(this);
        $(this.id + 'Year').onkeyup = this.keyUp.bindAsEventListener(this);
        $(this.id + 'Separator1').onkeyup = this.keyUp.bindAsEventListener(this);
        $(this.id + 'Separator2').onkeyup = this.keyUp.bindAsEventListener(this);
        
        $(this.id + 'Day').onblur = this.blur.bindAsEventListener(this);
        $(this.id + 'Month').onblur = this.blur.bindAsEventListener(this);
        $(this.id + 'Year').onblur = this.blur.bindAsEventListener(this);
        $(this.id + 'Separator1').onblur = this.blur.bindAsEventListener(this);
        $(this.id + 'Separator2').onblur = this.blur.bindAsEventListener(this);
        
        switch (this.props.dateFormat)
        {
            case "DayMonthYear":
                this.firstCell = $(this.id + 'Day');
                this.secondCell = $(this.id + 'Month');
                this.thirdCell = $(this.id + 'Year');
                break;
            case "MonthDayYear":
                this.firstCell = $(this.id + 'Month');
                this.secondCell = $(this.id + 'Day');
                this.thirdCell = $(this.id + 'Year');
                break;
            case "YearMonthDay":
                this.firstCell = $(this.id + 'Year');
                this.secondCell = $(this.id + 'Month');
                this.thirdCell = $(this.id + 'Day');
                break;
        }
        
        
        var today = new Date();
        today = today.format('yyyy.MM.dd');
        if (this.props.date)
        {
            if (!this.SetValue(this.props.date))                
                this.SetValue(today);
        }
        else if ($(this.id).value != null && $(this.id).value != '')
        {
            if (!this.SetValue($(this.id).value))                
                this.SetValue(today);
        }
        else
            this.SetValue(today);
        
    },
    
    PickDate: function(evt)
    {
        /*if (!$(this.id + 'CalendarLayer'))
        {
            var elem = Event.element(evt);
            this.layerPicker = document.createElement('div');
            this.layerPicker.id = this.id + 'CalendarLayer';
            this.layerPicker.className = 'DatePickerCalendarLayer';
            var pos = Position.cumulativeOffset(elem);
            this.layerPicker.style.top = (pos[1] + elem.offsetHeight + 5) + 'px';
            this.layerPicker.style.left = (pos[0] - 128 + elem.offsetWidth) + 'px';
            document.body.appendChild(this.layerPicker);
            var divCalendar = document.createElement('div');
            divCalendar.className = 'Calendar';
            divCalendar.id = this.id + 'Calendar';
            this.layerPicker.appendChild(divCalendar);
            var calendar = new Calendar(divCalendar.id, {onSelect: this.DateSelect.bind(this)});
            Nifty('div#' + this.layerPicker.id);
        }
        else
            document.body.removeChild(this.layerPicker);*/
        if (this.calendarTooltip != null)
        {
            if (this.calendarTooltip.visibility == 'visible')
                this.calendarTooltip.hide();
            else if (this.calendarTooltip.visibility == 'hidden')
                this.calendarTooltip.show();
        }
        else
        {
            var button = this.PickButton;//Event.element(evt);
            var calendarTooltip = new Tooltip(
                this.id + 'CalendarTooltip', 
                {
                    width: 138,
                    height: 148,
                    reference: this.id + 'DatePickerButtonContainer',
                    align: 'bottomleft'
                }
            );
            var divCalendar = document.createElement('div');
            divCalendar.className = 'Calendar';
            divCalendar.id = this.id + 'Calendar';
            calendarTooltip.element.appendChild(divCalendar);
            var calendar = new Calendar(divCalendar.id, {onSelect: this.DateSelect.bind(this)});
            //button.onclick = calendarTooltip.show;
            this.calendarTooltip = calendarTooltip;
        }
        //return false;
    },
    
    DateSelect: function(date)
    {
        this.SetValue(date);
        if (this.props.onSelect)
            this.props.onSelect(date);
        //document.body.removeChild(this.layerPicker);
        this.calendarTooltip.hide();
    },
    
    keyUp: function(evt)
    {
        var input = Event.element(evt);
        input.value = input.value.replace(/([^0-9])/g,"");
        
        
        
        if ((input.value.length == 2 && input.id != this.id + 'Year') || (input.value.length == 4 && input.id == this.id + 'Year'))
        {
            if (evt.keyCode != 9)
            {
                var maxValue;
                switch (input.id)
                {
                    case this.id + 'Day':
                        maxValue = 31;
                        break;
                    case this.id + 'Month':
                        maxValue = 12;
                        break;
                    case this.id + 'Year':
                        maxValue = 2100;
                        break;
                }
                if (parseInt(input.value) > maxValue)
                    input.value = maxValue;
                //if (input == this.thirdCell)
                    this.validateInput();
                    
                switch (input)
                {
                    case this.firstCell:
                        elem = this.secondCell;
                        break;
                    case this.secondCell:
                        elem = this.thirdCell;
                        break;
                    case this.thirdCell:
                        elem = this.firstCell;
                        break;
                }
                elem.focus();
                //input.blur();
            }
            else
                input.select();
        }
        
        //$('display').innerHTML = input.value + ' ' + evt.keyCode;
    },
    
    focus: function(evt)
    {//alert("foo");
        var input = Event.element(evt);
        if (input.id == this.id + 'Separator1')
        {
            $(this.id + 'Month').focus();
            return false;
        }
        else if (input.id == this.id + 'Separator2')
        {
            $(this.id + 'Year').focus();
            return false;
        }
        input.select();
        //$('display').innerHTML += '<br />focus';
    },
    
    blur: function(evt)
    {
        var input = Event.element(evt);
        
        var elem;
        
        
        if (input.value.length == 1 && input.value < 10)
            input.value = "0" + input.value;
        if (input.id == this.id + 'Year')
        {   
            if (input.value.length == 2)
                input.value = "20" + input.value;
            else if (input.value.length == 3)
                input.value = "1" + input.value;
        }
        //if (input == this.thirdCell)
            this.validateInput();
        //this.value = $(this.id + 'Hour').value + this.props.timeSeparator + $(this.id + 'Minute').value;
        //elem.focus();
    },
    
    SetValue: function(val)
    {
        if (typeof(val) != "string" && val.length != 10)
            return false;
        var regexp = new RegExp(/[0-2][0-9][0-9][0-9][^0-9][0-1][0-9][^0-9][0-3][0-9]/);
        if (!regexp.test(val))
            return false;
        $(this.id + 'Year').value = val.substring(0,4);
        $(this.id + 'Month').value = val.substring(5,7);
        $(this.id + 'Day').value = val.substring(8,10);
        this.value = val;
        
        
        //$(this.id + 'Container').firstChild.value = val;
        //this.Text = val;
        $(this.id).value = val;
        return true;
    },
    
    validateInput: function()
    {
        var dateSeparator = this.props.dateSeparator;
        var dateFormat, dateText;
        switch (this.props.dateFormat)
        {
            case "DayMonthYear":
                dateFormat = 'dd' + dateSeparator + 'MM' + dateSeparator + 'yyyy';
                dateText = $(this.id + 'Day').value + dateSeparator + $(this.id + 'Month').value + dateSeparator + $(this.id + 'Year').value;
                break;
            case "MonthDayYear":
                dateFormat = 'MM' + dateSeparator + 'dd' + dateSeparator + 'yyyy';
                dateText = $(this.id + 'Month').value + dateSeparator + $(this.id + 'Day').value + dateSeparator + $(this.id + 'Year').value;
                break;
            case "YearMonthDay":
                dateFormat = 'yyyy' + dateSeparator + 'MM' + dateSeparator + 'dd';
                dateText = $(this.id + 'Year').value + dateSeparator + $(this.id + 'Month').value + dateSeparator + $(this.id + 'Day').value;
                break;
        }
        if (!Date.isValid(dateText, dateFormat))
        {
            
            var month = $(this.id + 'Month').value;
            var date = Date.parseString($(this.id + 'Year').value + '-' + month + '-01','yyyy-MM-dd');
            var date = date.add('M', 1);
            var date = date.add('d', -1);
            var dayCountForThisMonth = date.getDate();
            $(this.id + 'Day').value = dayCountForThisMonth;
        }
        //$(this.id).value = $(this.id + 'Year').value + dateSeparator + $(this.id + 'Month').value + dateSeparator + $(this.id + 'Day').value;
        var val = $(this.id + 'Year').value + dateSeparator + $(this.id + 'Month').value + dateSeparator + $(this.id + 'Day').value;
        this.SetValue(val);
        if (this.props.onSelect)
            this.props.onSelect($(this.id).value);
    }
}
