(function(){

    TWP.Util.User = function(){
        var initialized = false;
        var isAuthenticated = false;
        var userName = null;
        var userId = "";
        var userCookie = null;
        var isFacebookUser = false;
        var userType = "washington-post";
        var validCookies = ["fbuname", "wpniuser"];

        function parseCookieUserName(cookie, startStr, offset){
            var start = -1, end = -1;
            start = (cookie.indexOf(startStr) + offset);
            end = (userCookie.indexOf(";", start)) == -1 ? userCookie.length : userCookie.indexOf(";",start);

            return cookie.substring(start, end);
        }

        function setCustomCookie(){
            var upcUrl = document.location.href;

            if(upcUrl.indexOf("localhost") > -1){
                var c = TWP.Util.Cookie.get();

                if(c.indexOf("WPNIUCID") == -1){
                    var d = new Date();
                    var i = "WPNI" + d.getTime() + "." + Math.round(Math.random()*10000);
                    d.setTime(d.getTime() + 31104000000);
                    TWP.Util.Cookie.create("WPNIUCID", i, "/", d, ".washingtonpost.com");
                }
            }
        }

        return {
            // Determine whether the user has the appropriate cookie
            init : function(){

                if(initialized){
                    return;
                }

                initialized = true;

                var userCookieName = "";

                // START: TEST
                // add a cookie for testing
                //TWP.Util.Cookie.create("headzip", "20009", "/");
                // END: TEST

                for(var a=0;a<validCookies.length;a++){
                    if(document.cookie.indexOf(validCookies[a]) != -1){
                        userCookie = document.cookie;
                        userType = validCookies[a];
                        isAuthenticated = true;
                        break;
                    }
                }

                if(isAuthenticated){
                    switch(userType){
                        case 'fbuname' :
                            userCookieName = parseCookieUserName(userCookie, userType, 8);
                            break;
                        default :
                            userCookieName = parseCookieUserName(userCookie, userType, 9);
                            break;
                    }

                    userName = (userCookieName.indexOf("@") !== -1) ? userCookieName.substring(0, userCookieName.indexOf("@")) : userCookieName;
                    userId = userCookieName;
                }

                setCustomCookie();
            },

            getUserCookie : function(){
                return (userCookie.length > 0 && typeof userCookie == "string") ? userCookie : null;
            },

            getUserType : function(){
                return (userType.length > 0 && typeof userType == "string") ? userType : null;
            },

            getUserId : function(){
                return userId;
            },

            getUserName : function(){
                return (typeof userName == "string") ? userName : "";
            },

            getAuthentication : function(){
                return isAuthenticated;
            }
        }

    }();
})();

