;
(function ($) {
    $.fn.autosave = function (a) {
        var b = $.extend({}, $.fn.autosave.options, a);
        var c = false;
        var d = false;
        var e = 0;
        var f = 0;
        var g = 0;

        function setEvents() {
            $($.fn.autosave.options.saving).hide();
            $($.fn.autosave.options.autosave).click(function () {
                $.fn.autosave.go();
                return false
            });
            $($.fn.autosave.options.restore).click(function () {
                $.fn.autosave.restore();
                return false
            });
            $($.fn.autosave.options.removeCookies).click(function () {
                $.fn.autosave.removeAllCookies();
                return false
            });
            $(window).unload(function () {
                $.fn.autosave.go();
                return true
            });
            setInterval(function () {
                if (d) {
                    $.fn.autosave.go();
                    d = false
                }
            }, $.fn.autosave.options.interval);
            c = true
        }
        return this.filter(':text, :radio, :checkbox, select, textarea').each(function () {
            if ($(this).is(':text, textarea')) {
                $.fn.autosave.values.text[e] = this;
                $(this).keyup(function () {
                    d = true
                });
                e++
            } else if ($(this).is('select')) {
                $.fn.autosave.values.text[e] = this;
                $(this).change(function () {
                    d = true
                });
                e++
            } else if ($(this).is(':checkbox')) {
                $.fn.autosave.values.check[f] = this;
                $(this).click(function () {
                    d = true
                });
                f++
            } else {
                $.fn.autosave.values.radio[g] = this;
                $(this).click(function () {
                    d = true
                });
                g++
            }
            if (!c) {
                setEvents()
            }
        })
    };
    $.fn.autosave.values = {
        'text': {},
        'check': {},
        'radio': {}
    };
    $.fn.autosave.options = {
        'autosave': '.autosave',
        'restore': '.autosave_restore',
        'removeCookies': '.autosave_removecookies',
        'saving': '.autosave_saving',
        'interval': 10000,
        'unique': '',
        'onBeforeSave': function () {},
        'onAfterSave': function () {},
        'onBeforeRestore': function () {},
        'onAfterRestore': function () {},
        'cookieCharMaxSize': 2000,
        'cookieExpiryLength': 1
    };
    $.fn.autosave.go = function () {
        $.fn.autosave.options.onBeforeSave();
        var m = $.fn.autosave.values;
        var u = $.fn.autosave.options.unique;

        function saveCookie(i, j, a) {
            $.cookie('autosave_' + u + i + '_' + j, a, {
                expires: $.fn.autosave.options.cookieExpiryLength
            })
        }
        function removeBiggerCookies(i) {
            var j = 1;
            while ($.cookie('autosave_' + u + i + '_' + j) !== null && j < 20) {
                $.cookie('autosave_' + u + i + '_' + j, null)
            }
        }
        for (i in m.text) {
            var b;
            var j = 0;
            b = $(m.text[i]).val();
            size = b.length;
            if (size < $.fn.autosave.options.cookieCharMaxSize) {
                saveCookie(i, 0, b)
            } else {
                removeBiggerCookies(i);
                for (var k = 0; k < size; k += $.fn.autosave.options.cookieCharMaxSize) {
                    saveCookie(i, j, b.substr(k, $.fn.autosave.options.cookieCharMaxSize));
                    j += 1
                }
            }
        }
        var c = '';
        for (i in m.check) {
            var b = $(m.check[i]).attr('checked') ? '1' : '0';
            c += b + ','
        }
        $.cookie('autosave_' + u + '_check', c);
        var d = '';
        for (i in m.radio) {
            if ($(m.radio[i]).is(':checked')) {
                d += i + ','
            }
        }
        $.cookie('autosave_' + u + '_radio', d);
        $.fn.autosave.saving();
        $.fn.autosave.options.onAfterSave()
    };
    $.fn.autosave.restore = function () {
        $.fn.autosave.options.onBeforeRestore();
        var m = $.fn.autosave.values;
        var u = $.fn.autosave.options.unique;
        for (i in m.text) {
            var j = 0;
            var a = '';
            while ($.cookie('autosave_' + u + i + '_' + j) !== null && j < 20) {
                a += $.cookie('autosave_' + u + i + '_' + j);
                j += 1
            }
            $(m.text[i]).val(a)
        }
        var b = $.cookie('autosave_' + u + '_check').split(',');
        if (b !== null) {
            b.pop();
            for (i in m.check) {
                var c = (b[i] == '1') ? 'checked' : '';
                $(m.check[i]).attr('checked', c)
            }
        }
        var d = $.cookie('autosave_' + u + '_radio').split(',');
        if (d !== null) {
            d.pop();
            for (i in d) {
                $(m.radio[d[i]]).attr('checked', 'checked')
            }
        }
        $.fn.autosave.options.onAfterRestore()
    };
    $.fn.autosave.removeAllCookies = function () {
        var u = $.fn.autosave.options.unique;
        for (var i = 0; i < 200; i++) {
            var j = 0;
            while ($.cookie('autosave_' + u + i + '_' + j) !== null && j < 20) {
                $.cookie('autosave_' + u + i + '_' + j, null)
            }
        }
        $.cookie('autosave_' + u + '_check', null);
        $.cookie('autosave_' + u + '_radio', null)
    };
    $.fn.autosave.saving = function () {
        $($.fn.autosave.options.saving).show().fadeTo(1000, 1).fadeOut(500)
    }
})(jQuery);
