EmailAFriend = Class.create({
    initialize: function( data ) {
        this.event_id = data.event_id || "";
        this.auction_id = data.auction_id || "";

        this.page_type = data.page_type;
        this.v = data.v;

        var form_submit_function = this.submit_form.bind( this );
        var thisobj = this;
        EmailAFriend.popup.addLinks( [ {
            link_id: data.share_link.id('share_link'),

            popup_align: {
                x: 'center',
                y: 'center'
            },

            onShow: function() {
                thisobj.on_link_click();
            }
        } ] );

        this.your_name = $('eaf_your_name').value;
        if ( this.your_name == '' ) {
            var mname = GetCookie( 'MNAME' );
            if ( mname != null & mname != '' ) {
                this.your_name = mname;
            }
        }
        this.your_email = $('eaf_your_email').value;

        $('eaf_submit').observe( 'click', form_submit_function );
        $('eaf_your_name').observe( 'keypress', this.update_message.bind( this ) );
        $('eaf_your_name').observe( 'keydown', this.update_message.bind( this ) );
        $('eaf_your_name').observe( 'keyup', this.update_message.bind( this ) );
        $('eaf_your_name').observe( 'change', this.update_message.bind( this ) );
    },

    update_message: function() {
        var eaf_message_name = $('eaf_message_name');
        if ( eaf_message_name ) {
            eaf_message_name.innerHTML = $('eaf_your_name').value;
        }
    },

    on_link_click: function() {
        $('eaf_form').style.display = "";
        $('eaf_confirm').style.display = "none";
        $('eaf_se_msg').style.display = "none";
        $('eaf_form_container').style.display = "";
        this.clear_form();
    },

    clear_form: function() {
        $('eaf_correct_msg').style.display = "none";
        $('eaf_se_msg').style.display = "none";

        [
            "your_name",
            "your_email",
            "recip_email1"
        ].each( function ( name ) {
            var id = 'eaf_' + name + '_label';
            $(id).className = "";
        } );

        [
            'recip_email1',
            'recip_email2',
            'recip_email3',
            'recip_email4'
        ].each( function( id ) {
            $('eaf_' + id).value = "";
        } );

        [
            'your_name',
            'your_email'
        ].each( function( id ) {
            $('eaf_' + id).value = this[id];
        }, this );

        $('eaf_copy_me').checked = true;

        this.update_message();
    },

    submit_form: function() {
        var parameters = {};

        parameters.v = this.v;
        parameters.page_type = this.page_type;

        parameters.event_id = this.event_id;

        parameters.auction_id = this.auction_id;

        $('eaf_cancel').disabled = "disabled";
        $('eaf_submit').disabled = "disabled";

        [
            'your_name',
            'your_email',
            'recip_email1',
            'recip_email2',
            'recip_email3',
            'recip_email4'
        ].each( function( id ) {
            parameters[id] = $('eaf_' + id).value;
        } );

        if ( $('eaf_copy_me').checked )
            parameters.copy_me = 1;

        var eafobj = this;
        new Ajax.Request( "/email_friend", {
            method: 'post',

            parameters: parameters,

            onSuccess: function( transport ) {
                // TODO: problem if we didn't get JSON

                $('eaf_cancel').disabled = "";
                $('eaf_submit').disabled = "";

                var data = transport.responseText.evalJSON();
                if ( data.errors ) {
                    $('eaf_correct_msg').style.display = "";

                    [
                        "your_name",
                        "your_email",
                        "recip_email1",
                        "recip_email2",
                        "recip_email3",
                        "recip_email4"
                    ].each( function ( name ) {
                        var id = 'eaf_' + name + '_label';
                        if ( typeof(data.errors[name]) == 'object' )
                            $(id).className = "errorMessage";
                        else
                            $(id).className = "";
                    } );
                }
                else if ( data.emails ) {
                    while ( $('eaf_recip_email_list').hasChildNodes() )
                        $('eaf_recip_email_list').removeChild( $('eaf_recip_email_list').firstChild );

                    for ( var i = 0 ; i < data.emails.length ; i ++ ) {
                        var email = new Element ( 'li' );
                        email.innerHTML = data.emails[i];
                        $('eaf_recip_email_list').appendChild( email );
                    }

                    $('eaf_confirm').style.display = "";
                    $('eaf_form').style.display = "none";
                    EmailAFriend.popup.show( {
                        anchor_id: document.body,
                        popup_align: {
                            x: 'center',
                            y: 'center'
                        }
                    } );
                    omniTracking.trackomni_favoriteshare(['products','events'],[tm_omni_temp.get('omnEID'),'event22'],'Email a Friend');
                }
                else if ( data.server_error ) {
                    eafobj.on_server_error();
                }
            },

            onFailure: function( transport ) {
                $('eaf_cancel').disabled = "";
                $('eaf_submit').disabled = "";

                eafobj.on_server_error();
            }
        });
    },

    on_server_error: function() {
        $('eaf_form_container').hide();
        $('eaf_correct_msg').hide();
        $('eaf_se_msg').show();
    }
});

EmailAFriend.popup = new Popup( 'eaf_display', {
    zIndex: 110,
    lightbox: { opacity: 0.6 },
    close_id: [ "eaf_form_close", "eaf_cancel", "eaf_conf_close", "eaf_conf_close_box" ]
} );

