var mload=(function(){
	var root=null;
	var tm=null;
	var s=5000;
	var intervalreload=null;
	return{
		sh:function(a,b){
			a = a || "box_loader";
			b = b || 0;
			root=document.getElementById(a);
			
			var htm=document.getElementById("ploader").innerHTML;
			root.innerHTML="";
			root.innerHTML=htm;
			if(tm){
				clearTimeout(tm);
			}
			if(b==1){
				tm=setTimeout(function(){root.innerHTML="";},s);
			}
		},
		hd:function(){
			if(tm){
				clearTimeout(tm);
			}
			root.innerHTML="";
		},
		init:function(a){
			var dataobj=a || null;
			if(dataobj==null) return;
			var idel=dataobj['idbox'] || "box_loader";
			
			var el=document.getElementById(idel);
			dataobj.el=el;
			dataobj.isi=el.innerHTML;
			
			dataobj.load=(function(ob){
				var obj=ob;
				return function(){
					var htm=document.getElementById("ploader").innerHTML;
					if(obj!=null){
						obj.el.innerHTML="";
						obj.el.innerHTML=htm;
					}
					
				}
			})(dataobj);
			
			dataobj.tutup=(function(ob){
				var obj=ob;
				return function(){
					if(obj!=null){
						obj.el.innerHTML=obj.isi;
					}
					
				}
			})(dataobj);
			
			return dataobj;
		},
		loading:function(a){
			var dataobj=a || null;
			var datael=null;
			if(dataobj.hasOwnProperty("el")){
				datael=document.getElementById(dataobj['el']);
				var htm="<img src='"+lokal+"plugins/plg_loader/css/img/ajaxloader.gif'/>";
				datael.innerHTML=htm;
			}
			var obj=new Object();
			obj.tutup=function(){
				if(datael!=null) datael.innerHTML="";
			}
			obj.pesan=function(ps){
				if(datael!=null) datael.innerHTML=ps;
			}
			return obj;
		},
		loader:function(a){
			var dataobj=a || null;
			var ket="Sedang memproses data .....";
			if(dataobj!=null){
				ket=dataobj['keterangan'];
			}
			
			$("#plg_keterangan_loader").html(ket);
			$("#box_grid_pesan_loader").hide();
			$("#grid_pesan_loader").html("");
			document.getElementById("box_grid_pesan_loader").className="box_grid_pesan_loader_show";
			
			document.body.style.overflow = "hidden";
			$("#plg_box_loader").show();
			
			function tutup(){
				if(intervalreload!=null) clearInterval(intervalreload);
				document.getElementById("box_grid_pesan_loader").className="box_grid_pesan_loader_show";
				document.body.style.overflow = "auto";
				$("#plg_box_loader").hide();
			};
			
			$("#plg_batal_loader").click(function(){
				if(dataobj!=null && dataobj.hasOwnProperty("batal") && dataobj.batal!=null){
					dataobj.batal();
				}
			  	tutup();
			});
			
			var obj=new Object();
			obj.pesan=function(a){
				$("#plg_keterangan_loader").html(a);
			};
			
			obj.proses=function(){
				if(dataobj!=null && dataobj.hasOwnProperty("proses") && dataobj.proses!=null){
					dataobj.proses();
				}
			};
			
			obj.tutup=function(){
				if(dataobj!=null && dataobj.hasOwnProperty("tutup") && dataobj.tutup!=null){
					dataobj.tutup();
				}
				tutup();
			};
			
			obj.batal=function(){
				if(dataobj!=null && dataobj.hasOwnProperty("batal") && dataobj.batal!=null){
					dataobj.batal();
				}
				
				tutup();
			};
			
			obj.error=function(a){
				document.body.style.overflow = "hidden";
			
				$("#box_img_loader").hide();
				$("#box_ket_loader").hide();
				
				$("#plg_box_loader").show();
				$("#box_grid_pesan_loader").show();
				document.getElementById("box_grid_pesan_loader").className="box_grid_pesan_loader_show box_grid_pesan_loader_anim";
				
				if (a.length>0){
					var datatb={
						box:"grid_pesan_loader",
						data:a,
						lebarmin:"300px",
						trklik:{"data":"id","fungsi":"barisklik"},
						title:"kategori Menu",
						isi:[
								{"col":"","judul":"No","tipe":"urut","align":"center","lebar":"40px","halaman":0},
								{"col":"pesan","judul":"Pesan","tipe":"data"}
							],
						mod:"mod",
						module:"loader"
					};
					tabel.multisub(datatb);
				}
				
			};
			
			return obj;
			
		},
		tutup:function(){
			if(intervalreload!=null) clearInterval(intervalreload);
			document.getElementById("box_grid_pesan_loader").className="box_grid_pesan_loader_show";
			document.body.style.overflow = "auto";
			$("#plg_box_loader").hide();
		},
		error:function(a){
			document.body.style.overflow = "hidden";
			
			$("#box_img_loader").hide();
			$("#box_ket_loader").hide();
			
			$("#plg_box_loader").show();
			$("#box_grid_pesan_loader").show();
			document.getElementById("box_grid_pesan_loader").className="box_grid_pesan_loader_show box_grid_pesan_loader_anim";
			
			if (a.length>0){
				var datatb={
					box:"grid_pesan_loader",
					data:a,
					lebarmin:"300px",
					trklik:{"data":"id","fungsi":"barisklik"},
					title:"kategori Menu",
					isi:[
							{"col":"","judul":"No","tipe":"urut","align":"center","lebar":"40px","halaman":0},
							{"col":"pesan","judul":"Pesan","tipe":"data"}
						],
					mod:"mod",
					module:"loader"
				};
				tabel.multisub(datatb);
			}
		},
		cekstatus:function(a){
			if(intervalreload!=null) clearInterval(intervalreload);
			intervalreload=setInterval(function(){
				mload.reloaddata(a);
			},5000);
		},
		reloaddata:function(a){
			var ik_data = {
				ajax: 1,
				mode: "plg",
				cl: "loader",
				ikdata: {0:"cekStatus",kode:a.kode}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					console.log(JSON.stringify(e));
					if(e!=false && e.hasOwnProperty("status")){
						if(e['status']==1){
							if(intervalreload!=null) clearInterval(intervalreload);
							a.sukses();
						}else if(e['status']>1){
							if(intervalreload!=null) clearInterval(intervalreload);
							mload.error(JSON.parse(e['pesan']));
						}
					}
				}
			});
		},
	};
})();
