
function initContactFormLink()
{
    $("#show-contact-link").show();

    $("#contact form").hide();

    $("#contact form").submit(function (e)
    {
        // TODO: Send form asynchronously
        // Reset form?
        // Maybe only hide form on ajax callback?

        $("#show-contact-link").slideDown(250);
        $("#contact form").slideUp(250);
        e.preventDefault();
    });

    $("#show-contact-link a").click(function (e)
    {
        $("#show-contact-link").slideUp(250);
        $("#contact form").slideDown(250);
        e.preventDefault();
    });
}

var contentSlideSpeed = 250;

// Load a new page using an AJAX request and run a fancy transition effect
function loadPage(page)
{
    var url = page+".php?contentOnly";

    var effectCompleted = false,
        ajaxCompleted   = false;

    var ajaxData = null;

    // Called once animation of old content and AJAX request have completed, delete old content,
    // insert new.
    function switchContent()
    {
        if (effectCompleted && ajaxCompleted)
        {
            effectCompleted = ajaxCompleted = false;

            // remove old content
            $("#content-wide, #content, #sidebar").remove();

            // insert new content
            $("#main").prepend(ajaxData);
            initPage();

            // set proper start positions and animate them into view
            $("#content-wide").css({ left: 870 });
            $("#content").css({ left: 600 });
            $("#sidebar").css({ right: 300 });


            $("#content-wide").animate({left: 0}, contentSlideSpeed);
            $("#content").animate({left: 0}, contentSlideSpeed);
            $("#sidebar").animate({right: 0}, contentSlideSpeed);
        }
    }

    // Fire off AJAX request, fade out current content
    // The last one of these two asynchronous events to complete will have to take the requested
    // data, insert it into the document and animate it back in.
    $.get(url, function success(data, textStatus, xhr)
    {
        ajaxCompleted = true;
        ajaxData = data;

        switchContent();
    });

    $("#content-wide").animate( { left: 870 }, contentSlideSpeed, function ()
            { effectCompleted = true; switchContent() });

    $("#content").animate( { left: 600 }, contentSlideSpeed,      function ()
            { effectCompleted = true; switchContent() });

    $("#sidebar").animate( { right: 300 }, contentSlideSpeed,     function ()
            { effectCompleted = true; switchContent() });
}


// Skip fade in IE since it uses filters instead of opacity, which doesn't look good..
// FIXME: try not using fadeOut/In at all, text AA still gets screwed in IE7
// FIXME: doesn't actually seem to work in IE9, fade looks borked like in older IEs, but
// support.opacity reports true.
if ($.support.opacity)
    var newsFadeSpeed = 350;
else
    var newsFadeSpeed = 0;


// Initialize newsitem nav buttons
function initNewsNav()
{
    // Fade the navbar in/out when mouse hovers over newsarea
    $("#newsarea").hover(
        function ()
        {
            $("#newsarea-nav").stop(true, true);
            $("#newsarea-nav").fadeIn(150);
        },
        function ()
        {
            $("#newsarea-nav").stop(true, true);
            $("#newsarea-nav").fadeOut(150);
        }
    );

    if (!$("#newsarea-nav").length) return;

    // Newest newsitem is shown by default (has 'active-newsitem' class). 'right' button shows the
    // next older news item. Nav buttons are hidden by default.

    // Show next button if there is more than one newsitem.
    if ($("#newsarea .newsitem").length > 1)
        $("#newsarea-nav-next").show();

    $("#newsarea-nav-prev").click(function () { prevNewsItem(); });
    $("#newsarea-nav-next").click(function () { nextNewsItem(); });

    // TODO: maybe use a single class (".noSelect") for this?
    // Prevent default event handlers from running
    function prevent(ev)
    {
        if (ev && ev.preventDefault)
            ev.preventDefault();

        return false;
    }

    // Prevent a selection from starting on the controlBar
    $("#newsarea-nav")[0].onselectstart = prevent; // For IE
    $("#newsarea-nav").mousedown(prevent);         // For everyone else
}

// Show next (older) news item. Ensures next button will be hidden if the last newsitem was shown.
function nextNewsItem()
{
    var $active = $("#newsarea .active-newsitem");
    var $next = $active.next(".newsitem");

    // Only fade when there is something to fade to ($next), and if there is precisely one active
    // newsitem (because if there's two, then a fade is already ongoing).
    if ($active.length == 1 && $next.length == 1)
    {
        // I'm fading the text and image child DIVs directly to workaround a problem in IE9 (and
        // probably lower) where fading doesn't work on absolutely positioned child elements under
        // certain conditions (as far as I have figured it out)
        $(".newsitem-text, .newsitem-image", $active).fadeOut(newsFadeSpeed,
            function () { $active.removeClass("active-newsitem") });
        $(".newsitem-text, .newsitem-image", $next).fadeIn(newsFadeSpeed);

        $next.addClass("active-newsitem");

        if ($next.next(".newsitem").length == 0)
            $("#newsarea-nav-next").hide();

        $("#newsarea-nav-prev").show();
    }
}

function prevNewsItem()
{
    var $active = $("#newsarea .active-newsitem");
    var $prev = $active.prev(".newsitem");

    if ($active.length == 1 && $prev.length == 1)
    {
        $(".newsitem-text, .newsitem-image", $active).fadeOut(newsFadeSpeed,
            function () { $active.removeClass("active-newsitem") } );
        $(".newsitem-text, .newsitem-image", $prev).fadeIn(newsFadeSpeed);

        $prev.addClass("active-newsitem");

        if ($prev.prev(".newsitem").length == 0)
            $("#newsarea-nav-prev").hide();

        $("#newsarea-nav-next").show();
    }
}

//$.fx.interval = 33.33;
//$.fx.interval = 5;
//$.fx.off = true;


// Initialize some JavaScript things for page-content, needs to not only be called on load, but also
// after a page is loaded through AJAX (laodPage()).
function initPage()
{
    initContactFormLink();
    initNewsNav();
}


$(function ()
{
    // Don't allow ugly focus decorations in FF and IE on page buttons
    $("#header div.button a").focus(function () { this.blur() });

    // Disable normal link functionality, should only work when JS is disabled
    $("#header div.button a").click(function () { return false });

    initPage();

    /*
    // Load an image and get its size in the background
    var img = $("<img>").load(function ()
    {
        //alert("Image dimensions: "+this.width+"x"+this.height);
    });
    img.attr('src', 'http://www.google.com/intl/en_ALL/images/logo.gif');
    */

});

