/*
 * UploadStatus 1.0
 *
 * Copyright (c) 2007 Felix Langfeldt (http://www.fixel.nl/)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://www.fixel.nl/
 *
 * Built upon jQuery 1.3.0 (http://jquery.com)
 * This also requires the jQuery dimensions plugin
 *
 * CHANGELOG
 * version 1.0, 20 July 2007
 *   NEW: Created plugin.
 */
 
(function($){
 	$.fn.uploadStatus = function(settings) {
		settings = $.extend({}, arguments.callee.defaults, settings);
		$(this).each(function(){
			this.$settings	= settings;
			
			$(this).submit(function(){
				//*** Set event methods.
				if (this.$settings.validate && this.$settings.validate.valid || !this.$settings.validate) {
					//*** Trigger the onUpload event.
					this.$uploaded = false;
					this.$settings.onUpload(this);

					//*** Send the form submittion to a hidden iFrame.
					this.$settings.serial = new Date().getTime();
					this.$settings.action = this.action;
					var frameId = 'jQuery' + this.$settings.serial;
					var io = createUploadIframe(frameId);
					var baseUrl = this.$settings.tracker + "?serial=" + this.$settings.serial + "&formId=" + this.id;

					this.action = baseUrl;
					this.method = "post";
					this.target = frameId;
					this.submit();

					//*** Get the upload progress.
					$(this).getUploadStatus();
				}
				
				return false;
			});
		});
		
		return this;
	};
	$.fn.uploadStatus.defaults = {
		tracker: "/cgi-bin/punchupload.cgi",
		refresh: 1000,
		validate: null
	};
	$.fn.updateUploadStatus = function(current, total, elapsed, error, filename) {
		var objForm = this.get(0);

		if (objForm.$uploaded != true) {
			if (elapsed == -1) {
				//*** Upload completed.
				objForm.$uploaded = true;
				objForm.$settings.onComplete(objForm, error);
				
				//*** Set hidden fields.
				$('#uploadname').remove();
				createHiddenField(objForm, "uploadname", filename);
				
				//*** Submit the original form.
				this.removeAttr("target");
				objForm.action = objForm.$settings.action;
				objForm.method = "post";
				objForm.submit();
			} else {
				if (error) {
					objForm.$settings.onError(objForm, error);
				} else {
					//*** Upload is progress.
					objForm.$settings.onUpdate(objForm, current, total, elapsed);
					setTimeout("$('#" + objForm.id + "').getUploadStatus()", objForm.$settings.refresh);
				}
			}
		}
	};
	$.fn.getUploadStatus = function() {
		var objForm = this.get(0);
		
		$.getJSON(objForm.$settings.tracker, { 
				serial:objForm.$settings.serial, 
				action:"get_progress_and_size" 
			}, function(objData) {
				$(objForm).updateUploadStatus(objData.currentSize, objData.totalSize, objData.elapsedTime, objData.error);
			}
		);
	};
    function createUploadIframe(frameId) {            
		if (window.ActiveXObject) {
			var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />');
			io.src = 'javascript:false';
		} else {
			var io = document.createElement('iframe');
			io.id = frameId;
			io.name = frameId;
		}

		io.style.position = 'absolute';
		io.style.top = '-1000px';
		io.style.left = '-1000px';

		document.body.appendChild(io);

		return io
    };
    function createHiddenField(form, name, value) {            
		if (window.ActiveXObject) {
			var io = document.createElement('<input type="hidden" name="' + name + '" value="' + value + '" />');
		} else {
			var io = document.createElement('input');
			io.type = "hidden";
			io.name = name;
			io.value = value;
		}

		$(form).append(io);
    };
})(jQuery);
