/****************************************************************************
*	
*	LEGACY STUFF.
*	These are the old functions that I wrote myself before 
*	I started using JQuery.  Eventually we can get rid of them,
*	but I rewrote the important ones using JQuery so that old stuff won't break.
*
****************************************************************************/

var loading = '<div id="loading">'+
				'<img src="gs/ajax-loader.gif"><br />'+
				'please wait.  loading...'+
			'</div>';

function formatItem(row) {
	return row[0];
}	

function showMessageBox(message) {
	if(message.length > 0) {
		$('#messages').html(message).css('top', $(document).scrollTop()+50).jqmShow();
		setTimeout('closeMessageBox()', 3000);
	}
}

function closeMessageBox() {
	$('#messages').jqmHide();
}

function showErrorBox(message) {
	var top  = $(document).scrollTop()+50;
	$('#errors').html(message).jqmShow().css('top', top);
}

function closeErrorBox() {
	$('#errors').jqmHide();
}

function closeAllBoxes() {
	closeMessageBox();
	closeErrorBox();
}
	
/*
*	Opens a dialog box and fills it with the content from the templates directory
*	Takes a map of params
*/
function dialogBox(template, params) {
	var url = "_get.php?template="+template;
	$('#dialog').html(loading).load(url, params).css('top', $(document).scrollTop()+50).jqmShow();
}

function closeDialogBox() {
	$('#dialog').jqmHide();
}

/*
*	Submits the form specified by the ID, posting it to
*	_do.php, also adding the formID as $_GET['action']
*/
function _do(formID) {
	var formname = '#'+formID;
	$(formname).ajaxSubmit({
		url: '_do.php?action='+formID,
		success: function(response) {
			if(response==1) {
				var action = $('#'+formID+' input[@name=successAction]').val();
				eval(action);
			} else showErrorBox(response);
		}
	});
}

/**
*	_put(template_from_templates_dir, [params], [destination]);
*	Gets the template in the templates directory and puts it into the container specified by the same ID
*	The second parameter can be a map of parameters.
*	The third parameter can be an alternative destination, if the destination isn't the same as the template name.
*/
function _put(template) {
	var params;
	
	// If the user has passed in a second argument, merge it with the params map 
	if(arguments[1]!=null) {
		params = arguments[1];
	}
	
	var destination = (arguments[2]!=null) 
		? arguments[2]
		: template;
		
	$('#'+destination).html(loading).load('_get.php?template='+template, params);
}





/****************************************************************************
*	
*	GEARBOX-SPECIFIC STUFF
*	Other functions used throughout the site.
*
****************************************************************************/

function clear_equipment(id) {
	$('#name_'+id).val('');
	$('#tips_'+id).val('');
	//findObj('name_'+id).value = '';
	//findObj('tips_'+id).value = '';
	submit_equipment();
}

function submit_equipment() {
	var data = $("#create_tutorial").formToArray();
	_put('equipment/add', data, 'equipment');
}


function moveStep(direction, step_id, tutorial_id) {
	var params = {action: "moveStep", step_id: step_id, direction: direction};
	$.post('_do.php', params, function(response) {
		if(response==1) {
			_put("steps/list", "tutorial_id="+tutorial_id, 'steps');
		} else showErrorBox(response);
	});
}

function loadEquipment(tutorial_id) {
	var data = {tutorial_id: tutorial_id};
	_put("equipment/list", data, 'equipment');	
}

function loadSteps(tutorial_id) {
	var data = {tutorial_id: tutorial_id};
	_put("steps/list", data, 'steps');	
}

/*
* Scripts for adding and delteing items list.
*/
function _addEquipment(template, formID)
{
	var params = $('#'+formID).formToArray();
	params.push({name: 'template', value: template}, {name: 'formID', value: formID}, {name: 'edit', value: 0}, {name: 'delete', value: 0});
	_put(template, params);
}

function _editEquipment(template,formID,editNum,del)
{
	var params = $('#'+formID).formToArray();
	params.push({name: 'template', value: template}, {name: 'formID', value: formID}, {name: 'edit', value: editNum}, {name: 'delete', value: del});
	_put(template, params);
}


