(function($) {

    "use strict";

	function pps_form_changed(){

		if(jQuery('#pps_password_protected_form_type').length>0){

			var type = jQuery('#pps_password_protected_form_type').val();

			if( 'custom' == type ) {

				jQuery('tr.pps_form_type').show();

			}else{

				jQuery('tr.pps_form_type').hide();

			}

		}

	}

    jQuery(document).ready(function(){

		pps_form_changed();

		if(jQuery('#pps_password_protected_form_type').length>0){

			jQuery('#pps_password_protected_form_type').on('change', function() {

				pps_form_changed();

			});

		}

        jQuery(document).on('click', '.pps-add-more-password', function(e){

            e.preventDefault();

            var me = jQuery(this);

            jQuery(this).closest('p').clone().appendTo(me.closest('.pps_password_protected_passwords'));

        });

        jQuery(document).on('click', '.pps-remove-password', function(e){

            e.preventDefault();

            if(jQuery(this).closest('.pps_password_protected_passwords').children('p').length>1) {

                jQuery(this).closest('p').remove();

			}

        });

        if(jQuery('.pps_password_protected_color').length>0){

			jQuery('.pps_password_protected_color').wpColorPicker();

        }

        jQuery('a.pps_form_bg_image_upload, .pps_form_bg_image').on('click', function(e) {

			e.preventDefault();

			var pps_protected = wp.media({

				title: 'Background Image',

				button: {

					text: 'Upload Image'

				},

				multiple: false

			})

			.on('select', function() {

				var attachment = pps_protected.state().get('selection').first().toJSON();

				jQuery('div.pps_form_bg_image').html('<img src="'+attachment.url+'" width="200" height="200">');

				jQuery('input.pps_form_bg_image').val(attachment.id);

			})

			.open();

		});

    });



})(jQuery);