/*
 * 팝업창 구동 스크립트를 모아놓았다.
 *
 * YUI_domevent.js, YUI_dragdrop이 필요하다.
 *
 */

//-- Drag&Drop 객체 (Yahoo UI)
var pupObject = new Array();

//-- 마지막으로 드래그한 녀석
var pupLastDrag = -1;

//-- 팝업 드래그 초기화
function pupInit(){
	//-- 모든 객체에 대해
	for(var i = 0 ; i < pupObject.length ; i++){
		//-- 화면스크롤 금지
		pupObject[i].scroll = false;
		//-- 드래그 시작시
		pupObject[i].startDrag = function(x,y){
			pupLastDrag = parseInt(this.id.substr(3,32));
			this.getEl().style.cursor = "move";
		}
		//-- 드래그중 상한좌표 설정
		//pupObject[i].onDrag = function(e){
		//	if(parseInt(this.getEl().style.top) < 120)
		//		this.getEl().style.top = 120;
		//}
		//-- 마우스 다운 (드래그 전)
		pupObject[i].onMouseDown = function(e){
			var n = parseInt(this.id.substr(3,32));
			if(_pupSetFront(parseInt(this.id.substr(3,32))))
				pupLastDrag = n;
			else	pupLastDrag = -1;
		}
		//-- 마우스 업 (드래그 후)
		pupObject[i].onMouseUp = function(e){
			this.getEl().style.cursor = "auto";
		}
		//-- 상한좌표?
		//pupObject[i].setYConstraint(50*i, 600-50*i);
	}
	_pupSetFront(pupObject.length-1);
}

//-- n번 객체를 가장 앞으로 이동한다.
function _pupSetFront(n){
	var obj = pupObject[n].getEl();
	if((obj.style.zIndex-10) == pupObject.length)	return(false);	//-- 이동되지 않았음 (각 obj의 zindex는 10+순번 이다)
	for(var i = 0 ; i < pupObject.length ; i++){
		var tmpobj = pupObject[i].getEl();
		if(tmpobj.style.zIndex > obj.style.zIndex)
			tmpobj.style.zIndex--;
		tmpobj.style.filter = "progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)";
	}
	obj.style.zIndex = pupObject.length + 10;
	obj.style.filter = "progid:DXImageTransform.Microsoft.BasicImage(grayscale=0)";
	return(true);	//-- 앞으로 옮겨졌다.
}

//-- n번 객체에 대해 클릭이 유효한 경우 link를 연다.
//-- 드래그 직후의 click 이벤트는 무시됨
//-- ZIndex 변경 click 이벤트는 무시됨
//-- 즉, 제일 위에 올려 있는 팝업은 순수하게 클릭한 경우만 처리된다.
function pupClick(n, link, target){
	if(pupLastDrag != n)
		window.open(link, target);
}

//-- 오늘 더 열지 않음 클릭
function pupClickBore(idx, checked){
	var todayDate = new Date();
	if(checked)	_setCookie("PUP" + idx, "1", (24-todayDate.getHours())*3600);
	else		_setCookie("PUP" + idx, "");

}

//-- 닫기 클릭
function pupClose(n){
	pupObject[n].getEl().style.display = "none";
}

