var _GA_SCOPE_VISITOR = 1,
    _GA_SCOPE_SESSION = 2,
    _GA_SCOPE_PAGE = 3,
    gaAdns;
if (!window.console) {
    window.console = {};
}
console.log = console.log ||
function () {};
console.info = console.info || console.log;
console.dir = console.dir || console.log;
console.group = console.group || console.log;
console.groupEnd = console.groupEnd ||
function () {};
(function ($) {
    var cpr = "gaAdns v2.1.3, Copyright 2010-2011, Stephane Hamel - gaAdns.com. Licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License";
    String.prototype.mreplace = function (h, o) {
        o = o || "g";
        var s = this;
        for (k in h) {
            if (Object.prototype.hasOwnProperty.call(h, k)) {
                s = s.replace(new RegExp(k, o), h[k]);
            }
        }
        return s;
    };
    gaAdns = {
        _: {
            _4Q: {
                account: "",
                loc: "4Q-WEB2",
                lID: 1,
                src: "http://4qinvite.4q.iperceptions.com/1.aspx",
                allowLinker: true,
                domainName: "none"
            },
            _formAnalysis: {
                onBounce: true,
                mode: "change",
                minFields: 3,
                formList: "",
                lastForm: null,
                lastField: "Empty",
                selector: "form"
            },
            _options: {
                debug: false,
                delay: 100
            },
            _setDayOfWeek: {
                index: 1,
                name: "DayOfWeek",
                value: null,
                scope: _GA_SCOPE_SESSION,
                days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
            },
            _setDualMode: {
                path: "//" + location.host + "/__utm.gif"
            },
            _setXDomain: {
                domainName: null,
                action: "click",
                include: null,
                exclude: /^$/,
                url: null,
                selector: "a"
            },
            _trackDownload: {
                onBounce: true,
                method: "page",
                category: "download",
                action: "click",
                value: 0,
                include: /\.(docx*|xlsx*|pptx*|exe|rar|zip|pdf|xpi|mp3|mp4|flv|wmv)$/i,
                exclude: /^$/,
                format: "/%category%/%action%/%hostname%%pathname%",
                selector: "a"
            },
            _trackError: {
                method: "event",
                category: "error",
                error: /(404|500)/,
                value: 0
            },
            _trackLoadTime: {
                onBounce: true,
                category: "loadTime",
                method: "event",
                bucket: [100, 500, 1500, 2500, 5000],
                time: (new Date).getTime(),
                value: 0,
                index: 1,
                scope: _GA_SCOPE_PAGE
            },
            _trackMailTo: {
                onBounce: true,
                method: "event",
                category: "mailto",
                action: "click",
                value: 0,
                include: /./,
                exclude: /^$/,
                selector: "a"
            },
            _trackOutbound: {
                onBounce: true,
                method: "event",
                category: "outbound",
                action: "click",
                value: 0,
                include: /^$/,
                exclude: /^$/,
                format: "/%category%/%action%/%hostname%%pathname%",
                selector: "a"
            },
            _trackPageview: {
                title: document.title,
                url: document.href,
                delay: 100
            },
            _trackRealBounce: {
                category: "NoBounce",
                delay: 30,
                value: 0
            }
        },
        isBounce: function () {
            var kv = document.cookie.match("(^|;) ?__utmb=([^;]*)(;|$)");
            return (kv ? parseInt(kv[2].split(/\./)[1]) : 1) === 1;
        },
        _4Q: function () {
            var a = arguments[1];
            window.pageTracker4Q = _gaq._getAsyncTracker();
            _gaq._push([a.tracker + "_setAllowLinker", a.allowLinker], [a.tracker + "_setDomainName", a.domainName]);
            $.getScript(a.src + "?sdfc=" + a.account + "&lID=" + a.lID + "&loc=" + a.loc);
        },
        _formAnalysis: function () {
            $.extend(gaAdns._._formAnalysis, arguments[1]);
            $(document).ready(function () {
                var a = gaAdns._._formAnalysis;
                $(a.selector).each(function () {
                    var n = $(this).attr("name");
                    if (n && (!a.formList || a.formList === n) && $(":input", this).not(":button,:hidden,:submit").length >= a.minFields) {
                        $(":input", this).bind(a.mode, function () {
                            gaAdns._._formAnalysis.lastForm = $(this).parents("form").attr("name"), gaAdns._._formAnalysis.lastField = $(this).attr("name");
                        });
                        $(this).submit(function (e) {
                            $(window).unbind("beforeunload.gaAdns");
                            var a = gaAdns._._formAnalysis;
                            if (!a.onBounce && gaAdns.isBounce() && a.lastForm) {
                                _gaq.push([a.tracker + "_trackEvent", "form analysis:" + a.lastForm, e.type, a.lastField]);
                                var form = this;
                                _gaq.push(function () {
                                    form.submit();
                                });
                                e.preventDefault();
                                return false;
                            }
                        });
                        $(window).bind("beforeunload.gaAdns", function () {
                            var a = gaAdns._._formAnalysis;
                            if (!a.onBounce && gaAdns.isBounce() && a.lastForm) {
                                _gaq.push([a.tracker + "_trackEvent", "form analysis:" + a.lastForm, "unload", a.lastField]);
                            }
                        });
                    }
                });
            });
        },
        _options: function () {
            $.extend(gaAdns._._options, arguments[1]);
        },
        _setDayOfWeek: function () {
            if (!gaAdns.isBounce()) {
                return;
            }
            var a = arguments[1];
            a.value = a.value || a.days[(new Date).getDay()];
            _gaq._push([a.tracker + "_setCustomVar", a.index, a.name, a.value, a.scope]);
        },
        _setDualMode: function () {
            var a = arguments[1];
            _gaq._push([a.tracker + "_setLocalGifPath", a.path + "#" + document.cookie.replace(/;\s*/g, "&")], [a.tracker + "_setLocalRemoteServerMode"]);
        },
        _setXDomain: function () {
            var a = arguments[1],
                url = location.href;
            if (a.url) {
                url = a.url;
            }
            var cd = null;
            if (a.domainName && a.include && ((new RegExp(a.domainName, "i")).test(url) || (cd = url.match(a.include)))) {
                if (!/\\\//.test(a.include)) {
                    _gaq._push([a.tracker + "_setDomainName", cd ? cd[0] : a.domainName], [a.tracker + "_setAllowLinker", true], [a.tracker + "_setAllowHash", false]);
                } else {
                    if (cd) {
                        _gaq._push([a.tracker + "_setDomainName", "none"], [a.tracker + "_setAllowLinker", true]);
                    } else {
                        _gaq._push([a.tracker + "_setAllowLinker", true], [a.tracker + "_setDomainName", a.domainName], [a.tracker + "_setAllowHash", false]);
                    }
                }
            }
            if (a.domainName && !a.include && (new RegExp(a.domainName, "i")).test(url)) {
                _gaq._push([a.tracker + "_setDomainName", a.domainName], [a.tracker + "_addIgnoredRef", a.domainName]);
                gaAdns._._setXDomain.include = a = /^$/;
            }
            if (!a.domainName && a.include && /^\//.test(a.include) && (new RegExp(a.include, "i")).test(url)) {
                _gaq._push([a.tracker + "_setCookiePath", a.include]);
            }
            $(document).ready(function () {
                var a = gaAdns._._setXDomain;
                if (a.include) {
                    $(a.selector).each(function () {
                        var h = $(this).attr("href");
                        if (a.include.test(h) && !a.exclude.test(h) && !/(mailto\:|__utma)/.test(h)) {
                            $(this).mouseup(function (e) {
                                if (/__utma/.test(this.href)) {
                                    return;
                                }
                                var pageTracker = a.tracker ? _gat._getTrackerByName(a.tracker.replace(/\./, "")) : _gat._getTrackerByName(),
                                    n = pageTracker._getLinkerUrl(this.href);
                                if (gaAdns._._options.debug) {
                                    console.info("_getLinkerUrl: " + this.href + " > " + n);
                                }
                                this.href = n;
                            });
                        }
                    });
                }
            });
        },
        _trackDownload: function () {
            $(document).bind("ready", arguments[1], function (e) {
                $(e.data.selector).each(function () {
                    var h = $(this).attr("href");
                    if (e.data.include.test(h) && !e.data.exclude.test(h)) {
                        $(this).bind("mouseup", e.data, function (e) {
                            if (!e.data.onBounce && gaAdns.isBounce()) {
                                return;
                            }
                            if (e.data.action === "click" && e.which !== 1) {
                                return;
                            }
                            var v = this.href.match(/\/\/(.+)$/)[1],
                                t = this.target.length ? this.target : "_self",
                                ftype = this.pathname.match(/\.([^\.]+)/);
                            var p = e.data.format.mreplace({
                                '%category%': e.data.category,
                                '%action%': e.which === 1 ? "click" : "mouseup",
                                '%target%': t,
                                '%value%': e.data.value,
                                '%pagename%': document.title,
                                '%title%': $(this).text(),
                                '%hash%': this.hash,
                                '%host%': this.host,
                                '%hostname%': this.hostname,
                                '%href%': this.href,
                                '%pathname%': this.pathname,
                                '%port%': this.port,
                                '%protocol%': this.protocol,
                                '%search%': this.search,
                                '%filetype%': ftype ? ftype[1] : "n/a",
                                '%location.hash%': document.location.hash,
                                '%location.host%': document.location.host,
                                '%location.hostname%': document.location.hostname,
                                '%location.href%': document.location.href,
                                '%location.pathname%': document.location.pathname,
                                '%location.port%': document.location.port,
                                '%location.protocol%': document.location.protocol,
                                '%location.search%': document.location.search,
                                '%location%': document.location
                            });
                            if (e.data.method === "event") {
                                var ar = p.split(/\//);
                                _gaq.push([e.data.tracker + "_trackEvent", ar.slice(1, 2).toString(), ar.slice(2, 3).toString(), ar.slice(3).join("/"), e.data.value]);
                            } else {
                                _gaq.push([e.data.tracker + "_trackPageview", p]);
                            }
                            if (e.which === 1 && !e.metaKey && t !== "_blank") {
                                var href = this.href;
                                _gaq.push(function () {
                                    window.open(href, t);
                                });
                                e.preventDefault();
                                return false;
                            }
                        });
                    }
                });
            });
        },
        _trackError: function () {
            var a = arguments[1];
            $.ajax({
                type: "HEAD",
                url: this.href,
                complete: function (XMLHttpRequest, textStatus) {
                    if (a.error.test(XMLHttpRequest.status)) {
                        _gaq._push(a.method === "event" ? [a.tracker + "_trackEvent", a.category, XMLHttpRequest.status, document.referrer, a.value] : [a.tracker + "_trackPageview", "/" + a.category + "/" + XMLHttpRequest.status + "/" + document.referrer]);
                    }
                }
            });
        },
        _trackLoadTime: function () {
            $.extend(gaAdns._._trackLoadTime, arguments[1]);
            $(document).ready(function () {
                var a = gaAdns._._trackLoadTime;
                if (!a.onBounce && gaAdns.isBounce()) {
                    return;
                }
                var diffTime = (new Date).getTime() - a.time,
                    i, bucketString;
                if (typeof a.bucket === "object") {
                    for (i = 0; i < a.bucket.length; i++) {
                        if (diffTime < a.bucket[i]) {
                            bucketString = i ? a.bucket[i - 1] + "-" + (a.bucket[i] - 1) : "0-" + (a.bucket[0] - 1);
                            break;
                        }
                    }
                    if (!bucketString) {
                        bucketString = a.bucket[a.bucket.length - 1] + "+";
                    }
                } else {
                    i = parseInt(diffTime / (a.bucket * 1000));
                    bucketString = i * a.bucket + "-" + ((i + 1) * a.bucket - 1);
                }
                _gaq.push(a.method === "event" ? [a.tracker + "_trackEvent", a.category, bucketString, "", a.value ? a.value : diffTime] : [a.tracker + "_setCustomVar", a.index, a.category, bucketString, _GA_SCOPE_PAGE]);
            });
        },
        _trackMailTo: function () {
            $(document).bind("ready", arguments[1], function (e) {
                $(e.data.selector).each(function () {
                    var h = $(this).attr("href");
                    if (!/^mailto:/i.test(h)) {
                        return;
                    }
                    if (e.data.include.test(h) && !e.data.exclude.test(h)) {
                        $(this).bind("mouseup", e.data, function (e) {
                            if (!e.data.onBounce && gaAdns.isBounce()) {
                                return;
                            }
                            if (e.data.action === "click" && e.which !== 1) {
                                return;
                            }
                            var v = this.href.match(/mailto:([^?]+)/)[1];
                            _gaq.push(e.data.method === "event" ? [e.data.tracker + "_trackEvent", e.data.category, e.data.action, v, e.data.value] : [e.data.tracker + "_trackPageview", "/" + e.data.category + "/" + v]);
                            setTimeout("location.href='" + this.href + "'", gaAdns._._options.delay);
                            this.href = "javascript:void()";
                            e.preventDefault();
                            return false;
                        });
                    }
                });
            });
        },
        _trackOutbound: function () {
            $(document).bind("ready", arguments[1], function (e) {
                if (e.data.exclude.toString() === "/^$/") {
                    e.data.exclude = new RegExp(location.hostname, "i");
                }
                $(e.data.selector).each(function () {
                    var h = this.href;
                    //console.info("_outbound:" + h);
                    if (!/^http/i.test(h)) {
                        return;
                    }
                    if (e.data.include.test(h) || !e.data.exclude.test(h) && (!gaAdns._._setXDomain.include || gaAdns._._setXDomain.include && !gaAdns._._setXDomain.include.test(h))) {
                        $(this).bind("mouseup", e.data, function (e) {
                            if (!e.data.onBounce && gaAdns.isBounce()) {
                                return;
                            }
                            if (e.data.action === "click" && e.which !== 1) {
                                return;
                            }
                            var h = $(this).attr("href"),
                                v = this.href.match(/\/\/(.+)$/)[1],
                                t = this.target.length ? this.target : "_self",
                                ftype = this.pathname.match(/\.([^\.]+)/);
                            var p = e.data.format.mreplace({
                                '%category%': e.data.category,
                                '%action%': e.which === 1 ? "click" : "mouseup",
                                '%target%': t,
                                '%value%': e.data.value,
                                '%pagename%': document.title,
                                '%title%': $(this).text(),
                                '%hash%': this.hash,
                                '%host%': this.host,
                                '%hostname%': this.hostname,
                                '%href%': this.href,
                                '%pathname%': this.pathname,
                                '%port%': this.port,
                                '%protocol%': this.protocol,
                                '%search%': this.search,
                                '%filetype%': ftype ? ftype[1] : "n/a",
                                '%location.hash%': document.location.hash,
                                '%location.host%': document.location.host,
                                '%location.hostname%': document.location.hostname,
                                '%location.href%': document.location.href,
                                '%location.pathname%': document.location.pathname,
                                '%location.port%': document.location.port,
                                '%location.protocol%': document.location.protocol,
                                '%location.search%': document.location.search,
                                '%location%': document.location
                            });
                            if (e.data.method === "event") {
                                var ar = p.split("/");
                                _gaq.push([e.data.tracker + "_trackEvent", ar.slice(1, 2).toString(), ar.slice(2, 3).toString(), ar.slice(3).join("/"), e.data.value]);
                            } else {
                                _gaq.push([e.data.tracker + "_trackPageview", p]);
                            }
                            if (e.which === 1 && !e.metaKey && t !== "_blank") {
                                var href = this.href;
                                _gaq.push(function () {
                                    window.open(href, t);
                                });
                                e.preventDefault();
                                return false;
                            }
                        });
                    }
                });
            });
        },
        _trackPageview: function () {
            if (typeof arguments[1] === "object") {
                var a = arguments[1],
                    oTitle = document.title;
                document.title = a.title;
                _gaq._push([arguments[0], a.url]);
                setTimeout("document.title = '" + oTitle + "'", a.delay);
            } else {
                _gaq._push([arguments[0], arguments[1]]);
            }
        },
        _trackRealBounce: function () {
            $(document).bind("ready", arguments[1], function (e) {
                if (!gaAdns.isBounce()) {
                    return;
                }
                setTimeout("_gaq.push(['" + e.data.tracker + "_trackEvent','" + e.data.category + "','over " + e.data.delay + " seconds', ''," + e.data.value + "])", e.data.delay * 1000);
            });
        }
    };
    Array.prototype.gaAdns = function () {
        this._push = Array.prototype.push;
        this.push = function () {
			for (v = 0; v < arguments.length; v++) {
                var a = arguments[v];
				/*2011-03-10:maxime.gagne@cossette.com: prevent error when one type of argument is a function */
				if(typeof a == 'function')
					continue;
                var tracker = a[0].split(".");
                var method = tracker.length & 2 ? tracker[1] : tracker[0];
                if (gaAdns[method] && method !== "_trackPageview" || method === "_trackPageview" && typeof a[1] === "object") {
                    if (a.length > 1) {
                        a[1] = $.extend({}, gaAdns._[method], a[1]);
                    } else {
                        a[1] = gaAdns._[method];
                    }
                    a[1].tracker = tracker.length & 2 ? tracker[0] + "." : "";
                    if (gaAdns._._options.debug) {
                        console.group("gaAddons." + method);
                        console.dir(a[1]);
                        console.groupEnd();
                    }
                    gaAdns[method].apply(this, a);
                } else {
                    if (gaAdns._._options.debug) {
                        console.group("ga." + a[0]);
                        a[1] ? console.dir(a[1]) : "";
                        console.groupEnd();
                    }
                    this._push.call(this, a);
                }
            }
            return arguments.length;
        };
    };
})(jQuery);
var _gaq = [];
_gaq.gaAdns();
