package as_components
{
    import mx.containers.HBox;
    import mx.controls.ComboBox;
    import mx.events.FlexEvent;
    import mx.events.ListEvent;
    import mx.formatters.DateFormatter;

    [Bindable]
    public class ComboBoxCodeBehind extends HBox
    {
        public var months:Array = new Array();
        public var dateformatter:DateFormatter = new DateFormatter();
        public var selectedIndex:int;
        public var days:Array;
        public var years:Array = new Array();
        public var monthsCB:ComboBox;
        public var daysCB:ComboBox;
        public var yearsCB:ComboBox;
        public var selectedIndexYear:int;
        public var selectedIndexMonth:int;
        public var selectedIndexDay:int;
        public var thisYear:int;


        public function ComboBoxCodeBehind()
        {
            super();
            init();
            this.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
        }

        private function init():void{
            var i:int;
            var now:Date = new Date();

            var currentMonth:int = now.getMonth();
            var currentYear:int = now.getFullYear();
            var currentDay:int = now.getDate();

            thisYear = currentYear;
            dateformatter.formatString = "MMMM";

            for (i=0; i<12; i++){
                now.setMonth(i);
                months[i] = dateformatter.format(now);
            }

            setYears(currentYear);
            setDays(currentMonth);

            selectedIndexMonth = currentMonth;
            selectedIndexYear = years.indexOf(currentYear);
            selectedIndexDay = days.indexOf(currentDay);
        }

        private function creationCompleteHandler(event:FlexEvent):void{
            monthsCB.addEventListener(ListEvent.CHANGE,monthsHandler);
            yearsCB.addEventListener(ListEvent.CHANGE, yearsHandler);
        }


        private function setDays(month:int):void{
            days = new Array();
            var i:int;

            if (month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11){
                for(i=1; i<=31; i++)
                days.push(i);
            }else if (month == 3 || month == 5 || month == 8 || month == 10){
                for(i=1; i<=30; i++)
                days.push(i);
            }else if (month == 1){
               if (thisYear % 4 == 0){
                    for(i=1; i<=29; i++)
                    days.push(i);
                }else{
                    for(i=1; i<=28; i++)
                    days.push(i);
                }
            }
        }

        private function setYears(year:int):void{
            var j:int;
            for(j=1950; j<=year; j++){
                years.push(j);
            }
        }

        private function monthsHandler(event:ListEvent):void{
            setDays(event.currentTarget.selectedIndex);
            selectedIndexDay = 0;
        }

        private function yearsHandler(event:ListEvent):void{
            if (monthsCB.selectedIndex == 1){
                var selectedDay:int = daysCB.selectedIndex;
                thisYear = event.currentTarget.selectedItem as int;
                setDays(monthsCB.selectedIndex);
                selectedIndexDay = selectedDay;
            }
        }
    }
}
