window.addEvent('domready',
    function()
    {
        /**
        * Creo il livello per il preload e lo posiziono dopo il footer
        */
        var preDiv = new Element('div', {
            'id': 'preload'
        });

        $('footer').grab(preDiv, 'after');

        /**
        * Ricavo l'elenco dell'anteprima copertine da precaricare
        */
        var req = new Request({url:'index.php?covers&js',
            onSuccess: function(response)
            {
                var imgList = "images/bg2.jpg,images/bg3.jpg" + response;
                imgList = imgList.split(',');

                /**
                * Effettuo il preload delle immagini ed elimino il livello preload
                */
                new Asset.images(imgList, {
                    onComplete: function()
                    {
                        $('preload').fade('out');
                    }
                });
            }
        });

        req.send();

        /**
        * Cerco tutti i link con attributo rel="js" e li do in utilizzo a javascript
        */
        $$('a[rel=js]').addEvent('click',
            function()
            {
                var req = new Request({url:this.href+'&js',
                    onSuccess: function(response)
                    {
                        if ($('text')) $('text').destroy();
                        $('cont').addClass('page');
                        var textdiv = new Element('div', {
                                'id': 'text',
                                'html': response
                        });

                        var hr = new Element('hr');
                        textdiv.grab(hr, 'bottom');
                        $('cont').grab(textdiv, 'bottom');

                        /**
                        * Cerco tutti i link con attributo rel="new" e li faccio aprire in
                        * una nuova pagina
                        */
                        $$('a[rel=new], a[rel=lightbox]').set('target', '_blank');

                        /**
                        * Attivo la lightbox per le immagini delle copertine
                        */
                        $$('a[rel=lightbox]').addEvent('click',
                            function()
                            {
                                var winSize = window.getSize();
                                var imgPath = this.href;
                                var imgText = this.title;
                                var lbBg = new Element('div', {
                                    'id': 'lbBg',
                                    'styles': {
                                        'opacity': 0,
                                        'position': 'absolute',
                                        'top': 0,
                                        'left': 0,
                                        'width': '100%',
                                        'height': '100%',
                                        'background-color': '#000000',
                                        'overflow': 'hidden'
                                    },
                                    'events': {
                                        'click': lbOff
                                    }
                                });

                                var lbWin = new Element('div', {
                                    'id': 'lbWin',
                                    'styles': {
                                        'opacity': 0,
                                        'position': 'absolute',
                                        'top': (winSize.y / 2) - 100,
                                        'left': (winSize.x / 2) - 100,
                                        'width': 200,
                                        'height': 200,
                                        'background': '#ffffff url(images/loading.gif) no-repeat 84px 84px',
                                        'overflow': 'hidden'
                                    }
                                });

                                var lbText = new Element('p', {
                                    'id': 'lbText',
                                    'text': imgText,
                                    'styles': {
                                        'position': 'relative',
                                        'top': 0,
                                        'left': 0,
                                        'opacity': 0,
                                        'text-align': 'left',
                                        'padding-top': 10,
                                        'padding-right': 26
                                    }
                                });

                                if (imgText == '') lbText.setStyle('height', 16);
                                var lbClose = new Element('a', {
                                    'id': 'lbClose',
                                    'title': 'Close',
                                    'styles': {
                                        'position': 'absolute',
                                        'bottom': 0,
                                        'right': 0,
                                        'width': 16,
                                        'height': 16,
                                        'cursor': 'pointer',
                                        'background': 'transparent url(images/close.gif) no-repeat right 0'
                                    },
                                    'events': {
                                        'mouseover': function()
                                        {
                                            this.setStyle('background-position', 'right -34px');
                                        },
                                        'mouseout': function()
                                        {
                                            this.setStyle('background-position', 'right 0');
                                        },
                                        'click': lbOff
                                    }
                                });

                                $('footer').grab(lbBg, 'after');
                                lbBg.grab(lbWin, 'after');
                                lbText.grab(lbClose);
                                lbWin.grab(lbText);
                                $('lbBg').set('tween', {
                                    'duration': 'short',
                                    onComplete: function()
                                    {
                                        $('lbWin').fade(1);
                                        var boxImg = new Asset.image(imgPath, {
                                            onload: function()
                                            {
                                                boxImg.setStyle('opacity', 0);
                                                $('lbWin').grab(boxImg, 'top');
                                                var imgSize = boxImg.getSize();
                                                $('lbWin').set('morph', {
                                                    onComplete: function()
                                                    {
                                                        lbText.fade(1);
                                                        boxImg.fade(1);
                                                    }
                                                });

                                                $('lbText').setStyle('width', imgSize.x - 26);
                                                var lbTextSize = lbText.getSize();
                                                var topPix = (winSize.y / 2) - (imgSize.y / 2) - (lbTextSize.y / 2) - 10;
                                                var leftPix = (winSize.x / 2) - (imgSize.x / 2) - 10;
                                                if (topPix < 30) topPix = 30;
                                                if (leftPix < 30) leftPix = 30;
                                                $$('body').setStyle('overflow', 'hidden');
                                                $('lbWin').morph({
                                                    'top': topPix,
                                                    'left': leftPix,
                                                    'padding': 10,
                                                    'height': imgSize.y + lbTextSize.y,
                                                    'width': imgSize.x,
                                                    'background-position': (imgSize.x / 2 - 6) + 'px ' + (imgSize.y / 2 - 6 + (lbTextSize.y / 2)) + 'px'
                                                });

                                                window.addEvent('resize',
                                                    function()
                                                    {
                                                        winSize = window.getSize();
                                                        var topPix = (winSize.y / 2) - (imgSize.y / 2) - (lbTextSize.y / 2) - 10;
                                                        var leftPix = (winSize.x / 2) - (imgSize.x / 2) - 10;
                                                        if (topPix < 30) topPix = 30;
                                                        if (leftPix < 30) leftPix = 30;
                                                        $('lbWin').setStyles({
                                                            'top': topPix,
                                                            'left': leftPix
                                                        });
                                                    }
                                                );
                                            }
                                        });
                                    }
                                });

                                $('lbBg').tween('opacity', 0.55);

                                /**
                                * Chiusura lightbox
                                */
                                function lbOff()
                                {
                                    $('lbWin').set('tween', {
                                            onComplete: function()
                                            {
                                                $('lbBg').set('tween', {
                                                    'duration': 'short',
                                                    onComplete: function()
                                                    {
                                                        $('lbWin').destroy();
                                                        $('lbBg').destroy();
                                                        $$('body').setStyle('overflow', 'auto');
                                                    }
                                                });

                                                $('lbBg').tween('opacity', 0);
                                            }
                                    });

                                    $('lbWin').tween('opacity', 0);
                                }

                                /**
                                * Disabilito href html
                                */
                                return false;
                            }
                        );
                    },
                    onFailure: function()
                    {
                        alert('Richiesta fallita!');
                    }
                });

                req.send();
                return false;
            }
        );

        /**
        * Cerco tutti i link con attributo rel="reset" e li do in utilizzo a javascript
        */
        $$('a[rel=reset]').addEvent('click',
            function()
            {
                if ($('text')) $('text').destroy();
                $('cont').removeClass('page');
                return false;
            }
        );
    }
);
