var banneriklan=(function(){
	var hal=0;
	var jumlah=30;
	var idbanner=0;
	var boxgambar=null;
	var boxurutan=null;
	var idgambar=0;
	return{
		caricanggih:function(){
			mload.sh();
			var ik_data = {ajax: 1,mode: "mod",cl: "banneriklan",
				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){
				banneriklan.loade();
			}
		},
		loade:function(){
			$("#hal_ke").val(0);
			banneriklan.load();
		},
		load:function(){
			var nohal=$("#hal_ke").val()
			jumlah=$("#jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#cari").val();
			
			$("#grid_banneriklan").html("");
			mload.sh();
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "banneriklan",
				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){
					
					mload.hd();
					//alert(JSON.stringify(e));
					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>";
							}
							$("#hal_ke").html(htm);
							
						}
						$("#hal_ke").val(nohal);
						
						var datatb={
							box:"grid_banneriklan",
							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},
									{"col":"judul","judul":"Judul Link","tipe":"data"},
									{"col":"tautan","judul":"Tautan","tipe":"data","lebar":"200px"},
									{"col":"keterangan","judul":"Keterangan","tipe":"data"},
									{"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:"banneriklan"
						};
						tabel.multisub(datatb);
					}
				}
			});
		},
		buat:function(a){
			document.location=lokal+"member/mod/banneriklan?id="+a;
		},
		kembali:function(){
			document.location=lokal+"member/mod/banneriklan";
		},
		edit:function(a){
			idbanner=a['id'];
			idgambar=a['idgambar'];
			
			var namaawal=a['nama_awal'];
			var filelama=a['nama_baru'];
			var folder=a['folder'];
			var prev="tidak";
			if(idgambar>0){
				prev="ya";
			}
			$("#box_gambar_banner").html("");
			upload.tambahupload("box_gambar_banner",banneriklan.objupload(namaawal,prev,filelama,folder));
			
			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 "
            });
			
		},
		simpan:function(){
			$("#loader_banneriklan").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_banneriklan").html(err);
				return;
			}
			
			var fg="inputbanner";
			if(idbanner>0){
				fg="editbanner";
			}
			mload.sh();
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "banneriklan",
				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){
					
					mload.hd();
					if(e==1){
						document.location=lokal+"member/mod/banneriklan";
					}
				}
			});
		},
		hapus:function(a){
			if(a<=0) return;
			var ps={"pesan":"Yakin akan menghapus link terkait ini ??",
					"title":"Peringatan Hapus data"};
				
			var boxyesno=boxpesan.yesno(ps);
			boxyesno.proses.onclick=function(){
				
				mload.sh();
				var ik_data = {
					ajax: 1,
					mode: "mod",
					cl: "banneriklan",
					ikdata: {0:"hapusData",id:a}
				};
				
				$.ajax({
					url: lokal+"man.ajax.php",
					type: "POST",
					dataType: "json",
					async:true,
					data: ik_data,
					success: function(e){
						mload.hd();
						if(e!=false){
							boxyesno.tutup();
							banneriklan.load();
						}
					}
				});
			}
		},
		simpanurutan:function(){
			var urutan=$("#urutan").val();
			
			mload.sh();
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "banneriklan",
				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){
					mload.hd();
					if(e==1){
						banneriklan.load();
						banneriklan.batalurutan();
					}
				}
			});
		},
		ubahurutan:function(a,b){
			var ps={"box":"box_urutan",
					"title":"Ubah Urutan","tinggimax":"fit","lebar":"300px"};
			$("#urutan").val(b);
			boxurutan=boxpesan.win(ps);
			boxurutan.id=a;
		},
		batalurutan:function(){
			if(boxurutan!=null) boxurutan.tutup();
			boxurutan=null;
		},
		objupload:function(a,b,c,d){
			var nama=a || "PILIH FILE UNTUK UPLOAD GAMBAR";
			var prev=b || "tidak";
			var filelama=c || "";
			var folder=d || "";
			
			var obp=new Object;
			obp.proses=function(dataupload){
				idgambar=dataupload.id;
				filelama=dataupload.namabaru;
				folder=dataupload.folder;
			};
			
			obp.hapus=function(a){
				if(idgambar>0){
					var ps={"pesan":"Yakin akan Membatalkan upload gambar ini ??",
						"title":"Peringatan Hapus data"};
					
					var boxyesno=boxpesan.yesno(ps);
					boxyesno.proses.onclick=function(){
						mload.sh();
						var ik_data = {
							ajax: 1,
							mode: "mod",
							cl: "banneriklan",
							ikdata: {0:"hapusGambar",id:idgambar}
						};
						
						$.ajax({
							url: lokal+"man.ajax.php",
							type: "POST",
							dataType: "json",
							async:true,
							data: ik_data,
							success: function(e){
								mload.hd();
								if(e==1){
									idgambar=0;
									upload.tambahupload("box_gambar_banner",banneriklan.objupload("PILIH FILE UNTUK UPLOAD GAMBAR",prev,filelama,folder));
									boxyesno.tutup();
								}
							}
						});
					};
				}else{
					idgambar=0;
					upload.tambahupload("box_gambar_banner",banneriklan.objupload());
				}
			};
			obp.lihat=function(a){
				var ps={"box":"box_preview",
						"title":"Preview","tinggimax":"fit"};
				document.getElementById("img_banner").src="";	
				boxgambar=boxpesan.win(ps);
				document.getElementById("img_banner").src=lokal+d+filelama;
			};
			
			obp.pilihfile=function(a){
				idgambar=0;
			};
			
			var dtpost={"jenis":2};
			var obj={"tautan":lokal+"service.php?pos=banneriklan&mode=mod&fl=upload",
				"status":lokal+"service.php?pos=banneriklan&mode=mod&fl=progres","tutup":"ya","finish":obp,
				"data":dtpost,"namafile":nama,"preview":prev};
			return obj;
		},
		setstatus:function(a,b,c,d){
			var st=1;
			if(tabel.gettabel(b).getdata(c,"status")==1){
				st=0;
			}
			
			mload.sh();
			var ik_data = {ajax: 1,mode: "mod",cl: "banneriklan",
				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){
					
					mload.hd();
					//alert(JSON.stringify(e));
					if(e==1){
						if(st==1){
							d.className="mini_ok";
						}else{
							d.className="mini_dis";
						}
						tabel.gettabel(b).setdata(c,"status",st);
					}
				}
			});
		}
	};
})();