/*
         ___                         _____       __
        /  /\        ________       /    /\     / /\
(-----//  /  \//----/_____/__\\--) /____/  \___/_/ //--------------------------)
      /  /   /      \     | //     \    \  /    /\/    this code was created by
     /__/   /        \    |///-----\\____\/____/  \                       sigea
(---\\  \  /   (-----\\___|/        /    /\    \  /       
      \__\/                        /____/ /\____\/            __       sigea.ch
                                   \____\/  \  /             /\_\--------------)
                                         \___\//-------------\/_/  

*/

function inplaceFileUpload(){
	var iframeID='iframe'+Math.floor(Math.random()*9999);
	var callback=null;
	var form,loading,loadingElement;
	this.submit=function(){
		if(loading){
			loadingElement=loading();
		}
		creatIframe();
		form.setAttribute('target', iframeID);
		return true;
	}
	this.setCallback=function(value){
		callback=value;
	}
	this.setLoading = function(value){
		loading=value;
	};
	this.setForm=function(value){
		form=value;
		form.setAttribute('target', iframeID);
		form.onsubmit = function(){
			if(loading){
				loadingElement=loading();
			}
			creatIframe();
			form.setAttribute('target', iframeID);
			return true;
		}
		return true;
	}
	var creatIframe=function(){
		var iframe=null;
		try{
			//scheiss ie
			iframe=document.createElement('<iframe name="'+iframeID+'">');
		}catch(e){
		}
		if(!iframe){
			iframe=document.createElement('iframe');
			iframe.setAttribute('name',iframeID);
		}
		iframe.style.display='none';
		iframe.setAttribute('src','about:blank');
		iframe.setAttribute('id',iframeID);
		iframe.onload = function(){
			loaded(this);
		}
		iframe.onreadystatechange=function(){
			if(iframe.readyState=='complete')
				loaded(this);
		}
		document.getElementsByTagName('body')[0].appendChild(iframe);
	}
	var loaded=function(iframe){
		iframeID=iframe.getAttribute('id');
		if(window.frames[iframeID].location.href != 'about:blank'){
			if(loadingElement){
				var parent = loadingElement.parentNode;
				parent.removeChild(loadingElement);
			}
			var request=new Object();
			request.responseText='';
			if(window.frames[iframeID].document.getElementsByTagName('body')[0].innerHTML)
				request.responseText=window.frames[iframeID].document.getElementsByTagName('body')[0].innerHTML;
			request.responseXML=window.frames[iframeID].document.getElementsByTagName('body')[0].cloneNode(true);
			
			callback(request);
			//window.setTimeout("removeElementById('"+iframeID+"')", 1000);
			window.setTimeout(function(){
				parent=document.getElementById(iframeID).parentNode;
				parent.removeChild(document.getElementById(iframeID));
				return true;
			}, 1000);
		}
	}
}
