
jQuery(function($) {

    var TxMchimp = {};

    TxMchimp.fields = [
        'tx_mchimp_fname',
        'tx_mchimp_email'
    ];
    
    TxMchimp.opt = {
        url     : 'index.php?eID=mchimp&L=' + $.tx_mchimp.L + '&id=' + $.tx_mchimp.id + '&ce=' + $.tx_mchimp.ce,
        dataType : 'json',
        //target  :'#' + $.tx_mchimp.formName,
        beforeSubmit : viewRequest,
        success : viewResponse
    };

    TxMchimp.init = function() {

        setLabels();
        
        // bind to the form's submit event
        $('#' + $.tx_mchimp.formName + '_1').submit(function() {

            cleanSubmit(this);
            $('.tx-mchimp-fielderror').remove();
            $(this).ajaxSubmit(TxMchimp.opt);
            return false;
        });
        
        $('#' + $.tx_mchimp.formName + '_2').submit(function() {
            
            cleanSubmit(this);
            $('.tx-mchimp-fielderror').remove();
            $(this).ajaxSubmit(TxMchimp.opt);
            return false;
        });

        // unsub link
        $('#tx_mchimp_unsub_' + $.tx_mchimp.key).click(function() {
            
            $('.tx-mchimp-subscribe').fadeOut(200, function() {
                $('.tx-mchimp-unsubscribe').fadeIn(200);
            });
            return false;
        });

        // back link
        $('#tx_mchimp_back_' + $.tx_mchimp.key).click(function() {

            $('.tx-mchimp-unsubscribe').fadeOut(200, function() {
                $('.tx-mchimp-subscribe').fadeIn(200);
            });
            return false;
        });

        //close
        $('.resp2CloseSub .tx-mchimp-btn-close').click(function() {
            $('.tx-mchimp-response-sub').slideUp(200, function() {
                $(this).fadeOut(200, function(){
                    $('.tx-mchimp-subscribe').fadeIn(100);
                });
            });
            setLabels(true);
            return false;
        });
        $('.resp2CloseUnsub .tx-mchimp-btn-close').click(function() {
            $('.tx-mchimp-response-unsub').slideUp(200, function() {
                $(this).fadeOut(200, function(){
                    $('.tx-mchimp-unsubscribe').fadeIn(100);
                });
            });
            setLabels(true);
            return false;
        });
    };

    function viewRequest(formData, jqForm, options) {

        $('.tx-mchimp-send-wrap').show('80');
    }


    function viewResponse(responseText, statusText, xhr, $form) {

        if(statusText === 'success' && ! responseText.error ) {

            if( $form.attr('id') === $.tx_mchimp.formName + '_1' ) {
                $('.tx-mchimp-send-wrap').hide('10',function() {
                    $('.tx-mchimp-subscribe').fadeOut(200,function(){
                        $('.tx-mchimp-response-sub').fadeIn(200);
                    });
                });

            } else if( $form.attr('id') === $.tx_mchimp.formName + '_2' ) {

                $('.tx-mchimp-send-wrap').hide('10',function() {
                    $('.tx-mchimp-unsubscribe').fadeOut(200,function(){
                        $('.tx-mchimp-response-unsub').fadeIn(200);
                    });
                });
            }
        } else {

            if( responseText.errorField === 'api' ) {
                $('.tx-mchimp-error').prepend('<p>' + responseText.text + '</p>').slideDown(200);
                $('.tx-mchimp-error a').click(function(){
                    setLabels();
                    $('.tx-mchimp-send-wrap').hide('10');
                    $('.tx-mchimp-error p').remove()
                    $(this).parent().slideUp(200);
                    return false;
                });
            } else {
                $('.tx-mchimp-send-wrap').hide('10',function() {
                    $( '#' + $form.attr('id') + ' :input[name=' + responseText.errorField + ']')
                    .after('<div class="tx-mchimp-fielderror">' + responseText.text + '</div>').slideDown('fast');
                    setLabels();
                });
            }
        }
    }

    function setLabels( cls ) {

        $.each(TxMchimp.fields, function() {

            var n = this.toString(),
            f = $(':input[name=' + n + ']'),
            clean = cls || false;

            f[0].defVal = f[0].defVal || f.attr('title');
            f.attr('title','');

            f.focusin(function(){
                var v = $(this).val();
                if(typeof v === 'string' && v === f[0].defVal) {
                    $(this).val('');
                }
            });

            f.focusout(function(){
                var v = $(this).val();
                if(typeof f[0].defVal === 'string' && v === '') {
                    $(this).val(f[0].defVal);
                }
            });

            if(f.val() === '' || clean ) {
                f.val(f[0].defVal);
            }       
        });
    }

    function cleanSubmit(form) {
        
        $.each(TxMchimp.fields, function() {

            var n = this.toString(),fid,f,v;
            fid = $(form).attr('id');
            
            f = $( '#' + fid +' :input[name=' + n + ']' );

            if( f.length > 0 ) {
                v = f.val();
                if( v === f[0].defVal ) {
                    f.val('');
                }
            }
        });
    }

    TxMchimp.init();
});

