
var newLayerZ = 1000;

var ua = navigator.userAgent.toLowerCase();
browser = {
            skt : /msie/.test( ua ) && /nate/.test( ua ),
            lgt : /msie/.test( ua ) && /([010|011|016|017|018|019]{3}\d{3,4}\d{4}$)/.test( ua ),
            opera : /opera/.test( ua ) || /opera mobi/.test( ua ),
            ipod : /webkit/.test( ua ) && /\(ipod/.test( ua ) ,
            iphone : /webkit/.test( ua ) && (/\(iphone/.test( ua ) || /\(device/.test( ua )),
            android : /webkit/.test( ua ) && /android/.test( ua ),
            chrome : /webkit/.test( ua ) && /chrome/.test( ua ),
            suck6 : /msie 6.0/.test( ua )
};

var Base64 = {
		 
		// private property
		_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
	 
		// public method for encoding
		encode : function (input) {
			var output = "";
			var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
			var i = 0;
	 
			input = Base64._utf8_encode(input);
	 
			while (i < input.length) {
	 
				chr1 = input.charCodeAt(i++);
				chr2 = input.charCodeAt(i++);
				chr3 = input.charCodeAt(i++);
	 
				enc1 = chr1 >> 2;
				enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
				enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
				enc4 = chr3 & 63;
	 
				if (isNaN(chr2)) {
					enc3 = enc4 = 64;
				} else if (isNaN(chr3)) {
					enc4 = 64;
				}
	 
				output = output +
				this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
				this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
	 
			}
	 
			return output;
		},
	 
		// public method for decoding
		decode : function (input) {
			var output = "";
			var chr1, chr2, chr3;
			var enc1, enc2, enc3, enc4;
			var i = 0;
	 
			input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	 
			while (i < input.length) {
	 
				enc1 = this._keyStr.indexOf(input.charAt(i++));
				enc2 = this._keyStr.indexOf(input.charAt(i++));
				enc3 = this._keyStr.indexOf(input.charAt(i++));
				enc4 = this._keyStr.indexOf(input.charAt(i++));
	 
				chr1 = (enc1 << 2) | (enc2 >> 4);
				chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
				chr3 = ((enc3 & 3) << 6) | enc4;
	 
				output = output + String.fromCharCode(chr1);
	 
				if (enc3 != 64) {
					output = output + String.fromCharCode(chr2);
				}
				if (enc4 != 64) {
					output = output + String.fromCharCode(chr3);
				}
	 
			}
	 
			output = Base64._utf8_decode(output);
	 
			return output;
	 
		},
	 
		// private method for UTF-8 encoding
		_utf8_encode : function (string) {
			string = string.replace(/\r\n/g,"\n");
			var utftext = "";
	 
			for (var n = 0; n < string.length; n++) {
	 
				var c = string.charCodeAt(n);
	 
				if (c < 128) {
					utftext += String.fromCharCode(c);
				}
				else if((c > 127) && (c < 2048)) {
					utftext += String.fromCharCode((c >> 6) | 192);
					utftext += String.fromCharCode((c & 63) | 128);
				}
				else {
					utftext += String.fromCharCode((c >> 12) | 224);
					utftext += String.fromCharCode(((c >> 6) & 63) | 128);
					utftext += String.fromCharCode((c & 63) | 128);
				}
	 
			}
	 
			return utftext;
		},
	 
		// private method for UTF-8 decoding
		_utf8_decode : function (utftext) {
			var string = "";
			var i = 0;
			var c = c1 = c2 = 0;
	 
			while ( i < utftext.length ) {
	 
				c = utftext.charCodeAt(i);
	 
				if (c < 128) {
					string += String.fromCharCode(c);
					i++;
				}
				else if((c > 191) && (c < 224)) {
					c2 = utftext.charCodeAt(i+1);
					string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
					i += 2;
				}
				else {
					c2 = utftext.charCodeAt(i+1);
					c3 = utftext.charCodeAt(i+2);
					string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
					i += 3;
				}
	 
			}
	 
			return string;
		}
	 
	};

var TPE = {
		
		_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
			
		encode : function (input) {
		
			var output = "";
			var i = 0;
			var j = 0;
			var kl = input.length;
			while (i < input.length) {		
				chr1 = input.charCodeAt(i++);
				j = i % kl;
				chr2 = input.charCodeAt(j);
				
				enc1 = ((chr1 & 255) << (8 - (chr2 % 8))) & 255;
				enc2 = (((chr1 & 255) >>> (chr2 % 8)) | enc1) & 255;
				
				output = output + String.fromCharCode(enc2);		
			}
			var rstr = encodeURIComponent(output);
			return rstr;
		},

		decode : function (input,key) {
			
			var output = "";
			input = decodeURIComponent(input);
			alert('decode '+input+' '+input.length);
			var i = 0;
			while (i < input.length) {
				chr1 = input.charCodeAt(i++);
				chr2 = key.charCodeAt(key.length - i);
				
				enc1 = ((chr1 & 255) >>> (8 - (chr2 % 8))) & 255;
				enc2 = (((chr1 & 255) << (chr2 % 8)) | enc1) & 255;
				
				output = output + String.fromCharCode(enc2);		
			}
			return output;
		}
	};

function getClientHeight() {

	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    return window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    return document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    return document.body.clientHeight;
	  } else return document.body.clientHeight; 
	
}

function getScrollY() {

	  if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    return window.pageYOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    return document.body.scrollTop;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    return document.documentElement.scrollTop;
	  }
	
}

function viewNewLayerModal(evt,id,url,left,top,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 + document.body.scrollTop;
	if(top < 0 && evt != null) evtTop = evt.clientY;

	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'
			});

		wrapobj = null;
		if($('wrap_div') != null) {
			wrapobj = $('wrap_div'); 
			$('wrap_div').insert(modal);
		} else {
			wrapobj = $('wrap');
			$('wrap').insert(modal);
		}
		
		newLayerZ ++;
		modal.style.zIndex = newLayerZ;
		if(!browser.suck6) modal.style.backgroundImage = "url('img/modal_bg.png')";

		modal.show();
		
		if(browser.suck6) {
			modal.style.width = wrapobj.offsetWidth + "px";
			modal.style.height = wrapobj.offsetHeight + "px";
		}

		if(closex) {
			modal.innerHTML=
				"<div id='"+id+"Layer' class='poplayermodal radius_15'>"+
				"<div class='captionLayer'><span id='"+id+"_spanInLayer' class='clickable no_underline closex'>X</span></div>"+
				"<div id='"+id+"_InLayer' style='padding:0px;background-color:black;'></div></div>";
				$(id+'_spanInLayer').onclick = function() {$('' + id + '').hide();};
		} else {
			modal.innerHTML=
				"<div id='"+id+"Layer' class='poplayermodal radius_15'>"+
				"<div class='captionLayer'></div>"+							
				"<div id='"+id+"_InLayer' style='padding:3px;background-color:black;'></div></div>";
		}
	}

	var newdiv = $(id+'Layer');

	newdiv.style.top = "-1000px";
	if(browser.suck6) {
		newdiv.style._width = "100px";
		newdiv.style._height = "100px";
	} else {
		$(id+'_InLayer').style.width = "100%";
		newdiv.style.width = "auto";
		newdiv.style.height = "auto";
	}

	newLayerZ++;
	newdiv.style.zIndex = newLayerZ;

	$(id).show();
	newdiv.show();
	
	if(!browser.suck6) {		
	}

	new Ajax.Updater(id+'_InLayer', url, { method: 'get',
		evalScripts: true,
		onComplete:function(){
			if(top < 0) {
				newtop = getClientHeight() - newdiv.offsetHeight;
				if(newtop <= 0) {
					newtop = evtTop;
					if(newtop < 0) newtop = 0;
				}
				//newdivbottom = newtop + newdiv.offsetHeight;
				//if(newdivbottom > $(id).offsetHeight) 
				//	newtop = document.body  $(id).offsetHeight - newdiv.offsetHeight;
				
				newdiv.style.top = newtop+"px";
				if(newtop < getScrollY()) window.scrollTo(0,newtop);
			} else {
				newdiv.style.top = top+"px";
				if(top < getScrollY() || ((top+newdiv.offsetHeight) > getClientHeight())) window.scrollTo(0,top);
			}
			
			if(left < 0) {
				left = ($(id).offsetWidth/2) - (newdiv.offsetWidth/2);
				newdiv.style.left = left+"px";
			} else newdiv.style.left = left+"px";
			
			newdiv.style.visibility="visible";
		}
	});

}

function hideLayer(divname) {
	
	$(divname).hide();
	
}

function findid(){
	
	var frm = document.findidForm;
	var name = frm.name.value;
	var email = frm.email.value;
	
		if(frm.name.value==""){
		alert("가입시 이름을 입력하세요.");
		frm.name.focus();
		return;
		}if(frm.email.value==""){
		alert("가입시 외부 이메일 주소를 입력하세요.");
		frm.email.focus();
		return;
		}
		
		frm.action = "/hub4/one/pop_findid.jsp";
		
		//frm.request({
		//	  onComplete: function(Response){ login_post(Response); }
		//	});
		
		name = encodeURIComponent(name);
		
		var url = "/hub4/one/pop_findid.jsp?name="+name+"&email="+email;
		
		new Ajax.Updater('findid_InLayer', url, { method: 'get',
			evalScripts: true,
			onComplete:function(){}
		});
}

function findpw(){
	
	var frm = document.findpwForm;
	var id = frm.id.value;
	var email = frm.email.value;
	
		if(frm.id.value==""){
		alert("ID를 입력하세요.");
		frm.id.focus();
		return;
		}if(frm.email.value==""){
		alert("가입시 외부 이메일 주소를 입력하세요.");
		frm.email.focus();
		return;
		}
		
		frm.action = "/hub4/one/pop_findpw.jsp";
		
		//frm.request({
		//	  onComplete: function(Response){ login_post(Response); }
		//	});
		
		var url = "/hub4/one/pop_findpw.jsp?id="+id+"&email="+email;
		
		new Ajax.Updater('findpw_InLayer', url, { method: 'get',
			evalScripts: true,
			onComplete:function(){}
		});
}

/* 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;
		}
		
		passwd = TPE.encode(passwd);		
			
		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('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 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);
	}
 
}
