var helpers = {

    getCookie: function(name) {
        var pattern = new RegExp('(^|; )' + name + '=([^;]*)');
        var m = document.cookie.match(pattern);
        return m && unescape(m[2]);
    },

    setCookie: function(name, value, days, path, domain, secure) {
        var c = name + '=' + escape(value);
        var expires = null;
        if (days) {
            expires = new Date(new Date().getTime() + (days * 24 * 60 * 60 * 1000));
        }
        if (expires) {
            c += '; expires=' + expires.toUTCString();
        }
        if (path) {
            c += '; path=' + path;
        }
        if (domain) {
            c += '; domain=' + domain;
        }
        if (secure) {
            c += '; secure';
        }
        document.cookie = c;
        return "woo";
    },

    parseUri: function(str) {
        var o = {
            strictMode: false,
            key: ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "anchor"],
            q: {
                name: "queryKey",
                parser: /(?:^|&)([^&=]*)=?([^&]*)/g
            },
            parser: {
                strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
                loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
            }
        },
            m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
            uri = {},
            i = 14;

        while (i--) {
            uri[o.key[i]] = m[i] || "";
        }

        uri[o.q.name] = {};
        uri[o.key[12]].replace(o.q.parser, function($0, $1, $2) {
            if ($1) {
                uri[o.q.name][$1] = $2;
            }
        });

        return uri;
    }
    
};

var uri = helpers.parseUri(window.location.href);

var mocTracking = {

    init: function(hrefMatches) {
        // "MOC123" is a fallback for example purposes
        // would normally be `var mocVal = uri.queryKey.c || helpers.getCookie('moc');`
        var mocVal = uri.queryKey.c || helpers.getCookie('moc') || "";
        if (mocVal) {
            helpers.setCookie('moc', mocVal, 0, '/');
            for (var a in hrefMatches) {
                $('a[href*="' + hrefMatches[a] + '"]').each(function() {
                    this.href = mocTracking.addHrefParam(this.href, 'c=' + mocVal);
                });
            }
        }
    },

    addHrefParam: function(current, newParam) {
        var currentUri = helpers.parseUri(current),
            divider = '?';

        if (currentUri.query) {
            divider = '&';
        }
        return current + divider + newParam;
    }

};

$(document).ready(function() {
    // Array of strings that will be matched in any href on the page
    // Matches will have the MOC appened to the link
    var linkArray = ["cfliving"];
    mocTracking.init(linkArray);
});
