requestRunning = false;
lastRequest = null;

function setAjax(el, post) {
    /*if (requestRunning || (!post && lastRequest == el.get('href')))
        return false; // BUG IE*/

    // Menu
    $$('.subcat-title').each(function (e) {
        e.removeClass('selected');
    });

    el = $(el);

    if (el.getParent() && el.getParent().hasClass('subcat-title') || el.hasClass('subcat-title')) {
        if (el.getParent().hasClass('subcat-title'))
            link = el.getParent();
        else
            link = el;

        link.addClass('selected');
    }

    // Sous menu
    $$('.subcat-content a').each(function (e) {
        e.removeClass('selected');
    });
    if (el.getParent().getParent().getParent().hasClass('subcat-content')) {
        link = el.getParent().getParent().getParent();
        el.addClass('selected');
    }

    // Menu du haut
    $$('.inMenu-head a').each(function (e) {
        e.removeClass('selected');
    });
    if (el.getParent().hasClass('inMenu-head')) {
        link = el.getParent();
        el.addClass('selected');
    }

    new Fx.Tween('inPage', {
        duration: 250,
        onStart: function() {
            requestRunning = true;
        },
        onComplete: function() {
            lastRequest = el.get('href');

            if (post)
                lastRequest = post.get('action');

            window.location.hash = lastRequest;

            request = new Request.HTML({
                url: baseUrl + 'ajax/' + lastRequest,
                onRequest: function() {},
                onCancel: function() {
                    $('inPage').set('html', 'Le chargement de la page a &eacute;t&eacute; annul&eacute;.');

                    new Fx.Tween('inPage', {
                        duration: 250,
                        onComplete: function() {
                            requestRunning = false;
                        }
                    }).start('opacity', 0, 1);
                },
                onFailure: function() {
                    $('inPage').set('html', 'Le chargement de la page a &eacute;chou&eacute;.');

                    new Fx.Tween('inPage', {
                        duration: 250,
                        onComplete: function() {
                            requestRunning = false;
                        }
                    }).start('opacity', 0, 1);
                },
                onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
                    $('inPage').set('html', responseHTML);

                    // Construction des slides
                    if ($('slideshow')) {
                        new slideGallery($('slideshow'), {
                            steps: 4,
                            speed: 600,
                            transition: Fx.Transitions.Sine.easeInOut,
                            mode: 'line'
                        });
                    }

                    // Images grande taille
                    $$('.thumb').each(function(elImg) {
                        elImg.addEvent('click', function(eImg) {
                            if (eImg.target.get('rel')) {
                                new Fx.Tween('image', {
                                    duration: 250,
                                    onComplete: function() {
                                        new Asset.image(eImg.target.get('rel'), {
                                            onload: function(img) {
                                                if ($('image')) {
                                                    if ($('image').getElement('.big')) {
                                                        $('image').getElement('.big').dispose();
                                                    }

                                                    img.set({
                                                        width: 380,
                                                        height: Math.round(380 * img.get('height') / img.get('width')),
                                                        'class': 'big'
                                                    }).inject('image', 'top');

                                                    new Fx.Tween('image', {
                                                        duration: 250
                                                    }).start('opacity', 0, 1);
                                                }
                                            }
                                        });
                                    }
                                }).start('opacity', 1, 0);
                            }
                        });
                    });

                    new Fx.Tween('inPage', {
                        duration: 250,
                        onComplete: function() {
                            requestRunning = false;
                        }
                    }).start('opacity', 0, 1);
                }
            });

            if (post)
                request.post(post);
            else
                request.get();
        }
    }).start('opacity', 1, 0);

    return true;
}

window.addEvent('domready', function() {
    // Réécriture des liens
    $$('a').each(function(el) {
        if (el.hasClass('ajax') && el.get('href') != '' && el.get('href') != '#') {
            el.addEvent('click', function(e) {
                e.stop();

                setAjax(el);
            });
        }
    });

    // Ancre de la page réelle ?
    var fragment = window.location.hash;
    fragment = fragment.substring(1, fragment.length);

    if (fragment) {
        new Request.HTML({
            url: baseUrl + 'ajax/' + fragment,
            onRequest: function() {},
            onCancel: function() {
                $('inPage').set('html', 'Le chargement de la page a &eacute;t&eacute; annul&eacute;.');

                new Fx.Tween('inPage', {
                    duration: 250,
                    onComplete: function() {
                        requestRunning = false;
                    }
                }).start('opacity', 0, 1);
            },
            onFailure: function() {
                $('inPage').set('html', 'Le chargement de la page a &eacute;chou&eacute;.');

                new Fx.Tween('inPage', {
                    duration: 250,
                    onComplete: function() {
                        requestRunning = false;
                    }
                }).start('opacity', 0, 1);
            },
            onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
                $('inPage').set('html', responseHTML);

                // Construction des slides
                if ($('slideshow')) {
                    new slideGallery($('slideshow'), {
                        steps: 4,
                        speed: 600,
                        transition: Fx.Transitions.Sine.easeInOut,
                        mode: 'line'
                    });
                }

                // Images grande taille
                $$('.thumb').each(function(elImg) {
                    elImg.addEvent('click', function(eImg) {
                        if (eImg.target.get('rel')) {
                            new Fx.Tween('image', {
                                duration: 250,
                                onComplete: function() {
                                    new Asset.image(eImg.target.get('rel'), {
                                        onload: function(img) {
                                            if ($('image')) {
                                                if ($('image').getElement('.big')) {
                                                    $('image').getElement('.big').dispose();
                                                }

                                                img.set({
                                                    width: 380,
                                                    height: Math.round(380 * img.get('height') / img.get('width')),
                                                    'class': 'big'
                                                }).inject('image', 'top');

                                                new Fx.Tween('image', {
                                                    duration: 250
                                                }).start('opacity', 0, 1);
                                            }
                                        }
                                    });
                                }
                            }).start('opacity', 1, 0);
                        }
                    });
                });

                new Fx.Tween('inPage', {
                    duration: 250,
                    onComplete: function() {
                        requestRunning = false;
                    }
                }).start('opacity', 0, 1);
            }
        }).get(baseUrl + 'ajax/' + fragment);
    } else {
        // Construction des slides
        if ($('slideshow')) {
            new slideGallery($('slideshow'), {
                steps: 4,
                speed: 600,
                transition: Fx.Transitions.Sine.easeInOut,
                mode: 'line'
            });
        }

        // Images grande taille
        $$('.thumb').each(function(elImg) {
            elImg.addEvent('click', function(eImg) {
                if (eImg.target.get('rel')) {
                    new Fx.Tween('image', {
                        duration: 250,
                        onComplete: function() {
                            new Asset.image(eImg.target.get('rel'), {
                                onload: function(img) {
                                    if ($('image')) {
                                        if ($('image').getElement('.big')) {
                                            $('image').getElement('.big').dispose();
                                        }

                                        img.set({
                                            width: 380,
                                            height: Math.round(380 * img.get('height') / img.get('width')),
                                            'class': 'big'
                                        }).inject('image', 'top');

                                        new Fx.Tween('image', {
                                            duration: 250
                                        }).start('opacity', 0, 1);
                                    }
                                }
                            });
                        }
                    }).start('opacity', 1, 0);
                }
            });
        });
    }
});
