// upload_handler.js
// copyright 2008, NetIntro, All Rights Reserved
// For other great NetIntro software, see BronsonHQ.com

var somethingIsUploaded = 0;
var bronsonUploadedFiles = new Object();
var bronsonuploaders = {};

function upload_handler() {
	trace('upload_handler');
	
	this.upload_swf = '/swf/uploader.swf';
	this.uploaderwidth = 300;
	this.uploaderheight = 30;
	this.upload_target = '/templates/util/uploader.php';
	this.button = '/gfx/elements/upload.png';
	this.types = '*.jpg;*.gif;*.png;';
	this.filetype = 'image';
	this.fieldname = ''; 
	this.formname = '';
	this.frm = null;
	this.statusTarget = '';
	this.uploaderTarget = '';
	this.messageTarget = '';
	this.filename = '';
	this.uploadHandler = '';
	this.guid = ''; 
	this.previewwidth = 200;
	this.previewheight = null;
	this.udrid = null;
	this.success = 0;
	this.ishidden = false;
	this.currpct = null;
	this.lastpct = null;
	this.lasttime = null;
	this.completedmessage = {
		'image' : '<p><strong>Success!</strong> Your image has finished uploading.  To see it at full size, just click on the thumbnail.  To change the file, use the uploader below this message.  To save changes, click Save at the bottom of this form.</p>'
		, 'video' : '<p><strong>Success!</strong> Your video has finished uploading.  You will be able to view it once it is finished encoding.  You can come back to this page to check if your video is finished encoding.  To change the video, use the uploader below this message.  To save changes, click Save at the bottom of this form.</p>'
		, 'default' : '<p><strong>Success!</strong> Your file has finished uploading.  To download it, click on the link at left.  To change the file, use the uploader below this message.  To save changes, click Save at the bottom of this form.</p>'
	};
		
	this.drawUploader = function(width, height, formname, types) {
		
		trace('uploadHander::drawUploader');

		if (formname) {
			this.formname = formname;
		}
		
		if (width) {
			this.uploaderwidth = width;
		}
		
		if(height) {
			this.uploaderheight = height;
		}
		
		if(!this.formname.length > 0) {
			trace('uploadHander::drawUploader::formname empty');
			return false;
		}
		
		this.frm = document.getElementById(formname);
		if(!this.frm) {
			trace('uploadHander::drawUploader::form "' + this.formname + '"doesn\'t exist');
			return false;
		}
		
		this.trgt = document.getElementById(this.uploaderTarget);
		if(!this.trgt) {
			trace('uploadHander::drawUploader::div "' + this.trgt + '"doesn\'t exist');
			return false;
		}
		
		trace("putting uploader in " + this.uploaderTarget);
		
		this.attributes = {
			"style" : "height:" + this.uploaderheight + "px" 
		};
		
		this.flashvars = {};
		this.params = {};
		this.params.menu = 'false';
		this.params.wmode = 'transparent';
		this.params.allowScriptAccess = 'allways';
		this.params.allowFullScreen = 'false';
		this.params.quality = 'high';
		this.params.scale = 'noscale';
		this.params.bgcolor = 'none';
		
		this.flashvars.types = this.types;
		this.flashvars.traceging = '';
		this.flashvars.filename = this.filename;
		this.flashvars.uploadHandler = this.upload_target;
		this.flashvars.button = this.button;
		this.flashvars.formname = this.formname;
		this.flashvars.fieldname = this.uploaderTarget;

		this.udrid = 'bronson_uploader_' + this.formname + '_' + this.fieldname + '_' + this.guid;
		swfobject.embedSWF(this.upload_swf, this.uploaderTarget, this.uploaderwidth, this.uploaderheight, "9.0.28", false, this.flashvars, this.params, this.attributes);
	
		bronsonuploaders[this.uploaderTarget] = this;
		trace("uploader in " + this.uploaderTarget + "::" + bronsonuploaders[this.uploaderTarget].uploaderTarget);
		
		return;
	};
	
	this.showMessage = function(arg) {
		trace('uploadHander::showMessage::' + arg);
		if (this.messageTarget) {
			var t = document.getElementById(this.messageTarget);
			t.innerHTML = arg;
		} else {
			trace(arg);
		}
	}
	
	this.commenceUpload = function() {
		trace('uploadHander::commenceUpload');
		return;
	}
	
	this.handleProgress = function(arg) {
		
		elem = document.getElementById(this.uploaderTarget);
		
		if(this.lasttime == null) {
			var d = new Date();
	        var s = d.getTime();
			this.lasttime = s;
			tme = "";
		} else if(arg % 5 == 0) {
			this.currpct = arg;
			if(this.lastpct != this.currpct) {
				trace('uploadHandler::handleProgress::NEW PCT');
				var d = new Date();
		        var s = d.getTime();
		        var offset = s - this.lasttime;
		
				var rmn = 100 - arg;
				var num = (offset/1000)*20*rmn;
				//var num = num/1000;
				var num = num/60;
				
				if(num > 3600) {
					num = num/3600;
					tme = Math.round(num) + " hours" + (Math.round(num) > 1 ? "s" : "");
				} else if(num > 60) {
					num = num/60;
					tme = Math.round(num) + " minute" + (Math.round(num) > 1 ? "s" : "");
				} else {
					tme = Math.round(num) + " second" + (Math.round(num) > 1 ? "s" : "");
				}
		
		        this.lasttime = s;
				this.lastpct = this.currpct;
			}
		}
		//this.disableForm();
				
		var stat_div = this.formname + '_' + this.fieldname + '_status_div';
		//u4aa41fcca6764_mediafile_status_div
	
		var pg = document.getElementById(stat_div);
		if (pg) { 
			pg.innerHTML = "Sent: " + arg + "%<br/>Estimated time remaining:" + tme;
		} else {
			trace("uploadHandler::no status div::" + stat_div);
		}
		
		elem = document.getElementById(this.uploaderTarget);
		
		if(this.ishidden == false) {
			trace("uploadHandler::handleProgress::style::" + elem.style.display);
			elem.style.visibility = 'hidden';
			elem.style.height = '0px';
			this.ishidden = true;
			if(pg) {
				pg.style.height = this.uploaderheight + "px";
				pg.style.width = this.uploaderwidth + "px";
				//pg.style.backgroundColor = "#ccc";
				pg.style.display = "block";
			}
			this.disableForm();
		}
		
		return;
	}
	
	this.disableForm = function() {
		trace("uploadHandler::disableForm::" + this.frm.onSubmit);
		//this.savedSubmit = this.frm.onSubmit;
		this.frm.onSubmit = function(e) {
			alert("UPLOAD IN PROGRESS");
			return false;
		}
		
		var sb = document.getElementById(this.formname + "_bronson_form_submit_button");
		if (sb) {
			sb.style.display = 'none';
		}
		
		trace("uploadHandler::disableForm::" + this.frm.onSubmit);
		return true;
	}
	
	this.completedUpload = function(filename) {
		trace("uploadHandler:completedUpload::" + filename);
		
		var stat_div = this.formname + '_' + this.fieldname + '_status_div';
		
		elem = document.getElementById(this.uploaderTarget);
		elem.style.visibility = 'visible';
		elem.style.height = this.uploaderheight + 'px';
		
		var dname = this.formname + '_' + this.fieldname + '_thumbnails';
		trace('uploadHandler:completedUpload::filling: ' + dname);
		trace(dname);
		if (this.frm) {
			var elem = document.getElementById(this.formname + '_file_' + this.fieldname);
			if (elem) { 
				elem.value = '/uploads/__tmp/' + filename;
				trace('set ' + this.formname + ' ' + this.fieldname + ' to ' + elem.value );	
			} else {
				trace("uploadHandler:completedUpload::could not find " + this.formname + "_file_" + this.fieldname);
			}
		
			var sb = document.getElementById(this.formname + "_bronson_form_submit_button");
			if (sb) {
					sb.style.display = 'block';
			}
			var pg = document.getElementById(stat_div);
			if (pg) { 
				
				compmsg = this.completedmessage['default'];
				if(this.completedmessage[this.filetype]) {
					compmsg  = this.completedmessage[this.filetype];
				}
				
				//pg.innerHTML = "<b>Upload Completed</b>";
				pg.style.backgroundColor = "none";
				pg.style.height = "auto";
				pg.innerHTML = compmsg;
			}
			if(this.frm.title.value == '') {
				fname = this.filename.split('.');
				this.frm.title.value = fname[0];
			}
		
			var tdiv = document.getElementById(dname);
			if (tdiv) {
				trace("uploadHandler:completedUpload::have " + dname);
				// tdiv.innerHTML = tdiv.innerHTML + "<br/><img src='/uploads/__tmp/" + filename + "' width='200' />";
				// now we replace the whole thing just in case there's something already there?
				d = new Date();
				s = d.getTime();
				if(this.filetype == 'image') {
					tdiv.innerHTML = "<a href='/uploads/__tmp/" + filename + "?rand=" + s + "' target='new'><img src='/uploads/__tmp/" + filename + "?rand=" + s + "' width='"  + this.previewwidth  + "' />";
				} else {
					tdiv.style.float = "none";
					tdiv.innerHTML = "<a style='display:block;padding:5px;border:solid 1px;' href='/uploads/__tmp/" + filename + "?rand=" + s + "' target='new'>" + filename + "</a>";
				}
			} else {
				trace("uploadHandler:completedUpload::no " + dname);
			}
		} else {
			trace("uploadHandler:completedUpload::no form to which to upload " + this.formname);
		}
		
		this.lasttime = null;
		this.currpct = null;
		this.ishidden = false;
		
		bronsonUploadedFiles[this.fieldname] = true;
		trace("uploadHandler:completedUpload::finished completedUpload");
	}
}
//End Obj

// document level event handler 
	function handleProgress (arg, fieldname) {
		if(arg % 10 == 0) {
			//trace("JS::handleProgress::" + arg + "::" + fieldname);
		}
		if(!bronsonuploaders[fieldname]) {
			trace('handleProgress::uploader not found in bronsonuploaders array "' +  fieldname + '"');
			return false;
		}
		//trace("JS::handleProgress::" + arg + "::" + bronsonuploaders[fieldname]);
		return bronsonuploaders[fieldname].handleProgress(arg);
	}

	function handleCancel(arg) {
		trace("uploadHandler:handleCancel::" + arg);
	}
	
	function handleError(arg) {
		trace("uploadHandler:handleError" + arg);
	}

	function handleComplete(arg, fieldname) {
		trace("uploadHandler:handleComplete::" + arg + "::" + fieldname);
		var stat_div = 'status_div_' + fieldname;
		var pg = document.getElementById("stat_div");
		if (pg) { 
			pg.innerHTML = "<b><i>Finished Upload</i> Make Sure to Save Your Changes!</b>";
		}
		this.success = 1;
		somethingIsUploaded = 1;
	}
	
	function handleSelect(arg) {
		trace("uploadHandler:handleSelect::" + arg);
	}
	
	function handleOpen(arg) {
	 	trace("uploadHandler:handleOpen::" + arg);
	}
	
	function handleSecurityError(arg) {
		 trace("uploadHandler:handleSecurityError::" + arg);
	}
	
	function completedUpload(fieldname, filename, formname) {
		trace("uploadHandler:completedUpload::" + fieldname + "::" + filename + "::" + formname);
		if(!bronsonuploaders[fieldname]) {
			trace('handleProgress::uploader not found in bronsonuploaders array "' +  fieldname + '"');
			return false;
		}
		//trace("JS::handleProgress::" + arg + "::" + bronsonuploaders[fieldname]);
		return bronsonuploaders[fieldname].completedUpload(filename);
	}

