// This is used for full, interactive forms
// use fancyAction for links and forms that consist of a single button.  It
// presents it's information through popups only, this one modifies the form to
// show what they need to change.
(function($) {
    $.fn.fancyForm = function( options ) {

        $(this).fadeOut('slow');

        var defaults = {

            // Okay, here goes:
            // we start off by fading out the form and dropping whatever errors
            // were left over from the last time before submitting. if the
            // result saved fine, we just fade the form back in and call
            // postSuccess.  Otherwise, success takes the resulting JSON from
            // the server, finds the ID/labels and sets the style accordingly.
            // and then fades the form in again.

            success: function( responseText, statusText ) {

                // Form was saved successfully
                if( responseText.Saved == true ) {
                    $('form').fadeIn('slow');
                    $.jGrowl( responseText.Message, { header: 'Information saved' } );
                    return options.postSuccess( responseText, statusText );
                }

                // probably pointless, but I like beating users over the head
                // with the fact that they suck.
                $.jGrowl( 'There was an error saving!', { header: 'Could not save' } );

                // Form was not.  append UL to the Errors divs and start
                //
                // all the stuff with ErrorsBorder is so that div.Errors isn't
                // displayed before any errors exist (as a thin red line) so we
                // add the additional class when it does exist
                $('div.Errors').addClass('ErrorsBorder');
                $('div.Errors').append('<ul></ul>');

                for ( prop in responseText ) {
                    // the ID will only be there if something errored
                    if( responseText[prop].id ) {
                        var inputID = responseText[prop].id;
                        var label   = responseText[prop].label;
                        var error   = responseText[prop].error;
                        $('#'+inputID).parents('form').siblings('div.Errors').each( function() {
                            $(this).children('ul').append( '<li>'+label+' : '+error+'</li>' );
                            $(this).fadeIn('slow');
                        });
                        // put a border around the input box and make the label red
                        $('#'+inputID).addClass('Error');
                        $('label[for='+inputID+']').addClass('Error');
                    }
                }

                $('form').fadeIn('slow');
                return options.postSuccess( responseText, statusText );
            },

            postSuccess: function( responseText, statusText ) {}
        };

        var options = $.extend( defaults, options );

        // Because file uploads can't be submitted over XMLHTTPRequest
        if( $(this).find('input[name="isJS"]').length <= 0 ) {
            $(this).append('<input type="hidden" name="isJS" value="1"/>');
        }

        $('div.Errors').fadeOut('slow');
        $('div.Errors').removeClass('ErrorsBorder');
        $('div.Errors').empty();

        $(this).ajaxSubmit({
            success: options.success
            , dataType: 'json'
        });

    };

})(jQuery);

