/**
*
*  AJAX IFRAME METHOD (AIM)
*  http://www.webtoolkit.info/
*
**/


FileUpload = {


 	id : undefined,
 	helperIframe: undefined,
 	submitButton: undefined,
 	imageTarget: undefined,
 	imageTmpNameTarget: undefined,
 	imageNameTarget: undefined,
 	form: undefined,
 	action: undefined,
 	loadDiv:undefined,

	initialize: function() {


	},

	upload : function(control, imageTarget, imageNameTarget,imageTmpNameTarget,type) {

		var res = control.value.split(".");
		if (res[res.length-1].toLowerCase() != type && type) {
			alert("Bitte nur "+type+" Dateien uploaden");
			return;
		}
		

		this.imageTarget = imageTarget;
		this.imageTmpNameTarget = imageTmpNameTarget;
		this.imageNameTarget = imageNameTarget;





		if (!this.helperIframe) {

			this.id = 'f' + Math.floor(Math.random() * 99999);
			var div = document.createElement('DIV');
			div.innerHTML = '<iframe style="display:none" src="about:blank" id="'+this.id+'" name="'+this.id+'" onload="FileUpload.loaded(\''+this.id+'\')"></iframe>';
			document.body.appendChild(div);

			this.helperIframe = $(this.id);

			this.loadDiv = document.createElement("div");
			this.loadDiv.style.position = "absolute";
			this.loadDiv.className = "uploadLoading";
			document.body.appendChild(this.loadDiv);

		}

		this.form = control.form;
		this.form.observe('submit', FileUpload.submit);
		this.action = this.form.action;
		this.form.action = "/_frontend/components/core/InputForm/upload.php";
		this.form.target = this.id;


		for (i=0;i<this.form.elements.length;i++) {

			if (this.form.elements[i].type=="submit") {
				this.form.elements[i].disabled =true;

				break;
			}
			if (this.form.elements[i].name != control.name && this.form.elements[i].type== "file") this.form.elements[i].disabled =true;

		}


		this.imageTarget.src = "/_skins/base/images/upload.gif";
		var xy = Element.cumulativeOffset(this.imageTarget);
		var height = Element.getHeight(this.imageTarget);
		var width = Element.getWidth(this.imageTarget);


		this.loadDiv.style.height = height +"px";
		this.loadDiv.style.width = width +"px";
		var ie = 0;
		if (document.all) { ie = 1;}
		this.loadDiv.style.top = (xy[1]+ie)+"px";
		this.loadDiv.style.left = (xy[0]+ie)+"px";
		this.loadDiv.style.display = "block";


		this.form.submit();



	},

 	loaded : function() {

 		if (this.helperIframe) {

			if (this.helperIframe.contentDocument) {
				var result = this.helperIframe.contentDocument;

			} else if (this.helperIframe.contentWindow) {
				var result = this.helperIframe.contentWindow.document;

			} else {
				var result= window.frames[this.helperIframe.id].document;

			}
			var bodyResult = result.body.innerHTML.split("|");
			var imagePath = bodyResult[0];

			if (imagePath) {


				var date = new Date();
				this.imageTarget.src = imagePath+".tmp.jpg"+"?"+date.getTime();

				this.imageTmpNameTarget.value = imagePath+".jpg";
				this.loadDiv.style.display = "none";
				this.imageNameTarget.value = bodyResult[1];

				this.form.action = this.action;
				this.form.target= "";

				for (i=0;i<this.form.elements.length;i++) {
				 	this.form.elements[i].disabled =false;

				}




			}

		}
	},

	submit: function() {
		for (i=0;i<this.elements.length;i++) {

			if (this.elements[i].type=="file") {
				this.elements[i].disabled = true;
			}
		}

		return false;

 	}


}



