/**
 * AJAXEmail.js
 * Tom McFarlin / December 2007
 *
 * This script uses AJAX operations to send email messages
 * from the sidebar.
 */

var AJAXEmail = Class.create();
AJAXEmail.prototype = {

	/*-----------------------------------------------------
	 * constructors
	 */

    /**
     * Initializes this manager by setting up event handlers.
     */
    initialize: function() {

        // setup the validator
        this.validator = new Validation('email-form');

        $('email_status').setStyle({ 
            display: 'none' 
        });

        $('hide-form').setStyle({
            display: 'none'
        });

        this._setupHandlers();

    },  // end constructor

    /*-----------------------------------------------------
	 * functions
	 */

    /**
     * Attaches event handlers to the Sign button and sets up
     * the AJAX operation handlers.
     */
    _setupHandlers: function() {

        var This = this;

        // display the form
        Event.observe('show-form', 'click', function(e) {

            // hide the show form link
            $('show-form').setStyle({
                display: 'none'
            });

            // display the email form
            new Effect.BlindDown($('email-form'));    
            
            // show the hide form link
            $('hide-form').setStyle({
                display: 'block'
            });

        });

        // hide the form
        Event.observe('hide-form', 'click', function(e) {
    
            // hide the hide form link
            $('hide-form').setStyle({
                display: 'none'
            });
        
           // hide the email form
           new Effect.BlindUp($('email-form'));

            // show the show form link
            $('show-form').setStyle({
                display: 'block'
            });


        });

        // email_send event handler
        Event.observe('email_send', 'click', function(e) {

            // make the user confirm the signing
            if(This.validator.validate()) {

                if(confirm("Please confirm that you wish to send this email.")) {

                    // setup the AJAX Request object to post the data
                    new Ajax.Request('php/emailer.php', {

                        method: 'post',

                        // form.serialize produces an error, so I'm hard coding this
                        parameters: {
                            sender_name: $('sender_name').value,   
                            email_from: $('email_from').value,
                            email_subject: $('email_subject').value,
                            message_content: $('message_content').value
                        }, // end parameters

                        // success callback
                        onSuccess: function(transport) {

                            // hide the email form and show the email status
                            new Effect.toggle($('email-form'));
                            new Effect.toggle($('email_status'));

                            // hide the hide form link
                            $('hide-form').setStyle({
                                display: 'none'
                            });

                        },

                        // failure callback
                        onFailure: function() {
                            alert('Your email failed to be sent. Please try again.');
                        }

                    }); // end AJAX request

                } // end if

            } // end if

        }); // end guestbook submission handler

    } // end _setupHandlers

} // end GuestbookManager class
