Calendar = Class.create();
Object.extend(Calendar.prototype, WebControl.prototype);

Object.extend(Calendar.prototype,
    {
        initialize : function(config)
        {
            WebControl.prototype.initialize.call(this, config);
            
            this.role = this.config.role;
            this.currentDate = new Date();
            this.selectedDate = new Date();
            this.month = ["Január", "Február", "Március", "Április", "Május", "Június", "Július", "Augusztus", "Szeptember", "Október", "November", "December"];
            this.week = ["Vasárnap", "Hétfő", "Kedd", "Szerda", "Csütörtök", "Péntek", "Szombat"];
            this.shortWeek = ["V", "H", "K", "Sz", "Cs", "P", "Sz"];
            
            var thisClass = this;
            
            // Show
            this.config.Show = function()
            {
                thisClass.reminderData = ActionHandler.GetResponse(
                {
                    url: "Components/Reminder/ReminderActionHandler.php",
                    action: "GetReminderData",
                    parameters: "year=" + thisClass.currentDate.getFullYear() + "&month=" + (thisClass.currentDate.getMonth() + 1)
                })
                
                var date = new Date(thisClass.currentDate.toDateString()); date.setDate(1);
                var dayOfWeek = date.getDay() + 7;
                var row;
                var today = new Date();
                
                // Clean the parent element
                $(thisClass.config.container).update("");
                
                // HEADER
                row = new Element("div");
                row.className = "row";
                row.insert(thisClass.GetTableCell(thisClass.currentDate.getFullYear() + ". " + thisClass.month[thisClass.currentDate.getMonth()], "currentButton", "current"));
                $(thisClass.config.container).insert(row);
                
                row = new Element("div");
                row.className = "row";
                row.insert(thisClass.GetTableCell("&lt;", "previousButton", "previous"));
                row.insert(thisClass.GetTableCell("Ugrás a mai napra", "todayButton", "today"));
                row.insert(thisClass.GetTableCell("&gt;", "nextButton", "next"));
                $(thisClass.config.container).insert(row);
                
                row = new Element("div");
                for(i = 1; i < 7; i++)
                {
                    row.insert(thisClass.GetTableCell(thisClass.shortWeek[i], "shortWeek"));
                }
                row.insert(thisClass.GetTableCell(thisClass.shortWeek[0], "shortWeekRight"));
                $(thisClass.config.container).insert(row);
                
                // DAY BUTTONS
                row = new Element("div");
                row.className = "row";
                
                // Add empty cells if necessary
                for(j = 0; j < (dayOfWeek-1) % 7; j++)
                {
                    if( (j-1) % 7 == 6 ) row.insert(thisClass.GetTableCell(null, "rightEmptyCell"));
                    else row.insert(thisClass.GetTableCell(null, "emptyCell"));
                }
                
                // Add day cells to the calendar
                while(thisClass.currentDate.getMonth() == date.getMonth())
                {
                    var hasEvent = thisClass.reminderData[date.getDate()] > 0;
                    
                    if( (dayOfWeek-1) % 7 == 6 )
                    {
                        if(Utils.IsDateEqual(date, thisClass.selectedDate))
                        {
                            if(hasEvent) row.insert(thisClass.GetTableCell(date.getDate().toString(), "rightMarkedCellSelected", "comfyCalendarDay" + date.getDate().toString()));
                            else row.insert(thisClass.GetTableCell(date.getDate().toString(), "rightCellSelected", "comfyCalendarDay" + date.getDate().toString()));
                        }
                        else
                        {
                            if(hasEvent) row.insert(thisClass.GetTableCell(date.getDate().toString(), "rightMarkedCell", "comfyCalendarDay" + date.getDate().toString()));
                            else row.insert(thisClass.GetTableCell(date.getDate().toString(), "rightCell", "comfyCalendarDay" + date.getDate().toString()));
                        }
                        $(thisClass.config.container).insert(row);
                        row = new Element("div");
                        row.className = "row";
                    }
                    else
                    {
                        if(Utils.IsDateEqual(date, thisClass.selectedDate))
                        {
                            if(hasEvent) row.insert(thisClass.GetTableCell(date.getDate().toString(), "markedCellSelected", "comfyCalendarDay" + date.getDate().toString()));
                            else row.insert(thisClass.GetTableCell(date.getDate().toString(), "cellSelected", "comfyCalendarDay" + date.getDate().toString()));
                        }
                        else
                        {
                            if(hasEvent) row.insert(thisClass.GetTableCell(date.getDate().toString(), "markedCell", "comfyCalendarDay" + date.getDate().toString()));
                            else row.insert(thisClass.GetTableCell(date.getDate().toString(), "cell", "comfyCalendarDay" + date.getDate().toString()));
                        }
                    }
                    date.setDate(date.getDate() + 1);
                    dayOfWeek++;
                }
                
                // Add empty cells if necessary
                while((dayOfWeek-1) % 7 != 0)
                {
                    if( (dayOfWeek-1) % 7 == 6 ) row.insert(thisClass.GetTableCell(null, "rightEmptyCell"));
                    else row.insert(thisClass.GetTableCell(null, "emptyCell"));
                    dayOfWeek++;
                }
                
                $(thisClass.config.container).insert(row);
            }
            
            // Add Events
            this.config.AddEvents = function()
            {
                Event.observe('previous', 'click', thisClass.PreviousClick.bindAsEventListener(thisClass));
                Event.observe('next', 'click', thisClass.NextClick.bindAsEventListener(thisClass));
                Event.observe('today', 'click', thisClass.TodayClick.bindAsEventListener(thisClass));
                
                i = 1;
                while(document.getElementById("comfyCalendarDay" + i.toString()) != null)
                {
                    Event.observe("comfyCalendarDay" + i.toString(), 'click', thisClass.DayClick.bindAsEventListener(thisClass));
                    i++;
                }
            };
        },
        
        GetTableCell : function(text, className, id)
        {
            var cell = new Element("a");
            cell.className = className;
            if(id != null) cell.id = id;
            if(text != null) cell.innerHTML = text;
            else cell.innerHTML = "&nbsp;";
            return cell;
        },
        
        PreviousClick : function(event)
        {
            this.currentDate.setMonth(this.currentDate.getMonth() - 1);
            this.Show();
        },
        
        NextClick : function(event)
        {
            this.currentDate.setMonth(this.currentDate.getMonth() + 1);
            this.Show();
        },
        
        TodayClick : function(event)
        {
            this.currentDate = new Date();
            this.selectedDate = new Date();
            this.Show();
            
            this.DayClickAction(this.selectedDate);
        },
        
        DayClick : function(event)
        {
            var day = ((Event.element(event).id).toString()).replace("comfyCalendarDay", "");
            var clickedDate = new Date(this.currentDate.toDateString()); clickedDate.setDate(day);
            this.selectedDate = clickedDate;
            this.Show();
            
            this.DayClickAction(clickedDate);
        },
        
        /* MODIFIABLE SECTION - Start ------------------------------------------------*/

        /*
            This function is called if a click event was realized on
            a day button.
            You can add your code to this function.
        */
        DayClickAction : function(clickedDate)
        {
            this.config.reminder.config.selectedDate = clickedDate;
            this.config.reminder.Show();
        }
        
        /* MODIFIABLE SECTION - End --------------------------------------------------*/
    }
);

