// Create namespaces
QL.namespace('application');

// quickenloans application function
QL.application = function()
{	
    /**
     * This rule will find any anchor element
     * that is not relative and open it in a 
	 * new window - potential for short-sightedness at HIGH
     */
    var openNewTargetWindow = function()
    {
        $E.addListener(document, 'click', function(e) {
            // Get element
            var el = $E.getTarget(e);
			
            // PDFs must go in new windows
            if(el.nodeName.toLowerCase() === 'button' && el.className == 'pdf') {
                var parentForm = $D.getAncestorByTagName( el, 'form' );
                parentForm.target = 'pdf_window';
            }

            // Internal Pops
            if(el.nodeName.toLowerCase() === 'a' && $D.hasClass(el, 'external_window') ) {
                // Stop event
                $E.preventDefault(e);

                // Pop window
                if ( el.getAttribute('id') == "mail_a_friend") {
                QL.utilities.Window.open(el.getAttribute('href', 1), { width: 620, height: 600, center: 1, resizable: 1, scrollbars: 0 });
                return;
            }
            QL.utilities.Window.open(el.getAttribute('href', 1), { width: 800, height: 600, center: 1, resizable: 1, scrollbars: 1 });

            }
            // Internal Pops (Term)
            if(el.nodeName.toLowerCase() === 'a' && $D.hasClass(el, 'external_window_helper') ) {
                // Stop event
                $E.preventDefault(e);

                QL.utilities.Window.open(el.getAttribute('href', 1), { width: 600, height: 300, center: 1, resizable: 1, scrollbars: 1 });
            }

            // Internal Pops (Term)
            if(el.nodeName.toLowerCase() === 'a' && $D.hasClass(el, 'external_window_helper') ) {
                // Stop event
                $E.preventDefault(e);

                QL.utilities.Window.open(el.getAttribute('href', 1), { width: 600, height: 300, center: 1, resizable: 1, scrollbars: 1 });
            }

            // Small Internal Popup
            if(el.nodeName.toLowerCase() === 'a' && $D.hasClass(el, 'more_info') ) {
                // Stop event
                $E.preventDefault(e);

                QL.utilities.Window.open(el.getAttribute('href', 1), { width: 350, height: 240, center: 1, resizable: 1, scrollbars: 1 });
            }

            // External Pops
            if (el.nodeName.toLowerCase() === 'a' && 
                ( (el.getAttribute('href', 2).match('http://') || el.getAttribute('href', 2).match('https://')) && (!el.getAttribute('href', 2).match( location.host ) ) && (!el.getAttribute('href', 2).match( '://my' ))  )) {
                // Stop event
                $E.preventDefault(e);

                // Pop window
                QL.utilities.Window.open(el.getAttribute('href', 1));
            }
            // New window for internal link with classname new_tabbed_window
            if(el.nodeName.toLowerCase() === 'a' && $D.hasClass(el, 'new_tabbed_window')){
                // Stop event
                $E.preventDefault(e);
	           
               	// Pop window
              	QL.utilities.Window.open(el.getAttribute('href', 1));
            }            
            // External Pops for linked images
            if( (el.nodeName.toLowerCase() === 'img' ) && $D.hasClass(el, 'external_window')) {
                    //Find the intended destination in the parent
                    var parentAnchor = $D.getAncestorByTagName(el, "a");

                    // Stop event
                    $E.preventDefault(e);

                    // Pop window
                    QL.utilities.Window.open(parentAnchor.getAttribute('href', 2));
            }
        });
    };
    
    /*
    * Set CM element tag on BBB logo links accross site
    * and in footer. (172369)
    */
    var bbbCoreTags = function()
    {
        // If BBB logo is clicked that is NOT the footer logo send to CM
        if($D.getElementsByClassName('bbbLogo')){
            var bbbLogo = $D.getElementsByClassName('bbbLogo'),
                url = window.location.pathname;
    
            $D.batch(bbbLogo, function(el){
                $E.on(el, 'click', function(ev){
                    var elAltText = $E.getTarget(ev).getAttribute('alt');
                    cmCreatePageElementTag( elAltText, url );
                });
            });
        }
        
        // If footer BBB logo is clicked send to CM
        if($D.get('navigation_accolades_betterBusinessBurow')){ 
            var footerBBBLogo = $D.get('navigation_accolades_betterBusinessBurow').firstChild;
            
            $D.batch(footerBBBLogo, function(el){
                $E.on(el, 'click', function(ev){
                    var elDescription =  $E.getTarget(ev).firstChild.nodeValue + ' ' + 'footer';
                    cmCreatePageElementTag( elDescription, url );
                });
            });
        }
        
        // If BBB logo without generic class name
        if($D.get('better_business_burow_icon')){ 
            var thirdVersionBBBLogo = $D.get('better_business_burow_icon');
            
            $E.on(thirdVersionBBBLogo, 'click', function(ev){
                var elDescription = $E.getTarget(ev).firstChild.nodeValue;
                cmCreatePageElementTag( elDescription, url );
            });
        }
    };

    /* Add class cmpe_tag to links
     * for core page element tags.
     * Passes to cmCreatePageElementTag() the
     * page url and the text within the link.
     */
    var coreMetricsPageElementTag = function()
    {
        // If there are elements on the page with class .cmpe_tag, continue
        if( $S.query('.cmpe_tag') == false ) { return false; }

        // Set page url and collect all page elements with class .cmpe_tag
        var url     = window.location.pathname,
            element = $S.query('.cmpe_tag');

        // On click, pass cmCreatePageElementTag the page url and element value
        $E.on(element, 'click', function(e) {
            cmCreatePageElementTag($E.getTarget(e).firstChild.nodeValue, url);
        });
    }

    return {

        init: function()
        {
            $E.onDOMReady(function() {
                openNewTargetWindow();
                coreMetricsPageElementTag();
                bbbCoreTags();
            });						
        }
    };

}();

// Run app
QL.application.init();
