IconCategory = new ( Class.create({
    
    initialize : function()
    {
    },
    
    Categories : Array(),
    
    CurrentIconCategoryID : 0,
    
    SelectedCategory : { id: 0, name: "" },
    
    LockFunctions : false,
    
    NewIconCategoryWindow : new Window({
        container: "window",
        title: "Új kategória felvétele",
        width: 500,
        contentURL: "Components/IconCategory/NewIconCategory.php"
    }),
    
    ModifyIconCategoryWindow : new Window({
        container: "window",
        title: "Kategória szerkesztése",
        width: 500,
        contentURL: "Components/IconCategory/ModifyIconCategory.php"
    }),
    
    Generate : function()
    {
        var thisClass = this;
        
        if(Object.isElement($("categoriesPopupMenu")))
        {
            thisClass.Categories.clear();
            var i = 0;
            $$('.categoryPopupMenu').each(
                function(element)
                {
                    thisClass.Categories[i++] = { id: element.value, name:element.innerHTML};
                    if(element.value == $("currentCategory").value)
                    {
                        $("categoriesButton").update("Kategória (" + element.innerHTML + ")");
                        thisClass.SelectedCategory = thisClass.Categories[i-1];
                        thisClass.CurrentIconCategoryID = element.value;
                    }
                }
            );
            
            
            $("categoriesPopupMenu").update("");
            thisClass.Categories.each( function(element) {
                var menuItem = '<div class="line">';
                    menuItem += '<a class="categoryLink" id="iconCategoryButton' + element.id + '" href="#">' + element.name + '</a>';
                    if(element.id != 0) { menuItem += '<a class="editLink" id="iconCategoryEditButton' + element.id + '" href="#">szerkeszt</a>'; }
                    menuItem += '</div>';
                $("categoriesPopupMenu").insert(menuItem);
                Event.observe("iconCategoryButton" + element.id, "click", thisClass.CategoryClicked.bindAsEventListener(thisClass));
                if(element.id != 0) { Event.observe("iconCategoryEditButton" + element.id, "click", thisClass.CategoryEditClicked.bindAsEventListener(thisClass)); }
            });
        }
        Event.observe("categoriesButton", "mouseover", thisClass.CategoriesButtonMouseOver.bindAsEventListener(thisClass));
        Event.observe("iconView", "mouseover", thisClass.IconViewMouseOver.bindAsEventListener(thisClass));
    },
    
    GenerateOptionTags : function()
    {
        var thisClass = this;
        var options = "";
        this.Categories.each( function(element) {
            if(element.id != 0)
            {
                if(element.id == thisClass.SelectedCategory.id)
                {
                    options += '<option value="' + element.id + '" selected>' + element.name + '</option>';
                }
                else
                {
                    options += '<option value="' + element.id + '">' + element.name + '</option>';
                }
            }
        });
        return options;
    },
    
    CategoriesButtonMouseOver : function(event)
    {
        Event.stop(event);
        var button = Event.element(event);
        var scrollOffsets = document.viewport.getScrollOffsets();
        var menuTop = button.viewportOffset().top + button.getHeight() + scrollOffsets.top;
        var menuLeft = button.viewportOffset().left + + scrollOffsets.left;
        $("categoriesPopupMenu").setStyle( {display: "block", top: menuTop + "px", left: menuLeft + "px" } );
    },
    
    IconViewMouseOver : function(event)
    {
        if(!Utils.IsMouseInElement( $("categoriesPopupMenu"), { X: Event.pointerX(event), Y: Event.pointerY(event) } ) )
        {
            $("categoriesPopupMenu").setStyle( {display: "none" } );
        }
    },
    
    CategoryClicked : function(event)
    {
        Event.stop(event);
        var categoryID = Event.element(event).id.gsub("iconCategoryButton", "");
        if(categoryID == 0)
        {
            this.GetNewIconCategoryWindow();
        }
        else
        {
            var categoryName = Event.element(event).innerHTML;
            this.SelectedCategory = { id: categoryID, name: categoryName };
            IconView.LoadTo("content");
        }
    },
    
    CategoryEditClicked : function(event)
    {
        if(IsRoleSuccess())
        {
            Event.stop(event);
            this.CurrentIconCategoryID = Event.element(event).id.gsub("iconCategoryEditButton", "");
            this.GetModifyIconCategoryWindow( $("iconCategoryButton" + this.CurrentIconCategoryID).innerHTML );
        }            
        else
        {
            NoRightWindow.Show(event);
        }
    },
    
    GetNewIconCategoryWindow : function()
    {
        var thisClass = this;
        this.NewIconCategoryWindow.Show();
        Event.observe("IconCategorySave", "click", this.SaveIconCategory.bindAsEventListener(this));
        Event.observe("IconCategoryCancel", "click", function(event) { Event.stop(event); thisClass.NewIconCategoryWindow.Close(); } );
        $("newIconCategoryName").focus();
    },
    
    GetModifyIconCategoryWindow : function(categoryName)
    {
        var thisClass = this;
        this.ModifyIconCategoryWindow.Show();
        $("newIconCategoryName").value = categoryName;
        Event.observe("IconCategorySave", "click", this.ModifyIconCategory.bindAsEventListener(this));
        Event.observe("IconCategoryCancel", "click", function(event) { Event.stop(event); thisClass.ModifyIconCategoryWindow.Close(); } );
        if(this.CurrentIconCategoryID == 1)
        {
            Event.observe("IconCategoryDelete", "click", this.CannotDeleteClicked.bindAsEventListener(this));
        }
        else
        {
            Event.observe("IconCategoryDelete", "click", this.DeleteIconCategoryClicked.bindAsEventListener(this));
        }
        $("newIconCategoryName").focus();
    },
    
    SaveIconCategory : function(event)
    {
        Event.stop(event);

        if(!this.LockFunctions)
        {
            this.LockFunctions = true;
            if(Utils.IsValidString($("newIconCategoryName").value, "NotNullOrEmpty"))
            {
                // SAVE IconCategory
                var pars = "IconCategoryName=" + $("newIconCategoryName").value;
                ActionHandler.GetResponse(
                {
                    url: "Components/IconCategory/IconCategoryActionHandler.php",
                    action: "SaveNewIconCategory",
                    parameters: pars
                });
                this.NewIconCategoryWindow.Close();
                IconView.LoadTo("content");
                this.LockFunctions = false;
            }
            else
            {
                $("newIconCategoryNameMessage").update("Nem adtál meg kategória nevet");
                $("newIconCategoryNameMessage").setStyle( {color : "red"} );
                this.LockFunctions = false;
            }
        }
    },
    
    ModifyIconCategory : function(event)
    {
        Event.stop(event);
        
        if(!this.LockFunctions)
        {
            this.LockFunctions = true;
            var pars = "&IconCategoryID=" + this.CurrentIconCategoryID;
                pars += "&IconCategoryName=" + $("newIconCategoryName").value;
            ActionHandler.GetResponse(
            {
                url: "Components/IconCategory/IconCategoryActionHandler.php",
                action: "UpdateIconCategory",
                parameters: pars
            });
            this.ModifyIconCategoryWindow.Close();
            IconView.LoadTo("content");
            this.LockFunctions = false;
        }
    },
    
    DeleteIconCategoryClicked : function(event)
    {
        Event.stop(event);
        
        var confirmWindow = new Window({
            container: "window",
            title: "Kategória törlés",
            width: 400,
            contentURL: "Components/IconCategory/Confirm.php"
        });
        confirmWindow.Show();
        $("Cancel").focus();
        
        var thisClass = this;
        
        Event.observe("Cancel", "click", function(event) { Event.stop(event); confirmWindow.Close(); } );
        Event.observe("OK", "click", function(event) { Event.stop(event); thisClass.DeleteIconCategory(); confirmWindow.Close(); });
    },
    
    CannotDeleteClicked : function(event)
    {
        Event.stop(event);
        
        var confirmWindow = new Window({
            container: "window",
            title: "Információ",
            width: 400,
            contentURL: "Components/IconCategory/CannotDelete.php"
        });
        confirmWindow.Show();
        $("OK").focus();
        
        Event.observe("OK", "click", function(event) { Event.stop(event); confirmWindow.Close(); } );
    },
    
    DeleteIconCategory : function()
    {
        var pars = "IconCategoryID=" + this.CurrentIconCategoryID;
        ActionHandler.GetResponse(
        {
            url: "Components/IconCategory/IconCategoryActionHandler.php",
            action: "DeleteIconCategory",
            parameters: pars
        });
        if(this.CurrentIconCategoryID == this.SelectedCategory.id)
        {
            this.SelectedCategory = { id: 1, name: "" };
        }
        IconView.LoadTo("content");
    }
    
}))();

