/**
 * Copyright (c) 2006 by Art + Logic Interactive. All rights reserved.
 *
 * Authored by: Software design and programming, Kevin Tatroe.
 */
/*
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

 * Neither the name Kevin Tatroe, nor the names of its contributors
   may be used to endorse or promote products derived from this software without
   specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @author  Kevin Tatroe <ktatroe@alinteractive.com>
 * @package CreamyFilling/Uploads
 * @version $Revision: 1.00 $
 * @copyright Copyright (c) 2006 by Art + Logic Interactive. All rights reserved.
 */

/** */

var uploadCount = 0;
var uploadSessionIds = {};
var uploadUpdater = false;

function beginUpload(uploadFrame, sessionId, onComplete)
{
	var progressBar = $(uploadFrame.name + "_progress");

	uploadFrame.form.submit();
	uploadCount = uploadCount + 1;
	
	uploadSessionIds[uploadFrame.name] = sessionId;
	uploadUpdater = new Ajax.PeriodicalUpdater({success: 'uploadSuccess', failure: 'uploadFailure' },'/resources/ajax/uploaderProgress.php', {'decay': 2, 'frequency': 0.5, 'method': 'post', 'parameters': 'sessionId=' + sessionId, 'onSuccess': function(request) { updateUploadProgress(progressBar, request) }, 'onFailure': function(request) { updateUploadFailure(progressBar, request) }, 'onComplete': function(request) { onComplete(sessionId, request) } })

	Element.show(progressBar.parentNode);
} // beginUpload()


function updateUploadProgress(progressBar, request)
{
	var percent = parseInt(request.responseText);

	if (!percent)
	{
		percent = 0;
	} // if

	progressBar.style.width = percent + "%";
	
	if(percent >= 100)
	{
		var identifier = progressBar.id.replace("_progress", "");

		if (uploadSessionIds[identifier])
		{
			uploadCount = uploadCount - 1;
			var inp = $(identifier);

			if(inp)
			{
				inp.value = uploadSessionIds[identifier];
			} // if
		} // if

		// turn off the updater and hide the progress bar
		cleanUpDownload(identifier, progressBar, request);
	} // if
} // updateUploadProgress()


function updateUploadFailure(progressBar, request)
{
	var identifier = progressBar.id.replace("_progress", "");

	// turn off the updater and hide the progress bar
	cleanUpDownload(identifier, progressBar, request);

	alert('Error: ' + request.responseText);
} // updateUploadFailure()


function submitUploadForm(form)
{
	if(uploadCount > 0)
	{
		alert("File upload in progress. Please wait until upload finishes and try again.");
	} // if
	else
	{
		form.submit();
	} // else
} // submitUploadForm()


function cleanUpDownload(identifier, progressBar, request)
{
	Element.hide(progressBar.parentNode);
	progressBar.style.width = 0;

	uploadCount = uploadCount - 1;

	uploadUpdater.stop();
	delete uploadUpdater;
	uploadUpdater = false;

	delete uploadSessionIds[identifier];
} // cleanUpDownload()
