function dtable_row_add(table, data) {
	var newidx = $('#dtable_' + table + ' tr').length;
	var newrow = $('#dtable_' + table + '___idx__').clone();
	newrow.attr('id', 'dtable_' + table + '_' + newidx);
	newrow.insertBefore('#dtable_' + table + '___idx__');
	newrow.html( newrow.html().replace(/__idx__/g,newidx) );
	newrow.show();
	if (data)
	{
		jQuery.each(data, function(key, val)
		{
			var datafield = $('#' + table + '_' + key + '\\[' + newidx + '\\]');
			if (datafield)
				datafield.attr('value', val);
		});
	}
}

function dtable_row_del(table, index)
{
	$('#dtable_' + table + '_' + index).remove();
}

function wr_delete_list_item(node)
{
	if (confirm($('#wr_delete_confirm_text').html()))
		$(node).parent('li').remove();
}

function wr_add_list_item(listclass, data)
{
	// decodeURIComponent needed to undo unnecessary escaping of some random html parameters by firefox
	if (data)
		$('#' + listclass + '_list').append( $.template( decodeURIComponent($('#' + listclass + '_list_template').html()) ), data);
}

function submit_form_edit_project()
{
	if (project_edit_init_extended)
	{
		$.each(['project_related_authors','project_images','project_attachments'], function() {
			collect_id_list(this);
		});
	
		var file_list = new Array;
		$('#related_pending_files_list li').each( function() {
			var file = new Object;
			file.hash = $(this).attr('rel');
			file.title = $(this).attr('_title');
			file.filename = $(this).attr('_filename');
			file.project_file_type_id = $(this).attr('_project_file_type_id');
			file_list.push(file);
		});
		$('#related_pending_files_json').val( $.toJSON(file_list) );
	}	
	
	$('#form_edit_project').submit();
}

function submit_form_edit_office()
{
	collect_id_list('office_related_authors');
	$('#form_edit_office').submit();
}

function collect_id_list(id)
{
	var id_list = new Array;
	$('#' + id + '_list li').each( function() { 
		id_list.push($(this).attr('rel'));
	});
	$('#' + id + '_id_list').val( $.toJSON(id_list) );
}

function init_uploader()
{
	var au = new AjaxUpload('#project_file', {
		action: '/liikmetele/minulooming/save/ajaxfile/',
		name: 'project_file',
		autoSubmit: false,
		onSubmit: function(file, extension) {
            if (! (extension && /^(jpg|pdf)$/.test(extension))){
                    // extension is not allowed, cancel upload
                    alert($('#wr_project_file_extension_error').html());
                    return false;
            }
		},
		onComplete : function(file, response){
			if (response && $.trim(response).length == 32)
			{
				wr_add_list_item('related_pending_files', {
					'file_id':$.trim(response),
					'title':$('#project_file_title').val(),
					'filename':file,
					'project_file_type_id':$('#project_file_type_id').val()
				});
				$('#related_pending_files_list_title').show();
			}
			$('#project_file_title').val('');
		}	
	});
	return au;
}

function init_authors_autocomplete(id, data)
{
	$('#' + id + '_author_id_autocomplete').autocomplete(data, {
		minChars: 0,
		width: 310,
		matchContains: 'word',
		autoFill: false,
		formatItem: function(row) {
			return row.firstname + ' ' + row.surname;
		}
	});
	$('#' + id + '_author_id_autocomplete').result(function(event, data, formatted){
		wr_add_list_item(id, data);
		$('#' + id + '_author_id_autocomplete').val('');
	});
	
}

function init_data_table(id)
{
	$('#' + id).dataTable( {		'aaSorting': [[ 1, 'asc' ]],		'sPaginationType': 'full_numbers',		'bLengthChange' : false,		'iDisplayLength' : 20,		'bStateSave' : true,		'sCookiePrefix': 'dtbl',		'oLanguage': {			'oPaginate': {				'sFirst': '<<<',				'sPrevious': '<',				'sNext': '>',				'sLast': '>>>'			},
			'sSearch': 'Filter: '		}	});
	$('#' + id + '_filter').prepend( $('#data_table_title') );
}

function project_type_onchange()
{
	if ($('#project_type_id').val() == $('#_project_type_id_other').val())
		$('#project_type_desc').show();
	else
		$('#project_type_desc').hide();
}

function search_filter(filter)
{
	$('#search_filter').val(filter);
	$('#search_form').submit();
	
}
