// JINDO 필요
// 쪽지 확인 

var paperChecker = {
	innerTimer : null,
	afterTime : 3000,
	calling : false,
	wndPopup : null,
	checkCount : 0,

	doMessage : function(res) {
		if( paperChecker.calling ) {
			try {
				var text = res.text();
				eval( "var result = " + text );
				
				if( result.hasPaper == "true" ) {
					paperChecker.stop();
					var url = "/slippaper/slippaper.nhn?paper=" + result.firstPaper;
					paperChecker.wndPopup = openWinNative(url, "slipPaper", 400, 300);
					// 팝업이 닫힐 때 다시 checker start.
				} else {
					paperChecker.start();
				}
			} catch(e) {
				if( ENV_isDev ) {
					alert( "DEV DEBUG:" + e.message );
				}
				// do nothing
			}
		}
		paperChecker.calling = false;
	},
	
	check : function() {
		if( paperChecker.isPopupClosed() ) {
			// do ajax
			var url = "/api/slippaperconfirm.nhn?m=slippaperconfirm";
			var ajax = $Ajax(url, { method:"GET", onload:paperChecker.doMessage });
			ajax.request();
			paperChecker.calling = true;
		}
	},

	start : function() {
		paperChecker.checkCount++;
		if( paperChecker.checkCount > 1 ) {
			paperChecker.afterTime = 15000; 
		}
		innerTimer = setTimeout("paperChecker.check()", paperChecker.afterTime);
	},
	
	stop : function() {
		if( paperChecker.innerTimer ) {
			clearTimeout(paperChecker.innerTimer);
		}
	},
	
	isPopupClosed : function() {
		if( paperChecker.wndPopup == null || paperChecker.wndPopup === undefined) {
			return true;
		}
		
		// Check the IE8 issue on window.closed
		if( paperChecker.wndPopup.closed ) {
			return true;
		}
		
		return false;
	}
};

if( edu_loginid_enc != "") {
	window.attachEvent( "onload", paperChecker.start );
}