/*
 * Javascript Functions
 *
 * Copyright (c) CLICKSPORTS
 * $Rev: 151 $
 * $Author: cs $
 * $Date: 2009-03-20 09:27:47 +0100 (Fr, 20 Mrz 2009) $
 *
 */

// Inverts all checkboxes (checked becomes unchecked)
function form_check_invert(checkbox_class) {
	
	$$('input.' + checkbox_class).each(function(checkbox){

		if(checkbox.checked) checkbox.checked = false;
		else checkbox.checked = true;

	});
	
}

// Submit a form given id of form.
function submit_form(form) { $(form).submit(); }

// Submit a form onchange, used for select-boxes
// Holds a third parameter if you dont want to
// submit the form on first option value (usually
// this value is empty)
function form_submit(form, select, checkfirst) {
	
	selected = select.options.selectedIndex;
	
	if(!checkfirst) $(form).submit();
	else if(selected != 0 && selected != 1 ) { $(form).submit(); }
	
}

/**
 * Add the event listener for getDirStructure()
 */
function addDirStructureEvent() {
	$('dirstructure').observe(
		'click',
		getDirStructure
	);
}

/**
 * Get the directory structure with ajax
 */
function getDirStructure(e) {
	
	elm = Event.element(e);
	
	indicator = $('filebrowser_indicator')
	indicator.show();

	new Ajax.Updater(
		elm,
		'/dirs/getAjaxDirs', {
			insertion: 'bottom',
			onSuccess: function(transport) {
				Event.stopObserving($('dirstructure'), 'click', getDirStructure);
				indicator.hide();
			}
		}
	);
}

/**
 * Toggle the filebrowser form to target another controller.
 */
function submit_downloadlist() {

	dlForm = $('filebrowser_form');
	if(typeof(dlForm) == 'undefined') return;
	
	// Change the parameter. Dirty but works.
	dlForm.writeAttribute({'action': '/downloadlists/saveToList'});
	dlForm.submit();
}
