var updater = null;		
var upload_identifier = null;
var Iframe = null;
var uploaderForm = null;
var callbackFunction = null;
var iframeContainerId = null;
var progressBarContainerId = null;
var progressBarContentUrl = null;
var iframeUploaderId = 'iframe_uploader';
var iframeCallback = '';//'index.php';
var request_num = 0;

function set_upload_identifier(ident){
	upload_identifier = ident;
}

function set_callback(clback){
	callbackFunction = clback;
}

function set_iframe_container(id){
	iframeContainerId = id;
}

function set_progress_bar_container(id){
	progressBarContainerId = id;
}

function set_progress_bar_url(url){
	progressBarContentUrl = url;
}
		
function createIframe(){
	var iframe_container = window.document.getElementById(iframeContainerId);	
	var callback;
	if(iframeCallback)
		callback = 'window.location=\'' + iframeCallback + '\'';
	else
		callback = '';
	
	if(Iframe==null && iframe_container){
//    Iframe = window.document.createElement('iframe');
//    Iframe.id = iframeUploaderId;
//    Iframe.name = iframeUploaderId;    
//    Iframe.onload = 'stopUploading();';          
//    Iframe.src = '';
//    Iframe.style.height = '100px';
//    Iframe.style.width = '100px';
//    Iframe.style.visibility = 'visible';    
//    iframe_container.appendChild(Iframe);    
		iframe_container.innerHTML = '<iframe id="'+iframeUploaderId+'" name="'+iframeUploaderId+'" onload="stopUploading();'+ callback +'" style="height:300px; width:500px; visibility:hidden;"></iframe>';
		Iframe = window.document.getElementById(iframeUploaderId);	
  }
}

function destroyIframe(){		
	var iframe_container = window.document.getElementById(iframeContainerId);	
	
	if(iframe_container)
		iframe_container.innerHTML = "";
	
	if(Iframe!=null){
		//iframe_container.removeChild(Iframe);
		Iframe=null;
	}
	
}


function stopUploading(){			
	//if it is already created we need to get the latest recorded upload status
	if( updater!=null ){
		
		updater.stop();
		updater=null;
		
		//getting the last information
		//new Ajax.Updater(progressBarContainerId, 'upload_progress_monitor.php?UPLOAD_IDENTIFIER='+upload_identifier, {
		new Ajax.Updater(progressBarContainerId, progressBarContentUrl, {		
		  method: 'get'  
		});
	}
	
	if(uploaderForm!=null)
		uploaderForm.attributes['target'].value="_self";

/*	
	var iframe_container = document.getElementById(iframeContainerId);
	var iframe = document.getElementById(iframeUploaderId);
	document.body.innerHTML = iframe.contentWindow.document.body.innerHTML;
*/
	destroyIframe();
		
}

function startUploading(uploader_form_id){
	uploaderForm = window.document.getElementById(uploader_form_id);	
	
	if(!uploaderForm){
		alert('Invalid form ID!');
		return false;
	}
			
	createIframe();
//	alert('IFrame created');
//	alert('IFrame uploader ID ' + iframeUploaderId);
	//alert(iframeUploaderId);
	uploaderForm.attributes['target'].value=iframeUploaderId;
	uploaderForm.submit();
//	alert('form sent created');
	//return;
	
	startProgressBar();		
	
	//alert('progres URL' + progressBarContentUrl);
	//alert('container ID = ' + progressBarContainerId);
	//alert('progress monitoring started');
}

function startProgressBarOld() {
				
	if(updater==null){
		//updater = new Ajax.PeriodicalUpdater(progressBarContainerId, 'upload_progress_monitor.php?UPLOAD_IDENTIFIER='+upload_identifier, {
		updater = new Ajax.PeriodicalUpdater(progressBarContainerId, progressBarContentUrl, {		
		  method: 'get', 
		  frequency: 2,
		  decay: 2
		});
	}
	else
		updater.start();//re-enabling updater if it is created
}

function startProgressBar() {
				
	//updater = new Ajax.Updater(progressBarContainerId, 'upload_progress_monitor.php?UPLOAD_IDENTIFIER='+upload_identifier, {
	request_num++;
	new Ajax.Request(progressBarContentUrl+'&'+request_num, {		
	  method: 'get', 
	  onSuccess: function(transport) {
//	  	alert(transport.responseText);
	  	if (transport && transport.responseText != '') {
	  		var el = document.getElementById(progressBarContainerId);
	  		el.innerHTML = transport.responseText;
	  		setTimeout("startProgressBar()",2000);
	  	}
	  }
	});
}

function onCompleteProgressBar(transport,params) {
	if (!transport || transport.responseText == '')
		updater.stop();
}

