/*
Upload Script for NeatUpload

Copyright (c) 2005 - 2007 by Pengcheng Rong (service@bizmodules.net) and Biz Modules Solutions(http://www.bizmodules.net)
All rights reserved.

To localize this script library, please make a copy and name it UploadProgress.(locale).js, for example, UploadProgress.de-DE.js, UploadProgress.it-IT.js.

Then you should modify these hard coded text strings in your duplicate:
********************************************************
Please wait while we upload your file.
Loading
Transfered
 of
 in total
time elapsed
remaining
Upload succeeded
Your video is being encoded, please wait
File upload have been cancelled
********************************************************

*/

var PLEASEWAIT = "Please wait while we upload your file";
var LOADING = "Loading";
var TRANSFERED = "Transfered";
var OF = "of";
var INTOTAL = "in total";
var TIMEELAPSED = "time elapsed";
var REMAINING = "remaining";
var SUCCEEDED = "Upload succeeded";
var BEINGENCODED = "Your video is being encoded, please wait";
var CANCELLED = "File upload have been cancelled";


var PortalId;
var Locale;
var InputControlId;
var CurrentPostBackId;
var ProgressContainer;
var PreviousProgress;
var IntervalHandler;
var IsUploading = false;
var CancelledBefore;

var MessageHeader =  "<table style='border:1px solid gray;' width='100%'><tr><td class=Normal>";
var MessageFooter = "</td></tr></table>";

function ShowUploadProgress(postBackId, inputId, portalId, locale)
{
	PortalId = portalId;
	Locale = locale;
	InputControlId = inputId;
	CurrentPostBackId = postBackId;

	ProgressContainer = document.getElementById(InputControlId + '_progress');

	var inputFile = document.getElementById(InputControlId);
	if (inputFile.value != '')
	{
		if (CurrentPostBackId == "")
		{
			ProgressContainer.innerHTML = MessageHeader + PLEASEWAIT + MessageFooter;
			return;
		}
		else
		{
			ProgressContainer.innerHTML = MessageHeader + "<font class=SubSubHead>"+ LOADING +"...</font>";
		}
		if (CancelledBefore)
		{
			window.setTimeout("IntervalHandler = window.setInterval('refreshUploadProgress();', 1000);", 10000);
		}
		else
		{
			IntervalHandler = window.setInterval("refreshUploadProgress();", 1000);
		}
		CancelledBefore = false;
	}
}

function refreshUploadProgress()
{
	var progressInfo = BizModules.UltraVideoGallery.UploadProgress.GetUploadProgress (CurrentPostBackId, PortalId, Locale, GetUploadProgress_CallBack);
}

function GetUploadProgress_CallBack(response)
{
	if (response.error != null)
	{ 
		ProgressContainer.innerHTML = MessageHeader + response.error.Message + MessageFooter;
		return;
	} 
	ShowFriendlyProgress(response.value);
}

function ShowFriendlyProgress(p)
{
	var strHtml="";
	var bolDoCancel = false;
	if (p.Status == 'NormalInProgress' || p.Status == 'ChunkedInProgress')
	{
		strHtml += "<table width='100%' border=0><tr><td class=SubSubHead>" + PLEASEWAIT + "</td></tr><tr><td class=Normal>"; 
		strHtml += TRANSFERED + " " + p.BytesRead + " " + OF + " " + p.BytesTotal + " " + INTOTAL+ ", " + p.BytesPerSec + ".</td></tr><tr><td>";
		strHtml += "<table width='100%' border=0><tr><td>";
		strHtml += GetPercentageHtml(p.FractionComplete);
		strHtml += "</td><td width=80>";


		if (CanCancel)
		{
			strHtml += "<input type=button class=CommandButton value=Cancel onclick='DoCancel()'>";
		}
		strHtml += "</td></tr></table></td></tr>";
		strHtml += "<tr><td class=Normal>" + TIMEELAPSED + ":" + p.TimeElapsed + " &nbsp;&nbsp; " + REMAINING + ":" + p.TimeRemaining + "</td></tr></table>";
	}
	else if (p.Status == "Completed")
	{
		if (PreviousProgress != undefined)
		{
			PreviousProgress += " .";
		}
		else
		{
			PreviousProgress = "<font class=SubSubHead>" + SUCCEEDED + "</font><p class=Normal>" + BEINGENCODED;
		}
		strHtml = PreviousProgress;
	}
	else if (p.Status == "Cancelled")
	{
		strHtml = CANCELLED;
		ClearRefreshInterval();
	}
	else if (p.Status == "Rejected")
	{
		strHtml = p.Rejection;
		ClearRefreshInterval();
		bolDoCancel = true;
	}
	else if (p.Status == "Failed")
	{
		strHtml = p.Failure;
		ClearRefreshInterval();
	}
	else
	{
		return;
	}

	ProgressContainer.innerHTML = MessageHeader + strHtml+ MessageFooter;

	if (bolDoCancel)
	{
		DoCancel();
	}
}

function ClearRefreshInterval()
{
	IsUploading = false;
	clearInterval(IntervalHandler);
}

function GetPercentageHtml(percentage)
{
	var percentageHtml = "<table width='100%' cellspacing='0' cellpadding='0' border='0'>" 
	percentageHtml+="	<tr>"
	percentageHtml+="		<td width='4'><img src='/DesktopModules/UltraVideoGallery/images/left.gif'></td>"
	percentageHtml+="		<td style=\"background-image:url(/DesktopModules/UltraVideoGallery/images/borderbg.gif);'\" height='18'>"
	percentageHtml+="			<table cellspacing='0' cellpadding='0' border='0' width='" + ToPercentage(percentage) + "' style=\"background-image:url(/DesktopModules/UltraVideoGallery/images/dot.gif); background-repeat: repeat-x; background-position: left;'\"><tr><td><img src='/DesktopModules/UltraVideoGallery/images/BorderBg.gif'></td></tr></table>"
	percentageHtml+="		</td>"
	percentageHtml+="		<td width='4'><img src='/DesktopModules/UltraVideoGallery/images/right.gif'></td>"
	percentageHtml+="	<tr>"
	percentageHtml+="</table>";

	return percentageHtml;
}

function ToPercentage(percentage)
{
	var percent = parseInt(percentage * 100);
	return percent + "%";
}

function CanCancel()
{
	try
	{
		if (window.stop || window.document.execCommand)
			return true;
		else
			return false;
	}
	catch (ex)
	{
		return false;
	}
}

function DoCancel() 
{
	if (document && document.stop)
		document.stop();
	else if (document && document && document.execCommand)
		document.execCommand('Stop');

	CancelledBefore = true;
	BizModules.UltraVideoGallery.UploadProgress.CancelUploadProgress (CurrentPostBackId, null);
}

