var fields 	=	{
						'Default - Name (Heading)':				{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-description',
																					],
                                                                    'properties':	{
                                                                        'meta_name_value'	: ''
                                                                    }
																},

						'Default - Contact Info (Heading)':		{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-description',
																					],
                                                                    'properties':	{
                                                                        'meta_name_value'	: ''
                                                                    }
                                                                },

						'Default - About Yourself (Heading)':	{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-description',
																					],
                                                                    'properties':	{
                                                                        'meta_name_value'	: ''
                                                                    }
                                                                },

						'Default - Username':					{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-description',
																						'.row-default-value',
																						'.row-required'
																					],
																	'properties':	{
																						'meta_name_value'	: ''
																					},
																	'required'	:	[
																						true
																					]
																},

						'Default - First Name':					{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-default-value',
																						'.row-required'
																					],
																	'properties':	{
																						'meta_name_value'	: 'first_name'
																					}
																},

						'Default - Last Name':					{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-default-value',
																						'.row-required'
																					],
																	'properties':	{
																						'meta_name_value'	: 'last_name'
																					}
																},

						'Default - Nickname':					{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-default-value',
																						'.row-required'
																					],
																	'properties':	{
																						'meta_name_value'	: 'nickname'
																					},
																	'required'	:	[
																						true
																					]
																},

						'Default - E-mail':						{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-description',
																						'.row-default-value',
																						'.row-required'
																					],
																	'properties':	{
																						'meta_name_value'	: ''
																					},
																	'required'	:	[
																						true
																					]
																},

						'Default - Website':					{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-description',
																						'.row-default-value',
																						'.row-required'
																					],
																	'properties':	{
																						'meta_name_value'	: ''
																					}
																},

						'Default - AIM':						{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-default-value',
																						'.row-required'
																					],
																	'properties':	{
																						'meta_name_value'	: 'aim'
																					}
																},

						'Default - Yahoo IM':					{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-default-value',
																						'.row-required'
																					],
																	'properties':	{
																						'meta_name_value'	: 'yim'
																					}
																},

						'Default - Jabber / Google Talk':		{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-default-value',
																						'.row-required'
																					],
																	'properties':	{
																						'meta_name_value'	: 'jabber'
																					}
																},
		
						'Default - Password':					{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-description',
																						'.row-required'
																					],
                                                                    'properties':	{
                                                                        'meta_name_value'	: ''
                                                                    },
                                                                    'required'	:	[
																						true
																					]
																},

						'Default - Repeat Password':			{	'show_rows'	:	[
																					'.row-field-title',
																					'.row-description',
																					'.row-required'
																					],
                                                                    'properties':	{
                                                                        'meta_name_value'	: ''
                                                                    },
                                                                    'required'	:	[
																						true
																					]
																},

						'Default - Biographical Info':			{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-row-count',
																						'.row-default-content',
																						'.row-required'
																					],
																	'properties':	{
																						'meta_name_value'	: 'description'
																					}
																},

						'Default - Display name publicly as':	{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-description',
																						'.row-default-value',
																						'.row-required'
																					],
                                                                    'properties':	{
                                                                        'meta_name_value'	: ''
                                                                    }
                                                                },

						'Heading':								{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-description',
																					],
                                                                    'properties':	{
                                                                        'meta_name_value'	: ''
                                                                    }
                                                                },

						'Input':								{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-default-value',
																						'.row-required',
																						'.row-overwrite-existing'
																					]
																},
						'Input (Hidden)':						{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-default-value',
																						'.row-overwrite-existing'
																					]
																},

						'Textarea':								{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-default-content',
																						'.row-required',
																						'.row-row-count',
																						'.row-overwrite-existing'
																					]
																},
                        'WYSIWYG':								{	'show_rows'	:	[
                                                                                        '.row-field-title',
                                                                                        '.row-meta-name',
                                                                                        '.row-description',
                                                                                        '.row-default-content',
                                                                                        '.row-required',
                                                                                        '.row-overwrite-existing'
                                                                                    ]
                                                                },

						'Select':								{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-default-option',
																						'.row-required',
																						'.row-overwrite-existing',
																						'.row-options',
																						'.row-labels'
																					]
																},	
						'Select (Multiple)':					{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-default-options',
																						'.row-required',
																						'.row-overwrite-existing',
																						'.row-options',
																						'.row-labels'
																					]
																},	
			
						'Select (Country)':						{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
                                                                                        '.row-default-option-country',
																						'.row-required',
																						'.row-overwrite-existing'
																					]
																},

						'Select (Timezone)':					{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
                                                                                        '.row-default-option-timezone',
																						'.row-required',
																						'.row-overwrite-existing'
																					]
																},

						'Checkbox':								{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-default-options',
																						'.row-required',
																						'.row-overwrite-existing',
																						'.row-options',
																						'.row-labels'
																					]
																},

						'Checkbox (Terms and Conditions)':		{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-required',
																						'.row-overwrite-existing'
																					],
																	'required'	:	[
																						true
																					]
																},

						'Radio':								{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-default-option',
																						'.row-required',
																						'.row-overwrite-existing',
																						'.row-options',
																						'.row-labels'
																					]
																},

						'Upload':								{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-allowed-extensions',
																						'.row-required',
																						'.row-allowed-upload-extensions'
																					]
																},

						'Avatar':								{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-allowed-image-extensions',
																						'.row-avatar-size',
																						'.row-required',
																						'.row-overwrite-existing'
																					]
																},

						'Datepicker':							{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-meta-name',
																						'.row-description',
																						'.row-default-value',
																						'.row-required',
																						'.row-date-format',
																						'.row-overwrite-existing'
																					]
																},

						'reCAPTCHA':							{	'show_rows'	:	[
																						'.row-field-title',
																						'.row-description',
																						'.row-public-key',
																						'.row-private-key',
                                                                                        '.row-captcha-pb-forms',
                                                                                        '.row-captcha-wp-forms',
																						'.row-required'
																					],
																	'required'	:	[
																						true
																					],
                                                                    'properties':	{
                                                                       'meta_name_value'	: ''
                                                                    }
																},

                        'Select (User Role)':					{	'show_rows'	:	[
                                                                                        '.row-field-title',
                                                                                        '.row-description',
                                                                                        '.row-user-roles',
                                                                                        '.row-required'
                                                                                    ],
                                                                    'properties':	{
                                                                        'meta_name_value'	: ''
                                                                    }
                                                                }

				}
var fields_to_show = [
	'.row-field-title',
	'.row-field',
	'.row-meta-name',
	'.row-required'
]

function wppb_hide_properties_for_already_added_fields( container_name ){

	jQuery( container_name + ' tr' ).each(function() {

		field = jQuery('.row-field pre', this).text();

		jQuery( 'li', this ).each(function() {
			var class_name = '';

			class_name = jQuery(this).attr('class');
			jQuery(this).hide();

			if ( ( field in fields ) ){
				var to_show = fields[field]['show_rows'];
				for (var key in to_show) {
					if ('.'+class_name == fields_to_show[key]){
						jQuery(this).show();
					}
				}
			}

		});
	});

    /* hide the delete button for username,password and email fields */
    jQuery( container_name + ' ' + '.element_type_default-e-mail .mbdelete,' + ' ' + container_name + ' ' + '.element_type_default-password .mbdelete,' + ' ' + container_name + ' ' + '.element_type_default-username .mbdelete'  ).hide();	// PB specific line
}



function wppb_hide_all ( container_name ){
	jQuery( container_name + ' ' + '.mb-list-entry-fields > li' ).each(function() {
		if ( !( ( jQuery(this).hasClass('row-field') ) || ( jQuery(this).children().hasClass('button-primary') ) ) ){
			jQuery(this).hide();
		}
	});

	jQuery( container_name + ' ' + '.mb-list-entry-fields .button-primary' ).attr( 'disabled', true );

	jQuery( container_name + ' ' + '.element_type_default-e-mail .mbdelete,' + ' ' + container_name + ' ' + '.element_type_default-password .mbdelete,' + ' ' + container_name + ' ' + '.element_type_default-username .mbdelete'  ).hide();	// PB specific line
	jQuery( container_name + ' ' + '.element_type_default-e-mail #field' + ', ' + container_name + ' ' + '.element_type_default-password #field' + ',  ' + container_name + ' ' + '.element_type_default-username #field' + ', ' + container_name + ' ' + '.element_type_default-e-mail #required' + ', ' + container_name + ' ' + '.element_type_default-password #required,'  + container_name + ' ' + '.element_type_default-username #required,'  + container_name + ' ' + '.element_type_checkbox-terms-and-conditions #required,'  + container_name + ' ' + '.element_type_recaptcha #required' ).attr( 'disabled', true );		// PB specific line
}


function wppb_disable_add_entry_button( container_name ){
	jQuery( container_name + ' ' + '.mb-list-entry-fields .button-primary' ).each( function(){
		
		jQuery(this).data('myclick', this.onclick );
		this.onclick = function(event) {			
			if ( jQuery(this).attr( 'disabled' ) ) {			
				return false;					
			}
			
			jQuery(this).data('myclick').call(this, event || window.event);
		};
	});
	
}


function wppb_edit_form_properties( container_name, element_id ){
	wppb_hide_all ( container_name );
	wppb_disable_add_entry_button ( container_name );

	field = jQuery( container_name + ' #' + element_id + ' ' + '#field' ).val();

	if ( ( field in fields ) ){
		var to_show = fields[jQuery.trim(field)]['show_rows'];
		for (var key in to_show)
			jQuery( container_name + ' #' + element_id + ' ' + to_show[key] ).show();

        var properties = fields[ jQuery.trim(field) ]['properties'];
        if( typeof properties !== 'undefined' && properties ) {
            for( var key in properties ) {
                if( typeof properties['meta_name_value'] !== 'undefined' ) {
                    jQuery( container_name + ' ' + '#meta-name').attr( 'readonly', true );
                }
            }
        }

		jQuery( container_name + ' ' + '.mb-list-entry-fields .button-primary' ).removeAttr( 'disabled' );

        //Handle user role sorting
        wppb_handle_user_role_field( container_name );
	}
}


function wppb_display_needed_fields( index, container_name, current_field_select ){
	var show_rows = fields[jQuery.trim(index)]['show_rows'];
	for (var key in show_rows) {
		jQuery(  show_rows[key], jQuery( current_field_select ).parents( '.mb-list-entry-fields' ) ).show();
	}

	var properties = fields[jQuery.trim(index)]['properties'];
	if ( ( ( typeof properties !== 'undefined' ) && ( properties ) ) ) { //the extra (second) condition is a particular case since only the username is defined in our global array that has no meta-name
		for (var key in properties) {
			if ( ( typeof properties['meta_name_value'] !== 'undefined' ) ){
				jQuery( container_name + ' ' + '#meta-name' ).val( properties['meta_name_value'] );
				jQuery( container_name + ' ' + '#meta-name' ).attr( 'readonly', true );
			}
		}
		
	}else{
        /* meta value when editing a field shouldn't change so we take it from the current entered value which is displayed above the edit form */
        if( jQuery( current_field_select).parents('.update_container_wppb_manage_fields').length != 0 ){
            meta_value = jQuery( '.row-meta-name pre', jQuery( current_field_select).parents( '.update_container_wppb_manage_fields' ).prev() ).text();
        }
        /* for the add form it should change */
        else{
            numbers = new Array();
            jQuery( '#container_wppb_manage_fields .row-meta-name pre').each(function(){
                meta_name = jQuery(this).text();
                if( meta_name.indexOf( 'custom_field' ) !== -1 ){
                    var meta_name = meta_name.replace('custom_field', '' );
                    /* we should have an underscore present in custom_field_# so remove it */
                    meta_name = meta_name.replace('_', '' );

                    if( isNaN( meta_name ) ){
                        meta_name = Math.floor((Math.random() * 200) + 100);
                    }
                    numbers.push( parseInt(meta_name) );
                }

            });
            if( numbers.length > 0 ){
                numbers.sort( function(a, b){return a-b} );
                numbers.reverse();
                meta_number = parseInt(numbers[0])+1;
            }
            else
                meta_number = 1;

            meta_value = 'custom_field_' + meta_number;
        }

		jQuery( container_name + ' ' + '#meta-name' ).val( meta_value );
		jQuery( container_name + ' ' + '#meta-name' ).attr( 'readonly', false );
	}

    //Handle user role sorting
    wppb_handle_user_role_field( container_name );
	
	var set_required = fields[jQuery.trim(index)]['required'];
	if ( ( typeof set_required !== 'undefined' ) && ( set_required ) ){
		jQuery( container_name + ' ' + '#required' ).val( 'Yes' );
		jQuery( container_name + ' ' + '#required' ).attr( 'disabled', true );
		
	}else{
		jQuery( container_name + ' ' + '#required' ).val( 'No' );
		jQuery( container_name + ' ' + '#required' ).attr( 'disabled', false );
	}
	
	jQuery( container_name + ' ' + '.mb-list-entry-fields .button-primary' ).removeAttr( 'disabled' );
}


/*
* Function that handles the sorting of the user roles from the Select (User Role)
* extra field
*
 */
function wppb_handle_user_role_field( container_name ) {

    jQuery( container_name + ' ' + '.row-user-roles .wck-checkboxes').sortable({

        //Assign a custom handle for the drag and drop
        handle: '.sortable-handle',

        create: function( event, ui ) {

            //Add the custom handle for drag and drop
            jQuery(this).find('div').each( function() {
                jQuery(this).prepend('<span class="sortable-handle"></span>');
            });

            $sortOrderInput = jQuery(this).parents('.row-user-roles').siblings('.row-user-roles-sort-order').find('input[type=text]');

            if( $sortOrderInput.val() == '' ) {
                jQuery(this).find('input[type=checkbox]').each( function() {
                    $sortOrderInput.val( $sortOrderInput.val() + ', ' + jQuery(this).val() );
                });
            } else {
                sortOrderElements = $sortOrderInput.val().split(', ');
                sortOrderElements.shift();

                for( var i=0; i < sortOrderElements.length; i++ ) {
                    jQuery( container_name + ' ' + '.row-user-roles .wck-checkboxes').append( jQuery( container_name + ' ' + '.row-user-roles .wck-checkboxes input[value=' + sortOrderElements[i] + ']').parent().parent().get(0) );
                }
            }
        },

        update: function( event, ui ) {
            $sortOrderInput = ui.item.parents('.row-user-roles').siblings('.row-user-roles-sort-order').find('input[type=text]');
            $sortOrderInput.val('');

            ui.item.parent().find('input[type=checkbox]').each( function() {
                $sortOrderInput.val( $sortOrderInput.val() + ', ' + jQuery(this).val() );
            });
        }
    });
}

function wppb_initialize_live_select( container_name ){	
	wppb_hide_all( container_name );
	jQuery(document).on( 'change', container_name + ' ' + '.mb-list-entry-fields #field', function () {
		field = jQuery(this).val();

		if ( field != ''){
			wppb_hide_all( container_name );
			wppb_display_needed_fields( field, container_name, this );
		}else{
			wppb_hide_all( container_name );
		}
	});
}

jQuery(function(){
 	wppb_initialize_live_select ( '#wppb_manage_fields' );
	wppb_initialize_live_select ( '#container_wppb_manage_fields' );
	
	wppb_hide_properties_for_already_added_fields( '#container_wppb_manage_fields' );
	wppb_disable_add_entry_button ( '#wppb_manage_fields' );
});