$(document).ready(function() {

    var page = $.url.param('page');

    /**
    * used to move down the sidebar footer to the bottom edge of an job offer page
    * PADDING_OFFSET: 3 x 15px (bottom paddings for 3 boxes)
    */
    var PADDING_OFFSET = 45;
    var jobs_content_height = $("#jobs_content").height();
    var jobs_menu_bottom_height = $("#jobs_menu_bottom").height();
    var jobs_menu_top_height = $("#jobs_menu_top").height();
    var new_margin = jobs_content_height - jobs_menu_top_height - jobs_menu_bottom_height - PADDING_OFFSET;

    if (typeof(page) !== 'undefined') {
        $("#" + page).addClass("bold");

        if (page == 'who-we-are') {
            $("#who-we-are").addClass("black");
        }
        if (page.substr(0, 3) == 'job') {
            // Always keep the 'Looking for you' menu item bolded if you are on an job offer page
            $("#looking-for-you").addClass("bold");

            // Move the job offer sidebar footer to the bottom edge of the height of a job offer
            $("#job_offer_foot").css("margin-top", new_margin + "px");
        }
    }

    if (!page) {
        YI.flash.load(YI.staticContent('flash/jobs/slide_team.swf'), 'slideshow_team', 365, 294);
    }
    if (page == 'working-atmosphere') {
        YI.flash.load(YI.staticContent('flash/jobs/slide_work.swf'), 'slideshow_work', 365, 294);
    }
    

    $(".menu_button").mouseenter(function() {
        $(this).addClass("menu_button_hover pointer_cursor");
    });

    $(".menu_button").mouseleave(function() {
        $(this).removeClass("menu_button_hover pointer_cursor");
    });

    $(".menu_button").click(function() {
        var link = $(this).find("a").attr("href");
            window.location = link;
    });
    

    $(".close_popup").hover(function() {
        $(this).addClass('pointer_cursor');
    });
    $(".close_popup").mouseout(function() {
        $(this).removeClass('pointer_cursor');
    });
    $(".close_popup").click(function() {
        tb_remove();
    });


    /**
    * Send job recommendation mail to a friend
    */
    $("#send_recommendation").click(function() {

        $(".error").hide();
        var your_name = $("input[name='your_name']").val();
        var your_email = $("input[name='your_email']").val();
        var friends_name = $("input[name='friends_name']").val();
        var friends_email = $("input[name='friends_email']").val();
        var message = $("textarea[name='message']").val();

        if (your_name == '' || your_email == '' || friends_name == '' || friends_email == '' || message == '') {
            $("#emptyFieldErrorRecommendation").show();

        } else if (!validateEmail(your_email) || !validateEmail(friends_email)) {
                $("#wrongMailErrorRecommendation").show();
        } else {

            $.ajax({
                type: "POST",
                url: "job_mails.php",
                data: {
                    type: 'recommendation',
                    your_name: your_name,
                    your_email: your_email,
                    friends_name: friends_name,
                    friends_email: friends_email,
                    page_name: page,
                    message: message
                },
                dataType: 'json',
                beforeSend: function() {
                  $("#progressRecommendation").show();
                },
                success: function(msg) {
                    $("#progressRecommendation").hide();
                    if (msg) {
                        tb_remove();
                    } else {
                        $('#notSentErrorRecommendation').show();
                    }
                },
                error: function() {
                    $("#progressRecommendation").hide();
                    $('#notSentErrorRecommendation').show();
                }
            });

        }

    });

    /**
    * Send job application information mail to a candidate
    */
    $("#send_application").click(function() {

        $(".error").hide();
        var your_name = $("input[name='app_your_name']").val();
        var your_email = $("input[name='app_your_email']").val();
        var job_name = $("#job-name").html();

        if (your_name == '' || your_email == '') {
            $("#emptyFieldErrorApplication").show();

        } else if (!validateEmail(your_email)) {
                $("#wrongMailErrorApplication").show();
        } else {

            $.ajax({
                type: "POST",
                url: "job_mails.php",
                data: {
                    type: 'application',
                    your_name: your_name,
                    your_email: your_email,
                    job_name: job_name
                },
                dataType: 'json',
                beforeSend: function() {
                  $("#progressApplication").show();
                },
                success: function(msg) {
                    $("#progressApplication").hide();
                    if (msg) {
                        tb_remove();
                    } else {
                        $('#notSentErrorApplication').show();
                    }
                },
                error: function() {
                    $("#progressApplication").hide();
                    $('#notSentErrorApplication').show();
                }
            });

        }
    });

});
