
var newLayerZ = 99;

function viewNewLayer(evt,id,url,parent_div,title,left,top,width,height,closex,complete) {
	
	//alert(evt.clientY + document.body.scrollTop);
	//#overlay {   visibility: hidden; position: absolute; left: 0px; top: 0px; width:100%; height:100%; text-align:center; z-index: 1000;}
	var evtTop = 0;
	if(top < 0 && evt != null) evtTop = evt.clientY + $(parent_div).scrollTop + top;
	
	if($(id) == null) {
	
		var newdiv = new Element('div');
		newdiv.id = id;
		
		newdiv.setStyle({
			position : 'absolute',
			background : 'white',
			border : '1px solid black',
			margin : '0 auto',
			padding : '2px',
			display : 'none',
			height : 'auto',
		    width : 'auto'
			});
		
		$(parent_div).insert(newdiv);
		
		newLayerZ ++;
		
		if(closex) {
		
			newdiv.innerHTML=
				"<div id='"+id+"_CapLayer' style='position:relative;padding:2px;height:17px;width:"+width+"px;background-color:black;'>"+
				"<div id='"+id+"_NameLayer' style='float:left;width:auto;height:17px;color:white;font-size:15px;font-weight:bold;text-align:left;overflow:hidden;'>"+title+"</div>"+
				"<div style='float:right;width:20px;height:17px;color:white;font-size:15px;font-weight:bold;text-align:right;'>"+
				"<span id='"+id+"_spanInLayer' class='clickable'>X</span></div></div>"+						
				"<div id='"+id+"_InLayer' style='position:relative;background-color:white;border:0px solid red;width:auto;height:auto;'></div>";
			
				$(id+'_spanInLayer').onclick = function() {$('' + id + '').hide();};
		
		} else {

			newdiv.innerHTML=
				"<div id='"+id+"_CapLayer' style='padding:2px;height:17px;width:"+width+"px;background-color:black;'>"+
				"<div id='"+id+"_NameLayer' style='float:left;width:auto;height:17px;color:white;font-size:15px;font-weight:bold;text-align:left;overflow:hidden;'>"+title+"</div></div>"+
				"<div id='"+id+"_InLayer' style='position:relative;background-color:white;border:0px solid red;'></div>";
			
		}
		
	}
	
	newdiv = $(id);
	
	newdiv.style.left = left+"px";
	newdiv.style.top = top+"px";
	newdiv.style.minWidth = width+"px";
	newdiv.style.width = "auto";
	newdiv.style.minHeight = height+"px";
	newdiv.style._height = height+"px";
	newdiv.style.height = "auto";
	newdiv.style.zIndex = newLayerZ;
	
	if(top < 0) {
		newtop = (evtTop - newdiv.offsetHeight);
		if(newtop < 0) newtop = 0;
		newdiv.style.top = newtop+"px";
	} else
		newdiv.style.top = top+"px";
	
	if(newdiv.style.display == 'none') newdiv.show();
	
	var divInLayer = $(id+'_InLayer');
	var divCapLayer = $(id+'_CapLayer');
	
	new Ajax.Updater(divInLayer, url, { method: 'get',
		evalScripts: true,
		onComplete:function(){			
			//newdiv.style.width=(divInLayer.offsetWidth+6)+"px";
		 	//newdiv.style.height=(divInLayer.offsetHeight+21)+"px";
		 	//divCapLayer.style.width = (divInLayer.offsetWidth)+"px";
			if(typeof(complete)=='function') complete();
		}
	});
	
}


function viewNewLayerModal(evt,id,url,parent_div,title,left,top,width,height,closex) {
	
	//alert(evt.clientY + document.body.scrollTop);
	//#overlay {   visibility: hidden; position: absolute; left: 0px; top: 0px; width:100%; height:100%; text-align:center; z-index: 1000;}

	var evtTop = 0;
	if(top < 0 && evt != null) evtTop = evt.clientY + $(parent_div).scrollTop + top;

	if($(id) == null) {
	
		var modal = new Element('div');
		modal.id = id;
		//modal.class = 'modal';
		
		modal.setStyle({
			position : 'absolute',
			left : '0px',
			top : '0px',
			width : '100%',
			height : '100%',
			display : 'none'
			});
		
		$(parent_div).insert(modal);
		newLayerZ ++;
		modal.style.zIndex = newLayerZ;
		modal.style.backgroundImage = "url('/hub4/img/modal_bg.png')";
		
		modal.show();
		
		if(closex) {
		
			modal.innerHTML=
				"<div id='"+id+"Layer' style='visibility:hidden;position:absolute;background-color:white;border:0px solid red;padding:2px;text-align:center;'>"+
				"<div style='position:relative;padding:2px;min-height:17px;_height:17px;_width:"+(width-4)+"px;background-color:black;'>"+
				"<div style='position:absolute;top:1px;left:1px;width:"+(width-24)+"px;min-height:17px;_height:17px;color:white;font-size:15px;font-weight:bold;text-align:left;overflow:hidden;'>"+title+"</div>"+
				"<div style='position:absolute;top:1px;right:2px;width:20px;min-height:17px;_height:17px;color:white;font-size:15px;font-weight:bold;text-align:right;'>"+
				"<span id='"+id+"_spanInLayer' class='clickable'>X</span></div></div>"+
				"<div id='"+id+"_InLayer' style='position:relative;background-color:white;border:0px solid red;padding:0px;'></div></div>";
			
				$(id+'_spanInLayer').onclick = function() {$('' + id + '').hide();};
		
		} else {

			modal.innerHTML=
				"<div id='"+id+"Layer' style='visibility:hidden;position:absolute;background-color:white;border:0px solid red;padding:2px;'>"+
				"<div style='padding:2px;min-height:17px;_height:17px;width:"+(width-4)+"px;background-color:black;'>"+
				"<div style='float:left;width:"+(width-4)+"px;min-height:17px;_height:17px;color:white;font-size:15px;font-weight:bold;text-align:left;overflow:hidden;'>"+title+"</div></div>"+
				"<div id='"+id+"_InLayer' style='position:relative;background-color:white;'></div></div>";
			
		}
		
	}
	
	var newdiv = $(id+'Layer');
	
	newdiv.style.left = left+"px";
	newdiv.style.minWidth = width+"px";
	newdiv.style._width = width+"px";
	newdiv.style.width = "auto";
	newdiv.style.minHeight = height+"px";
	newdiv.style._height = height+"px";
	newdiv.style.height = "auto";
	newLayerZ++;
	newdiv.style.zIndex = newLayerZ;
	
	if(top < 0) {
		newtop = (evtTop - newdiv.offsetHeight);
		if(newtop < 0) newtop = 0;
		newdiv.style.top = newtop+"px";
	} else
		newdiv.style.top = top+"px";			
	
	
	newdiv.style.visibility="visible";
	
	newdiv.show();
	
	var divInLayer = id+'_InLayer';
	
	new Ajax.Updater(id+'_InLayer', url, { method: 'get',
		evalScripts: true
		//onComplete:function(){
			//$(newdiv).style.width=($(divInLayer).offsetWidth+6)+"px";
			//$(newdiv).style.height=($(divInLayer).offsetHeight+21)+"px";
		//}			
	});
	
	if($(id).style.display == 'none') $(id).show();
	if(newdiv.style.visibility='hidden') newdiv.style.visibility="visible";
	
}

function updateNewLayer(id,url) {
	
	var newdiv = $(id);
	newdiv.style.visibility="visible";
	newdiv.show();
	
	var divInLayer = id+'_InLayer';
	
	new Ajax.Updater(id+'_InLayer', url, { method: 'get',
		evalScripts: true
		//onComplete:function(){
			//$(newdiv).style.width=($(divInLayer).offsetWidth+6)+"px";
			//$(newdiv).style.height=($(divInLayer).offsetHeight+21)+"px";
		//}			
	});
	
	if($(id).style.display == 'none') $(id).show();
	if(newdiv.style.visibility='hidden') newdiv.style.visibility="visible";
	
}

function updateNewLayerModal(id,url) {
	
	var newdiv = $(id+'Layer');
	newdiv.style.visibility="visible";
	newdiv.show();
	
	var divInLayer = id+'_InLayer';
	
	new Ajax.Updater(id+'_InLayer', url, { method: 'get',
		evalScripts: true
		//onComplete:function(){
			//$(newdiv).style.width=($(divInLayer).offsetWidth+6)+"px";
			//$(newdiv).style.height=($(divInLayer).offsetHeight+21)+"px";
		//}			
	});
	
	if($(id).style.display == 'none') $(id).show();
	if(newdiv.style.visibility='hidden') newdiv.style.visibility="visible";
	
}

function hideLayer(divname) {
	
	$(divname).hide();
	
}

/* for LOGIN/LOGOUT */

function login(){
	
	var frm = document.loginForm;
	var id = frm.id.value;
	var passwd = frm.passwd.value;
	
		if(frm.id.value==""){
		alert("아이디를 입력하세요.");
		frm.id.focus();
		return;
		}if(frm.passwd.value==""){
		alert("비밀번호를 입력하세요.");
		frm.passwd.focus();
		return;
		}
		
		frm.action = "/hub4/indi/login_process.jsp";
		
		//frm.request({
		//	  onComplete: function(Response){ login_post(Response); }
		//	});
		
		var url = "/hub4/indi/login_process.jsp?id="+id+"&passwd="+passwd;
		
		new Ajax.Request(url, {
			  onComplete: function(Response){ login_post(Response); }
		});
}

function login_post(Response) {
	
	//alert(Response.responseText+'|');

	if(Response.responseText == 'ok') {
		hideLayer('login');
	    refreshTopLineDiv();
	} else if(Response.responseText == 'fail') {
		alert('id와 passwd를 확인해 주세요');
	} else if(Response.responseText == 'missing param') {
		alert('id와 passwd를 확인해 주세요');
	}
	
}

function move(event){
	if (event.keyCode == "13") {
	login();
	}
}

function logout() {
	
	new Ajax.Request('/hub4/indi/logout.jsp', {
		  onSuccess: function(response) {
			//alert(response.responseText);
		    if(response.responseText=='ok') {refreshTopLineDiv();}
		  }
		});
	
}

function refreshTopLineDiv() {
	
	//alert($('retoinPID').innerHTML);
	var retoinPID = $('retoinPID').innerHTML;
	if(retoinPID=='configure') {
		new Ajax.Updater('user_sign_div', '/hub4/indi/user_sign_div.jsp', { method: 'get',
			evalScripts: true,
			onComplete:function(){}
		});
		new Ajax.Updater('mid_content_div', '/hub4/indi/configure_mid_content_div.jsp', { method: 'get',
			evalScripts: true,
			onComplete:function(){}
		});		
	} else if(retoinPID=='make_retoin') {
		window.location='/hub4/make_retoin.jsp';		
	} else window.location.reload();
	
}

function colorToHex(color) {
    if (color.substr(0, 1) === '#') {
        return color;
    }
    var digits = /(.*?)rgb\((\d+), (\d+), (\d+)\)/.exec(color);
    
    var red = parseInt(digits[2]);
    var green = parseInt(digits[3]);
    var blue = parseInt(digits[4]);
    
    var rgb = blue | (green << 8) | (red << 16);
    return digits[1] + '#' + rgb.toString(16);
};

function ban_auto_toggle(id) {

	//alert(colorToHex($('ban_auto_'+id).style.color));
	if(colorToHex($('ban_auto_'+id).style.color) == '#777777') {
		$('ban_auto_'+id).style.color = "#FF0000";
	} else $('ban_auto_'+id).style.color = '#777777';

}

function comma(res_cost){  // 세자리 단위의 콤마표시.
	var result ="";
	var res = res_cost+"";
	
	var sign = res.substring(0,1);
	
	if(sign == '-' || sign == '+') {		
		res = res.substring(1);
	} else sign = '';
		
	for(var i=0; i<res.length; i++)
	{
		var tmp = res.length-(i+1);
		if(i%3==0 && i!=0)
			result = ',' + result;
		result = res.charAt(tmp) + result;
	}
	return sign+result;
}


function setCookie(key,value,expires) {
	
	//alert(key+' '+value+' '+expires);
	var C = new CookieHandler();
	C.setCookie(key, value, expires);

}

function getCookie(key) {

	var C = new CookieHandler();
	//alert(key+' '+C.getCookie(key));
	return C.getCookie(key);

}


/**
*
*  Javascript cookies
*  http://www.webtoolkit.info/
*
**/
 
function CookieHandler() {
 
	this.setCookie = function (name, value, seconds) {
 
		if (typeof(seconds) != null) {
			var date = new Date();
			date.setTime(date.getTime() + (seconds*1000));
			var expires = "; expires=" + date.toGMTString();
		}
		else {
			var expires = "";
		}
 
		document.cookie = name+"="+value+expires+"; path=/";
	}
 
	this.getCookie = function (name) {
 
		name = name + "=";
		var carray = document.cookie.split(';');
 
		for(var i=0;i < carray.length;i++) {
			var c = carray[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
		}
 
		return null;
	}
 
	this.deleteCookie = function (name) {
		this.setCookie(name, "", -1);
	}
 
}

function viewNewLayerMsgModal(evt,id,msg,parent_div,title,left,top,width,height,closex) {
	
	//alert(evt.clientY + document.body.scrollTop);
	//#overlay {   visibility: hidden; position: absolute; left: 0px; top: 0px; width:100%; height:100%; text-align:center; z-index: 1000;}

	var evtTop = 0;
	if(top < 0 && evt != null) evtTop = evt.clientY + $(parent_div).scrollTop + top;

	if($(id) == null) {
	
		var modal = new Element('div');
		modal.id = id;
		//modal.class = 'modal';
		
		modal.setStyle({
			position : 'absolute',
			left : '0px',
			top : '0px',
			width : '100%',
			height : '100%',
			display : 'none'
			});
		
		$(parent_div).insert(modal);
		newLayerZ ++;
		modal.style.zIndex = newLayerZ;
		modal.style.backgroundImage = "url('/hub4/img/modal_bg.png')";
		
		modal.show();
		
		if(closex) {
		
			modal.innerHTML=
				"<table width='100%' height='100%'><tr><td height='100%' align='center' valign='middle'>" +
				"<div id='"+id+"Layer' style='position:relative;visibility:hidden;background-color:white;border:0px solid red;padding:2px;'>"+
				"<div style='position:relative;padding:2px;min-height:17px;_height:17px;background-color:black;'>"+
				"<div style='float:left;width:"+(width-24)+"px;min-height:17px;_height:17px;color:white;font-size:15px;font-weight:bold;text-align:left;overflow:hidden;'>"+title+"</div>"+
				"<div style='float:right;width:20px;min-height:17px;_height:17px;color:white;font-size:15px;font-weight:bold;text-align:right;'>"+
				"<span id='"+id+"_spanInLayer' class='clickable'>X</span></div></div>"+
				"<table height='100%' width='100%'><tr><td height='100% width='100%' align='center' valign='center' id='"+id+"_InLayer'></td></tr></table></div>" +
				"</td></tr></table>";
			
				$(id+'_spanInLayer').onclick = function() {$('' + id + '').hide();};
		
		} else {

			modal.innerHTML=
				"<div id='"+id+"Layer' style='visibility:hidden;background-color:white;border:0px solid red;padding:2px;'>"+
				"<div style='padding:2px;min-height:17px;_height:17px;width:"+(width-4)+"px;background-color:black;'>"+
				"<div style='float:left;width:"+(width-4)+"px;min-height:17px;_height:17px;color:white;font-size:15px;font-weight:bold;text-align:left;overflow:hidden;'>"+title+"</div></div>"+
				"<div id='"+id+"_InLayer' style='background-color:white;'></div></div>";
			
		}
		
	}
	
	var newdiv = $(id+'Layer');
	
	if(left > 0) {
		newdiv.style.left = left+"px";
	}
	
	newdiv.style.minWidth = width+"px";
	newdiv.style._width = width+"px";
	newdiv.style.width = width+"px";
	
	newdiv.style.minHeight = height+"px";
	newdiv.style._height = height+"px";
	newdiv.style.height+"px";
	
	newLayerZ++;
	newdiv.style.zIndex = newLayerZ;
	
	if(top > 0) {
		newdiv.style.top = top+"px";
	} else if(top == -1 ) {
		newtop = (evtTop - newdiv.offsetHeight);
		if(newtop < 0) newtop = 0;
		newdiv.style.top = newtop+"px";
	}					
	
	newdiv.show();
	
	$(id+'_InLayer').innerHTML = msg;
	$(id+'_InLayer').style.height = (newdiv.offsetHeight - 17) + "px";
	$(id+'_InLayer').style.color = "black";
	
	newdiv.style.visibility="visible";
	
	if($(id).style.display == 'none') $(id).show();
	if(newdiv.style.visibility='hidden') newdiv.style.visibility="visible";
	
}
