/**
* A simple flash form uploader
* It doesnt fallback to anything if javascript is disabled, mainly because i use it under controled circumstances
**/
FlashFormUpload = function (aFormId, aFieldName, aTagContent){
	
	/**
	* PUBLIC VARIABLES
	* Can be read/write from outside
	**/
	//swf related vars
	this.uid = Math.round(Math.random() * Math.random() * 999999);
	this.swfUrl = "";
	//ui text vars
	this.uploadingStr = "Téléversement";
	this.uploadedStr = "Fichier envoyé avec succès";
	this.chooseBtnStr = "Choisir un fichier";
	this.uploadBtnStr = "Téléverser";
	
	//ui color vars
	this.bgColor = 0xff0000;
	this.loadBarColor = 0x00ff00;
	this.textColor= 0xffffff;
	
	//error message
	this.fileSizeErrorStr = "Fichier trop lourd";
	
	//upload file
	this.useFullFilePath = false;
	this.uploadFileUrl = "http://multimedia.cegep-matane.qc.ca/ffuploader/ffu.upload.php";
	this.maxFileSize = 100; //mo (max is 100 thanks to adobe!:P)
	this.maxChar = 25;
	this.targetDir = "";
	this.allowedTypes = "*.jpg;*.png;*.gif;";
	this.allowedTypesMessage = "Fichiers supportés";
	this.initFileName = "";
	
	//callback functions
	this.errorCallback = null;
	this.uploadedCallback = null;
	this.clearedCallback = null;
	this.initCallback = null;
	/**
	* PRIVATE VARIABLES & FUNCTIONS
	* Cannot be read/write from outside (directly)
	* Need a getter to be read, a setter to be set
	**/
	var _formId = aFormId;
	var _tagContent = aTagContent;
	var _form = document.getElementById(_formId);
	
	this.fieldName = aFieldName;
	/**
	* GETTERS FOR FUNCTIONS AND VARIABLES
	**/
	FlashFormUpload.prototype.getForm = function(){
		return _form;
	}
	
	FlashFormUpload.prototype.getTagContent = function(){
		return _tagContent;
	}
	
	FlashFormUpload.prototype.getFieldName = function(){
		return _fieldName;
	}
	
}
/**
* Initial setup of the flash uploader
* This call is needed for the flash uploader to appear in the form and work
**/
FlashFormUpload.prototype.setup = function(aObj){
	
	/**
	* Instance reference
	* Needed to call good instance from FLash
	**/
	if(aObj == undefined){
		FlashFormUpload.debug("You must pass the name of this instance to the setup function...\nSee admin for details");
		return;
	}else var _obj = aObj;

	/**
	* FLASH SETUP
	* Hidden form field is created here
	* Ref to the instance is passed here as flashvars and other usefull params
	**/
	var flashvars = {
		ffuObj:_obj, 
		uploadingStr:this.uploadingStr,
		uploadedStr:this.uploadedStr,
		chooseBtnStr:this.chooseBtnStr,
		uploadBtnStr:this.uploadBtnStr,
		bgColor:this.bgColor,
		loadBarColor:this.loadBarColor,
		textColor:this.textColor,
		fileSizeErrorStr:this.fileSizeErrorStr,
		uploadFileUrl:this.uploadFileUrl, 
		maxFileSize:this.maxFileSize,
		targetDir:this.targetDir,
		allowedTypes:this.allowedTypes,
		allowedTypesMessage:this.allowedTypesMessage,
		uid:this.uid
	};
	var params = {
	  allowScriptAccess: "always", //needed with ExternalInterface (as3)
	  wmode:"transparent"
	};
	var attributes = {
		id : "tag" + this.uid, 
		name : "tag" + this.uid
	};
	//flash container...
	var contentTag = (this.getTagContent() != null) ? this.getTagContent() : "tag" + _obj;
	//is added
	if(this.getTagContent() == null)this.getForm().innerHTML += "<div id='" + contentTag + "'></div>";
	//Create the swf file
	swfobject.embedSWF(this.swfUrl + "FlashFormUpload.swf?"+this.uid, contentTag, "400", "40", "10.0.0",this.swfUrl + "expressInstall.swf", flashvars, params, attributes);
	//hidden field created here
	this.getForm().innerHTML += "<input name='" + this.fieldName + "' id='" + this.fieldName + "' type='text'/>";
	//hidden field reference for later use
	/** BUG #1 
	* This reference is lost but the name is still good??
	**/
	//this.hiddenField = document.getElementById(this.fieldName);
}

/**
* Once the file to upload is selected and valid
* TODO : Encapsulate
**/
FlashFormUpload.prototype.fileSelected = function(aFileName, aFileSize){
	//todo...
	document.getElementById(this.fieldName).value = "";
	//callback if any...
	if(this.clearedCallback)this.clearedCallback(this.fieldName);
}

/**
* Once the file is cleaned before uploading a new one
* TODO : Encapsulate
**/
FlashFormUpload.prototype.fileCleared = function(){
	document.getElementById(this.fieldName).value = "";
	//callback if any...
	if(this.clearedCallback)this.clearedCallback(this.fieldName);
}

/**
* Once the file upload is completed
* We want to populate the hidden field with the name of the uploaded file
* TODO : Encapsulate
**/

FlashFormUpload.prototype.fileUploaded = function(aFileName, aFullPath){
	if(this.useFullFilePath)	{
		document.getElementById(this.fieldName).value = aFullPath;
	}	else	{
		document.getElementById(this.fieldName).value = aFileName;
	}
	//callback if any...
	if(this.uploadedCallback)this.uploadedCallback(this.fieldName);
}

/**
* If the form is modified, we want to push the previously saved file name into flash form uploader
**/

FlashFormUpload.prototype.initFile = function(aFileName){
	this.initFileName = aFileName;
}

/**
* Called by flash when the swf is loaded. Then as3 and javascript can interact
**/

FlashFormUpload.prototype.swfLoaded = function(){
	if(this.initFileName != ""){
		document.getElementById("tag" + this.uid).initFile(this.initFileName);
		document.getElementById(this.fieldName).value = this.initFileName;	
		if(this.initCallback)this.initCallback(this.initFileName);
	}
}


/**
* In case upload receive error
* Alert it to client
**/
FlashFormUpload.debug = function(aDebug){
	alert(aDebug);
}
