// Dirty, awful solution that I'm very sorry for. 
function cmCreatePageElementTag() { return true; };

// 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' )) && (!el.getAttribute('href', 2).match( '://www.onereversemortgage.com' )) )) {
                // 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));
            }
        });
    };
    

    return {

        init: function()
        {
            $E.onDOMReady(function() {
                openNewTargetWindow();
                QL.utilities.Anchor.prepareFauxAnchors();
            });						
        }
    };

}();

// Run app
QL.application.init();
