        // Cool fading menu by ABTOMAT 2010 Version 1.1
        // 
        // Version 1.1:
        // - Fixed bug: when youre moving mouse on menu quickly, the last
        // menu item blinks.
        // - Improved self-debug: now when you're trying to set trigger or
        // container by name, but that one doesn't exist, script says about it.
        //
        // Version 1.0:
        // Initial release.
        
        function Menu()
        {
            this.container = null;
            this.trigger = null;
            this.items = [];
            this.alpha = 100;
            this.alphaPeriod = 10;
            this.itemsDelay = 100;
            // non-manageables
            this.status = 2;
            // 0 - hidden
            // 1 - showing
            // 2 - shown
            // 3 - hiding
            this.statusDelay = 0;
        }

        function MenuItem()
        {
            this.container = null;
            this.alpha = 100;
            this.alphaPeriod = 10;
            // non-manageables
            this.status = 2;
            // 0 - hidden
            // 1 - showing
            // 2 - shown
            // 3 - hiding
            this.statusDelay = 0;
        }

        MenuItem.prototype.setAlpha = function(value)
	{
	    if(value == 0) {
	        this.container.style.display = 'none';
	    } else {
	        this.container.style.display = 'block';
	    }

	    this.container.style.opacity = (value / 100);
	    this.container.style.MozOpacity = (value / 100);
	    this.container.style.KhtmlOpacity = (value / 100);
	    this.container.style.filter = "alpha(opacity=" + value + ")";
	}

        MenuItem.prototype.setAlphaPeriod = function(value)
        {
            this.alphaPeriod = value;
        }

        MenuItem.prototype.setContainer = function(container)
        {
            if(container != null)
                {
                    this.container = container;
                }
                else
                {
                    alert("Container you're trying to set is null!");
                }
        }

        MenuItem.prototype.hide = function(statusDelay)
        {
            if (statusDelay==null){statusDelay = 0;}
            this.statusDelay = statusDelay;
            this.status = 3;
            this.hiding()
        }

        MenuItem.prototype.hideImmediately = function()
        {
            this.status = 0;
            this.alpha = 1;
            this.setAlpha(1);
        }

        MenuItem.prototype.hiding = function()
        {
            if(this.status == 3)
            {
                var menuitem = this;
                if (this.statusDelay > 0)
                {
                    this.statusDelay -= this.alphaPeriod;
                }
                else
                {
                    this.alpha -= 5;
                }
                if (this.alpha <= 1)
                    {
                        this.alpha = 1;
                        this.status = 0;
                    }
                else
                    {
                        setTimeout(function(){menuitem.hiding()}, this.alphaPeriod);
                    }
                this.setAlpha(this.alpha);
            }
        }

        MenuItem.prototype.show = function(statusDelay)
        {
            if (statusDelay==null){statusDelay = 0;}
            this.statusDelay = statusDelay;
            this.status = 1;
            this.showing()
        }

        MenuItem.prototype.showImmediately = function()
        {
            this.status = 2;
            this.alpha = 100;
            this.setAlpha(100);
        }


        MenuItem.prototype.showing = function()
        {
            if(this.status == 1)
            {
                var menuitem = this;
                if (this.statusDelay > 0)
                {
                    this.statusDelay -= this.alphaPeriod;
                }
                else
                {
                    this.alpha += 5;
                }

                if (this.alpha >= 100)
                    {
                        this.alpha = 100;
                        this.status = 2;
                    }
                else
                    {
                        setTimeout(function(){menuitem.showing()}, this.alphaPeriod);
                    }
                this.setAlpha(this.alpha);
            }
        }

        Menu.prototype.setAlpha = function(value)
	{
	    if(value == 0) {
	        this.container.style.display = 'none';
	    } else {
	        this.container.style.display = 'block';
	    }

	    this.container.style.opacity = (value / 100);
	    this.container.style.MozOpacity = (value / 100);
	    this.container.style.KhtmlOpacity = (value / 100);
	    this.container.style.filter = "alpha(opacity=" + value + ")";
	}

        Menu.prototype.setItemsDelay = function(value)
        {
            this.itemsDelay = value;
        }

        Menu.prototype.setAlphaPeriod = function(value)
        {
            this.alphaPeriod = value;
            for (var i=0; i<this.items.length; i++)
            {
                this.items[i].setAlphaPeriod(value);
            }
        }

        Menu.prototype.setTrigger = function(trigger)
        {
            if(trigger != null)
                {
                    this.trigger = trigger;
                    var menu = this;
                    this.trigger.onmouseover = function()
                    {
                        menu.show();
                    }
                    this.trigger.onmouseout = function()
                    {
                        menu.hide(menu.itemsDelay*menu.items.length);
                    }
                }
                else
                {
                    alert("Trigger you're trying to set is null!");
                }
        }

        Menu.prototype.setTriggerById = function(triggerid)
        {
            var trigger = document.getElementById(triggerid);
            if (trigger!=null)
            {
                this.setTrigger(trigger);
            }
            else
            {
                alert("Trigger with id '"+triggerid+"' you are trying to set does not exist!");
            }            
        }

        Menu.prototype.setContainer = function(container)
        {
            if(container != null)
                {
                    this.container = container;
                    var elements = this.container.getElementsByTagName("*")

                    for (var i=0; i<elements.length; i++)
                    {
                        var menuItem = new MenuItem()
                        menuItem.setContainer(elements[i]);
                        this.items.push(menuItem);
                    }
                    var menu = this;
                    this.container.onmouseover = function()
                    {
                        menu.show();
                    }
                    this.container.onmouseout = function()
                    {
                        menu.hide(menu.itemsDelay*menu.items.length);
                    }

                    if(this.trigger!=null)
                    {
                        this.trigger.onmouseover = function()
                        {
                            menu.show();
                        }
                        this.trigger.onmouseout = function()
                        {
                            menu.hide(menu.itemsDelay*menu.items.length);
                        }
                    }

                    this.hideImmediately();
                }
                else
                {
                    alert("Container you're trying to set is null!");
                }
        }

        Menu.prototype.setContainerById = function(containerid)
        {
            var container = document.getElementById(containerid);
            if (container!=null)
            {
                this.setContainer(container);
            }
            else
            {
                alert("Container with id '"+containerid+"' you are trying to set does not exist!");
            }
        }

        Menu.prototype.hide = function(statusDelay)
        {
            if (statusDelay==null){statusDelay = 0;}
            this.statusDelay = statusDelay;
            this.status = 3;
            this.hiding()

            for (var i=0; i<this.items.length; i++)
            {
                this.items[i].hide((this.items.length-1-i)*this.itemsDelay+3);
            }
        }

        Menu.prototype.hideImmediately = function()
        {
            this.status = 0;
            this.alpha = 0;
            this.setAlpha(0);
            for (var i=0; i<this.items.length; i++)
            {
                this.items[i].hideImmediately();
            }
        }

        Menu.prototype.hiding = function()
        {
            if(this.status == 3)
            {
                var menu = this;
                if (this.statusDelay > 0)
                {
                    this.statusDelay -= this.alphaPeriod;
                }
                else
                {
                    this.alpha -= 5;
                }
                if (this.alpha <= 0)
                    {
                        this.alpha = 0;
                        this.status = 0;
                    }
                else
                    {
                        setTimeout(function(){menu.hiding()}, this.alphaPeriod);
                    }
                this.setAlpha(this.alpha);
            }
        }

        Menu.prototype.show = function(statusDelay)
        {
            if (statusDelay==null){statusDelay = 0;}
            this.statusDelay = statusDelay;
            this.status = 1;
            this.showing()

            for (var i=0; i<this.items.length; i++)
            {
                this.items[i].show((i*this.itemsDelay)+statusDelay);
            }
        }

        Menu.prototype.showImmediately = function()
        {
            this.status = 2;
            this.alpha = 100;
            this.setAlpha(100);
            for (var i=0; i<this.items.length; i++)
            {
                this.items[i].showImmediately();
            }
        }


        Menu.prototype.showing = function()
        {
            if(this.status == 1)
            {
                var menu = this;
                if (this.statusDelay > 0)
                {
                    this.statusDelay -= this.alphaPeriod;
                }
                else
                {
                    this.alpha += 5;
                }

                if (this.alpha >= 100)
                    {
                        this.alpha = 100;
                        this.status = 2;
                    }
                else
                    {
                        setTimeout(function(){menu.showing()}, this.alphaPeriod);
                    }
                this.setAlpha(this.alpha);
            }
        }
        // End of Menu Class definition