﻿//intercepts all requests for set/get a cookie
//redirects to db storage
var SESSION_GUID;
var cookieCache = new Cache();

jQuery.cookie = function(guid, name, value, options) {
    var r;
    var opt = $.extend({ async: true }, options);

    if (name == undefined) return null;
    
    //set a cookie
    if (typeof value != 'undefined' && value !== null) {
        var request = {
            id: guid,
            data: value,
            name: name
        }
        
        cookieCache.setItem(name, value, { expirationAbsolute: null,
            expirationSliding: 3600,
            priority: CachePriority.High
        });

        $.ajax({
            url: '/Service.asmx/SaveState',
            type: 'POST',
            datatype: 'json',
            async: opt.async,
            data: JSON.stringify(request),
            timeout: 10000,
            contentType: 'application/json; charset=utf-8',
            error: function() { },
            success: function(guid) {
                r = JSON.parse(guid);
            }
        });
    }

    else if (value === null) {
        
        //if it's null in the cookieCache then it's already been deleted
        if(cookieCache.contains(name) && cookieCache.getItem(name) === null){
			return null;
		}
        
        //delete
        var request = {
            id: guid,
            name: name
        }
        cookieCache.setItem(name, null, { expirationAbsolute: null,
            expirationSliding: 3600,
            priority: CachePriority.High
        });

        $.ajax({
            url: '/Service.asmx/DeleteState',
            type: 'POST',
            datatype: 'json',
            async: opt.async,
            data: JSON.stringify(request),
            timeout: 10000,
            contentType: 'application/json; charset=utf-8',
            error: function() { }
        });
    }

    //get a cookie
    else {
		//if the key is in the cookieCache then we know about it (even if it's null) so don't
		//go to the server for it.
		if(cookieCache.contains(name)){
          return cookieCache.getItem(name);
        }

        var request = {
            id: guid,
            name: name
        }

        $.ajax({
            url: '/Service.asmx/RetrieveCookieData',
            type: 'POST',
            datatype: 'json',
            async: false,
            data: JSON.stringify(request),
            timeout: 10000,
            contentType: 'application/json; charset=utf-8',
            error: function() { },
            success: function(data) {
                r = JSON.parse(data);
                cookieCache.setItem(name, r, { expirationAbsolute: null,
                    expirationSliding: 3600,
                    priority: CachePriority.High
                });
            }
        });
    }
    return r;
};

jQuery.deleteEntireCookieForGuid = function(guid, async) {
    var request = { id: guid };
    cookieCache.clear();

    $.ajax({
        url: '/Service.asmx/DeleteEntireState',
        type: 'POST',
        datatype: 'json',
        async: async,
        data: JSON.stringify(request),
        timeout: 10000,
        contentType: 'application/json; charset=utf-8',
        error: function() { }
    });
};

jQuery.setCookieInBulk = function(guid, parameters, async) {
    var request = { id: guid, pairs: parameters };

    // save these guys in the cookie cache too
    for (var key in parameters) {
        cookieCache.setItem(key, parameters[key], { expirationAbsolute: null,
            expirationSliding: 3600,
            priority: CachePriority.High
        });
    }
    
    $.ajax({
        url: '/Service.asmx/SaveBulkState',
        type: 'POST',
        datatype: 'json',
        async: async,
        data: JSON.stringify(request),
        timeout: 10000,
        contentType: 'application/json; charset=utf-8',
        error: function() { },
        success: function(guid) {
            r = JSON.parse(guid);
        }
    });
}

jQuery.createGuid = function() {
    var request = {};
    var guid;

    $.ajax({
        url: '/Service.asmx/CreateNewSessionKey',
        type: 'POST',
        datatype: 'json',
        async: false,
        data: JSON.stringify(request),
        timeout: 10000,
        contentType: 'application/json; charset=utf-8',
        error: function () {},
        success: function(key) {
            guid = JSON.parse(key);
        }
    });
    return guid;
}