/**
 * TotalStorage
 *
 * Copyright (c) 2012 Jared Novack & Upstatement (upstatement.com)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Total Storage is the conceptual the love child of jStorage by Andris Reinman, 
 * and Cookie by Klaus Hartl -- though this is not connected to either project.
 */

(function($) {
    'use strict';
    var ls = window.localStorage;
    var supported;
    if (typeof ls == 'undefined' || typeof window.JSON == 'undefined') {
        supported = false;
    } else {
        supported = true;
    }
    $.totalStorage = function(key, value, options) {
        return $.totalStorage.impl.init(key, value);
    }
    $.totalStorage.setItem = function(key, value) {
        return $.totalStorage.impl.setItem(key, value);
    }
    $.totalStorage.getItem = function(key) {
        return $.totalStorage.impl.getItem(key);
    }
    $.totalStorage.getAll = function() {
        return $.totalStorage.impl.getAll();
    }
    $.totalStorage.deleteItem = function(key) {
        return $.totalStorage.impl.deleteItem(key);
    }
    $.totalStorage.impl = {
        init: function(key, value) {
            if (typeof value != 'undefined') {
                return this.setItem(key, value);
            } else {
                return this.getItem(key);
            }
        },
        setItem: function(key, value) {
            if (!supported) {
                try {
                    $.cookie(key, value);
                    return value;
                } catch (e) {
                    console.log('Local Storage not supported by this browser. Install the cookie plugin on your site to take advantage of the same functionality. You can get it at https://github.com/carhartl/jquery-cookie');
                }
            }
            var saver = JSON.stringify(value);
            ls.setItem(key, saver);
            return this.parseResult(saver);
        },
        getItem: function(key) {
            if (!supported) {
                try {
                    return this.parseResult($.cookie(key));
                } catch (e) {
                    return null;
                }
            }
            return this.parseResult(ls.getItem(key));
        },
        deleteItem: function(key) {
            if (!supported) {
                try {
                    $.cookie(key, null);
                    return true;
                } catch (e) {
                    return false;
                }
            }
            ls.removeItem(key);
            return true;
        },
        getAll: function() {
            var items = new Array();
            if (!supported) {
                try {
                    var pairs = document.cookie.split(";");
                    for (var i = 0; i < pairs.length; i++) {
                        var pair = pairs[i].split('=');
                        var key = pair[0];
                        items.push({
                            key: key,
                            value: this.parseResult($.cookie(key))
                        });
                    }
                } catch (e) {
                    return null;
                }
            } else {
                for (var i in ls) {
                    if (i.length) {
                        items.push({
                            key: i,
                            value: this.parseResult(ls.getItem(i))
                        });
                    }
                }
            }
            return items;
        },
        parseResult: function(res) {
            var ret;
            try {
                ret = JSON.parse(res);
                if (ret == 'true') {
                    ret = true;
                }
                if (ret == 'false') {
                    ret = false;
                }
                if (parseFloat(ret) == ret && typeof ret != "object") {
                    ret = parseFloat(ret);
                }
            } catch (e) {}
            return ret;
        }
    }
})(jQuery);