﻿(function ($) {

    var methods = {
        init: function (options) {
            return this.each(function () {
                var $this = $(this);
                var data = $this.data('pheValidation');
                var validation = $('<div />', {
                    text: $this.attr('title')
                });

                // If the plugin hasn't been initialized yet
                if (!data) {
                    $(this).data('pheValidation', {
                        response: null
                    });

                    //$(this).append("<div class='ui-state-error ui-corner-all' style='padding:0 .7em'><div><span class='ui-icon ui-icon-alert' style='float: left; margin-right: .3em;'></span><strong>Error:</strong></div><div style='margin-top: 5px;'><ul style='font-weight: normal;'></ul></div></div>");
                    //$(this).hide();
                }
            });
        },
        reset: function () {
            $('ul', $(this)).empty();
            $(this).hide();
        },
        alert: function (error) {
            $('ul', $(this)).append('<li>' + error + '</li>');
            $(this).show();
        },
        load: function (content) {
            var data = $(this).data('pheValidation');
            var $this = $(this);
            $(this).data('pheValidation', data);

            // clear out any old errors.
            $('ul', $this).empty();

            $.each(content.Errors, function (index, value) {
                $('ul', $this).append('<li>' + value + '</li>');
            });

            $('ul', this).show();
            $(this).show();
        }
    };

    $.fn.pheValidation = function (method) {

        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.pheValidation');
        }

    };

})(jQuery);
