﻿/***************************************************************************************
******************************** 팝업 드래그 레이어 *************************************
****************************************************************************************/

// 브라우저 버전별 체크
var ns4=document.layers
var ie4=document.all
var ns6=document.getElementById&&!document.all

// 넷스케이프 계열의 낮은 버전 브라우저를 위한....
var dragswitch=0;
var nsx;
var nsy;
var nstemp;

function drag_dropns(name){
	if (!ns4)
		return;
	temp=eval(name);
	temp.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP);
	temp.onmousedown=gons;
	temp.onmousemove=dragns;
	temp.onmouseup=stopns;
}

function gons(e){
	temp.captureEvents(Event.MOUSEMOVE);
	nsx=e.x;
	nsy=e.y;
}

function dragns(e){
	if (dragswitch==1){
		temp.moveBy(e.x-nsx,e.y-nsy);
		return false;
	}
}

function stopns(){
	temp.releaseEvents(Event.MOUSEMOVE);
}
// 넷스케이프 계열 낮은 버전 브라우저를 위한.... 끝


// 크로스브라우저의 높은 버전을 위한 변수 정의
var crossobj;
var tempx;
var tempy;
var offsetx;
var offsety;

// 마우스 드래그에 의한 레이어 움직임 통제
function drag_drop(e){
	if (ie4&&dragapproved){
		crossobj.style.left=tempx+event.clientX-offsetx;
		// 움직인레이어의 왼쪽  = 원래위치 + 현재마우스위치 - 눌렀을당시 마우스위치  즉 원래레이어위치에서 마우스가 움직인만큼만 더해라
		crossobj.style.top=tempy+event.clientY-offsety;
		return false;
	}
	else if (ns6&&dragapproved){
		crossobj.style.left=tempx+e.clientX-offsetx+"px";
		crossobj.style.top=tempy+e.clientY-offsety+"px";
		return false;
	}
}


//크로스브라우징을 위해 각 변수가 정의
function initializedrag(e){
	// 크로스브라우징을 위한 오브젝트 선택 getElementById를 쓸것인지 all.아이디명을 쓸건인지
	crossobj=ns6? document.getElementById("drag_popup") : document.all.drag_popup;

	// 이벤트가 일어난 오브젝트 선택
	var firedobj=ns6? e.target : event.srcElement;

	// 브라우저별 최상위 오브젝트 이름과 DTD 정의에 따라 body, documentElement의 차이
	var topelement=ns6? "html" : document.compatMode && document.compatMode!="BackCompat"? "documentElement" : "body";


	// while 과 if 문은 어떤 효용이 있는지 잘 모르겠다. 팝업을 움직이는데는 꼭 필요하지는 않다.
	// while문 전체와 if의 조건을 제거해도 효과에 이상은 없다
	while (firedobj.tagName!=topelement.toUpperCase() && firedobj.id!="dragbar"){
		firedobj=ns6? firedobj.parentNode : firedobj.parentElement;
	}

	if (firedobj.id=="dragbar"){
		offsetx=ie4? event.clientX : e.clientX;
		offsety=ie4? event.clientY : e.clientY;
		tempx=parseInt(crossobj.style.left);
		tempy=parseInt(crossobj.style.top);

		dragapproved=true;
		document.onmousemove=drag_drop;
	}
}

// 문서어디에서 마우스를 떼도 더이상 드레그 되지 않도록
document.onmouseup=new Function("dragapproved=false");


// 창닫기와 쿠기 발급
function popupClose(e,myCookie) {
	var obj=ns6? e.target : event.srcElement;
	while(obj.tagName != "DIV") {
		obj=ns6? obj.parentNode : obj.parentElement;
	}
	obj.style.display = 'none';

	//하루 한번만 열기
	if(myCookie) {
		setCookie(myCookie,'ippo',1);
	}
}

// 현재 보유 쿠기 검사
function getCookie( name ) {

	// 현재 웹사이트에 맞는 모든 쿠기를 cookieName=cookieValue; cookieName=cookieValue; cookieName=cookieValue 형태로 저장된다.
	var allCookie = document.cookie;

	// cookieValue 만 리턴 시키기 위해서 찾는 문자가 " cookieName= "를 찾게한다.
	var nameOfCookie = name + "=";

	// allCookie에서 cookieName과  합치하는 cookieValue 를 substring(시작점,끝점)으로 가져올거라 최초 시작점 0으로 설정
	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 ) { //allCookie의 맨 마지막 자리가 일때와 아닐때의 끝자리 
				endOfCookie = document.cookie.length;
			}
			
			//끝점이 시작자리가 되고 위에서 구한 끝자리까지의 값이 cookieValue가 된다. 
			return unescape( document.cookie.substring( y, endOfCookie ) );
		}
		
		// 일치하는 쿠기이름이 아닐때 쿠기와 다음쿠기의 구분은 한칸 뛰는 것으로 구분하여 다음 쿠키의 시작점을 잡는다.
		x = document.cookie.indexOf( " ", x ) + 1;

		// 쿠기가 하나도 없으면 while문을 벗어나라
		if ( x == 0 ) break;
	}

	// 쿠기가 없다면 빈값을 리턴한다.
	return "";
}

//새로운 쿠기 등록
function setCookie( name, value, expiredays ) {

	// 오늘 날짜를 구하고
	var todayDate = new Date();

	// 오늘 날짜에 ?일을 더하고
	// 여기서 시간단위를 자유롭게 설정할 수 있다.
	todayDate.setDate( todayDate.getDate() + expiredays );
	
	//쿠키를 새롭게 등록한다.
	document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";"

	//setCookie 에서 값을 escape하고 getCookie에서 unescape 하는 것을 주의하자
}