var imagemanager=(function(){
	var hal=0;
	var jumlah=30;
	var dataobj=null;
	var boximage=null;
	var boxgambar=null;
	var jenisgambar=2;
	return{
		open:function(a){
			dataobj=a || null;
			$("#grid_imagemanager").html("");
			$("#boxuploadimage").html("");
				var ps={"box":"box_imagemanager",
						"title":"Image Manager","tinggimax":"fit"};
			boximage=boxpesan.win(ps);
			boximage.obj=dataobj;			
			imagemanager.loaddaftar();
		},
		enter:function(e){
			if(e.which == 13 || e.keyCode == 13){
				imagemanager.loade();
			}
		},
		loade:function(){
			$("#im_hal_ke").val(0);
			imagemanager.loaddaftar();
		},
		loaddaftar:function(){
			var nohal=$("#im_hal_ke").val()
			jumlah=$("#im_jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#im_cari").val();
			
			mload.sh("box_loader_im");
			var ik_data = {
				ajax: 1,
				mode: "plg",
				cl: "imagemanager",
				ikdata: {0:"getGambar",1:hal,2:jumlah,3:cr,4:jenisgambar}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					mload.hd();
					
					if(e!=false){
						if(e.length>0 && e[0]['halaman']>0){
							var htm;
							for(var i=0;i<e[0]['halaman'];i++){
								htm+="<option value='"+(i+1)+"'>"+(i+1)+"</option>";
							}
							$("#im_hal_ke").html(htm);
							
						}
						$("#im_hal_ke").val(nohal);
						
						var datatb={
							box:"grid_imagemanager",
							data:e,
							lebarmin:"500px",
							trklik:{"data":"id","fungsi":"barisklik"},
							title:"kategori Menu",
							isi:[
									{"col":"","judul":"No","tipe":"urut","align":"center","lebar":"50px","halaman":hal,"jumlah":jumlah},
									{"align":"center","judul":"View","tipe":"tpl","tpl":"btlihat_imagemanager","lebar":"60px"},
									{"col":"nama_awal","judul":"Nama File","tipe":"data"},
									{"align":"center","judul":"Proses","tipe":"tpl","tpl":"btproses_imagemanager","lebar":"40px"},
								],
							mod:"plg",
							module:"getGambar"
						};
						tabel.multisub(datatb);
					}
				}
			});
		},
		uploadimage:function(){
			boximage.idgambar=0;
			upload.tambahupload("boxuploadimage",imagemanager.objupload());
		},
		pilihfile:function(a,b,c){
			var obj=new Object;
			obj.idgambar=a;
			obj.namabaru=tabel.gettabel(b).getdata(c,"nama_baru");
			obj.namalama=tabel.gettabel(b).getdata(c,"nama_awal");
			obj.ukuran=tabel.gettabel(b).getdata(c,"ukuran");
			obj.gambar=lokal+"gambar/"+tabel.gettabel(b).getdata(c,"nama_baru");
			obj.gambarthumb=lokal+"gambar/thumb/"+tabel.gettabel(b).getdata(c,"nama_baru");
			if(boximage.obj!=null){
				boximage.obj.proses(obj);
				boximage.tutup();
				boximage=null;
			}
		},
		kembali:function(){
			if(boximage!=null) boximage.tutup();
			boximage=null;
		},
		objupload:function(a,b,c){
			var nama=a || "PILIH FILE UNTUK UPLOAD GAMBAR";
			var prev=b || "tidak";
			var filelama=c || "";
			
			var obp=new Object;
			obp.proses=function(dataupload){
				$("#boxuploadimage").html("");
				imagemanager.loaddaftar();
				boximage.idgambar=0;
			};
			
			obp.hapus=function(a){
				if(boximage.idgambar>0){
					var ps={"pesan":"Yakin akan Membatalkan upload ini ??",
						"title":"Peringatan Hapus data"};
					
					var boxyesno=boxpesan.yesno(ps);
					boxyesno.proses.onclick=function(){
						mload.sh();
						var ik_data = {
							ajax: 1,
							mode: "plg",
							cl: "imagemanager",
							ikdata: {0:"hapusGambar",1:boximage.idgambar}
						};
						
						$.ajax({
							url: lokal+"man.ajax.php",
							type: "POST",
							dataType: "json",
							async:true,
							data: ik_data,
							success: function(e){
								mload.hd();
								if(e==1){
									boximage.idgambar=0;
									boxyesno.tutup();
								}
							}
						});
					};
				}else{
					boximage.idgambar=0;
				}
			};
			obp.lihat=function(a){
				
			};
			
			obp.pilihfile=function(a){
				boximage.idgambar=0;
			};
			
			var dtpost={"jenis":jenisgambar};
			var obj={"tautan":lokal+"service.php?pos=imagemanager&mode=plg&fl=upload",
				"status":lokal+"service.php?pos=imagemanager&mode=plg&fl=progres","tutup":"ya","finish":obp,
				"data":dtpost,"namafile":nama,"preview":prev};
			return obj;
		},
		getgambar:function(a){
			hasil=lokal+"/gambar/thumb/"+a;
			return hasil;
		},
		getjenis:function(){
			return jenisgambar;
		}
	};
})();
