﻿(function() {
    /*
    ** Externalytics - Pierre Lecocq - AWE
    ** Track your external links thanks to Analytics
    */
SearchPerformance = {            
        virtual_page: '/outgoing',
        virtual_page_downloads: '/outgoing/download',
        virtual_page_mailto: '/outgoing/mailto',

        track_mailto: false,
        track_downloads: false,

        // mydomains = my domain names (those you DO NOT WANT to track)
        //    E.g: 'www.mydomain.com' excludes only this domain
        //    E.g: '.domain.com' excludes the domain and all its subdomains - don't forget the DOT in the begining -
        mydomains: ['.idev', '.123preview.com', '.tchatche.com', 'google-analytics.com', 'xiti.com'],

        // downloads = file extentions concidered as (downloads those you WANT to track)
        downloads: ['.doc', '.docx', '.ppt', '.pptx', '.xls', '.xlsx', '.pdf', '.exe', '.dll', '.so', '.dmg', '.tgz', '.tar', '.tar.gz', '.tar.bz2', '.zip', '.rar'],

        clink: '', // Current link. Must be accessible by other methods. Big up to Hogan's Heroes (Papa Shultz, in french).

        // Here comes the magic
        externalytics: function() {
            if (typeof(pageTracker) == 'undefined')
                return;
                    
            var links = document.getElementsByTagName('a');
            var x = y = n = m = l = flag = oc = reg = 0;

            for (x = 0, n = links.length; x < n; x++) {
                this.clink = links[x];
                // Is domain allowed ?
                for (flag = true, y = 0, m = this.mydomains.length; y < m; y++) {
                    if (this.clink.host.indexOf(this.mydomains[y]) > -1) {
                        flag = false;
                    }
                }

                if (flag == false || (this.clink.host == '' && this.clink.protocol != 'mailto:')) {
                    continue;
                }

                // Already has onClick event ?
                oc = this.clink.getAttribute('onclick');

                if (oc != null && (oc.indexOf('urchinTracker') > -1 || oc.indexOf('_trackPageview') > -1)) {
                    continue;
                }

                // So ... Mess the Fuck Up !                                                     
                this.setAnalOnclick('pageTracker._trackPageview("' + this.virtual_page + '/' + (this.clink.host + this.clink.pathname + this.clink.search) + '")', oc);

                if (this.track_downloads == true) {
                    for (flag = true, y = 0, m = this.downloads.length; y < m; y++) {
                        if (RegExp(this.downloads[y] + '$', 'i').test(this.clink.href) == true) {
                            this.setAnalOnclick('pageTracker._trackPageview("' + this.virtual_page_downloads + '/' + this.clink.host + this.clink.pathname + this.clink.search + '"', oc);
                            break;
                        }
                    }
                }

                if (this.track_mailto == true && this.clink.protocol.indexOf('mailto:') > -1) {
                    this.setAnalOnclick('pageTracker._trackPageview("' + this.virtual_page_mailto + '/' + this.clink.href + '")', oc);
                }
            }
        },

        setAnalOnclick: function(instructions, already_onclick) {
            if ((navigator.userAgent.indexOf("Gecko") > -1) == true) {
                this.clink.setAttribute("onclick", instructions + '; ' + ((already_onclick == null) ? '' : already_onclick));
            }
            else {
                this.clink.onclick = new Function(instructions + '; ' + ((already_onclick == null) ? '' : already_onclick));
            }
        }
    };
})();
