Icon = new ( Class.create({
    
    initialize : function()
    {
    },
    
    LockFunctions : false,
    
    CurrentIconID : -1,
    
    IconWindow : new Window({
        container: "window",
        title: "Új ikon felvétele",
        width: 400,
        contentURL: "Components/Icon/NewIcon.php"
    }),
    
    GetNewIconWindow : function(event) { Event.stop(event); this.GetIconWindow("new") },
    
    GetIconWindow : function(type)
    {   
        var thisClass = this;
        
        if(type == "modify")
        {
            this.IconWindow.config.title = "Ikon módosítása";
        }
        
        this.IconWindow.Show();
        
        $("newIconCategory").update(IconCategory.GenerateOptionTags());
        
        if(type == "new")
        {
            $("newIconNumber").value = 1;
            Event.observe("iconSave", "click", this.CheckAndSaveIcon.bindAsEventListener(this));
        }
        if(type == "modify")
        {
            Event.observe("iconSave", "click", this.ModifyIcon.bindAsEventListener(this));
        }
        
        Event.observe("iconCancel", "click", function(event) { Event.stop(event); thisClass.IconWindow.Close(); } );
        
        $("newIconURL").focus();
    },
    
    CheckAndSaveIcon : function(event)
    {
        Event.stop(event);
        
        if(!this.LockFunctions)
        {
            this.LockFunctions = true;
            if(Utils.IsValidString($("newIconURL").value, "URL"))
            {
                // SAVE ICON
                var pars = "iconURL=" + escape($("newIconURL").value);
                    pars += "&iconName=" + $("newIconName").value;
                    pars += "&iconNumber=" + $("newIconNumber").value;
                    pars += "&iconCategory=" + $("newIconCategory").value;
                ActionHandler.GetResponse(
                {
                    url: "Components/Icon/IconActionHandler.php",
                    action: "SaveNewIcon",
                    parameters: pars
                });
                this.IconWindow.Close();
                IconView.LoadTo("content");
                this.LockFunctions = false;
            }
            else
            {
                $("newIconUrlMessage").update("Helytelen cím(URL)");
                $("newIconUrlMessage").setStyle( {color : "red"} );
                this.LockFunctions = false;
            }
        }
    },
    
    IconButtonClicked: function(event)
    {
        var id = Event.element(event).id.gsub("iconEditImage");
        var pars = "iconID=" + id;
        ActionHandler.GetResponse(
        {
            url: "Components/Icon/IconActionHandler.php",
            action: "IconClicked",
            parameters: pars
        });
    },
    
    ModifyIconClicked : function(event)
    {
        Event.stop(event);
        
        this.CurrentIconID = Event.element(event).alt;
        
        var pars = "iconID=" + this.CurrentIconID;
            pars += "&iconCategory=" + IconCategory.SelectedCategory.id;
        var iconData = ActionHandler.GetResponse(
        {
            url: "Components/Icon/IconActionHandler.php",
            action: "GetIconData",
            parameters: pars
        });
        
        this.GetIconWindow("modify");
        
        $("newIconURL").value = iconData.url;
        $("newIconName").value = iconData.name.gsub("&nbsp;"," ").gsub("null", "");
        $("newIconNumber").value = iconData.number;
        $("newIconCategory").value = iconData.category;
    },
    
    ModifyIcon : function(event)
    {
        Event.stop(event);
        
        if(!this.LockFunctions)
        {
            this.LockFunctions = true;
            var pars = "&iconID=" + this.CurrentIconID;
                pars += "&iconURL=" + escape($("newIconURL").value);
                pars += "&iconName=" + $("newIconName").value;
                pars += "&iconNumber=" + $("newIconNumber").value;
                pars += "&iconCategoryOld=" + IconCategory.SelectedCategory.id;
                pars += "&iconCategoryNew=" + $("newIconCategory").value;
            ActionHandler.GetResponse(
            {
                url: "Components/Icon/IconActionHandler.php",
                action: "UpdateIcon",
                parameters: pars
            });
            this.IconWindow.Close();
            IconView.LoadTo("content");
            this.LockFunctions = false;
        }
    },
    
    DeleteIconClicked : function(event)
    {
        Event.stop(event);
        
        this.CurrentIconID = Event.element(event).alt;
        
        var confirmWindow = new Window({
            container: "window",
            title: "Ikon törlés",
            width: 400,
            contentURL: "Components/Icon/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.DeleteIcon(); confirmWindow.Close(); });
    },
    
    DeleteIcon : function()
    {
        var pars = "iconID=" + this.CurrentIconID;
            pars += "&iconCategory=" + IconCategory.SelectedCategory.id;
        ActionHandler.GetResponse(
        {
            url: "Components/Icon/IconActionHandler.php",
            action: "DeleteIcon",
            parameters: pars
        });
        IconView.LoadTo("content");
    }
    
}))();

