function imagePop() {
    $(".item-img, .ads-img").hover(function (e) {
        var nxtImg = $(this).next().children('img');
        pos = $(this).position();
        var top_offset = (nxtImg.height() - $(this).height()) / 2;
        nxtImg.css({
                    "top": - top_offset
                }).fadeIn("fast");
    }, function () {
        $(this).next().children('img').fadeOut('normal');
    });
}
function close_modal() {
    $('#mask').fadeOut(500);
    $('.modal_window').fadeOut(500);
}
function show_modal(modal_id) {
    $('#mask').css({
                'height': $(document).height() + "px",
                'width': $(document).width() + "px",
                'display': 'block',
                opacity: 0
            });
    $('#mask').fadeTo(500, 0.8);
    $('#' + modal_id).fadeIn(500);
}
function initMenus() {
    $('li.collapse').hide();
    $('li.expanded').show();
    $('li.subitems_title').click(function () {
        var checkElement = $(this).next();
        if ((checkElement.is('li.collapse')) && (checkElement.is(':visible'))) {
            checkElement.slideUp('normal');
            $(this).removeClass('opened');
            return false;
        }
        if ((checkElement.is('li.collapse')) && (!checkElement.is(':visible'))) {
            $("li.subitems_title").removeClass('opened');
            $("li.expanded").removeClass('expanded').addClass('collapse');
            $(this).addClass('opened');
            $(' li.collapse:visible').slideUp('normal');
            checkElement.slideDown('normal');
            return false;
        }
    });
}
$(document).ready(function () {
    maxWidth = 720;
    minWidth = 178;
    delay = 900;
    var tab = new Array();
    tab.a1 = (0);
    tab.a2 = (180);
    tab.a3 = (360);
    tab.a4 = (540);
    $("#slide div").hover(function () {
        $("#slide div").css({
                    'z-index': '1'
                });
        $(this).css({
                    'z-index': '100'
                });
        if ($(this).is("#a4")) {
            $(this).animate({
                        width: 720 + "px",
                        textIndent: 0,
                        opacity: 1
                    }, {
                        queue: false,
                        duration: delay
                    });
        } else {
            $(this).animate({
                        width: maxWidth + "px",
                        left: 0,
                        textIndent: 0,
                        opacity: 1,
                        marginLeft: 0
                    }, {
                        queue: false,
                        duration: delay
                    });
        }
        jQuery("h2", this).fadeIn(700);
    }, function () {
        var gauche = 0;
        gauche = tab[this.id];
        if ($(this).is("#a4")) {
            $(this).animate({
                        width: 2 + minWidth + "px",
                        textIndent: - 540 + "px",
                        opacity: 0.7
                    }, {
                        queue: false,
                        duration: delay
                    });
        } else {
            $(this).animate({
                        width: minWidth + "px",
                        left: gauche + "px",
                        textIndent: - gauche + "px",
                        opacity: 0.7
                    }, {
                        queue: false,
                        duration: delay
                    });
        }
        jQuery("h2", this).fadeOut(0);
    });

    $('.close_modal').click(function () {
        close_modal();
        $('#dialog').remove();
    });
    initMenus();
    $('#navbox, #headernav').click(function(event) {
        if ($(event.target).is('#logout')) {
            event.preventDefault();
            $.getJSON('/logout/', function(data) {
                if (data.logout == 'ok') {
                    window.location.reload();
                }
            });
        }
        if ($(event.target).is('a.activate_modal')) {

            event.preventDefault();
            var modal_id = $(event.target).attr('name');
            var modal_url = "/aj" + modal_id + "/";


            $('#modal_window').html('').load(modal_url).removeClass().addClass('modal_window').addClass(modal_id);
          
            var window_width = $(window).width();
            var window_height = $(window).height();
            $('.modal_window').each(function () {
                var modal_height = $('#modal_window').height();
                var modal_width = $('#modal_window').width();
                var top = (window_height - modal_height) / 2;
                var left = (window_width - modal_width) / 2;
                $(this).css({
                            'top': top,
                            'left': left
                        });
            });
            show_modal("modal_window");
        }
    });
    $("#homelink").hover(function () {
        $(this).animate({
                    "opacity": "0"
                }, {
                    queue: false,
                    duration: 500
                });
    }, function () {
        $(this).animate({
                    "opacity": "1"
                }, {
                    queue: false,
                    duration: 500
                });
    });
    imagePop();
});
$(document).ready(function () {
    $(".showdocs").click(function(event) {
        $(this).next().slideToggle();
    });
})
$(function() {
    $('#button').live('click', function(event) {
        event.preventDefault();
        $('#message').html("<img id='loaderimg' src='/css/loader.gif'>");
        var form = $("#modal_content");
        var data = {}
        var object = $(form).find('#id_subject').val();
        var email = $(form).find('#id_email').val();
        var messagetext = $(form).find('#id_message').val();
        var code = $(form).find('#id_antispam_0').val();
        var captcha = $(form).find('#id_antispam_1').val();
        data = "object=" + object + "&email=" + email + "&message=" + messagetext + "&antispam_0=" + code + "&antispam_1=" + captcha;
        $.post("/ajcontact/",
                data,
                function(responseData) {
                    $("#message").html(" ");
                    if (responseData.success == "False") {
                        var message = '<p class="errors">';
                        message += "Corrigez les erreurs suivantes : <ul class='errors'>"
                        for (var i in responseData.errors) {
                            message += "<li>" + responseData.errors[i] + "</li>";
                        }
                        message += "</ul></p>";
                        $('#message').append(message);
                    }
                    if (responseData.success == 'True') {
                        var message = "<p>" + responseData.message + "</p>";
                        $('#message').append(message);
                        setTimeout("close_me(); ", 2000)
                    }
                    ;
                },
                "json"
        );
    });
    $('.close_me').live('click', function() {
        close_me();
    });
});
function close_me() {
    $('#mask').fadeOut(500);
    $('.modal_window').fadeOut(500);
    $('#dialog').remove();
}
