var managbanner=(function(){
	var hal=0;
	var jumlah=30;
	var idbanner=0;
	var idgambar=0;
	var boxgambar=null;
	var boxurutan=null;
	
	return{
		caricanggih:function(){
			mload.sh();
			var ik_data = {ajax: 1,mode: "mod",cl: "managbanner",
				ikdata: {0:"datacari01",1:1}
			};
			
			$.ajax({url: lokal+"man.ajax.php",type: "POST",dataType: "json",async:true,data: ik_data,
				success: function(e){
					mload.hd();
					if(e!=false){
						var boxcari;
						var obj=new Object;
						obj.proses=function(a){
							$("#cari").val(a);
							boxcari.tutup();
							document.getElementById("btcari").click();
						};
						var datacari={"data":e,"proses":obj}
						boxcari=caricanggih.buka(datacari);
					}
				}
			});
		},
		enter:function(e){
			if(e.which == 13 || e.keyCode == 13){
				managbanner.loade();
			}
		},
		loade:function(){
			$("#hal_ke").val(0);
			managbanner.load();
		},
		load:function(){
			var nohal=$("#hal_ke").val()
			jumlah=$("#jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#cari").val();
			
			$("#grid_managbanner").html("");
			var loaderdata=mload.loading({"el":"loader_daatabanner"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "managbanner",
				ikdata: {0:"getTabel",1:hal,2:jumlah,3:cr}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					
					loaderdata.tutup();
					//alert(JSON.stringify(e));
					if(e!=false && e.length>0){
						
						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>";
							}
							$("#hal_ke").html(htm);
							
						}
						$("#hal_ke").val(nohal);
						
						var datatb={
							box:"grid_managbanner",
							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},
									{"judul":"Judul Banner","tipe":"tpl","tpl":"btdetailbanner"},
									{"judul":"urutan","tipe":"tpl","tpl":"bturutan","lebar":"50px"},
									{"align":"center","judul":"Status","tipe":"tpl","tpl":"btstatus","lebar":"60px"},
									{"align":"center","judul":"","tipe":"tpl","tpl":"btedit","lebar":"60px"},
									
								],
							mod:"mod",
							module:"managbanner"
						};
						tabel.multisub(datatb);
					}else{
						loaderdata.pesan("BELUM ADA DATA BANNER");
					}
				}
			});
		},
		buat:function(a){
			document.location=lokal+"member/mod/managbanner?id="+a+"&scrmenu="+modmenu.getmenuscr();
		},
		kembali:function(){
			document.location=lokal+"member/mod/managbanner?scrmenu="+modmenu.getmenuscr();
		},
		edit:function(a){
			idbanner=a['id'];
			idgambar=a['idgambar'];
			
			var ted=tinymce.init({
                selector: "textarea#keterangan",
                theme: "modern",
                height: 200,
                relative_urls: false,
                menubar:false,
                remove_script_host: false,
                plugins: [
                     "advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker",
                     "searchreplace visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
                     "save table contextmenu directionality emoticons template paste textcolor"
                ],
            
                toolbar: "undo redo | styleselect | bold italic fontselect fontsizeselect | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent "
            });
            
            
			$("#box_gambar_banner").html("");
			var namalama=a['namaawal'];
			var prev="tidak";
			if(idgambar>0) prev="ya";
			upload.tambahupload("box_gambar_banner",managbanner.objupload(namalama,prev));
		},
		simpan:function(){
			$("#loader_banner_err").html("");
			var jd=$("#judul").val();
			var tt=$("#tautan").val();
			var ket=tinyMCE.get('keterangan').getContent();
			
			var err="";
			if(jd=="") err+="Judul banner belum diisi.</br>";
			if(idgambar<=0) err+="Banner tidak/belum selesai di upload.</br>";
			
			if(err!=""){
				$("#loader_banner_err").html(err);
				return;
			}
			
			var fg="inputbanner";
			if(idbanner>0){
				fg="editbanner";
			}
			var loadersimpan=mload.loader({"keterangan":"Proses simpan data"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "managbanner",
				ikdata: {0:fg,judul:jd,tautan:tt,
					keterangan:ket,idgambar:idgambar,
					id:idbanner}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					
					if(e==1){
						managbanner.kembali();
					}else{
						loadersimpan.error([{"pesan":"Gagal simpan data banner"}]);
					}
				}
			});
		},
		hapus:function(a){
			if(a<=0) return;
			var boxyesno=null;
			var databox={
				judul:"Yakin akan menghapus data banner ini ?? ",
				proses:function(){
					boxyesno.tutup();
					var loaderhapus=mload.loader({"keterangan":"Proses hapus data.."});
					var ik_data = {
						ajax: 1,
						mode: "mod",
						cl: "managbanner",
						ikdata: {0:"hapusData",id:a}
					};
					
					$.ajax({
						url: lokal+"man.ajax.php",
						type: "POST",
						dataType: "json",
						async:true,
						data: ik_data,
						success: function(e){
							if(e==1){
								loaderhapus.error([{"pesan":"Berhasil "}]);
								managbanner.load();
							}else{
								loaderhapus.error([{"pesan":"Gagal "}]);
							}
						}
					});
				}
			};
			boxyesno=boxwin.yesno(databox);
			boxyesno.buka();
			
		},
		
		ubahurutan:function(a,b,c,d){
			var ps={"box":"box_urutan",
					"judul":"Urutan Banner ",tinggi:"auto","lebar":"400"};
		
			boxurutan=boxwin.load(ps);
			boxurutan.id=a;
			boxurutan.buka();
			
			$("#loader_simpan_urutan").html("");
			$("#urutan").val(tabel.gettabel(b).getdata(c,"urutan"));
			
		},
		simpanurutan:function(){
			$("#loader_simpan_urutan").html("");
			var urutan=$("#urutan").val();
			if(urutan==""){
				$("#loader_simpan_urutan").html("Nomor urutan banner belum diisi!");
				return;
			}
			
			var loaderurutan=mload.loading({"el":"loader_simpan_urutan"});
			
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "managbanner",
				ikdata: {0:"ubahurutan",id:boxurutan.id,urutan:urutan}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					loaderurutan.tutup();
					if(e==1){
						managbanner.load();
						managbanner.batalurutan();
					}else{
						loaderurutan.pesan("Error update urutan banner.");
					}
				}
			});
		},
		batalurutan:function(){
			if(boxurutan!=null) boxurutan.tutup();
			boxurutan=null;
		},
		setstatus:function(a,b,c,d){
			var st=1;
			if(tabel.gettabel(b).getdata(c,"status")==1){
				st=0;
			}
			
			var loaderstatus=mload.loader({"keterangan":"Update status.."});
			var ik_data = {ajax: 1,mode: "mod",cl: "managbanner",
				ikdata: {0:"setStatus",status:st,id:a}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",type: "POST",dataType: "json",async:true,data: ik_data,
				success: function(e){
					loaderstatus.tutup();
					if(e==1){
						if(st==1){
							d.className="mini_ok";
						}else{
							d.className="mini_dis";
						}
						tabel.gettabel(b).setdata(c,"status",st);
					}else{
						loaderstatus.error([{"pesan":"Gagal update status"}]);
					}
				}
			});
		},
		lihatdokumen:function(a){
			var iddok=a;
			if(iddok<=0) return;
			var myWindow = window.open(lokal+"service.php?pos=managbanner&mode=mod&fl=download&id="+iddok, "", "left=100,top=50,width=800, height=600");
		},
		objupload:function(a,b){
			var nama=a || "PILIH FILE UNTUK UPLOAD BANNER";
			var prev=b || "tidak";
			
			var obp=new Object;
			obp.proses=function(dataupload){
				idgambar=dataupload.id;
				filelama=dataupload.nama;
				
				$("#box_gambar_banner").html("");
				upload.tambahupload("box_gambar_banner",managbanner.objupload(filelama,"ya"));
			};
			
			obp.hapus=function(a){
				
			};
			obp.lihat=function(a){
				managbanner.lihatdokumen(idgambar);
			};
			
			obp.pilihfile=function(a){
				idgambar=0;
			};
			
			var dtpost={"jenis":4};
			var obj={"tautan":lokal+"service.php?pos=managbanner&mode=mod&fl=upload",
				"status":lokal+"service.php?pos=managbanner&mode=mod&fl=progres","tutup":"tidak","finish":obp,
				"data":dtpost,"namafile":nama,"preview":prev};
			return obj;
		},
		
	};
})();