function fieldValue(field) {
	switch (field.type) {
		case 'textarea':
		case 'text':
		case 'password':
			return(field.value);

		case 'radio':
			var value = null;
			$(field).siblings("input[name="+ field.name +"]").andSelf().each(function () {
				if (this.checked) {
					value = this.value;
				}
			});
			return(value);

		case 'checkbox':
			if (this.checked) {
				return(this.value ? this.value : 'checked');
			}
			return(null);

		default:
			return(null);
	}
}

jQuery.fn.unCherry = function() {
	$(this).filter(".cherry").removeClass('cherry').attr('value', '');
}

function bindCherry() {
	function cherryActive() {
		if ($(this).hasClass('cherry')) {
			this.origValue = this.value;
			$(this).unCherry();
		}
	}

	$(".cherry").click(cherryActive).focus(cherryActive).blur(function () {
		if (!this.value && !$(this).hasClass('cherry')) {
			$(this).addClass('cherry').attr('value',this.origValue);
		}
	});
}

jQuery.fn.nfUploadWidget = function(user_opts) {
	$ = jQuery;

	var opts = $.extend({
		base_dir: '',
		change_label: 'Change Image',
		remove_label: 'Remove Image',
		placeholder_img: 'nf/images/question_mark.png'
		// bgcolor: '#ffffff',
	}, user_opts);

	opts['placeholder_img'] = opts['base_dir'] + opts['placeholder_img'];

	if (!arguments.callee.count) { arguments.callee.count = 0; }
	arguments.callee.count++;

	var input = this;
	var widget = $("\
		<div class='nfUploadWidget'>\
			<div class='imgContainer'>\
				<img src='' />\
			</div>\
			<div class='uploadQueue' id='queue_"+ arguments.callee.count +"'></div>\
			<div class='change' id='upload_button_"+ arguments.callee.count +"'>button</div>\
			<button class='remove'>"+ opts['remove_label'] +"</button>\
		</div>\
	");

	//if (input.attr('id')) { $("label[for="+ input.attr('id') +"]").hide(); }

	widget.insertAfter(input.hide());

	var image = widget.find('img');
	var thumb_attrs =
		'&id='+ input.attr('value') +
		'&width='+ ((image.parent().width() > 0) ? image.parent().width() : 150) + // Default to 150x200
		'&height='+ ((image.parent().height() > 0) ? image.parent().height() : 200) +
		((opts['bgcolor']) ? '&bgcolor='+ opts['bgcolor'] : '');
	var image_src = (input.attr('value')) ?
		(opts['base_dir'] +"nf/upload_handler.php?action=preview&scale_type=fit"+ thumb_attrs) :
		(opts['placeholder_img']);

	image.attr('src', image_src);

	widget.find('button.remove').click(function() {
		input.attr('value', '');
		image.attr('src', opts['placeholder_img']);
		return(false);
	});

	widget.find('div.change').uploadify({
		'uploader': opts['base_dir'] +'nf/uploadify.swf',
		'script': opts['base_dir'] +'nf/upload_handler.php',
		'scriptData': { 'action': 'new_file' },
		'queueID': 'queue_'+ arguments.callee.count,
		'auto': true,
		'cancelImg': opts['base_dir'] +'nf/images/stop.png',
		'buttonText': opts['change_label'],
		'onSelect': function(event, queueId, fileObj) {
			image.attr('src', opts['base_dir'] +'nf/images/spinner_big.gif');
		},
		'onError': function(event, queueId, fileObj, errorObj) {
			image.attr('src', opts['base_dir'] +'nf/images/image_missing.png');
			return(false);
		},
		'onCancel': function(event, queueId, fileObj, data) {
			$(event.target).parent().find(".uploadifyQueueItem").slideUp(function() { $(this).remove() });
			image_src = (input.attr('value')) ? (opts['base_dir'] +"nf/upload_handler.php?action=preview&id="+ input.attr('value')) : (opts['placeholder_img']);
			image.attr('src', image_src);
			return(false);
		},
		'onComplete': function(event, queueId, fileObj, response, data) {
			$(event.target).parent().find(".uploadifyQueueItem").slideUp(function() { $(this).remove() });

			if (parseInt(response) == response) {
				// Fill in the real input box with the new upload id
				input.attr('value', response);

				var thumb_attrs =
					'&id='+ response +
					'&width='+ ((image.parent().width() > 0) ? image.parent().width() : 150) + // Default to 150x200
					'&height='+ ((image.parent().height() > 0) ? image.parent().height() : 200) +
					((opts['bgcolor']) ? '&bgcolor='+ opts['bgcolor'] : '');
				image.attr('src', opts['base_dir'] +"nf/upload_handler.php?action=preview&scale_type=fit&id="+ thumb_attrs);
			} else {
				input.attr('value', '');
				image.attr('src', opts['base_dir'] +'nf/images/image_missing.png');
			}
			return(false);
		}
	});
}

$(document).ready(function () {
	bindCherry();
});

