$(function () {

    global_rotate = '';
    global_location = window.location.href; // Hackish

    // Open links in a new window
    $('a.external').click(function () {
        window.open(this.href); return false;
    });

    // Only show sub-nav on about pages
    if ($('div#section_about').length || $('div#section_leadership_alt').length || $('div#section_press').length || $('div#section_press_release').length) {
        $('#top_nav_second_sublist').addClass('active').show();
        if ($('#section_leadership_alt').length) $('#newsright_top_nav_leadership').addClass('active');
        if ($('#section_press').length) $('#newsright_top_nav_press').addClass('active');
        /*$('.top_nav_second').sub_nav({
        'target' : '#top_nav_second_sublist',
        'timeout' : 1000,
        'action_func' : function() { $('#top_nav_second_sublist').fadeInSafe(300); },
        'close_func' : function()  { $('#top_nav_second_sublist').fadeOutSafe(300);}
        });*/
    }

    if ($('div#section_contact_form').length || $('div#section_thank_you').length) {
        $('#top_nav_fifth_sublist').addClass('active').show();
    }

    $("#contact_form").submit(function (e) {
        e.preventDefault();
        $.post($("#contact_form").attr('action'), $("#contact_form").serialize(), function () {
            window.location = '/thank-you.html';
        });
    });

    // Contact Us
    $('#contact_us_submit').click(function () {
        if ($("#contact_form").valid()) {
            $("#contact_form").submit();
        }
        return false;
    });

    // Contact Us - Jobs
    $('#qamorelink').click(function () {
        $('#qamore').fadeInSafe(300);
        $('#qamorelink').fadeOutSafe(300);
        $('#qalesslink').fadeInSafe(300);
    });

    $('#qalesslink').click(function () {
        $('#qamore').fadeOutSafe(300);
        $('#qalesslink').fadeOutSafe(300);
        $('#qamorelink').fadeInSafe(300);
    });

    // Close Popup
    $('div.popup a, div.mask').click(function () {
        $('div.popup, div.mask').fadeOutSafe(500);
        return false;
    });

    // Open Popup
        $('#newsright_headshots a').click(function () {
            window.location = global_location + this.hash;
            $(this.hash).fadeInSafe(500);
            $('div.mask').fadeInSafe(500);
            return false;
        });

    // Deeplinking
    $(window).load(function () {
        if (window.location.hash) {
            $(window.location.hash).fadeInSafe(500);
            $('div.mask').fadeInSafe(500);
        }
    });

    // Rotating images on homepage
    if ($('div#section_homepage').length) {
        global_rotate = setInterval(function () {
            current = $('.rotate.active').index() - 1;
            if ((current + 1) == $('.rotate').length) {
                var prev = $('.rotate.active').removeClass('active');
                $('.rotate:eq(0)').fadeInSafe(1000, function () { prev.hide(); }).addClass('active');
            } else {
                var prev = $('.rotate.active').removeClass('active').show();
                $('.rotate:eq(' + (current + 1) + ')').fadeInSafe(1000, function () { prev.hide(); }).addClass('active');
            }
        }, 5000);
    }

    // Contact form validation
    $("#contact_form").validate();
});
