

// preload a hidden image

$(document).ready(function()
{
	// round corners

	setTimeout("$('.corner').corner('10px')", 200);


	// preload an image

	var image = new Image(1,1);

	image.src="images/thin_slice.png";


	// fix form submission for IE

	submit_on_enter();
});



// add loading graphic to main div

function add_loader()
{
	$('#main_div #search_div').after('<center><img src="images/loading.gif" alt="Loading..." title="Loading..." /></center><br />');

	return false;
}



// for sending hard copy requests

function hard_copy()
{
	var options = { 
		url: '?action=copier',
		success: update_main
		};

	$(this).ajaxSubmit(options);

	return false;
}



// update after edit input blur

function update_field( record_id, field_name )
{
	// get value of input

	var current_val = $.trim($('#' + field_name + '_' + record_id).html());

	var value = $('#' + field_name + '_' + record_id + '_input').val();


	// set content of associated div (and hide, in case it was previously empty)

	$('#' + field_name + '_' + record_id).html(value).hide();


	return false;
}



// function for handling edit button clicks

function edit( id )
{
	// if inputs are currently showing, submit form

	if( $("#edit_" + id + " .result_input:visible").length > 0 )
	{
		submit_edit(id);
	}


	// flip to the input fields

	$('.result_' + id + ' div').toggle();

	return false;
}


function submit_edit( id )
{
	$("#edit_"+id).ajaxSubmit({url:'?action=edit', success: edit_callback});

	return false;
}



// after editing a dsm

function edit_callback( response )
{
	// if successful, move input values to data divs

	if( 'success' == response.split('|')[0] )
	{
		// get id

		var id = response.split('|')[1];


		// get values from inputs

		inputs = $('.result_' + id + ' input');

		values = {};

		inputs.each(function(){ values[$(this).attr('name')] = $(this).val();});

		for( index in values )
		{
			$('#' + index + '_' + id).html( values[index] );
		}

		

		// toggle edit form if necessary

		if( $("#edit_" + id + " .result_input:visible").length > 0 )
		{
			$('.result_' + id + ' div').toggle();
		}

		return false;
	}


	// otherwise alert failure

	alert('Had a problem, try again later!');

	return false;
}




// download a DSM

function download( id )
{
	window.location = '?action=download&id=' + id;
}



// get a link to a DSM

function get_link( id )
{
	$.post('?action=link&id=' + id, {}, function(response){

		title = 'The following URL has been activated for 72 hours';

		options = { buttons: { "Close": function() { $(this).dialog("close"); } }, height: 100, width: 750 };

		$('<div title="' + title + '" class="dialog">' + response + '</div>').dialog(options);

		});
}



// delete a DSM

function delete_dsm( id )
{
	// verify user intent

	if( confirm( 'Really delete?' ) )
	{
		$('#edit_' + id + ' .result_actions').html('Deleting...');

		$.post('?action=delete&id=' + id, {}, delete_callback);
		
		return;
	}

	return;
}



// after deleting a dsm

function delete_callback( response )
{
	// if successful, move input values to data divs

	if( 'success' == response.split('|')[0] )
	{
		// get id

		var id = response.split('|')[1];


		// remove search row

		$('.result_' + id).remove();

		return;
	}


	// otherwise alert failure

	alert('Had a problem, try again later!');

}



// load a resource to the main div

function load( url )
{
	$.post( url, {}, update_main );
}



// IE submission fix

function submit_on_enter()
{
	return false;

	$('input').keydown(function(e){

		if (e.keyCode == 13 && browserName=="Microsoft Internet Explorer") 
		{
//			$(this).parents('form').submit();

			alert('ie');

//			return false;
		}
		else
		{
			alert(browserName);
		}

	});

	return false;
}



// replace the html of the main div

function update_main( html )
{
	$('#main_div').html( html );

	// fix form submission for IE

	submit_on_enter();

	$('.corner').corner();

	return false;
}


// request logout and refresh page

function logout()
{
	$.post('/?action=logout', {}, function(response){ document.location = ''; });
}



// after logging in, replace homepage with manager

function loginCallback( login_response )
{
	if( 'success' == login_response )
	{
		name = $('#name_input').attr('value');

		$('#name_input').attr('value','Logging in...');

		$.post('?action=manager',{},
			function(response){
			$('body').html(response);
			$('title').append( ': ' + name);
			setTimeout("$('.corner').corner();",150);
			});
		return;
	}

	// display error message

	alert( login_response );
}



function mass_download()
{
	$.post( '?action=massdownload&countonly=countonly', {}, function(response)
	{ 
		if(response && '0' != response)
		{
			count = 25 < response ? 25 : response;

			alert('You will be prompted to download the next ' 
				+ count + ' files out of ' + response + ' remaining.'); 

			window.location = '?action=massdownload'; 
		} 
		else 
		{ 
			alert('There are no new files to download.'); 
		} 
	});
}



