/**
 * app.com site JavaScript.
 *
 * @package    App
 * @subpackage UI
 * @copyright  Copyright 2009 Spenlen Media, Inc. (http://spenlen.com)
 * @version    $Id$
 */


/**
 * App global namespace object.
 * @var Object
 */
var App = {};


App.Links = {
    
    /**
     * Looks for links to PDF documents or to external content and adds an
     * onclick handler to open those links in a new browser window.
     */
    init : function ()
    {
        $$('A').each(function (link) {
            var suffix = link.pathname.slice(-4);
            if ((link.hasClassName('external')) ||
                ((link.hostname != '') && (link.hostname.indexOf('fiveonfivemag') == -1)) ||
                (suffix == '.pdf')) {
                    if (isInternetExplorer) {
                        link.target = '_blank';
                    }
                    link.observe('click', App.Links.externalLinkOnClick);
            } else if ((link.hasClassName('download')) ||
                       (suffix == '.doc') || (suffix == '.xls') || (suffix == '.ppt')) {
                    link.observe('click', App.Links.downloadLinkOnClick);
            }
        });
    },
    
    /**
     * onclick handler for PDF and external links. Opens the link target URL
     * in a new browser window.
     */
    externalLinkOnClick : function (event)
    {
        App.Links.trackLinkClick(this);
        if (! isInternetExplorer) {
            event.stop();
            window.open(this.href);
        }
    },
    
    /**
     * onclick handler for download links. Registers the link click with
     * Google Analytics.
     */
    downloadLinkOnClick : function (event)
    {
        App.Links.trackLinkClick(this);
    },
    
    /**
     * Helper function for external links, downloadable documents, and any other
     * links for which we want to track clicks in Google Analytics.
     */
    trackLinkClick : function (link)
    {
        if (typeof pageTracker == 'undefined') {
            return;
        }
        if (((link.protocol == 'http:') || (link.protocol == 'https:')) && (link.hostname.indexOf('fiveonfivemag') == -1)) {
            pageTracker._trackPageview('/outbound/' + link.href);
        } else {
            pageTracker._trackPageview('/downloads/' + link.pathname);
        }
    }
    
};
document.observe('dom:loaded', App.Links.init);
