/*
 * cookie library
 * 쿠키생성, 제거 등의 메소드들을 정의한다.
 *
 * @author Seokhee Choi, dantes98@gmail.com
 * @version 1.0, since 2008/01/15
 */

function setExpire(expire, id)
{
	segio_set_Cookie(id, "pcookie", expire);
	hide_div(id);
}

function segio_set_Cookie(name, value, expiredays)
{
	var today = new Date();
	var expires = new Date();
	var hour = 23 - today.getHours();
	var minute = 59 - today.getMinutes();
	var second = 59 - today.getSeconds();

	expires.setTime(today.getTime() + 1000*60*60*hour + 1000*60*minute + 1000*second);

	document.cookie = name + "=" + escape( value ) + ";path=/;expires=" + expires.toGMTString() + ";";
}

/** 현재 도메인을 리턴 */
function get_current_domain(){
	var Dns;
	Dns=location.href;
	Dns=Dns.split("//");
	Dns=Dns[1].substr(0,Dns[1].indexOf("/"));
	return Dns;
}

function segio_get_Cookie(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 checkCookie(name)
{
	if(segio_get_Cookie(name) == "")
		return false;
	else
		hide_div(name);
}

/*
 * Div library
 * div 현태의 팝업에 대해 생성, 삭제, 이동에 관한 메소드들을 정의한다.
 *
 * @author SeokHee Choi, dantes98@gmail.com
 * @version 1.0, since 2008/01/15
 */

var selected_div = null;
var isDragin = false;
var isClick = false;
var offsetX, offsetY;
var num = 0;
var popup_zIndex = 2020;

/** 팝업창을 이동시키기 위해 id를 선택한다. */
function select_id(id)
{
    selected_div = document.getElementById(id);
	addIndex();
	document.onmousedown = mousedown;
}

/**
 * 단순히 addIndex() 만을 호출하기 위해 id를 선택한다.
 * 이 이벤트 이후에는 파업창이 이동하지 않아야 한다.
 */
function select_id2(id)
{
    selected_div = document.getElementById(id);
	addIndex();
	isClick = true;
}

function mousedown(e)
{
	if(selected_div == null || selected_div == 'undefined')
		return;
	if(isClick == true)
	{
		isClick = false;
		return;
	}

	if(navigator.appName.charAt(0) == 'N')
	{
    	offsetX = e.clientX - getObjectLeft(selected_div) ;
    	offsetY = e.clientY - getObjectTop(selected_div) ;
	}
	else
	{
    	offsetX = event.clientX - getObjectLeft(selected_div) ;
    	offsetY = event.clientY - getObjectTop(selected_div) ;
	}
    isDragin = true;

	document.onmousemove = mousemove;
}

function mousemove(e)
{
	document.onmousedown = null;

	if(isDragin)
	{
		var X = 0, Y = 0;
		if(navigator.appName.charAt(0) == 'N')
		{
			X = (e.clientX-offsetX)+"px";
			Y = (e.clientY-offsetY)+"px";
		}
		else
		{
			X = (event.clientX-offsetX)+"px";
			Y = (event.clientY-offsetY)+"px";
		}
		selected_div.style.left = X;
		selected_div.style.top = Y;
	}

	document.onmouseup = mouseup;

	return false;
}

/** 오브젝트의 left 위치 */
function getObjectLeft(obj)
{
	if(navigator.appName.charAt(0) != 'N')
	{
		return obj.offsetLeft;
	}
	else
	{
		if(obj.offsetParent == document.body)
		{
			return obj.offsetLeft;
		}
		else
		{
			return obj.offsetLeft + getObjectLeft(obj.offsetParent);
		}
	}
}

/** 오프젝트의 top 위치 */
function getObjectTop(obj)
{
	if(navigator.appName.charAt(0) != 'N')
	{
			return obj.offsetTop;
	}
	else
	{
		if(obj.offsetParent == document.body)
			return obj.offsetTop;
		else
			return obj.offsetTop + getObjectTop(obj.offsetParent);
	}
}

function mouseup(e)
{
	isDragin = false;
	selected_div = null;

	if((typeof mouseChk) == 'function'){
		document.onmousemove = null;
		document.onmousedown = mouseChk;
	}
	else{
		document.onmousemove = null;
	}
	document.onmousemove = null;
	return;
}

/** 여러개의 팝업이 있는 경우, 클릭시 팝업의 우선순의를 높힌다. */
function addIndex()
{
	if(selected_div != null)
	{
		popup_zIndex += 1;
		selected_div.style.zIndex = popup_zIndex;
	}
}

/** 팝업창을 닫는다. */
function hide_div(id)
{
	var div = document.getElementById('main_popup');
	div.removeChild(document.getElementById(id));
}

/**
 * 팝업창을 닫는다.
 * 일반적인 방법보다는 멋지게 사라지게 한다. 
 */
function toggleMultimedia(id){
	var textFade = document.getElementById(id);
	var parentNode = textFade.parentNode;

	// 원래는 밑의 방식이 맞으나 강제팝업의 경우, 통합공지와 같이 사라지므로 사용하지 않는다.
	//document.getElementsByTagName('body')[0].removeChild(parentNode);
	parentNode.removeChild(textFade);
/*	
	if(textFade.filters(0).status == 2){ 
		textFade.filters(0).Stop(); 
		
	if(textFade.style.display == "none") 
		textFade.style.display = "block"; 
	else 
		textFade.style.display = "none"; 		
		window.setTimeout("toggleMultimedia(id)", 1); 
	}
	 
	textFade.filters(0).Apply(); 
	
	if(textFade.style.displsy == "none") 
		textFade.style.display = "block"; 
	else 
		textFade.style.display = "none"; 
		
	textFade.filters(0).Play();
	*/
} 

/** 현재페이지의 주소를 반환한다.*/
function getLocation()
{
	var location = document.location.href;
	location = location.substring(7);

	var index = location.indexOf('/');
	location = location.substring(index);

	return location;
}


/** 
 * 토탈공지의 제목을 클릭하면 해당하는 팝업을 보여준다.  
 * 이 메소드는 ajax.js 가 먼저 호출되어야 한다.
 */
function showPopupId(id)
{
	var _id = document.getElementById(id);

	// 기존에 이미 떠있는 경우에는 호출하지 않는다.
	if(_id != null)
		return;

	var current_path = getLocation();

	// 팝업호출시에는 파라메터값으로 현재 페이지의 주소를 넘겨준다.
	// 서버에서는 페이지 주소와 일치하는 팝업을 반환하여 준다.
	$GET("/segio/popup/popup_insert.php?mode=call_popup&fname="+id+"&current_path="+current_path, function(text){
		var _body = document.getElementsByTagName('body')[0];
		var para = document.createElement("div");
		para.setAttribute("id", "pPopup"+num);
		para.innerHTML = text;
		_body.appendChild(para);
		num++;
	});

	var popupdiv = document.getElementById('popupdiv');
	if(popupdiv)
		popupdiv.style.zIndex = 0;
}

/*
 * Ajax library
 *
 * @author Seokhee Choi, dantes98@gmail.com
 * @version 1.0, since 2008/01/15
 */

/** 브라우저에 맞게 XmlHttpRequest 를 반환한다. */
function createXmlRequest()
{
	if(window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP");
	else
	{
		if(window.XMLHttpRequest)
			return new XMLHttpRequest();
	}
	return null;
}

function $getId(id)
{
	return document.getElementById(id);
}

function $write(id, content)
{
	var obj = $getId(id);
	if(obj != null)
		obj.innerHTML = content;
}

function $append(id, content)
{
	var obj = $getId(id);
	if(obj != null)
	{
		obj.innerHTML += content;
	}
}

function $getHTML(id)
{
	var obj = $getId(id);
	if(obj != null)
		return obj.innerHTML;
	return null;
}

function $GET(uri, action)
{
	var obj = createXmlRequest();
	obj.onreadystatechange = function(){

		// 4 : loaded, 200 : response OK
		if(obj.readyState == 4 && obj.status == 200)
		{
			if(typeof(action) == 'function')
				action(obj.responseText);
			else
			{
				if(typeof(action) == 'string')
				{
					var result = obj.responseText;
					eval(action);
				}
			}
		}
	};
	obj.open('get', uri);
	obj.send(null);
}

