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


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


RONE.SubNav = {
    
    /**
     * Registers the onclick handler for sub-navigation menu items whose
     * link targets are just anchors.
     */
    init : function ()
    {
        $$('#subNav A').each(function (link) {
            if (link.hash !== '#') {
                link.observe('click', RONE.SubNav.setCurrent);
            }
        });
    },

    setCurrent : function ()
    {
        this.up('UL').select('A.current').each(function (link) {
            link.removeClassName('current');
        });
        this.addClassName('current');
    }
    
};
document.observe('dom:loaded', RONE.SubNav.init);



RONE.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.slice(-13) != 'response1.com') && (link.hostname != 'response1.spenlen.com')) ||
                (suffix == '.pdf')) {
                    if (isInternetExplorer) {
                        link.target = '_blank';
                    }
                    link.observe('click', RONE.Links.externalLinkOnClick);
            } else if ((link.hasClassName('download')) ||
                       (suffix == '.doc') || (suffix == '.xls') || (suffix == '.ppt')) {
                    link.observe('click', RONE.Links.downloadLinkOnClick);
            }
        });
    },
    
    /**
     * onclick handler for PDF and external links. Opens the link target URL
     * in a new browser window.
     */
    externalLinkOnClick : function (event)
    {
        RONE.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)
    {
        RONE.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.slice(-13) != 'response1.com') && (link.hostname != 'response1.spenlen.com')) {
            pageTracker._trackPageview('/outbound/' + link.href);
        } else {
            pageTracker._trackPageview('/downloads/' + link.pathname);
        }
    }
    
};
document.observe('dom:loaded', RONE.Links.init);
