/**
 * 설명		: DIV 레이어 팝업
 * 작성자	: 비즈미디어 개발팀 임병선
 * 인코딩	: UTF-8
 * 작성일	: 2011.05.09
 * 수정일	: 2011.09.23
**/

/**
 * 오브젝트 생성
**/
var __bizPopup = function (agDivId)
{
	this.divID			= agDivId;			//생성할 팝업 DIV ID
	this.addPosX		= "0";				//(가로축)px 단위로 입력, minus(-) 값은 중앙에서 왼쪽으로 이동
	this.addPosY		= "0";				//(세로축)px 단위로 입력(최솟값 0)
	this.pageWidth		= "0";
};

/**
 * 초기 설정
**/
__bizPopup.prototype.config = function (agX, agY)
{
	this.addPosX = agX;
	this.addPosY = agY;
};

/**
 * resize 이벤트 발생시 DIV 위치를 재설정
**/
__bizPopup.prototype.resetPosition = function ()
{
	var targetID;

	this.resetPageWidth();

	targetID = document.getElementById(this.divID).style;
	if(this.pageWidth < contentWidth)
	{
		targetID.left = ((parseInt(contentWidth) / 2) + parseInt(this.addPosX))+"px";
	}
	else
	{
		targetID.left = ((parseInt(this.pageWidth) / 2) + parseInt(this.addPosX))+"px";
	}
	targetID.top = this.addPosY+"px";
};

/**
 * 브라우저 가로사이즈를 업데이트함
**/
__bizPopup.prototype.resetPageWidth = function ()
{
	this.pageWidth = this.getClientWidth();
};

/**
 * 브라우저 가로사이즈를 구함
**/
__bizPopup.prototype.getClientWidth = function ()
{
	return document.body.clientWidth;
};

/**
 * 최상위 Z-INDEX 변경
**/
__bizPopup.prototype.setTopZIndexExchange = function (divPopup)
{
	var maxnum = 0;
	var applyZIndex;
	for(key in divPopup)
	{
		if(maxnum < $("#"+key).css("z-index"))
		{
			maxnum = $("#"+key).css("z-index");
		}
	}
	applyZIndex = parseInt(maxnum) + 1;

	$("#"+this.divID).css("z-index", applyZIndex);
};

/**
 * 레이어 드래그 후의 DIV 위치값 재설정
**/
__bizPopup.prototype.setPosAfterDrag = function (contentWidth, offsetLeft, offsetTop)
{
	var applyPosX,contentHalf;

	this.resetPageWidth();

	contentHalf = parseInt(contentWidth / 2);

	if(this.pageWidth <= contentWidth)
	{
		if(offsetLeft <= contentHalf)
		{
			applyPosX = contentHalf - offsetLeft;
			applyPosX = "-"+applyPosX;
		}
		else
		{
			applyPosX = offsetLeft - contentHalf;
			applyPosX = applyPosX;
		}
	}
	else
	{
		var pageHalf = parseInt(this.pageWidth / 2);
		if(offsetLeft <= pageHalf)
		{
			applyPosX = pageHalf - offsetLeft;
			applyPosX = "-"+applyPosX;
		}
		else
		{
			applyPosX = offsetLeft - pageHalf;
			applyPosX = applyPosX;
		}
	}
	this.addPosX = applyPosX;
	this.addPosY = offsetTop;
};
/* @오브젝트 끝 */


/* 쿠키정보 설정 */
function SetCookieInfo(name, value, expiredays)
{
	var todayDate = new Date();
	todayDate.setDate( todayDate.getDate() + expiredays );
	document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";";
}

/* 쿠키정보 가져오기 */
function GetCookieInfo(name)
{
	var nameOfCookie = name + "=";
	var x = 0;
	while ( x <= document.cookie.length )
	{
		var y = (x+nameOfCookie.length);
		if ( document.cookie.substring( x, y ) == nameOfCookie ) {
			if ( (endOfCookie=document.cookie.indexOf( ";", y )) == -1 )
					endOfCookie = document.cookie.length;
					return unescape( document.cookie.substring( y, endOfCookie ) );
		}
		
		x = document.cookie.indexOf( " ", x ) + 1;
		
		if ( x == 0 )
			break;
	}
	return "";
}

/* 다시 열지않기 설정 */
function divPopupClose(agDivId)
{
	SetCookieInfo(agDivId, "Y", 1);
	document.getElementById(agDivId).style.display = "none";
}

/* 팝업 보여주기 */
function showPopup(arr)
{
	var cookieVal = "";
	for(key in arr)
	{
		cookieVal = GetCookieInfo(key);
		if(cookieVal != "Y")
		{
			eval(key+'.resetPosition()');
			document.getElementById(key).style.display = "block";
		}
	}
}

/* #오브젝트 생성 */
var tempVar = Array();
for(key in divPopup)
{
	tempVar = divPopup[key].split(",");

	//오브젝트 생성 및 설정
	eval('var '+key+' = new __bizPopup("'+key+'");');
	eval(key+'.config("'+tempVar[0]+'","'+tempVar[1]+'");');
}

$(document).ready(function (){

	/* #이벤트 설정 */
	$(window).resize(function (){
		for(key in divPopup)
		{
			eval(key+'.resetPosition()');
		}
	});
	showPopup(divPopup);

	for(divId in divPopup)
	{
		eval('$("#'+divId+'").drag(function(e, obj){$(this).css({top: obj.offsetY,left: obj.offsetX});}).mouseup(function (){'+divId+'.setPosAfterDrag(contentWidth,$(this).offset().left,$(this).offset().top);}).mousedown(function(){'+divId+'.setTopZIndexExchange(divPopup);});');
	}
});
