News = Class.create();
Object.extend(News.prototype, WebControl.prototype);

Object.extend(News.prototype,
    {
        SelectedCategory : 0,
        
        initialize : function(config)
        {
            WebControl.prototype.initialize.call(this, config);
            var thisClass = this;
            
            this.config.Show = function()
            {
                GlobalLoading.Start();
                
                $(thisClass.config.container).update("");
                
                thisClass.config.categories = ActionHandler.GetResponse(
                {
                    url: "Components/News/NewsActionHandler.php",
                    action: "GetCategories"
                });
                
                thisClass.config.newsData = ActionHandler.GetResponse(
                {
                    url: "Components/News/NewsActionHandler.php",
                    action: "GetNewsData"
                });
                
                // MENU
                var menu = new Element("div");
                menu.className = "newsMenu";
                $(thisClass.config.container).update("");
                var length = thisClass.config.categories.length;
                for(var i = 0; i < length; i++)
                {
                    menu.insert('<a id="newsCategoryButton' + i + '" class="button">' + thisClass.config.categories[i].name + '</a>');
                }
                $(thisClass.config.container).insert(menu);
                
                thisClass.LoadSeletedCategory();
                
                GlobalLoading.Stop();
            }
            
            this.config.AddEvents = function()
            {
                var length = thisClass.config.categories.length;
                for(var i = 0; i < length; i++)
                {
                    Event.observe("newsCategoryButton" + i, "click", thisClass.ButtonClicked.bindAsEventListener(thisClass));
                }
            }
        },
        
        ButtonClicked: function(event)
        {
            Event.stop(event);
            
            GlobalLoading.Start();
            
            this.SelectedCategory = parseInt(Event.element(event).id.gsub("newsCategoryButton", ""));
            this.LoadSeletedCategory();
            
            GlobalLoading.Stop();
        },
        
        LoadSeletedCategory: function()
        {
            // CATEGORY
            var category = new Element("div");
            category.id = "newsCategory";
            category.className = "newsCategory";
            
            var title = new Element("div");
            title.className = "title";
            title.update("<h1>" + this.config.categories[this.SelectedCategory].name + "</h1>");
            category.insert(title);
            var newsLength = this.config.newsData[this.SelectedCategory + 1].length;
            for(var j = 0; j < newsLength; j++)
            {
                var line = new Element("div")
                line.className = "line";
                
                var link = new Element("a");
                link.href = this.config.newsData[this.SelectedCategory + 1][j].link;
                link.target = "_blank";
                link.update(this.config.newsData[this.SelectedCategory + 1][j].title);
                line.insert(link);
                category.insert(line);
                
                // Second condition is for the stupid IE
                if(!this.config.newsData[this.SelectedCategory + 1][j].description.blank() &&
                   this.config.newsData[this.SelectedCategory + 1][j].description != '\u00a0')
                {
                    var line = new Element("div")
                    line.className = "line";
                    line.insert("<p>" + this.config.newsData[this.SelectedCategory + 1][j].description + "</p>");
                    category.insert(line);
                }
            }
            
            if($("newsCategory") != null)
            {
                $("newsCategory").remove();
            }
            $(this.config.container).insert(category);
            
            var count = Math.floor((document.viewport.getWidth() - 250) / 140);
            var newsWidth = (count * 130 - 12) + "px";
            $("newsCategory").setStyle({width: newsWidth });
        }
    }
);

