// thanks dojo!
// renamed to fit within our namespace
IMVU.util.Cookie = function() {} 

IMVU.util.Cookie.setCookie = function(name, value, days, path, domain, secure){

    //summary: sets a cookie.
    var expires = -1;
    if((typeof days == "number")&&(days >= 0)){
        var d = new Date();
        d.setTime(d.getTime()+(days*24*60*60*1000));
        expires = d.toGMTString();
    }
    value = escape(value);
    document.cookie = name + "=" + value + ";"
        + (expires != -1 ? " expires=" + expires + ";" : "")
        + (path ? "path=" + path : "")
        + (domain ? "; domain=" + domain : "")
        + (secure ? "; secure" : "");
}
IMVU.util.Cookie.set = IMVU.util.Cookie.setCookie;

IMVU.util.Cookie.getCookie = function(name){
    
    //summary: Gets a cookie with the given name.
    var idx = document.cookie.lastIndexOf(name+'=');
    if(idx == -1) { return null; }
    var value = document.cookie.substring(idx+name.length+1);
    var end = value.indexOf(';');
    if(end == -1) { end = value.length; }
    value = value.substring(0, end);
    value = unescape(value);
    return value; //String
}
IMVU.util.Cookie.get = IMVU.util.Cookie.getCookie;

IMVU.util.Cookie.deleteCookie = function(name){
    
    //summary: Deletes a cookie with the given name.
    IMVU.util.Cookie.setCookie(name, "-", 0);
}

IMVU.util.Cookie.setObjectCookie = function(name, obj, days, path, domain, secure, clearCurrent){
    
    //summary: Takes an object, serializes it to a cookie value, and either
    //sets a cookie with the serialized value.
    //description: If clearCurrent is true, then any current cookie value
    //for this object will be replaced with the the new serialized object value.
    //If clearCurrent is false, then the existing cookie value will be modified
    //with any changes from the new object value.
    //Objects must be simple name/value pairs where the value is either a string
    //or a number. Any other value will be ignored.    
    if(arguments.length == 5){ // for backwards compat
        clearCurrent = domain;
        domain = null;
        secure = null;
    }
    var pairs = [], cookie, value = "";
    if(!clearCurrent){
        cookie = IMVU.util.Cookie.getObjectCookie(name);
    }
    if(days >= 0){
        if(!cookie){ cookie = {}; }
        for(var prop in obj){
            if(obj[prop] == null){
                delete cookie[prop];
            }else if((typeof obj[prop] == "string")||(typeof obj[prop] == "number")){
                cookie[prop] = obj[prop];
            }
        }
        prop = null;
        for(var prop in cookie){
            pairs.push(escape(prop) + "=" + escape(cookie[prop]));
        }
        value = pairs.join("&");
    }
    IMVU.util.Cookie.setCookie(name, value, days, path, domain, secure);
}

IMVU.util.Cookie.getObjectCookie = function(name){
    
    //summary: Gets an object value for the given cookie name.
    var values = null, cookie = IMVU.util.Cookie.getCookie(name);
    if(cookie){
        values = {};
        var pairs = cookie.split("&");
        for(var i = 0; i < pairs.length; i++){
            var pair = pairs[i].split("=");
            var value = pair[1];
            if( isNaN(value) ){ value = unescape(pair[1]); }
            values[ unescape(pair[0]) ] = value;
        }
    }
    return values;
}

IMVU.util.Cookie.isSupported = function(){
    
    //summary: Tests the browser to see if cookies are enabled.
    if(typeof navigator.cookieEnabled != "boolean"){
        IMVU.util.Cookie.setCookie("__TestingYourBrowserForCookieSupport__",
            "CookiesAllowed", 90, null);
        var cookieVal = IMVU.util.Cookie.getCookie("__TestingYourBrowserForCookieSupport__");
        navigator.cookieEnabled = (cookieVal == "CookiesAllowed");
        if(navigator.cookieEnabled){
            // FIXME: should we leave this around?
            this.deleteCookie("__TestingYourBrowserForCookieSupport__");
        }
    }
    return navigator.cookieEnabled; //boolean
}

/*Copyright (c) 2005 JSON.org*/
var JSON = function () {
    var m = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        s = {
            'boolean': function (x) {
                return String(x);
            },
            number: function (x) {
                return isFinite(x) ? String(x) : 'null';
            },
            string: function (x) {
                if (/["\\\x00-\x1f]/.test(x)) {
                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return '\\u00' +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    });
                }
                return '"' + x + '"';
            },
            object: function (x) {
                if (x) {
                    var a = [], b, f, i, l, v;
                    if (x instanceof Array) {
                        a[0] = '[';
                        l = x.length;
                        for (i = 0; i < l; i += 1) {
                            v = x[i];
                            f = s[typeof v];
                            if (f) {
                                v = f(v);
                                if (typeof v == 'string') {
                                    if (b) {
                                        a[a.length] = ',';
                                    }
                                    a[a.length] = v;
                                    b = true;
                                }
                            }
                        }
                        a[a.length] = ']';
                    } else if (x instanceof Object) {
                        a[0] = '{';
                        for (i in x) {
                            v = x[i];
                            f = s[typeof v];
                            if (f) {
                                v = f(v);
                                if (typeof v == 'string') {
                                    if (b) {
                                        a[a.length] = ',';
                                    }
                                    a.push(s.string(i), ':', v);
                                    b = true;
                                }
                            }
                        }
                        a[a.length] = '}';
                    } else {
                        return;
                    }
                    return a.join('');
                }
                return 'null';
            }
        };
    return {
        copyright: '(c)2005 JSON.org',
        license: 'http://www.JSON.org/license.html',

        stringify: function (v) {
            var f = s[typeof v];
            if (f) {
                v = f(v);
                if (typeof v == 'string') {
                    return v;
                }
            }
            return null;
        },

        parse: function (text) {
            try {
                return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                        text.replace(/"(\\.|[^"\\])*"/g, ''))) &&
                    eval('(' + text + ')');
            } catch (e) {
                return false;
            }
        }
    };
}();