


$(document).ready(function() {
    track_files();
});

function track_files() {
    $('a').each(function() {
        if (should_track(this)) {
            $(this).click(track_file);
        }
    });

    $('form#.donate-now').each(function() {
        $(this).submit(function() {
            __utmLinkPost(this);
        });
    });
}

function should_track(link)
{
    return link.protocol == 'mailto:' || $(link).hasClass('pseudo-external') || link.hostname != location.host
        || link.pathname.match(/\.(doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)$/);
}

function track_file(array_element)
{
    var file_path = '';

    if (this.protocol == 'mailto:')
    {
        file_path = '/mail/' + $(this).attr('href').substr(7);
    }
    else
    {
        if (location.host != this.hostname)
        {
            var host = trim(this.hostname, '/');
            file_path = "/external/" + host;
        }

        var path = trim(this.pathname, '/');

        if (path)
            file_path += '/' + path;
    }

    urchinTracker(file_path);
}

/* Remove specified character from beginning or end of string */
function trim(str, chr)
{
    if (str[0] == chr)
        str = str.substring(1);
 
    if (str[str.length - 1] == chr)
        str = str.substring(0, str.length - 1);
 
    return str;
}