// submitting a new memo form is a bit tricky because if a file
// has been attached, then the submit can't be asynchronous, in
// which case we target a hidden iframe to fake it.
function submitNewMemoForm(form) {
	prepare_memo_selection();
	
	 // check first that there is some memo content
	 if ($('memo_content').value.strip() == '') {
	   $("memo_submit").disable();
	 } else {
	   // submit
	   $('memo_form_cover').clonePosition(form).show();
	   var file_input = $('memo_attachment');
	   if (file_input && file_input.value) { // no async
	     return true;
	   } else { // async ok
	     form.request();
	     return false;
	   }
	 }
}

function prepare_memo_selection() {
	var selection = current_selection();
	if (selection.is_empty()) { // set form values based on current page
		$('memo_origin_type').value = show_origin_type;
		$('memo_origin_id').value = show_origin_id;
		if (show_origin_type == 'AudioFile') {
		    if (current_player)
		        $('memo_paragraph_index').value = parseInt(current_player.position()/1000);
		    else
		        $('memo_paragraph_index').value = 0;
		}
	} else { // set the form values from current selection
		selection.set_form_values('memo');
	}
}

function toggleAttachFile(input) {
	var element = '<input type="file" name="attachment[uploaded_data]" id="memo_attachment"/>';
	if (input.visible()) {
		Effect.Fade($('memo_attach_hide'), {duration: 0.25, queue:'end'});
		Effect.SlideUp(input, {duration: 0.25, queue:'end'});
		Effect.Appear($('memo_attach_show'), {duration: 0.25, queue:'end'});
		
		// put in a fresh file input element (so nothing gets uploaded)
		input.update(element);
	}
	else{
	  // put in a fresh file input element
	  input.update(element);
	
		Effect.Fade($('memo_attach_show'), {duration: 0.25, queue:'end'});
		Effect.SlideDown(input, {duration: 0.25, queue:'end'});
		Effect.Appear($('memo_attach_hide'), {duration: 0.25, queue:'end'});
	}
	
	//setTimeout("updateSidebarSize();", 850);
}
