jQuery(function($) {
    /* METTRE TOUS LES SCRIPTS POUR LE SITE ICI PLUTOT QUE DIRECTEMENT DANS LA PAGE! */


    /* ***************************************************************
    OUVERTURE DES LIENS EXTERNE DANS UN NOUVEAU TAB / WINDOW 
    Note : ajouter class="in" si vous voulez qu'il s'ouvre 
    dans la même fenêtre.
    *************************************************************** */


    var createAcoOverlay = function() {
        $('<div />', {
            'id': 'aco-overlay',
            'css': {
                'opacity': 0
            }
        })
		.appendTo('body')
		.show()
		.animate({
		    'opacity': .7
		}, 600);
        // Set overlay in absolute for IE6, Bind on 
        if ($.browser.msie && $.browser.version < 7) {
            $('#aco-overlay').css({
                'position': 'absolute',
                'top': '0px',
                'left': $(window).scrollLeft() + 'px',
                'width': $(window).width(),
                'height': $(document).height()
            });
            $(window).bind('scroll.aco-overlay-ie6 resize.aco-overlay-ie6', function() {
                $('#aco-overlay').css({
                    'width': $(window).width(),
                    'left': $(window).scrollLeft() + 'px'
                });
            });
        }
    },
	destroyAcoOverlay = function() {
	    $('#aco-overlay')
		.animate({
		    'opacity': 0
		}, 600, function() {
		    $(this).remove();
		});
	    if ($.browser.msie && $.browser.version < 7) {
	        $(window).unbind('scroll.aco-overlay-ie6 resize.aco-overlay-ie6');
	    }
	};

    var acoPopup = function() {
        createAcoOverlay();

        $('#aco-popup')
		.show()
		.css({
		    left: (($(window).width() - $('#aco-popup').width()) / 2) + 'px',
		    top: (($(window).height() - $('#aco-popup').height()) / 2) + 'px'
		});

        $(window).bind('resize.fun', function() {
            $('#aco-popup')
			.css({
			    left: (($(window).width() - $('#aco-popup').width()) / 2) + 'px',
			    top: (($(window).height() - $('#aco-popup').height()) / 2) + 'px'
			});
        });

        if ($.browser.msie) {
            if ($.browser.version < 7) {
                $('#aco-popup').css({
                    position: 'absolute',
                    left: (($(window).width() - $('#aco-popup').width()) / 2 + $(window).scrollLeft()) + 'px',
                    top: (($(window).height() - $('#aco-popup').height()) / 2 + $(window).scrollTop()) + 'px'
                });

                $(window).unbind('resize.fun');
                $(window).bind('scroll.fun resize.fun', function() {
                    var offset = 0;
                    if ($(window).width() < $('#aco-popup').width() - offset || $(window).height() < $('#aco-popup').height() - 50) return;
                    $('#aco-popup')
					.css({
					    left: (($(window).width() - $('#aco-popup').width()) / 2 + $(window).scrollLeft()) + 'px',
					    top: (($(window).height() - $('#aco-popup').height()) / 2 + $(window).scrollTop()) + 'px'
					});

                }).trigger('scroll.fun');
            }
            $('#aco-popup').hide();
            setTimeout(function() {
                $('#aco-popup').show();
            }, 600);
        } else {
            $('#aco-popup')
			.css({
			    'opacity': 0
			})
			.animate({
			    'opacity': 1
			}, 600);
        }
    };

    $('#popup-close').click(function() {
        if ($(this).data('closing')) return;
        $(this).data('closing', true);
        destroyAcoOverlay();
        if ($.browser.msie) {
            $('#aco-popup').hide();
            $(this).data('closing', false);
        } else {
            $('#aco-popup')
			.fadeOut(600, function() {
			    $('#fun-close').data('closing', false);
			});
        }
        $(window).unbind('scroll.fun resize.fun');
        return false;
    });


    acoPopup();

});