﻿function JSONCookie() {

    this.save = function (name, data) {
        $.cookie(name, JSON.stringify(data), { path: '/' });
    };

    this.load = function (name) {
		if($.cookie(name) === undefined) {
			return null;
		}

        return JSON.parse($.cookie(name));
    };

	this.remove = function (name) {
        $.cookie(name, null, { path: '/' });
    };

	this.getValueByKey = function (name, key) {
		var cookie = this.load(name);
		if (!cookie) {
			return null;
		}
		return cookie[key] === undefined ? null : cookie[key];
	};

	this.setValueByKey = function (name, key, value) {
		var cookie = this.load(name);

		if(!cookie) {
			cookie = this.create(name);
		}

		cookie[key] = value;

		this.save(name, cookie);
	};

	this.create = function (name) {
		var defaultValue = {};

		this.save(name, defaultValue);

		return defaultValue;
	};
}
