/*!
 * Google Analytics customizations for "PPFA."
 * Version: 3
 *
 * ! Please don't touch this file without consulting Cardinal Path.
 *
 * Copyright 2011 Cardinal Path
 * @author Eduardo Cereto <ecereto@cardinalpath.com>
 */
(function() {
    // Cardinal Path helper object
    window._cp = window._cp || {};
    /**
     * Used to obtain a value from a string of key/value pairs
     *
     * @private
     */
    function _getVal(l, n, s) {
        if (!l || l == '' || !n || n == '' || !s || s == '') return '-';
        var i, i2, i3, c = '-';
        i = l.indexOf(n);
        i3 = n.indexOf('=') + 1;
        if (i > -1) {
            i2 = l.indexOf(s, i);
            if (i2 < 0) {
                i2 = l.length;
            }
            c = l.substring((i + i3), i2);
        }
        return c;
    }
    /**
     * Set Cookie
     *
     * @private
     */
    function _setCookie(k, v) {
        var c = k + '=' + v;
        c += '; path=/';
        document.cookie = c;
    }
    /**
     * Universal Event Handler
     *
     * @private
     */
    function cp_Event(elem, evt, fnct) {
        var fnct2 = function(event) {
            e = event || window.event;
            fnct.call(elem, e);
        }
        if (elem.addEventListener) {
            elem.addEventListener(evt, fnct, false);
        }
        else if (elem.attachEvent) {
            elem.attachEvent('on' + evt, fnct);
        }
    }
    /**
     * Send Events to the pageTracker when outbound links are clicked
     *
     * @public
     */
    window._cp._trackOutboundLinks = function(pageTracker) {
        if (typeof pageTracker.length === 'undefined') {
            pageTracker = [pageTracker];
        }
        var links = document.links;
        for (var i = 0; i < links.length; i++) {
            if (
                links[i].href.indexOf('http') == 0 &&
                links[i].href.indexOf(document.location.host) < 0
           ) {
                cp_Event(
                    links[i],
                    'mousedown',
                    (function(l) {
                        return function() {
                            var h = l.href.substring(l.href.indexOf('//') + 2);
                            var i = h.indexOf('/') > -1 ? h.indexOf('/') : undefined;
                            var j = h.indexOf('__utma') > -1 ? h.indexOf('__utma') : undefined;
                            for (var k = 0; k < pageTracker.length; k++) {
                                pageTracker[k]._trackEvent(
                                    'Outbound',
                                    h.substring(0, i),
                                    h.substring(i, j)
                                );
                            }
                        }
                    })(links[i])
                );
            }
        }
    }
    /**
     * Persists ocalization query parameter on hire.com(authoria) to send on GA
     * pageviews.
     *
     * @public
     */
    window._cp._getPageLocalized = function(page) {
        if (page === undefined) {
            page = window.location.pathname + window.location.search;
        }
        // Checks if there is a display mode present and set a cookie with it
        var COOKIE_LOC = 'ppfa_displaymode';
        var loc;
        if (page.indexOf('displaymode=') > 0) {
            loc = _getVal(page, 'displaymode=', '&');
            _setCookie(COOKIE_LOC, loc);
        } else {
            // Use the displaymode on the cookie to set a variable with it
            loc = _getVal(document.cookie, COOKIE_LOC + '=', ';');
            if (loc != '-') {
                page += page.indexOf('?') >= 0 ? '&' : '?';
                page += 'displaymode=' + loc;
            }
        }
        return page;
    }
})();
