﻿jQuery(document).ready(function() {

    jQuery("a").each(function() {
        if (tagLink(this.href)) {
            this.href = pageTracker._getLinkerUrl(this.href);
        }
    });

});

var URLs = new Array('4adventure.com', 'silverpop.com');
var noTagURLs = new Array();

function tagLink(link) {

    if (!link) { return false; }

    // Get rid of http:// and querystring in link (if exists).
    if (link.indexOf('://') > 0)
        link = link.substring(link.indexOf('://') + 3);

    if (link.indexOf('?') > 0)
        link = link.substring(0, link.indexOf('?'));

    if (link.indexOf('/') > 0)
        link = link.substring(0, link.indexOf('/'));

    // First check, if it's in the list of URLs to skip.
    if (typeof (noTagURLs) != 'undefined') {
        if (noTagURLs.length) {
            for (var i = 0; i < noTagURLs.length; i++) {
                if ((noTagURLs[i].length) && (link.indexOf(noTagURLs[i]) >= 0)) {
                    return 0;
                }
            }
        }
    }

    // Get Current URL and clean up.
    var thisURL = new String(window.location);
    if (thisURL.indexOf('://') > 0)
        thisURL = thisURL.substring(thisURL.indexOf('://') + 3);

    if (thisURL.indexOf('?') > 0)
        thisURL = thisURL.substring(0, thisURL.indexOf('?'));

    if (thisURL.indexOf('/') > 0)
        thisURL = thisURL.substring(0, thisURL.indexOf('/'));

    // Don't tag links to current domain.
    if (thisURL == link.substring(link.indexOf('://'), link.indexOf('/'))) {
        return 0;
    }


    for (var i = 0; i < URLs.length; i++) {
        // if (link.indexOf(URLs[i]) >= 0){
        // if ( (link == URLs[i])  || (link.match(regExp))){
        var regExp = new RegExp("\\." + URLs[i] + '$', 'i');
        if (link.match(regExp) || (link == URLs[i])) {
            return 1;
        }
    }

    // If we got this far, link shouldn't be tagged.
    return 0;
}

