var tabel=(function(){
	var module=null;
	var mod=null;
	var data=null;
	var altpress=false;
	var shiftpress=false;
	var fldcol=[];
	var arrklik=[];
	var cellid=null;
	var datacell=null;
	var boxisip=new Object;
	var keyboxsisip=[];
	var jumlahtabel=0;
	var tabelobject=new Object;
	var arrtabel=[];
	var hashtabel=new Object;
	var arrdata=new Object;
	
	return{
		
		multisub:function(a){
			arrklik=[];
			$(document).keydown(function (e) {tabel.kpress(e);});
			$(document).keyup(function (e) {tabel.keyup(e);});
			
			var objtabel=new Object;
			document.onkeyup=tabel.keyup;
			var isi=a['isi'] || null;
			var box=a['box'] || null;
			data=a['data'] || null;
			mod=a['mod'];
			module=a['module'];
			var minlb=a['lebarmin']||null;
			var klik=a['klik'] || null;
			var fontsize=a['fontsize'] || null;
			var tabelclass=a['tbclass'] || "table_grid";
			var tabelparr=a['tabelpar'] || 0;
			var barisparr=a['barispar'] || 0;
			var header=a['header'] || "YA";
			var tambahdata=a['tambahdata'] || null;
			if(data.length>0){
				var Obj=new Object;
				Obj=data[0];
				fldcol=Object.keys(Obj);
			}
			
			var htm="";
			htm+='<div style="max-width:100%;overflow:auto" class="grid_tabel">';
			var tabelstyle="";
			if(minlb!=null){
				tabelstyle=" style=\"min-width:"+minlb+"\"";
			}
			
			htm+='<table'+tabelstyle+' class="'+tabelclass+'">';
			if(isi!=null){
				if(header=="YA"){
					htm+='<thead>';
					htm+='<tr>';
					for(var i=0;i<isi.length;i++){
						var align=isi[i]['align']||"left";
						
						var thcl="";
						if(i>0){
							thcl="garis_kiri";
						}
						switch(align)
						{
							case "left":
								htm+='<th style="text-align:left" class="'+thcl+'">'+isi[i]['judul']+'</th>';
								break;
							case "right":
								htm+='<th style="text-align:right" class="'+thcl+'">'+isi[i]['judul']+'</th>';
								break;
							case "center":
								htm+='<th style="text-align:center" class="'+thcl+'">'+isi[i]['judul']+'</th>';
								break;
						}
					}
					htm+='</tr>';
					htm+='</thead>';	
				}
				
				
				if(data!=null){
					var pertama=0;
					var trclass="";
					htm+="<tbody>";
					for(var i=0;i<data.length;i++){
						if(tambahdata!=null){
							data[i][tambahdata['nama']]=tambahdata['data'];
						}
						
						if(pertama==0){
							trclass="bg_pertama";
							pertama=1;
						}else{
							trclass="bg_kedua";
							pertama=0;
						}
						htm+="<tr class='"+trclass+"' id='tabel_tr_"+jumlahtabel+"_"+i+"' onclick='tabel.barisklikmulti("+jumlahtabel+","+i+")'>";
						for(var n=0;n<isi.length;n++){
							var align=isi[n]['align']||"left";
							var lebar=isi[n]['lebar']||null;
							var valg=isi[n]['valg']||"middle";
							var lb="";
							if(lebar!=null){
								lb=' width="'+lebar+'"';
							}
							var td="<td"+lb;
							switch(align)
							{
								case "left":
									td+=" align='left'";
									break;
								case "right":
									td+=" align='right'";
									break;
								case "center":
									td+=" align='center'";
									break;
							}
							td+=" valign='"+valg+"'";
							if(fontsize!=null){
								td+=" style='font-size:"+fontsize+"'";
							}
							if(n!=0){
								td+="class='garis_kiri_td'";
							}
							
							td+=">";
							htm+=td;
							
							
							switch(isi[n]['tipe']){
								case "urut":
									htm+=(i+1)+isi[n]['halaman'];
									break;
								case "data":
									//htm+=(data[i][isi[n]['col']]!=null)?data[i][isi[n]['col']]:"";
									if(isi[n]['format']=="desimal"){
										htm+=(data[i][isi[n]['col']]!=null)?accounting.formatNumber(data[i][isi[n]['col']], 0, ".", ","):"";
									}else if(isi[n]['format']=="desimal2"){
										htm+=(data[i][isi[n]['col']]!=null)?accounting.formatNumber(data[i][isi[n]['col']], 2, ".", ","):"";
									}else if(isi[n]['format']=="tanggal"){
										htm+=(data[i][isi[n]['col']]!=null)?kudus.tanggal(data[i][isi[n]['col']]):"";
									}else if(isi[n]['format']=="tanggalwaktu"){
										htm+=(data[i][isi[n]['col']]!=null)?kudus.tanggal(data[i][isi[n]['col']],"/","2"):"";
									}else{
										htm+=(data[i][isi[n]['col']]!=null)?data[i][isi[n]['col']]:"";
									}
									break;
								case "tpl":
									var idcl=isi[n]['tpl']+"_"+jumlahtabel+"_"+i;
									var htmrep=(document.getElementById(isi[n]['tpl']).outerHTML).replace(/id=\"(.*?)\"/g, 'id=\"'+idcl+'\"').replace(/display:none/,'display:block').replace(/display: none/,'display:block');
									var htmind=htmrep.match(/##{baris_tabel}/g);
									if(htmind && htmind.length>0){
										for(var x=0;x<htmind.length;x++){
											htmrep=htmrep.replace(/##{baris_tabel}/g,i);
										}
									}
									var htmdata=htmrep.match(/##{dataindex_tabel}/g);
									if(htmdata && htmdata.length>0){
										for(var x=0;x<htmdata.length;x++){
											htmrep=htmrep.replace(/##{dataindex_tabel}/g,(JSON.stringify(data[i])).replace(/\'/g,'\\\'').replace(/\"/g,"\'"));
										}
									}
									var htmnotabel=htmrep.match(/##{jumlah_tabel}/g);
									if(htmnotabel && htmnotabel.length>0){
										for(var x=0;x<htmnotabel.length;x++){
											htmrep=htmrep.replace(/##{jumlah_tabel}/g,jumlahtabel);
										}
									}
									var htmcol=htmrep.match(/#{.*?}/g);
									if(htmcol && htmcol.length>0){
										for(var x=0;x<htmcol.length;x++){
											var ganti=data[i][(htmcol[x]).substring(2,((htmcol[x]).length-1))];
											if(ganti==null || ganti=="null") ganti="";
											htmrep=htmrep.replace(htmcol[x],ganti.toString().replace("'","\\'"));
										}
									}
									htmrep=htmrep.replace(/amp;/g,"");
									htmrep=htmrep.replace(/&gt;/g,">");
									htmrep=htmrep.replace(/'/g,"\'");
									//htmrep=htmrep.replace(/\"/g,"");
									var htmev=htmrep.match(/#.*?#/g);
									
									
									if(htmev && htmev.length>0){
										for(var x=0;x<htmev.length;x++){
											htmrep=htmrep.replace(htmev[x],eval((htmev[x]).substring(1,((htmev[x]).length-1))));
										}
									}
									
									htm+=htmrep;
									break;
							}
							htm+="</td>";
						}
						htm+="</tr>";
						htm+="<tr id='tabel_tr_sisip_"+jumlahtabel+"_"+i+"' style='display:none;background:#f6f6f6;'><td style='padding-left:35px;' colspan='"+isi.length+"' id='tabel_td_sisip_"+jumlahtabel+"_"+i+"'></td></tr>"
						
					}
					htm+="</tbody>";
				}
			}
				
			htm+='</table>';
			htm+='</div>';
			
			if(box!=null){
				$("#"+box).html(htm);
			}
			
			objtabel.expand=new Object;
			objtabel.maxim=(function(j,ob){
				return function(a,b){
					document.getElementById("tabel_td_sisip_"+j+"_"+b).innerHTML="";
					document.getElementById("tabel_td_sisip_"+j+"_"+b).innerHTML=a.html;
					document.getElementById("tabel_tr_sisip_"+j+"_"+b).style.display="";
					document.getElementById("tabel_tr_sisip_"+j+"_"+b).style.minHeight="100px";
					objtabel.expand[b]=1;
				};
			})(jumlahtabel,objtabel);
			
			objtabel.maximobj=(function(j){
				
				return function(a,b){
					var obparr=new Object;
					obparr.tabel=j;
					obparr.baris=b;
					tabelobject[j].parrent=obparr;
					
					document.getElementById("tabel_td_sisip_"+j+"_"+b).innerHTML="";
					document.getElementById("tabel_td_sisip_"+j+"_"+b).appendChild(a);
					document.getElementById("tabel_tr_sisip_"+j+"_"+b).style.display="";
					objtabel.expand[b]=1;
				};
				
			})(jumlahtabel);
			
			objtabel.minim=(function(j){
				return function(b){
					
					if(objtabel.expand[b]!=null){
						document.getElementById("tabel_td_sisip_"+j+"_"+b).innerHTML="";
						document.getElementById("tabel_tr_sisip_"+j+"_"+b).style.display="none";
					}
				};
			})(jumlahtabel);
			
			objtabel.clearexpand=function(a,b){
				if(objtabel.expand[b]!=null){
					document.getElementById("tabel_td_sisip_"+jumlahtabel+"_"+b).innerHTML="";
				}
			};
			
			objtabel.getdata=(function(dt){
				return function(a,b){
					return dt[a][b];
				};
			})(data);
			
			objtabel.setdata=(function(dt){
				return function(a,b,c){
					dt[a][b]=c;
				};
			})(data);
			
			objtabel.getdatabaris=(function(dt){
				return function(a){
					return dt[a];
				};
			})(data);
			
			objtabel.fulldata=(function(a){
				return function(){
					return a;
				};
			})(data);
			
			objtabel.setparr=function(a,b){
				objtabel.parrent.tabel=a;
				objtabel.parrent.baris=b;
			};
			
			objtabel.tabel=(function(a){
				return function(){
					return a;
				};
			})(jumlahtabel);
			
			objtabel.baris=(function(a){
				return function(){
					return a;
				};
			})(data.length);
			
			objtabel.parrent=new Object;
			objtabel.parrent.tabel=tabelparr;
			objtabel.parrent.baris=barisparr;
			
			tabelobject[jumlahtabel]=objtabel;
			jumlahtabel++;
			
			return objtabel;
		},
		barisklik:function(a,b,c){
			if(!altpress){
				arrklik=[];
				var pertama=0;
				var trclass="";
				
				for(var i=0;i<data.length;i++){
					if(pertama==0){
						trclass="bg_pertama";
						pertama=1;
					}else{
						trclass="bg_kedua";
						pertama=0;
					}
					$("#tabel_tr_"+i).attr("class",trclass);
				}
			}
			
			var Obj=new Object;
			for(var i=0;i<fldcol.length;i++){
				
				Obj[fldcol[i]]=data[c][fldcol[i]];
			}
			
			arrklik.push(Obj);
			
			$("#tabel_tr_"+c).addClass("klik");
			
		},
		barisklikmulti:function(a,b){
			if(!altpress){
				
				var pertama=0;
				var trclass="";
				for(var i=0;i<arrtabel.length;i++){
					var arbr=hashtabel[arrtabel[i]].baris;
					
					for(var n=0;n<arbr.length;n++){
						var pertama=0;
						var trclass="";
						
						if(pertama==0){
								trclass="bg_pertama";
								pertama=1;
						}else{
							trclass="bg_kedua";
							pertama=0;
						}
						
						$("#tabel_tr_"+hashtabel[arrtabel[i]].tabel+"_"+arbr[n]).attr("class",trclass);
					}
				}
				arrtabel=[];
				hashtabel=new Object;
			}
			var ada=false;
			for(var i=0;i<arrtabel.length;i++){
				if(arrtabel[i]==a){
					ada=true;
				}
			}
			if(!ada){
				arrtabel.push(a);
				var obj = new Object;
				var baris=[];
				obj.tabel=a;
				baris.push(b);
				obj.baris=baris;
				
				hashtabel[a]=obj;
			}else{
				var arrb=hashtabel[a].baris;
				var bada=false;
				for(var i=0;i<arrb.length;i++){
					if(arrb[i]==b){
						bada=true;
					}
				}
				if(!bada){
					hashtabel[a].baris.push(b);
				}
			}
			$("#tabel_tr_"+a+"_"+b).addClass("klik");
		},
		kpress:function(e){
			var KeyID = (window.event) ? event.keyCode : e.keyCode;
			if(KeyID==18){
				altpress=true;
			}
		},
		keyup:function(e){
			var KeyID = (window.event) ? event.keyCode : e.keyCode;
			if(KeyID==18){
				altpress=false;
			}
		},
		dataklik:function(){
			return arrklik;
		},
		cellklik:function(a,b,c){
			if(cellid!=null){
				cellid.html(datacell);
			}
			cellid=$("#"+a);
			datacell=cellid.html();
			cellid.html($("#"+b).html());
			
			var Obj=new Object;
			for(var i=0;i<fldcol.length;i++){
				Obj[fldcol[i]]=data[c][fldcol[i]];
			}
			
			datacell=Obj;
		},
		getdatacell:function(){
			return datacell;
		},
		getdata:function(a,b){
			return data[a][b];
		},
		gettabel:function(a){
			return tabelobject[a];
		}
	};
})();
