// js/admin-form.js

jQuery( document ).ready( function() {
	if ( tcc_admin_options.showhide ) {
		jQuery.each( tcc_admin_options.showhide, function( counter, item ) {
			targetableElements( item );
		});
	}
	jQuery( '.form-colorpicker'  ).wpColorPicker();
	jQuery( '.form-image'        ).click( function( e ) { imageUploader( this, e ); });
	jQuery( '.form-image-delete' ).click( function( e ) { imageDelete( this ); });
});

function imageDelete( el ) {
	var ans = confirm( 'Remove this image?' ); // FIXME: localize this
	if ( ans ) {
		var iuField = jQuery( el.parentNode ).data( 'field' );
		var iuInput = document.getElementById( iuField + '_input' );
		var iuImage = document.getElementById( iuField + '_img' );
		iuInput.value = '';
		iuImage.src   = '';
		jQuery( iuImage ).addClass( 'hidden' );
	}
}

function imageUploader( el, e ) {
	e.preventDefault();
	var iuTitle  = jQuery( el.parentNode ).data( 'title' );
	var iuButton = jQuery( el.parentNode ).data( 'button' );
	var iuField  = jQuery( el.parentNode ).data( 'field' );
	var custom_uploader = wp.media({
		title: iuTitle,
		button: { text: iuButton, },
		multiple: false
	});
	custom_uploader.on( 'select', function() {
		var attachment = custom_uploader.state().get( 'selection' ).first().toJSON();
		if ( iuField ) {
			var iuInput = document.getElementById( iuField + '_input' );
			var iuImage = document.getElementById( iuField + '_img' );
			iuInput.value = attachment.url;
			iuImage.src   = attachment.url;
			jQuery( el.parentNode ).children( '.form-image-container' ).removeClass( 'hidden' );
			jQuery( el.parentNode ).children( '.form-image-delete' ).removeClass( 'hidden' );
		}
	});
	custom_uploader.open();
}

function targetableElements( item ) {
	showhideAdminElements( '.'+item.origin, '.'+item.target, item.show, item.hide );
}

function showhideAdminElements( origin, target, show, hide ) {
	if ( origin && target ) {
		var radio = jQuery( origin + ' input:radio:checked' );
		if ( radio ) {
			var state = jQuery( radio ).val();
			if ( state ) {
				if ( show ) {
					if ( state === show ) {
						jQuery( target ).parent().parent().show( 2000 ); //removeClass('hidden');
					} else {
						jQuery( target ).parent().parent().hide( 2000 ); //addClass('hidden');
					}
				} else if ( hide ) {
					if ( state === hide ) {
						jQuery( target ).parent().parent().hide( 2000 ); //addClass('hidden');
					} else {
						jQuery( target ).parent().parent().show( 2000 ); //removeClass('hidden');
					}
				}
			}
		}
	}
}

@@ -164,16 +164,21 @@
  ) %7B%0A%09%09%09
+if ( 
 targetab
@@ -186,25 +186,24 @@
 eElement
-s
 ( item )
 ;%0A%09%09%7D);%0A
@@ -198,18 +198,218 @@
 em )
-;%0A%09%09%7D);%0A%09%7D
+ ) %7B%0A%09%09%09%09var origin = '.' + item.origin + ' input:radio';%0A%09%09%09%09jQuery( origin ).change( item, function( e ) %7B%0A%09%09%09%09%09targetableElement( e.data );%0A%09%09%09%09%7D);%0A%09%09%09%7D%0A%09%09%7D);%0A%09%7D%0A//%09showhideElements( jQuery( '.showhide' ) );
 %0A%09jQ
@@ -1899,16 +1899,384 @@
 unction 
+showhideElements( els ) %7B%0A%09jQuery( els ).each( function( el ) %7B%0A%09%09var target = jQuery( el ).attr( 'data-item' );%0A%09%09var show   = jQuery( el ).attr( 'data-show' );%0A%09%09if ( show ) %7B%0A%09%09%09showhideAdminElement( el, target, show, null );%0A%09%09%7D%0A%09%09var hide = jQuery( el ).attr( 'data-hide' );%0A%09%09if ( hide ) %7B%0A%09%09%09showhideAdminElement( el, target, null, hide );%0A%09%09%7D%0A%09%7D);%0A%7D%0A%0Afunction 
 targetab
@@ -2284,17 +2284,16 @@
 eElement
-s
 ( item )
@@ -2296,16 +2296,23 @@
 em ) %7B%0A%09
+return 
 showhide
@@ -2323,17 +2323,16 @@
 nElement
-s
 ( '.'+it
@@ -2415,17 +2415,16 @@
 nElement
-s
 ( origin
@@ -2544,16 +2544,23 @@
  ( radio
+.length
  ) %7B%0A%09%09%09
@@ -3082,15 +3082,46 @@
 %0A%09%09%09%7D%0A%09%09
-%7D%0A%09%7D
+%09return true;%0A%09%09%7D%0A%09%7D%0A%09return false;
 %0A%7D%0A

add targetableElement function, other cleanup