/*
	A simple class for displaying file information and progress
	Note: This is a demonstration only and not part of SWFUpload.
	Note: Some have had problems adapting this class in IE7. It may not be suitable for your application.
*/

// Constructor
// file is a SWFUpload file object
// targetID is the HTML element id attribute that the FileProgress HTML structure will be added to.
// Instantiating a new FileProgress object with an existing file will reuse/update the existing DOM elements
function PadDigits(n, totalDigits) { 
    n = n.toString(); 
    var pd = ''; 
    if (totalDigits > n.length) 
    { 
        for (i=0; i < (totalDigits-n.length); i++) 
        { 
            pd += '0'; 
        } 
    } 
    return pd + n.toString(); 
} 

function ClearInstructions(el, originalString) {
	if(el.value==originalString)
	   el.value='';
}

function HelperStatus(elId,status) {
	document.getElementById("Help_"+elId).innerHTML = status;
}

function TermsUpdate() {
	if (swfu) {
		var file = swfu.getFile();
		if (file)
			EnableSubmitButton(file.id);
	}
}

// here we'll check to see that all data points have been entered
function EnableSubmitButton(batchName){
	var state = true;
	if (document.getElementById("Title_" + batchName).value != "") {
		if (document.getElementById("Description_" + batchName).value != "") {
			state = false;
			HelperStatus(batchName, "Ready for upload.");
		}
		else 
			HelperStatus(batchName, "Please enter a description.");
	}
	else
		HelperStatus(batchName, "Please enter a title.");			
		
	document.getElementById("Submit_"+batchName).disabled = state
	// check if Batch mode and enable the batch button if 
	// every one in the batch is ready to go - check if
	// the buttons are available.
	if (swfu.customSettings.enableBatchButton === true) {
	//	Submit_SWFUpload_0_2
		var allAvailable = false;
		var stats = swfu.getStats();
		var file = swfu.getFile();
		for(var i=file.index;i<file.index+stats.files_queued;i++) {
			if(document.getElementById("Submit_SWFUpload_0_"+i).disabled == true)
				allAvailable=true;  // if we find somethat is disabled, leave the batch button disabled.
		}
		document.getElementById("btn_batchUpload").disabled = allAvailable;
	}
}

function addOption(el, optionString) {
	var option = document.createElement("option");
	option.value = "-1";
	option.appendChild(document.createTextNode(optionString));
	el.appendChild(option);
}

function fullMonthName(month){
	monthsOfYear = new Array("January", "February", "March", "April", "May",
    "June", "July", "August", "September", "October", "November", "December"); 
	return(monthsOfYear[month]);
}

function disableMe(id){
	document.getElementById(id).disabled = true;
}


function FileProgress(file, targetID) {
	this.fileProgressID = file.id;

	this.opacity = 100;
	this.height = 0;

	this.fileProgressWrapper = document.getElementById(this.fileProgressID);
	if (!this.fileProgressWrapper) {
		this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "progressWrapper";
		this.fileProgressWrapper.id = this.fileProgressID;
		
		this.fileProgressElement = document.createElement("div");
		this.fileProgressElement.className = "progressContainer";
		
		var progressCancel = document.createElement("a");
		progressCancel.className = "progressCancel";
		progressCancel.href = "#";
		progressCancel.style.visibility = "hidden";
		progressCancel.appendChild(document.createTextNode(" "));
		
		var progressText = document.createElement("div");
		progressText.className = "progressName";
		progressText.appendChild(document.createTextNode(file.name));
		
		var progressBar = document.createElement("div");
		progressBar.className = "progressBarInProgress";
		
		var progressStatus = document.createElement("div");
		progressStatus.className = "progressBarStatus";
		progressStatus.innerHTML = "&nbsp;";
		
		var progressDetails = document.createElement("div");
		progressDetails.className = "progressDetails";
		
		var progressDetailsTitle = document.createElement("div");
		progressDetailsTitle.className = "progressDetailsTitle";
		progressDetailsTitle.innerHTML = "*Title:";
		
		var progressTitle = document.createElement("input");
		progressTitle.id = "Title_" + file.id;
		progressTitle.className = "progressTitle";
		var txtTitle = "Please enter a title for your video.";
		progressTitle.onfocus = new Function("evt", "ClearInstructions(this,\"" + txtTitle + "\");EnableSubmitButton(\"" + file.id + "\");");
		progressTitle.onblur = new Function("evt", "EnableSubmitButton(\"" + file.id + "\");");
		progressTitle.onkeyup = new Function("evt", "EnableSubmitButton(\"" + file.id + "\");");
		progressTitle.value = txtTitle;

		var progressDetailsDescription = document.createElement("div");
		progressDetailsDescription.className = "progressDetailsDescription";
		progressDetailsDescription.id = "progressDetailsDescription";
		progressDetailsDescription.innerHTML = "*Description:";
		
		var progressDescription = document.createElement("textarea");
		progressDescription.id = "Description_" + file.id;
		progressDescription.className = "progressDescription";
		progressDescription.rows = 4;
		var txtDescription = "Tell us about what you're sending. Be sure to describe your material as completely as possible - It's important to include specific details that answer the questions who, what, where and when.";
		progressDescription.onfocus = new Function("evt", "ClearInstructions(this,\"" + txtDescription + "\"); EnableSubmitButton(\"" + file.id + "\");");
		progressDescription.onblur = new Function("evt", "EnableSubmitButton(\"" + file.id + "\");");
		progressDescription.onkeyup = new Function("evt", "EnableSubmitButton(\"" + file.id + "\");");
		progressDescription.value = txtDescription;
		progressDetails.appendChild(progressDetailsTitle);
		progressDetails.appendChild(progressTitle);
		progressDetails.appendChild(progressDetailsDescription);
		progressDetails.appendChild(progressDescription);

		this.fileProgressElement.appendChild(progressCancel);
		this.fileProgressElement.appendChild(progressText);
		this.fileProgressElement.appendChild(progressStatus);
		this.fileProgressElement.appendChild(progressBar);
		this.fileProgressElement.appendChild(progressDetails);
		
		var progressGo = document.createElement("input");
		progressGo.id = "Submit_" + file.id;
		progressGo.className = "progressGo";
		progressGo.href = "#";
		progressGo.type = "button";
		progressGo.value = "Submit";
		progressGo.disabled = "disabled";
		progressGo.onclick = new Function("evt", "swfu.startUpload(\"" + file.id + "\"); this.blur();disableMe(\"Submit_" + file.id + "\"); ");

		var progressHelper = document.createElement("div");
		progressHelper.className = "HelpUpload";
		progressHelper.id = "Help_" + file.id;
		progressHelper.innerHTML = "Please enter a title."
		
		this.fileProgressElement.appendChild(progressHelper);
		this.fileProgressElement.appendChild(progressGo);
		
		
		this.fileProgressWrapper.appendChild(this.fileProgressElement);
		
		document.getElementById(targetID).appendChild(this.fileProgressWrapper);
		this.batchButton();
	} else {
		this.fileProgressElement = this.fileProgressWrapper.firstChild;
	}

	this.height = this.fileProgressWrapper.offsetHeight;

}

FileProgress.prototype.batchButton = function() {
	if (swfu.customSettings.enableBatchButton === true) {
		// add the batch upload button.
		var batchBtn = document.getElementById("btn_batchUpload");
		if (batchBtn) {
			// just update the button text to batch count
			batchBtn.value = "Batch Upload (" + swfu.getStats().files_queued + ")";
		}
		else {
			// undefined, we need to create one.
			var btn = document.createElement("input");
			btn.id = "btn_batchUpload";
			btn.href = "#";
			btn.className = "btn_batchUpload";
			btn.type = "button";
			btn.value = "Batch Upload (" + swfu.getStats().files_queued + ")";
			btn.disabled = "disabled";
			btn.onclick = new Function("evt", "swfu.startUpload();this.blur();");
			var superParent = document.getElementById(swfu.customSettings.progressTarget).parentNode;
			superParent.appendChild(btn);
		}
	}
};  // end enableBatchButton

FileProgress.prototype.setProgress = function (percentage) {
	this.fileProgressElement.className = "progressContainer green";
	this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[3].style.width = percentage + "%";
};
FileProgress.prototype.setComplete = function () {
	this.fileProgressElement.className = "progressContainer blue";
	this.fileProgressElement.childNodes[3].className = "progressBarComplete";
	this.fileProgressElement.childNodes[3].style.width = "";

	var oSelf = this;
	setTimeout(function () {
		oSelf.disappear();
	}, 5000);
};
FileProgress.prototype.setError = function () {
	this.fileProgressElement.className = "progressContainer red";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";

	var oSelf = this;
	setTimeout(function () {
		oSelf.disappear();
	}, 5000);
};
FileProgress.prototype.setCancelled = function () {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";
	this.batchButton();

	var oSelf = this;
	setTimeout(function () {
		oSelf.disappear();
	}, 2000);
};
FileProgress.prototype.setStatus = function (status) {
	this.fileProgressElement.childNodes[2].innerHTML = status;
};

// Show/Hide the cancel button
FileProgress.prototype.toggleCancel = function (show, swfUploadInstance) {
	this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
	if (swfUploadInstance) {
		var fileID = this.fileProgressID;
		this.fileProgressElement.childNodes[0].onclick = function () {
			swfUploadInstance.cancelUpload(fileID);
			return false;
		};
	}
};

// Fades out and clips away the FileProgress box.
FileProgress.prototype.disappear = function () {

	var reduceOpacityBy = 15;
	var reduceHeightBy = 4;
	var rate = 30;	// 15 fps

	if (this.opacity > 0) {
		this.opacity -= reduceOpacityBy;
		if (this.opacity < 0) {
			this.opacity = 0;
		}

		if (this.fileProgressWrapper.filters) {
			try {
				this.fileProgressWrapper.filters.item("DXImageTransform.Microsoft.Alpha").opacity = this.opacity;
			} catch (e) {
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
				this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + this.opacity + ")";
			}
		} else {
			this.fileProgressWrapper.style.opacity = this.opacity / 100;
		}
	}

	if (this.height > 0) {
		this.height -= reduceHeightBy;
		if (this.height < 0) {
			this.height = 0;
		}

		this.fileProgressWrapper.style.height = this.height + "px";
	}

	if (this.height > 0 || this.opacity > 0) {
		var oSelf = this;
		setTimeout(function () {
			oSelf.disappear();
		}, rate);
	} else {
		this.fileProgressWrapper.style.display = "none";
	}
};