var utamapopup=(function(){
	var jumlahReq=0;
	var sizew={};
	var sizegb={};
	var STATUSPOPUP=0;
	var linkgambar="";
	var tautanurl="";
	return{
		reqcb:function(jumlah){
			var requestcb = new reqcb({
			    jumlahReq: jumlah
			});
			return requestcb;
		},
		init:function(){
			$("#box_main_popup").hide();
			var obj=new Object;
			var cbf=utamapopup.reqcb(1);
			
			sizew = {
			  lebar: window.innerWidth || document.body.clientWidth,
			  tinggi: window.innerHeight || document.body.clientHeight
			}
			
			
			function displayWindowSize(){
				utamapopup.ubahukuran();
			}
			window.addEventListener("resize", displayWindowSize);
			cbf.addCallbackToQueue(true, function() {
	            utamapopup.cekpopup();
	        });
		},
		cekpopup:function(){
			STATUSPOPUP=0;
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "utamapopup",
				ikdata: {0:"getPopup"}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					
					//console.log(JSON.stringify(e));
					if(e!=null && e.hasOwnProperty("id")){
						var img = new Image();
					    img.onload = function(){
					    	STATUSPOPUP=1;
					        sizegb={
					        	lebar:this.width,
					        	tinggi:this.height
					        };
					        
					        utamapopup.ubahukuran();
					    };
					    linkgambar= lokal+e['folder']+e['nama_baru'];
					    tautanurl=e['tautan']; 
					    img.src = linkgambar; 
					}
				}
			});
		},
		ubahukuran:function(){
			if(STATUSPOPUP!=1) return;
			sizew = {
			  lebar: window.innerWidth || document.body.clientWidth,
			  tinggi: window.innerHeight || document.body.clientHeight
			}
			
			if(sizew.hasOwnProperty("lebar") && sizegb.hasOwnProperty("lebar")){
				var lebarbox=sizew['lebar']-100;
				var tinggibox=sizew['tinggi']-100;
				
				var lebarbaru=0;
				var tinggibaru=0;
				var persentinggi=0;
				var persenlebar=0;
				
				if(sizew['lebar']>sizew['tinggi']){
					persentinggi=parseFloat(tinggibox/sizegb['tinggi']);
				
					lebarbaru=sizegb['lebar']*persentinggi;
					tinggibaru=tinggibox;
					
					if(lebarbaru>lebarbox){
						lebarbaru=lebarbox;
						persenlebar=parseFloat(lebarbox/sizegb['lebar']);
						tinggibaru=sizegb['tinggi']*persenlebar;
					}
				}else{
					persenlebar=parseFloat(lebarbox/sizegb['lebar']);
					tinggibaru=sizegb['tinggi']*persenlebar;
					lebarbaru=lebarbox;
					
					if(tinggibaru>tinggibox){
						tinggibaru=tinggibox;
						persentinggi=parseFloat(tinggibox/sizegb['tinggi']);
						lebarbaru=sizegb['lebar']*persentinggi;
					}
				}
				
				$("#box_main_popup").show();
				
				document.getElementById("boxgambar_popup").style.width=lebarbaru+"px";
				document.getElementById("boxgambar_popup").style.height=tinggibaru+"px";
				document.getElementById("boxgambar_popup").style.marginLeft="-"+parseFloat(lebarbaru/2)+"px";
				document.getElementById("boxgambar_popup").style.marginTop="-"+parseFloat(tinggibaru/2)+"px";
			
				document.getElementById("gambar_popup").src = linkgambar;
				if(tautanurl!="#" && tautanurl!=""){
					document.getElementById("tautan_popup").href=tautanurl;
				}
				document.body.style.overflow = "hidden";
			}
		},
		tutup:function(){
			document.body.style.overflow = "auto";
			$("#box_main_popup").hide();
			STATUSPOPUP=0;
		}
	}
})();
