PartyCalendar = new ( Class.create({
    
    Config : {},
    PartyData : null,
    CurrentDate : new Date(),
    SelectedDate : new Date(),
    Month : ["Január", "Február", "Március", "Április", "Május", "Június", "Július", "Augusztus", "Szeptember", "Október", "November", "December"],
    Week : ["Vasárnap", "Hétfő", "Kedd", "Szerda", "Csütörtök", "Péntek", "Szombat"],
    ShortWeek : ["V", "H", "K", "Sz", "Cs", "P", "Sz"],
    Height : 0,
    CellWidth : 0,
    TableCellContents : new Array(),
    RowsNumber : 0,
    
    initialize : function()
    {
    },
    
    PartyCalendarButtonClicked : function()
    {
        this.Config = { container: "content", role: AccountRole };
        this.Show();
    },
    
    Show : function()
    {
        this.Height = 200;
        this.CellWidth = 50;
        this.PartyData = ActionHandler.GetResponse(
        {
            url: "Components/PartyCalendar/PartyCalendarActionHandler.php",
            action: "GetCalendarData",
            parameters: "year=" + this.CurrentDate.getFullYear() + "&month=" + (this.CurrentDate.getMonth() + 1)
        })
        
        var date = new Date(this.CurrentDate.toDateString()); date.setDate(1);
        var dayOfWeek = date.getDay() + 7;
        var row;
        var today = new Date();
        var height = document.viewport.getHeight() - $(this.Config.container).viewportOffset().top - 30 - 35 - 10; // - header row - button line - bottom padding
        if(this.Height < height) this.Height = height;
        var cellWidth = Math.floor(($(this.Config.container).getWidth() - 10) / 7 - 2);
        if(this.CellWidth < cellWidth) this.CellWidth = cellWidth;
        this.TableCellContents.clear();
        this.RowsNumber = 0;
        
        // Clean the parent element
        $(this.Config.container).update("");
        
        // Current date and navigation buttons
        row = new Element("div");
        row.className = "partyCalendarButtonLine";
        row.insert(this.GetButton("&lt;", "button", "partyPrevious"));
        row.insert(this.GetButton("Ugrás a mai napra", "button", "partyToday"));
        row.insert(this.GetButton("&gt;", "button", "partyNext"));
        row.insert('<span>' + this.CurrentDate.getFullYear() + '. ' + this.Month[this.CurrentDate.getMonth()] + '</span>');
        $(this.Config.container).insert(row);
        
        // Calendar table
        var table = new Element("table");
        
        // Calendar header - shortWeeks
        var tr = new Element("tr");
        for(i = 1; i < 7; i++)
        {
            var th = new Element("th");
            th.setStyle({top: "0px"});
            th.setStyle({width: this.CellWidth + "px"});
            th.update(this.ShortWeek[i]);
            tr.insert(th);
        }
        var th = new Element("th");
        th.setStyle({top: "0px"});
        th.setStyle({width: this.CellWidth + "px"});
        th.update(this.ShortWeek[0]);
        tr.insert(th);
        table.insert(tr);
        
        tr = new Element("tr");
        
        // Add empty cells if necessary
        for(j = 0; j < (dayOfWeek-1) % 7; j++) { tr.insert(this.GetTableCell(null, null)); }
        
        // Add day cells to the calendar
        while(this.CurrentDate.getMonth() == date.getMonth())
        {
            var tableCell = this.GetTableCell(date.getDate(), this.PartyData[date.getDate()]);
            //if(Utils.IsDateEqual(date, this.SelectedDate)) { tableCell.setStyle({backgroundColor: "#ffffcc" });}
            tr.insert(tableCell);
            
            if( (dayOfWeek-1) % 7 == 6 )
            {
                table.insert(tr);
                tr = new Element("tr");
                var tmpDate = new Date(this.CurrentDate);
                tmpDate.setDate(date.getDate() + 1);
                if(this.CurrentDate.getMonth() == tmpDate.getMonth())
                {
                    this.RowsNumber++;
                }
            }
            
            date.setDate(date.getDate() + 1);
            dayOfWeek++;
        }
        
        // Add empty cells if necessary
        while((dayOfWeek-1) % 7 != 0) { tr.insert(this.GetTableCell(null, null)); dayOfWeek++; }
        
        table.insert(tr);
        this.RowsNumber++;
        
        this.SetCellsContentHeight();
        
        $(this.Config.container).insert('<table class="partyCalendar" cellSpacing="0" cellPadding="0">' + table.innerHTML + '</table>');
        
        this.AddEvents();
    },
        
        
    AddEvents : function()
    {
        Event.observe('partyPrevious', 'click', this.PreviousClick.bindAsEventListener(this));
        Event.observe('partyNext', 'click', this.NextClick.bindAsEventListener(this));
        Event.observe('partyToday', 'click', this.TodayClick.bindAsEventListener(this));
        
        var date = new Date(this.CurrentDate.toDateString()); date.setDate(1);
        while(this.CurrentDate.getMonth() == date.getMonth())
        {
            
            Event.observe('cellContent' + date.getDate(), 'click', this.DayClick.bindAsEventListener(this));
            date.setDate(date.getDate() + 1);
        }
    },
    
    GetButton : function(text, className, id)
    {
        var cell = new Element("button");
        cell.className = className;
        if(id != null) cell.id = id;
        if(text != null) cell.update(text);
        else cell.update("&nbsp;");
        return cell;
    },
    
    GetTableCell : function(day, reminderData)
    {
        var td = new Element("td");
        var cellContent = new Element("div");
        cellContent.className = "cellContent";
        if(day != null) cellContent.id = "cellContent" + day.toString();
        cellContent.setStyle({width: this.CellWidth - 5 + "px", float: "left", overflow: "hidden"});
        
        if(day == null) { cellContent.insert('<div class="cellHeader"><span>&nbsp;</span></div>'); }
        else { cellContent.insert('<div class="cellHeader"><span>' + day + '</span></div>'); }
        
        if(reminderData != null)
        {
            for(var i = 0; i < reminderData.length; i++)
            {
                var text = reminderData[i].clientName;
                if(reminderData[i].event_description != null)
                {
                    text += " - " + reminderData[i].event_description;
                }
                
                cellContent.insert('<span>' + text + '</span>');
            }
        }
        this.TableCellContents[this.TableCellContents.length] = cellContent;
        td.insert(cellContent);
        return td;
    },
    
    SetCellsContentHeight: function()
    {
        var cellHeight = Math.floor(this.Height / this.RowsNumber);
        for(var i = 0; i < this.TableCellContents.length; i++)
        {
            this.TableCellContents[i].setStyle({height: cellHeight - 10 + "px" });
        }
    },
    
    PreviousClick : function(event)
    {
        this.CurrentDate.setMonth(this.CurrentDate.getMonth() - 1, 1);
        this.Show();
    },
    
    NextClick : function(event)
    {
        this.CurrentDate.setMonth(this.CurrentDate.getMonth() + 1, 1);
        this.Show();
    },
    
    TodayClick : function(event)
    {
        this.CurrentDate = new Date();
        this.SelectedDate = new Date();
        this.Show();
    },
    
    DayClick : function(event)
    {
        clickedElement = Event.element(event);
        if(clickedElement.nodeName == "SPAN") clickedElement = clickedElement.parentNode;
        
        var day = ((clickedElement.id).toString()).replace("cellContent", "");
        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)
    {
        //alert(clickedDate);
        
        
        //this.config.reminder.config.selectedDate = clickedDate;
        //this.config.reminder.Show();
    }
    
    /* MODIFIABLE SECTION - End --------------------------------------------------*/

}))();
