/**
 * iframeFileUplod for mailformplus
 */
var IframeFileUpload = new Class({

	options: {
	},

	initialize: function(form, options){
		this.setOptions(options);

		if (!window.ie6) {
			SI.Files.stylizeAll();
		}

		this.addEvents(form);
		this.theClickManager = new ClickManager();
	},

	addEvents: function(pform){
		pform.getElements('input[type=file]').each(function(element){
			element.addEvent(	'change',
				function(){
					this.uploadFile(element,pform);
				}.bind(this)
			);
		}.bind(this));
	},

	uploadFile: function(element,pform){

		this.theClickManager.activate();
		spinner = new Element('img');
		spinner.setProperties({
			src: 'fileadmin/templates/alexgastro/images/mailform/ajax_upload_orange.gif',
			width: 16,
			height: 16,
			title: "Lade hoch..."
		});

		if (element.getParent().getNext()) {
			element.getParent().getNext().remove();
		}

		var loading = new Element('p');
		spinner.injectInside(loading);
		loading.injectAfter(element.getParent());

		var n = 'f' + Math.floor(Math.random() * 99999);
		var iframe = new Element('iframe', {
	    	'styles': {
		        'display': 'none'
		    },
		    'src': 'about:blank',
		    'id': n,
		    'name': n
		});

		var box = new Element('div', {
	    	'styles': {
		        'display': 'none'
		    },
		    'id': 'box-'+n
		});
		var result = new Element('div', {
	    	'styles': {
		        'display': 'none'
		    }
		});


		box.adopt(iframe);
		box.adopt(result);
		$$('body').adopt(box);

		var oldTarget = pform.getProperty('target');
		var oldAction = pform.getProperty('action');

		pform.setProperty('target',n);
		pform.setProperty('action','fileupload/');

		pform.getElements('input[type=file]').each(function(el){
			el.setProperty('disabled', 'disabled');
		}.bind(this));
		element.removeProperty('disabled');
				
		//clickMan.waitCursor(true);
		iframe.addEvent(
			'load',
			function(){
				var iFrameContent = "";
		
		        if (iframe.contentDocument) {
		            iFrameContent = iframe.contentDocument;
		        } else if (iframe.contentWindow) {
		            iFrameContent = iframe.contentWindow.document;
		        } else {
		            iFrameContent = window.frames[id].document;
		        }
				result.setHTML(iFrameContent.body.innerHTML);
				var errors = result.getElements('#error');
				var errorFlag = 0;
				$$('#error').each(function(element){
					element.remove();
					errorFlag = 1;
				});

				if (errorFlag) {
					loading.setHTML('Hochladen fehlgeschlagen!');
					loading.adopt(errors);
					element.value = '';
				} else {
					loading.setHTML('Hochladen erfolgreich!');
					// element.setProperty('disabled', 'disabled');
					element.getParent().addClass('uploaded');
				}
				var fileinfoel = element.getParent().getElement('.fileinfo');
				if(fileinfoel){
					fileinfoel.setText('');
				}
				
				
				pform.getElements('input[type=file]').each(function(el){
					el.removeProperty('disabled');
				}.bind(this));
				

				this.theClickManager.deactivate();
				result.remove();

				pform.setProperty('target',oldTarget);
				pform.setProperty('action',oldAction);
				// box.remove();
			}.bind(this)
		);
		if(pform.submit());
	}

});

IframeFileUpload.implement(new Events());
IframeFileUpload.implement(new Options());
IframeFileUpload.implement(new Chain());