// Site-wide page-specific initialization.
$(document).ready(function() {

    // Initialize download tracking.
    init_trackDownloads();
    
    // Initialize external urls.
    init_externallinks();

    var megaConfig = {
        interval: 500,
        sensitivity: 7,
        over: addMega,
        timeout: 500,
        out: removeMega
    };
    $('li.mega').hoverIntent(megaConfig);
        

}); // end ready()

function addMega()
{
    $(this).addClass("hovering");
}
    
function removeMega()
{
    $(this).removeClass("hovering");
}
    
// Initialize the pagelinks on a section overview page.
function init_pagelinks()
{
    $('.pagelink').each(function() {
        $(this).find('img').hover(
            function() {
                status = $(this).parent().find('a').attr('href');
            },
            function() {
                status = '';
            }
            );
        var img = $(this).find('img');
        img.click(function() {
            location = $(this).parent().find('a').attr('href');
        });
        img.css('cursor', 'pointer');
    });
}

// Initialize the datelinks on a section overview page.
function init_datelinks()
{
    $('.datelink').each(function() {
        $(this).find('.calendar-date').hover(
            function() {
                status = $(this).parent().find('a').attr('href');
            },
            function() {
                status = '';
            }
            );
        var tag = $(this).find('.calendar-date');
        tag.click(function() {
            location = $(this).parent().find('a').attr('href');
        });
        tag.css('cursor', 'pointer');
    });
}

// Initialize external links to open in a separate tab/window.
function init_externallinks()
{
    var myURL = location.protocol + '//' + location.hostname;
    var ext = $("a[href^='http://']");
    if (ext != null)
        ext.not("[href^='+myURL+']").attr('target', '_blank');
}

// Function called when a jQuery Ajax error occurs.
function onAjaxError(event, request, options, error)
{
    // Restore the default cursor.
    $('body').css('cursor', 'default');
   
    var msg = "I'm sorry, we were unable to complete your request at this time. Please try again later."
    $('#errortext').html('<span class="error">' + msg + '</span>');
    $('#faildialog').dialog('open');
}

// Function to track a download event.
function trackDownload(evt)
{
    if (_gaq != null)
        _gaq.push(['_trackPageview', evt.data.target]);
}

// Function to initialize a page to track any downloads on the page.
function init_trackDownloads()
{
    $("a[href$='.pdf'], a[href$='.mp3']").each(
        function()
        {
            $(this).bind('click', {
                target: $(this).attr('href')
                }, trackDownload);
        }
    );
}
