var profile=(function(){
	var boxlokasiarea=null;
	var idlogo=0;
	var folderlogo="";
	var namafile="";
	var filebaru="";
	var idheaderhalaman=0;
	var namafileheaderhalaman="";
	var namabaruheaderhalaman="";
	var folderheaderhalaman="";
	return{
		load:function(a){
			var sett={};
			if(a.hasOwnProperty("setting")) sett=JSON.parse(a['setting']);
			if(sett.hasOwnProperty("nama")) $("#nama").val(sett['nama']);
			if(sett.hasOwnProperty("alamat")) $("#alamat").val(sett['alamat']);
			if(sett.hasOwnProperty("lokasi")) $("#lokasi").val(sett['lokasi']);
			if(sett.hasOwnProperty("email")) $("#email").val(sett['email']);
			if(sett.hasOwnProperty("telp")) $("#telp").val(sett['telp']);
			if(sett.hasOwnProperty("fax")) $("#fax").val(sett['fax']);
			if(sett.hasOwnProperty("hp")) $("#hp").val(sett['hp']);
			if(sett.hasOwnProperty("wa")) $("#wa").val(sett['wa']);
			
			if(sett.hasOwnProperty("header")) $("#theader").val(sett['header']);
			if(sett.hasOwnProperty("header2")) $("#theader2").val(sett['header2']);
			if(sett.hasOwnProperty("idlogo"))  idlogo=sett['idlogo'];
			
			if(sett.hasOwnProperty("namafile") && sett.hasOwnProperty("namafolder") && sett.hasOwnProperty("namabaru") && sett['namafile']!="" && idlogo>0){
				folderlogo=sett['namafolder'];
				filebaru=sett['namabaru'];
				upload.tambahupload("upload_logo",profile.objupload(sett['namafile'],"ya"));
			}else{
				upload.tambahupload("upload_logo",profile.objupload("PILIH FILE LOGO","tidak"));
			}
			
			
			if(sett.hasOwnProperty("idheaderhalaman"))  idheaderhalaman=sett['idheaderhalaman'];
			if(sett.hasOwnProperty("namaheaderhalaman") && sett.hasOwnProperty("folderheaderhalaman") && sett.hasOwnProperty("namabaruheaderhalaman") && sett['namaheaderhalaman']!="" && idheaderhalaman>0){
				folderheaderhalaman=sett['folderheaderhalaman'];
				namabaruheaderhalaman=sett['namabaruheaderhalaman'];
				upload.tambahupload("upload_header_halaman",profile.objuploadheader(sett['namaheaderhalaman'],"ya"));
			}else{
				upload.tambahupload("upload_header_halaman",profile.objuploadheader("PILIH FILE HEADER HALAMAN","tidak"));
			}
			
			
			var ted=tinymce.init({
                selector: "textarea#deskripsi",
                theme: "modern",
                height: 100,
                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 "
            });
            
			if(sett.hasOwnProperty("deskripsi")){
				$("#deskripsi").html(sett['deskripsi']);
			}
		},
		bukapeta:function(){
			var strlokasi=$("#lokasi").val();
			var lokasi="-7.0247703,110.3488083";
			if(strlokasi!="" && strlokasi.split(",").length==2){
				lokasi=strlokasi.split(",")[0]+","+strlokasi.split(",")[1];
			}
			
			var ps={"box":"box_winpeta",
						"title":"Input Koordinat","tinggimax":"fit","tinggi":kudus.tinggifit()-200+"px","edit":true,"mode":"buat","zoom":8,"lokasi":lokasi};
				
			boxlokasiarea=boxpesan.peta(ps);
			boxlokasiarea.proses.onclick=function(){
				$("#lokasi").val(boxlokasiarea.tanda);
				boxlokasiarea.tutup();
			};
		},
		
		simpan:function(){
			var nama=$("#nama").val();
			var al=$("#alamat").val();
			var lok=$("#lokasi").val();
			var em=$("#email").val();
			var tp=$("#telp").val();
			var fx=$("#fax").val();
			var hp=$("#hp").val();
			var wa=$("#wa").val();
			var headr=$("#theader").val();
			var headr2=$("#theader2").val();
			var des=tinyMCE.get('deskripsi').getContent();
			var err="";
			if(nama=="") err+="link media sosial belum diisi.</br>";
			if(err!=""){
				var ps={"title":"Peringatan","pesan":err};
				boxpesan.peringatan(ps);
				return;
			}
			
			var profile={nama:nama,alamat:al,lokasi:lok,email:em,telp:tp,hp:hp,fax:fx,wa:wa,
				idlogo:idlogo,header:headr,header2:headr2,idheaderhalaman:idheaderhalaman,
				deskripsi:des};
				
			var loadersimpan=mload.loader({"keterangan":"Simpan Profile"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "managprofile",
				ikdata: {0:"updateprofile",profile:profile}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					
					if(e==1){
						loadersimpan.error([{"pesan":"Simpan Profile Berhasil"}]);
					}else{
						loadersimpan.error([{"pesan":"Tidak bisa menyimpan profile"}]);
					}
				}
			});
		},
		objupload:function(a,b){
			var nama=a || "PILIH FILE LOGO";
			var prev=b || "tidak";
			
			var obp=new Object;
			obp.proses=function(dataupload){
				idlogo=dataupload.id;
			};
			
			obp.hapus=function(a){
				
			};
			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+folderlogo+filebaru;
			};
			
			obp.pilihfile=function(a){
				idlogo=0;
			};
			
			var dtpost={"jenis":3};
			var obj={"tautan":lokal+"service.php?pos=managprofile&mode=mod&fl=upload",
				"status":lokal+"service.php?pos=managprofile&mode=mod&fl=progres","tutup":"tidak","finish":obp,
				"data":dtpost,"namafile":nama,"preview":prev};
			return obj;
		},
		
		objuploadheader:function(a,b){
			var nama=a || "PILIH FILE HEADER HALAMAN";
			var prev=b || "tidak";
			
			var obp=new Object;
			obp.proses=function(dataupload){
				idheaderhalaman=dataupload.id;
			};
			
			obp.hapus=function(a){
				
			};
			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+folderheaderhalaman+namabaruheaderhalaman;
			};
			
			obp.pilihfile=function(a){
				idheaderhalaman=0;
			};
			
			var dtpost={"jenis":3};
			var obj={"tautan":lokal+"service.php?pos=managprofile&mode=mod&fl=upload",
				"status":lokal+"service.php?pos=managprofile&mode=mod&fl=progres","tutup":"tidak","finish":obp,
				"data":dtpost,"namafile":nama,"preview":prev};
			return obj;
		},
	};
})();