var prgress = 0;

function cls_jview(){
	// 변환 체크 시간 5초  
	this.refreshTime= 5000;
	
	// 변환 여부 체크 할지 말지 
	this.call = true;

	this.instance = ""; //인스턴스 이름
	
	// 파일 다운로드 받을 주소
	this.url;

	// 파일 생성 날짜 (선택사항)
	// yyyyMMddHHssmm 형식
	// 예) 20090203112302
	this.fdate;
	
	// 진행상태를 물어볼 주소
	this.progress_url;
	
	// 미리보기 페이지 주소 
	this.viewpage_url;
	
	// 상태값 
	this.STATUS_WAIT;
	this.STATUS_PDFCONVERTING;
	this.STATUS_IMAGECONVERTING;
	this.STATUS_COMPLETE; 
	this.STATUS_ERROR;

	// 진행상태 UI반전을 위해서 	
	this.v1 = 0;	
	
	// 결과를 출력할 object;
	this.outObj;
	
	// 페이지 미리보기 	
	this.view = function(url, fdate){
		if(fdate == null){
			fdate = "";
		}
		jf_openWindow(this.viewpage_url+"?url="+encodeURIComponent(this.url) + "&fdate="+fdate, 'docuemntView', 'yes', 'yes', 1100, 880, 1, 1, 1);
	}
	
	// 이벤트 처리
	this.viewProc = function(responseXML){
		// 서버측에서 보낸 응답 처리
		// 전달 받은 데이터를 이용하여 UI 처리 	
		var xml = responseXML.getElementsByTagName("result");
		
		// 접속 에러 
		if(xml[0]==null){
			this.error(404);
			return;
		}		
		var result = xml[0].childNodes[0].nodeValue;
		var out;
		switch(result){
			case this.STATUS_WAIT:
				out = "";
				break;
			case this.STATUS_PDFCONVERTING:
				if(prgress==3){
					out = "";
					this.call = false;
					break;
				}
				out = "변환중";
				prgress = prgress + 1;
				break;
			case this.STATUS_IMAGECONVERTING:
				if(prgress==5){
					out = "";
					this.call = false;
					break;
				}
				if(this.v1 ==0){
					this.v1 =1;
					out = "변환중..";
				}
				else{
					this.v1 = 0;
					out = "변환중..."
				}
				prgress = prgress + 1;
				break;
			case this.STATUS_COMPLETE:
				out = "<a href=\"#\" onclick=\"";
				if(	this.fdate ==null){
					out += this.instance+".view('"+this.url+"');"; 
				}
				else{
					out += this.instance+".view('"+this.url+"','"+this.fdate+"');";
				}
				out += "return false;\" onkeypress=''>PDF로 보기</a>";
				
				this.call = false;
				break;
			case this.STATUS_ERROR:
				out = "";
				this.call = false;
				break;
		}
		if(this.outObj!=null){
			this.outObj.innerHTML=out;	
		}
	}
	
	// 서버 접속 에러 처리 
	this.error = function(status){
		this.outObj.innerHTML="::<font color='white'>" + status + "</font>";
//		this.outObj.innerHTML="::";
		this.call = false;	
	}	
	
	// 진행사항 체크 
	this.refresh = function(){
		if( this.call){
			var param = "url=" + encodeURIComponent(this.url) + "&fdate=" + this.fdate;  
			var result = jf_xmlHttpPostUserError(this.progress_url, param, 
					this.instance+".viewProc", this.instance+".error", true);
	
			if(result == -1){
				//alert("접속 에러");
				this.call = false;
			}
			else{
				setTimeout(this.instance+".refresh()", this.refreshTime);
			}
		}
	}
}

