/**
 * div layer popup utility
 *
 * usage : (new LayerDivPopup("url", "20090920", "20091005", "_delayDeliveryPopup", 385, null)).init();
 *
 * required library : jquery core
 *
 * @param paramUrl : 읽어올 content url
 * @param paramStartDt : yyyyMMdd 형식으로 지정된 날짜(시작일포함)부터 노출된다. 지정하지 않으면 체크하지 않는다.
 * @param paramFinishDt : yyyyMMdd 형식으로 지정된 날짜(종료일포함)까지 노출된다. 지정하지 않으면 체크하지 않는다.
 * @param paramCookieName : 쿠키 값을 체크하여 해당날까지는 노출하지 않는다. ( '오늘그만보기' 기능에서 사용 )
 * @param paramOffsetX : div 의 left, 지정하지 않으면 설정하지 않는다.
 * @param paramOffsetY : div 의 top, 지정하지 않으면 설정하지 않는다.
 * @param paramWidth : div 의 width, 지정하지 않으면 설정하지 않는다.
 * @param paramHeight : div 의 height, 지정하지 않으면 설정하지 않는다.
 *
 */

function LayerDivPopup(
		paramUrl,
		paramStartDt,
		paramFinishDt,
		paramCookieName,
		paramOffsetX,
		paramOffsetY,
		paramWidth,
		paramHeight ) {

	/** attributes */
	this.divObj = null;

	/** functions */
	this.init = function() { // 초기화
		this.initParamameter();

		if ( this.checkValidPopup() ) {
			this.createLayerDiv();
			this.loadContent();
		} else {
			// 보여줄 필요가 없으면 아무것도 하지 않는다.
		}
	};
	this.initParamameter = function() {
		if ( isNaN(paramOffsetX) ) paramOffsetX = 0;
		if ( isNaN(paramOffsetY) ) paramOffsetY = 0;
		if ( isNaN(paramWidth) ) paramWidth = 0;
		if ( isNaN(paramHeight) ) paramHeight = 0;
		if ( paramStartDt == null || paramStartDt.length != 8 ) paramStartDt = "";
		if ( paramFinishDt == null || paramFinishDt.length != 8 ) paramFinishDt = "";
//		alert("paramOffsetX : '" + paramOffsetX + "'");
//		alert("paramOffsetY : '" + paramOffsetY + "'");
//		alert("paramWidth : '" + paramWidth + "'");
//		alert("paramHeight : '" + paramHeight + "'");
//		alert("paramStartDt : '" + paramStartDt + "'");
//		alert("paramFinishDt : '" + paramFinishDt + "'");
	};

	this.createLayerDiv = function() {
		this.divObj = $("<div style=\"display:none; position:absolute; left:"
				+ paramOffsetX + "pt; top:" + paramOffsetY + "pt; z-index:1002;\"></div>");
		$(document.body).append(this.divObj);
	};

	this.initDivContent = function() { // div내의 버튼 초기화
		var dummyThis = this;

		if ( paramWidth > 0 ) this.divObj.width(paramWidth);
		if ( paramHeight > 0 ) this.divObj.height(paramHeight);

		// noMoreDivToday 클래스 속성 : 클릭시 쿠키정보 저장 후 닫기
		$(".noMoreDivToday", this.divObj).click(function() {
			dummyThis.setNoMoreCookie();
			dummyThis.hide();
		});
		// closeDiv 클래스 속성 : 클릭시 div 숨기기
		$(".closeDiv", this.divObj).click(function() {
			dummyThis.hide();
			return false;
		});
	};
	this.checkValidPopup = function() { // 날짜 및 쿠키정보 체크하여 조건에 맞으면 보여준다.
		// 날짜체크
		var curDateStr = this.getTodayStr();
		if ( (paramStartDt != "" && curDateStr < paramStartDt)
				|| (paramFinishDt != "" && paramFinishDt < curDateStr) ) return false;

		// 쿠키체크
		var cookieDateStr = this.getNoMoreCookie();
		if ( cookieDateStr != "" && cookieDateStr >= curDateStr ) return false;

		return true;
	};
	this.loadContent = function() { // 내용을 읽어온다.
		var dummyThis = this;
		this.divObj.load(paramUrl, null, function() {
			dummyThis.initDivContent();
			dummyThis.show();
			$(".popup").draggable();
		});
	};

	/** util function */
	this.getTodayStr = function() {
		var curDate = new Date();
		var curYear = curDate.getFullYear();
		var curMonth = curDate.getMonth() + 1;
		var curDay = curDate.getDate();
		return "" + curYear + (curMonth<10?"0":"") + curMonth + (curDay<10?"0":"") + curDay;
	};
	this.setNoMoreCookie = function() { // 오늘 날짜를 쿠키에 설정
		document.cookie = paramCookieName + "=" + this.getTodayStr();
	};
	this.getNoMoreCookie = function() { // 쿠키값 조회. 날짜형식으로 들어있다.
		var cookie = document.cookie;
		var first = cookie.indexOf(paramCookieName + "=");
		if ( first >= 0 ) {
			var str = cookie.substring(first, cookie.length);
			var last = str.indexOf(";");
			if ( last < 0 ) {
				last = str.length;
			}
			str = str.substring(0, last).split("=");
			if ( str[1].length != 8 ) return "";
			else return str[1];
		} else {
			return "";
		}
	};

	/** action function */
	this.show = function() { // 보이기
		this.divObj.show();
	};
	this.hide = function() { // 숨기기-객체제거
		this.divObj.remove();
	};

	this.init();
}
