foresee.capone =
{
    tagEventListeners:function()
    {
        // copy the original method and create a new one
        // that calls the old one, but also writes our cookie
        foresee.oldCloseTrackerWin = foresee.closeTrackerWin;
        foresee.closeTrackerWin = function()
        {
            // check the killTrackerList to see
            // if this anchor is a member
            var bMemberOfKillList=false;

            // we're either on a form or from an anchor
            // either way, we need to extract the url to evaluate
            // and it will be in two different locations depending on the browser
            if( this.event && this.event.srcElement )
            {
                if( this.event.srcElement.form )
                    var href = this.event.srcElement.form.action;
                else if( this.event.srcElement.nodeName.match(/IMG/) )
                    var href = this.event.srcElement.parentNode.getAttribute("href");
                else if( this.event.srcElement.href )
                    var href = this.event.srcElement.href;
            }
            else
            {
                if( this.form )
                    var href = this.form.action;
                else if( this.href )
                    var href = this.href;
            }

            for( i=0; i<foresee.killTrackerList.length; i++ )
            {
                var str = foresee.killTrackerList[i];
                if( href && eval("href.match("+str+")") )
                {
                    bMemberOfKillList = true;
                    break;
                }
            }

            // write our cookie indicating it was closed by the white-list
            // unless the href was found to be on the killTrackerList
            if( !bMemberOfKillList )
                foresee.ForeCStdSetCookie ('ForeSeeWhiteListClosed', 1, null, '/', '.capitalone.com', false );
            else foresee.ForeCStdSetCookie ('ForeSeeWhiteListClosed', 0, null, '/', '.capitalone.com', false );


            if( foresee.scoutClosed )
                return;

            // call the original close function
            var fsrCookie = jQuery.cookie('FSRCookie');
            if( fsrCookie && fsrCookie.match(/\|ScoutRunningCheck=.*==/i) )
                foresee.oldCloseTrackerWin();

            foresee.scoutClosed=true;
        }

        // add the event to all the anchors
        foresee.fsrTagEventListeners( foresee.closeTrackerWin );

        // use the foresee.selectorKillMatches array to
        // attach the closeTrackerWin to the onclick event
        // of various non-anchor inputs
        for( var i=0; i<foresee.selectorKillMatches.length; i++ )
        {
            var selectKillMatch = $(foresee.selectorKillMatches[i]);
            if( selectKillMatch.length>0 )
                selectKillMatch.click( foresee.closeTrackerWin );
        }
    },

    additionalForeSeeSetup:function()
    {
        if (foresee.capone.initialized == true) return;
        
        foresee.capone.initialized = true;

        // if our cookie has been written by the close function
        // then they have come back from going off site, but
        // never actually took the survey they agreed to take
        // and are in the same session.
        var ForeSeeWhiteListClosed = jQuery.cookie('ForeSeeWhiteListClosed');
        if( ForeSeeWhiteListClosed==='1' )
        {
            foresee.ForeCStdSetCookie ('ForeSeeWhiteListClosed', 0, null, '/', foresee.triggerParms["domain"], false );

            // foresee writes this ScoutRunningCheck key/value pair in it's cookie...
            // we must remove it in order to trigger the tracker window
            var fsrCookie = jQuery.cookie('FSRCookie');
            foresee.ForeCStdSetCookie ('FSRCookie', fsrCookie.replace(/\|\|ScoutRunningCheck=ScoutClosed/i,''), null, '/', foresee.triggerParms["domain"], false );
            foresee.openTrackerWin();
        }

        var fsrCookie = jQuery.cookie('FSRCookie');
        if( fsrCookie && fsrCookie.match(/\|ScoutRunningCheck=.*==/i) )
            foresee.capone.tagEventListeners();
    },

    initialized: false
};

if (typeof completeForeseeSetup == 'function')
{
    completeForeseeSetup();
}
else if (typeof(c1zipcode) != 'undefined' && typeof(c1region) != 'undefined')
{
    foresee.fsrWriteCookie('Foresee_zipCode', c1zipcode);
    foresee.fsrWriteCookie('Foresee_zipRegion', c1region);
    foresee.capone.additionalForeSeeSetup();
}

