var DealerWebsite = DealerWebsite || {};
DealerWebsite.Utils = DealerWebsite.Utils || {};

DealerWebsite.Utils.CookieManager = function(cookieName, defaultCookieValue) {
	var cookie;
	var external = {};
	var internal = {};
	external = {
	    getValue : function(paramName, defaultValue) {
		    var cookieValue = internal.getCookieValue(cookieName);
		    value = $.evalJSON(cookieValue)[paramName];
		    if (value === undefined) {
                return defaultValue;
		    }
		    return value;
	    },
	    
	    hasValue : function(paramName) {
            var cookieValue = internal.getCookieValue(cookieName);
		    value = $.evalJSON(cookieValue)[paramName];
		    return (value !== undefined);
	    },

	    setValue : function(variableName, variableValue) {
		    var cookieValue = $.evalJSON(internal.getCookieValue(cookieName));
		    cookieValue[variableName] = variableValue;
		    internal.setCookieValue(cookieName, cookieValue);
	    },

	    clearCookie : function() {
		    $.cookie(cookieName, null, {
			    path : '/'
		    });
	    },

	    setValueIfEmpty : function(variableName, variableValue) {
		    if (external.getValue(variableName) === undefined) {
			    var cookieValue = $.evalJSON(internal
			            .getCookieValue(cookieName));
			    cookieValue[variableName] = variableValue;
			    internal.setCookieValue(cookieName, cookieValue);
		    }
	    },

	    getCookieKeys : function() {
		    var cookieKeys = [];
		    var key;
		    var cookieValue = $.evalJSON($.cookie(cookieName));
		    var str = "";
		    for (key in cookieValue) {
			    if (cookieValue.hasOwnProperty(key)) {
				    cookieKeys.push(key);
			    }
		    }

		    return cookieKeys;
	    }
	};

	internal = {
	    setInitialValue : function(cookieValue) {
		    internal.setCookieValue(cookieName, cookieValue);
	    },

	    getOrCreateCookie : function(cookieName, initialCookieValue) {
		    if (internal.getCookieValue(cookieName) === null) {
			    internal.setInitialValue(initialCookieValue);
		    }

		    cookie = internal.getCookieValue(cookieName);
	    },

	    getCookieValue : function(cookieName) {
		    return $.cookie(cookieName);
	    },

	    setCookieValue : function(cookieName, cookieValue) {
		    $.cookie(cookieName, $.toJSON(cookieValue), {
			    path : '/'
		    });
	    }
	};

	internal.getOrCreateCookie(cookieName, defaultCookieValue);
	return external;
};
