var upload=(function(){
	var idupload=new Object;
	var tmprogress=null;
	var obelupload=[];
	var objupload=new Object;
	var obtimer=new Object;
	
	var htmstatus=new Object;
	var obproses=new Object;
	var obdataupload=new Object;
	var modelihat=0;
	return{
		win:function(a){
			var boxps=new Object;
			var ps={"box":"box_uplaod","title":a,"close":"tidak"};
			boxps.box=boxpesan.win(ps);
			boxps.bttutup=document.getElementById("bt_plg_upload_batal");
			boxps.btupload=document.getElementById("bt_plg_upload");
			boxps.tutup=function(){
				boxps.box.tutup();
			};
			return boxps;
		},
		genid:function(a){
			var text = "";
		    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
		    for( var i=0; i < a; i++ )
		        text += possible.charAt(Math.floor(Math.random() * possible.length));
		    return text;
		},
		inline:function(a){
			var idhasil=a['hasil'] || null;
			var tautan=lokal+a['tautan'];
			var status=a['status'];
			var data=a['data'];
			var tutup=a['tutup'] || "ya";
			var finish=a['finish'] || null;
			
			if(idhasil==null) return;
			
			var gentb=upload.genid(6);
			var div=document.createElement("div");
			div.style.display="none";
			
			var htmupload="";
			htmupload+="<iframe name=\"if"+gentb+"\" id=\"if"+gentb+"\" style=\"display:none;\"></iframe> ";
			htmupload+="<form name=\"form"+gentb+"\" method=\"post\" enctype=\"multipart/form-data\" action=\""+tautan+"\" target=\"if"+gentb+"\" id=\"form"+gentb+"\">";
			
			htmupload+="<div id=\"box_"+gentb+"\">";
			htmupload+="<div>";
			htmupload+="<input type=\"hidden\" name=\"APC_UPLOAD_PROGRESS\" id=\"progress_"+gentb+"\" value=\""+gentb+"\"/>";
			htmupload+="<span><input type=\"file\" name=\"file_"+gentb+"\" id=\"file_"+gentb+"\" /></span>";
			htmupload+="<input type='hidden' name='upload_data' id='upload_data' value='"+JSON.stringify(data)+"' />";
			htmupload+="</div>";
			htmupload+="</div>";
			htmupload+="</form>";
			htmupload+="</div>";
			div.innerHTML=htmupload;
			document.body.appendChild(div);
			
			var statushtm="<div class=\"progress_bar\">";
			statushtm+="<span id=\"status_bar_"+gentb+"\" class=\"status_bar\"></span>";
			statushtm+="</div>";
			
			var obj=new Object;
			obj.id=gentb;
			obj.progres=function(){
				upload.progres(gentb,status);
			};
			obj.file=document.getElementById("file_"+gentb);
			obj.proses=function(){
				var namafile=document.getElementById("file_"+gentb).files[0].name;
				var boxhasil=document.getElementById(idhasil);
				var htmhasil="<div class='box_selesai'>"+namafile+" <span id='persen_"+gentb+"'></span> <span class='tutup_file' id='upload_delete_"+gentb+"'></span></div>";
				if(tutup=="tidak"){
					htmhasil="<div class='box_selesai'>"+namafile+" <span id='persen_"+gentb+"'></span></div>";
				}
				htmhasil+=statushtm;
				boxhasil.innerHTML=htmhasil;
								
				$("#form"+gentb).submit(function(e)
				{
					var formObj = $(this);
					var formURL = formObj.attr("action");
					var formData = new FormData(this);
					$.ajax({
				     	url: formURL,
						type: 'POST',
				        data:  formData,
						mimeType:"multipart/form-data",
						contentType: false,
				        cache: false,
				     	processData:false,
						success: function(data, textStatus, jqXHR)
						{
							var datajs=JSON.parse(data);
							if(datajs['status']=="SUKSES"){
								
								idupload[gentb]=datajs['data'];
								idupload["nama_"+gentb]=namafile;
								if(finish!=null){
									finish.proses(datajs['data']);
								}
							}else{
								upload.stopprogres();
								if(finish['error']!=null){
									finish.error();
								}
								var ps={"pesan":datajs['pesan'],"title":"Kesalahan upload."};
								boxpesan.peringatan(ps);
								boxhasil.innerHTML="<div class='box_selesai'>ERROR UPLOAD</div>";
							}
							
						},
						error: function(jqXHR, textStatus, errorThrown) 
						{
							upload.stopprogres();
							var ps={"pesan":"Upload dokumen terjadi kesalahan.","title":"Kesalahan upload."};
							boxpesan.peringatan(ps);
							boxhasil.innerHTML="<div class='box_selesai'>ERROR UPLOAD</div>";
							if(finish['error']!=null){
								finish.error();
							}
						} 	        
						});
					    e.preventDefault();  
					    //e.unbind();
					}); 
				$("#form"+gentb).submit();
			};
			
			return obj;
		},
		tambahupload:function(a,b){
			var tutup=b['tutup'] || "tidak";
			var tautan=b['tautan'];
			var tautanstatus=b['status'];
			var data=b['data'];
			var finish=b['finish'] || null;
			var namafile=b['namafile'] || "PILIH FILE UNTUK UPLOAD KONTEN";
			var link=b['link'] || null;
			var prev=b['preview'] || "tidak";
			var uploadstatus=b['disupload'] || "tidak";
			
			disupload="";
			if(uploadstatus=="ya") disupload="disabled=\"true\"";
			
			
			var gentb=upload.genid(6);
			obelupload.push(gentb);
			obdataupload[gentb]=finish;
			
			var statushtm="<div class=\"progress_bar\" style=\"margin-bottom:7px\" id=\"box_status_bar_"+gentb+"\">";
			statushtm+="<span id=\"status_bar_"+gentb+"\" class=\"status_bar\" ></span>";
			statushtm+="</div>";
			
			var htm=$("#"+a).html();
			var hhtmdel="";
			var hhtmprev="";
			htm+="<div class=\"box_upload\" id=\"box_upload_"+gentb+"\" onclick=\"upload.pilihfile('"+gentb+"')\">";
			if(tutup=="ya") hhtmdel="<span class='tutup_file' onclick='upload.hapus(\""+gentb+"\")'></span>";
			if(prev=="ya") hhtmprev="<span class='preview_file' onclick='upload.lihat(\""+gentb+"\")'></span>";
			htm+="<span class=\"box_selesai\" id=\"box_selesai_"+gentb+"\"><label id=\"lbl_"+gentb+"\">"+namafile+" </label>"+hhtmprev+hhtmdel+"</span>";
			htm+="</div>";
			$("#"+a).html(htm+statushtm);
			
			objupload[gentb]=b;
			
			var div=document.createElement("div");
			div.style.display="none";
			div.id="box_if_"+gentb;
			var htmupload="";
			htmupload+="<iframe name=\"if"+gentb+"\" id=\"if"+gentb+"\" style=\"display:none;\"></iframe> ";
			htmupload+="<form name=\"form"+gentb+"\" method=\"post\" enctype=\"multipart/form-data\" action=\""+tautan+"\" target=\"if"+gentb+"\" id=\"form"+gentb+"\">";
			
			htmupload+="<div id=\"box_"+gentb+"\">";
			htmupload+="<div>";
			htmupload+="<input type=\"hidden\" name=\"APC_UPLOAD_PROGRESS\" id=\"progress_"+gentb+"\" value=\""+gentb+"\"/>";
			htmupload+="<span><input "+disupload+" type=\"file\" name=\"file_"+gentb+"\" id=\"file_"+gentb+"\" /></span>";
			htmupload+="<input type='hidden' name='upload_data_"+gentb+"' id='upload_data_"+gentb+"' value='"+JSON.stringify(data)+"' />";
			htmupload+="</div>";
			htmupload+="</div>";
			htmupload+="</form>";
			htmupload+="</div>";
			div.innerHTML=htmupload;
			document.body.appendChild(div);
			
			
			$("#form"+gentb).submit(function(e)
			{
				var boxhasil=document.getElementById("lbl_"+gentb);
				var formObj = $(this);
				var formURL = formObj.attr("action");
				var formData = new FormData(this);
				$.ajax({
			     	url: formURL,
					type: 'POST',
			        data:  formData,
					mimeType:"multipart/form-data",
					contentType: false,
			        cache: false,
			     	processData:false,
					success: function(data, textStatus, jqXHR)
					{
						upload.stopmultiprogres(gentb);
						
						var datajs=JSON.parse(data);
						if(datajs['status']=="SUKSES"){
							
							$("#status_bar_"+gentb).attr("style","width:100%;");
							$("#box_selesai_"+gentb).attr("style","color:green;");		
							document.getElementById("persen_"+gentb).innerHTML="(ready)";
							var idx=obelupload.indexOf(gentb);
							
							datajs['data']['idx']=idx;
							if(finish!=null){
								finish.proses(datajs['data']);
							}
						}else{
							
							var ps={"pesan":datajs['pesan'],"title":"Kesalahan upload."};
							boxpesan.peringatan(ps);
							boxhasil.innerHTML="ERROR UPLOAD";
							//if(finish.error!=null) finish.error();
						}
						
					},
					error: function(jqXHR, textStatus, errorThrown) 
					{
						upload.stopmultiprogres(gentb);
						
						var ps={"pesan":"Upload dokumen terjadi kesalahan.","title":"Kesalahan upload."};
						boxpesan.peringatan(ps);
						boxhasil.innerHTML="ERROR UPLOAD";
						if(finish.error!=null) finish.error();
						
					} 	        
				});
			    e.preventDefault();  
			    //e.unbind();
			});
			
			htmstatus[gentb]=tautanstatus;
			var obtambah=new Object;
			obtambah.namafile=document.getElementById("lbl_"+gentb);
			return obtambah;
		},
		pilihfile:function(a){
			if(objupload[a]==null || modelihat==1) {
				modelihat=0;
				return;
			}
			
			var file=document.getElementById("file_"+a);
			
			file.onchange=function(){
				$("#box_selesai_"+a).attr("style","color:black;");	
				var idx=obelupload.indexOf(a);
				if(obdataupload[a]!=null){
					if(obdataupload[a].pilihfile !=null) obdataupload[a].pilihfile(idx);
				}
				
				var namafile=document.getElementById("file_"+a).files[0].name;
				var boxhasil=document.getElementById("lbl_"+a);
				var htmhasil=namafile+"&nbsp;&nbsp;<span id='persen_"+a+"'></span>";
				
				boxhasil.innerHTML=htmhasil;
				
				$("#form"+a).submit();
				
				if(obproses[a]!=null) clearInterval(obproses[a]);
				var boxpersen=document.getElementById("persen_"+a);
				boxpersen.innerHTML="(0%)";
				$("#status_bar_"+a).attr("style","width:0%;");
				var tm=setInterval(function(){
					var datapost = {"progress_key": a};
					
					$.ajax({
						url: htmstatus[a],
						type: "POST",
						dataType: "json",
						async:true,
						data: datapost,
						success: function(e){
							console.log(JSON.stringify(e));
							if(e!=false){
								if(e['status']=="SUKSES"){
									$("#status_bar_"+a).attr("style","width:"+e['pesan']+"%;");
									
									boxpersen.innerHTML="("+parseInt(e['pesan'])+"%)";
									if(e['pesan']==100){
										clearInterval(tm);
										
									}
								}else{
									clearInterval(tm);
								}
							}else{
								clearInterval(tm);
							}
						}
					});
				},500);
				
				obproses[a]=tm;
			};
		
			file.click();
			
		},
		hapus:function(a){
			
			var idx=obelupload.indexOf(a);
			document.getElementById("box_upload_"+a).outerHTML="";
			document.getElementById("box_if_"+a).outerHTML="";
			document.getElementById("box_status_bar_"+a).outerHTML="";
			obelupload.splice(idx, 1);
			
			
			delete objupload[a];
			delete htmstatus[a];
			if(obdataupload[a]!=null){
				if(obdataupload[a].hapus!=null) obdataupload[a].hapus(idx);
				delete obdataupload[a];
			}
			
		},
		lihat:function(a){
			var dataobj=objupload[a];
			var idx=obelupload.indexOf(a);
			modelihat=1;
			dataobj.finish.lihat(idx);
		},
		clearupload:function(){
			for(var i=0;i<obelupload.length;i++){
				var gentb=obelupload[i];
				if(obdataupload[gentb]!=null){
					obdataupload[gentb].hapus(i);
					delete obdataupload[gentb];
				}
				
				delete objupload[gentb];
				delete htmstatus[gentb];
			}
			obelupload=[];
		},
		progres:function(a,b){
			tmprogress=setInterval(function(){
				var datapost = {"progress_key": a};
				
				$.ajax({
					url: lokal+b,
					type: "POST",
					dataType: "json",
					async:true,
					data: datapost,
					success: function(e){
						
						if(e!=false){
							if(e['status']=="SUKSES"){
								$("#status_bar_"+a).attr("style","width:"+e['pesan']+"%;");
								
								var boxpersen=document.getElementById("persen_"+a);
								boxpersen.innerHTML="("+parseInt(e['pesan'])+"%)";
								if(e['pesan']==100){
									clearInterval(tmprogress);
									
								}
							}else{
								clearInterval(tmprogress);
							}
						}else{
							clearInterval(tmprogress);
						}
					}
				});
			},500);
		},
		stopprogres:function(){
			if(tmprogress!=null) clearInterval(tmprogress);
		},
		getidupload:function(){
			return idupload;
		},
		stopmultiprogres:function(a){
			if(obproses[a]!=null) clearInterval(obproses[a]);
		},
		setmodelihat:function(a){
			modelihat=a;
		}
	};
})();
