(function(){

    TWP.Util.Cookie = function(){

        return {

            // Gets the cookie for the domain
            get : function(){
                return document.cookie;
            },

            /// <summary>Gets the value of the a cookie by name</summary>
            /// <param name="name" type="string">The name of the cookie</param>
            /// <returns>The name's value</returns>
            read : function(name){

                if(!name){
                    return null;
                }

                if(document.cookie.length > 0){

                    var strStart = name + "=";
                    var cookieSplit = document.cookie.split(";");

                    for(var b=0;b<cookieSplit.length;b++){

                        var c = cookieSplit[b];
                        c = (c).replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g, '');
                        
                        if(c.charAt(0) != ''){ // trim whitespace
                            c = c.substring(0, c.length);

                            if(c.indexOf(strStart) == 0){
                                return c.substring(strStart.length, c.length);
                            }
                        }

                    }
                }

                return null;
            },

            /// <summary>Sets a cookie</summary>
            /// <param name="name" type="string">The name of the cookie</param>
            /// <param name="value" type="string">The value for the cookie</param>
            /// <param name="path" type"string"></param>
            /// <param name="expires" type"object">When the cookie will expire, defaults to 30 days</param>
            /// <param name="domain" type="string"></param>
            /// <returns></returns>
            create : function(name, value, path, expires, domain){
                var cookieVal = "";

                if(!name && !expires && !value){
                    return;
                }

                if (!expires) {
                    var d = new Date();
                    d.setTime(d.getTime() + (30*24*60*60*1000));
                    expires = d;
                }

                cookieVal = name + "=" + value + "; expires=" + expires.toGMTString();

                if(path){
                    cookieVal += "; path=" + path;
                }

                if(domain){
                    cookieVal += "; domain=" + domain;
                }

                document.cookie = cookieVal;

            },

            /// <summary>Removes a cookie</summary>
            /// <param name="name">The name of the cookie to remove</param>
            /// <returns></returns>
            remove : function(name){
                this.set(name, "", -1);
            }
        }
    }();
})();

