$(document).ready(function () {

    // Toggle the drawer when a subscribe link is clicked
    $("a.sub").click(function () {
        toggleDrawer();
        return false;
    });

    //This tidies the layout in IE
    if (!jQuery.support.leadingWhitespace) {
        $("a.thumb img").each(function (index) {
            var top = (229 - $(this).height()) / 2;
            $(this).css('margin-top', top);
            $(this).css('margin-bottom', top);
        });
    }

    // Move article images up on hover in all browsers except IE
    if (!$.browser.msie) {
        $("div.post.hentry").mouseover(function () {
            $(this).stop().animate({
                queue: false,
                marginTop: "-10px",
                paddingBottom: "10px"
            }, 200);
            $(this).find('h2').stop().animate({
                queue: false,
                paddingTop: "20px"
            }, 200);
        }).mouseout(function () {
            $(this).find('h2').stop().animate({
                queue: false,
                paddingTop: "10px"
            }, 200);
            $(this).stop().animate({
                queue: false,
                marginTop: "0px",
                paddingBottom: "0px"
            }, 200);
        });
    }

    // Dropcap the first letter of articles
    $('#copy p:first').fancyletter();

    // Fade nav elements into the background when scrolling
    $(window).scroll(function () {
        $('nav#main').stop();
        if ($(window).scrollTop() <= 100) {
            $('nav#main').stop().fadeTo("normal", 1.0);
            $('nav#next').stop().fadeTo("slow", 1.0);
            $('nav#previous').stop().fadeTo("slow", 1.0);
        } else {
            $('nav#main').stop().fadeTo("slow", 0.35);
            $('nav#next').stop().fadeTo("slow", 0.55);
            $('nav#previous').stop().fadeTo("slow", 0.55);
        }


    });

    // Fade nav elements back up if moused over when they're faded out 
    $('nav#next').mouseover(function () {
        $('nav#next').stop().fadeTo("fast", 1.0);
    });

    $('nav#previous').mouseover(function () {
        $('nav#previous').stop().fadeTo("fast", 1.0);
    });

    $('nav#main').mouseover(function () {
        $('nav#main').stop().fadeTo("fast", 1.0);
    });

    $('nav#next').mouseout(function () {
        if ($(window).scrollTop() > 100) {
            $('nav#next').stop().fadeTo("fast", 0.55);
        }
    });

    $('nav#previous').mouseout(function () {
        if ($(window).scrollTop() > 100) {
            $('nav#previous').stop().fadeTo("fast", 0.55);
        }
    });

    $('nav#main').mouseout(function () {
        if ($(window).scrollTop() > 100) {
            $('nav#main').stop().fadeTo("fast", 0.35);
        }
    });

    // Function to toggle the subscribe drawer
    function toggleDrawer() {
        // ignore if already visible
        if ($("a#down").is('.active')) {
            $("a#down").removeClass('active');
            $("#subscribe-options").hide();
            $("#subscribe-tray").animate({
                height: "31px"
            }, 200);
            $("#breadcrumbs").fadeIn("slow");
            $("#miniurl").fadeIn("slow");
        } else {
            $("#breadcrumbs").hide();
            $("#miniurl").hide();
            $("a#down").addClass('active'); //change the arrow link
            $("#subscribe-tray").animate({
                height: "61px"
            }, 200);
            $("#subscribe-options").fadeIn("slow");
        }
    }

});
