/********************************************************************
Vricks Republic Publishing S&A
Beginning : 2009-08-15
Final : 2009-08-21
*********************************************************************/
var COOKIE_DOMAIN = document.domain;//'www.tera-online.com';
function SetCookie (name, value, exday) {/*{{{*/
   var argv = SetCookie.arguments;
   var argc = SetCookie.arguments.length;
//   var expires = (2 < argc) ? argv[2] : null;
    if(exday)
    {
      var d = new Date();
      var time = d.getTime()+60*60*24*1000*exday;
      d = new Date(time);
      var expires = d;
    }
    else
      var expires = null;
//   var path = (3 < argc) ? argv[3] : null;
  var path = '/';
   var domain = (4 < argc) ? argv[4] : COOKIE_DOMAIN; 
   var secure = (5 < argc) ? argv[5] : false;
   document.cookie = name + "=" + escape (value) +
      ((expires == null) ? "" :
         ("; expires=" + expires.toGMTString())) +
      ((path == null) ? "" : ("; path=" + path)) +
      ((domain == null) ? "" : ("; domain=" + domain)) +
      ((secure == true) ? "; secure" : ""); 
} /*}}}*/
function getCookieVal(offset)//{{{
{
	var endstr = document.cookie.indexOf(';', offset);
	if(endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}//}}}
function GetCookie(name)//{{{
{
	var arg = name + '=';
	var alen = arg.length;
	var clen = document.cookie.length;
	var i=0;
	while(i<clen) {
		var j = i + alen;
		if(document.cookie.substring(i, j) ==arg)
			return getCookieVal(j);
		i = document.cookie.indexOf(' ', i) + 1;
		if(i==0) break;
	}
	return null;
}//}}}
function setCookie(name,value,sec)
{
  var exp = new Date();
  if((name==null)||(value==null)) return false; 

  exp.setTime(exp.getTime()+(sec*1000));
  document.cookie = escape(name)+"="+escape(value)+"; "+"expires="+exp.toGMTString()+"; path=/; domain=tera-online.com";
}

function saveEmail(email)
{
  var exp = new Date(2010,7,10);
  var name = "setemail";

  document.cookie = escape(name)+"="+escape(email)+"; "+"expires="+exp.toGMTString()+"; path=/";
}
function get_body_inner_size()/*{{{*/
{
	var result = new Object;
	if(self.innerWidth)
	{
		result.width = self.innerWidth;
		result.height = self.innerHeight;
		return result;
	}
	else if (document.documentElement && typeof(document.documentElement.clientHeight) != 'undefined') 
	{
		result.width = document.documentElement.clientWidth;
		result.height = document.documentElement.clientHeight;
		return result;
	}
	else if (document.body) { 
		result.width = document.body.clientWidth;
		result.height = document.body.clientHeight;
		return result;
	}
}/*}}}*/
function get_body_scroll()/*{{{*/
{
	var x,y;
	if(typeof(self.pageYOffset)!='undefined') 
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && typeof(document.documentElement.scrollTop) != 'undefined') 
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body)  
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	var result = new Object;
	result.left = x;
	result.top = y;
	return result;
}/*}}}*/
function get_body()/*{{{*/
{
	var oBodyScroll = get_body_scroll();
	var oBodySize = get_body_inner_size();
	var oBody = new Object;
	oBody.width = oBodySize.width;
	oBody.height = oBodySize.height;
	oBody.top = oBodyScroll.top;
	oBody.left = oBodyScroll.left;

	return oBody;
}/*}}}*/

//loading
function addLoadEvent(func){
	var oldonload = window.onload;
	if(typeof window.onload != "funcion") {
		window.onload = func;
	}else{
		oldonload();
		func();
	}
}

//transparent png
function setPng24(obj) {
	obj.width = obj.height= 1;
	obj.className = obj.className.replace(/\bpng24\b/i,"");
	obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ obj.src +"',sizingMethod='image');";
	obj.src = "http://download.tera-online.com/images/blank.gif";
	return "";
}

//window size control
function windowScrollControl() {
	//IE6,IE7,IE8,FF,Oprea,Ghrome,Safari,Netscape
	if(screen.width <= "1024") {
		document.getElementById("mainWrap").style.width = "997px";
		document.getElementById("mainWrap").style.overflow = "auto";
	}
	if(screen.width > "1024" && screen.width <= "1152") {
		document.getElementById("mainWrap").style.width = "1125px";
		document.getElementById("mainWrap").style.overflow = "auto";
	}
	if(screen.width > "1152" && screen.width <= "1280") {
		document.getElementById("mainWrap").style.width = "1253px";
		document.getElementById("mainWrap").style.overflow = "auto";
	}
	if(screen.width > "1280" && screen.width <= "1400") {
		document.getElementById("mainWrap").style.width = "1373px";
		document.getElementById("mainWrap").style.overflow = "auto";
	}
}

//tab
function tabSwap(s, n) {
	for(var i = 1; i < n; i++) {
		if(s == i) {
			try {
				var account = document.getElementById("account" + i).style;
				account.display = "block";
				/*if(!document.all) {
					account.marginLeft = "-10px";
					account.position = "relative";
				}*/
				eval("document.tab"+i).src = "http://download.tera-online.com/images/tab/tab_"+ i +"_on.gif";
				document.title.src = "images/title_"+ i +".gif";

			} catch (e) { }
		}else{
			try {
				var account = document.getElementById("account" + i).style;
				account.display = "none";
				eval("document.tab"+i).src = "http://download.tera-online.com/images/tab/tab_"+ i +"_off.gif";
			} catch (e) { }
		}
	}
}

//images swap
function mediaImgSwap(n) {
	for(i=1; i<7; i++) {
		var obj = document.getElementById("mImg" + i);
		if(i == n) {
			var holder = document.getElementById("mediaHolder");
			holder.src = obj.src.replace(/thum/, "view");
		}
	}
}

function mediaImgSwapScreen(n,f,o) {
	for(i=1; i<7; i++) {
		var obj = document.getElementById("mImg" + i);
		if(i == n) {
			var holder = document.getElementById("mediaHolder");
			holder.src = obj.src.replace(/thum/, "view");
			document.frm.dn.value = f;
			document.frm.on.value = o;
		}
	}
	//alert(document.frm.on.value)
}

//images swap for wallpapers
function mediaImgSwapWall(n,f,d1,d2,d3,d4,d5,d6,d7,d8,o1,o2,o3,o4,o5,o6,o7,o8) {

	document.frm.downimg1.value = d1;
	document.frm.downimg2.value = d2;
	document.frm.downimg3.value = d3;
	document.frm.downimg4.value = d4;
	document.frm.downimg5.value = d5;
	document.frm.downimg6.value = d6;
	document.frm.downimg7.value = d7;
	document.frm.downimg8.value = d8;

	document.frm.downimg1_ori.value = o1;
	document.frm.downimg2_ori.value = o2;
	document.frm.downimg3_ori.value = o3;
	document.frm.downimg4_ori.value = o4;
	document.frm.downimg5_ori.value = o5;
	document.frm.downimg6_ori.value = o6;
	document.frm.downimg7_ori.value = o7;
	document.frm.downimg8_ori.value = o8;

	for(i=1; i<7; i++) {
		var obj = document.getElementById("mImg" + i);
		if(i == n) {
			//alert(f)
			var holder = document.getElementById("mediaHolder");
			holder.src = obj.src.replace(/thum/, "view");
			//document.frm.fullimg.value = "/uploadV1/wall/"+f;
			document.frm.fullimg.value = f;
			for(h=1;h<=8;++h) {
				dv = eval("d"+h);
				ov = eval("o"+h);
				if(h==1) {
					//alert(dv)
				}
				if(dv == "") {
					document.getElementById("down"+h).style.display = "none";
					//document.frm.downimg1.value = "";
				} else {
					document.getElementById("down"+h).style.display = "block";
					//document.frm.downimg1.value = dv;
				}
			}
		}
	}
}

function getMovieName(movieName) {
	//if (navigator.appName.indexOf("Microsoft") != -1) {
	if (document.all) {
		return window[movieName]
	}
	else {
		return document[movieName]
	}
}

//media player layer
var fullUrl = "";

function showFullLayer(_LargeURL){

	
	try {
		var obj = document.getElementById("flashFull");
		var w = document.body.clientWidth;
		var h = document.body.scrollHeight;
		obj.className = "showPlayer";
		obj.style.top = ((h/3) - (600/2)) + "px";
		obj.style.left = ((w/2) - (970/2)) + "px";

		var back = document.getElementById("backDim");
		
		back.className = "backDimOn";
		back.style.width = w;
		back.style.height = h;

		fullUrl = _LargeURL;


		setTimeout("showFullLayerStart()", 1000);
	} catch(e) { 
		
		//alert("error: "+e)
		
	}
	
}

function showFullLayerStart() {
	//alert("showFullLayerStart: ");
	try {
		
		getMovieName("videoPlayerLarge").displayVideo(fullUrl);
	} catch(e) { 
		
		//alert("error(showFullLayerStart): "+e)
		//alert("getMovieName('videoPlayerLarge'): "+getMovieName("videoPlayerLarge"));
		//alert("getMovieName('videoPlayerLarge').displayVideo: "+getMovieName("videoPlayerLarge").displayVideo);
		//document.getElementById("videoPlayerLarge").displayVideo(fullUrl);
	}
	
	
}

function hideFullLayer(){
	var obj = document.getElementById("flashFull");
	var back = document.getElementById("backDim");

	back.className = "backDimOff";
	back.style.width = "0";
	back.style.height = "0";

	obj.className = "hidePlayer";
	obj.style.top = "-2000px";
	obj.style.left = "-2000px";
}

//media images layer
var activeWallFlag = 0;
function showFullLayerImg(obj){
	var obj = document.getElementById(obj);
	var back = document.getElementById("backDim");
	var w = document.body.clientWidth;
	var h = document.body.scrollHeight;

	var pic = document.getElementById("mediaHolder");
	var fullImg = obj.getElementsByTagName("img")[0];

	obj.className = "showLayer";
	obj.style.top = ((h/3) - (600/2)) + "px";
	obj.style.left = ((w/2) - (970/2)) + "px";

	fullImg.src = pic.src;
	fullImg.src = fullImg.src.replace(/view/, "full");

	back.className = "backDimOn";
	back.style.width = w;
	back.style.height = h;
}

function hideFullLayerImg(obj){
	var obj = document.getElementById(obj);
	var back = document.getElementById("backDim");

	obj.className = "hideLayer";
	back.className = "backDimOff";
	back.style.width = "0";
	back.style.height = "0";

	activeWallFlag = 0;
}


function showFullLayerImgWall(obj,f){
	activeWallFlag = 1;

	//document.getElementById("fullImgDiv").style.backgroundImage = "url()";
	document.images.fullImgDiv.src = "http://download.tera-online.com/images/blank.gif";

	var obj = document.getElementById(obj);
	var back = document.getElementById("backDim");
	var w = document.body.clientWidth;
	var h = document.body.scrollHeight;

	var pic = document.getElementById("mediaHolder");
	//var fullImg = f;

	obj.className = "showLayer";
	obj.style.top = ((h/3) - (600/2)) + "px";
	obj.style.left = ((w/2) - (970/2)) + "px";

	//fullImg.src = pic.src;
	//alert(fullImg.src)
	//fullImgDiv.src = f;
	//fullImg.src = fullImg.src.replace(/view/, "full");
	//document.getElementById("fullImgDiv").style.backgroundImage = "url("+f+")";
	document.images.fullImgDiv.src = f;

	back.className = "backDimOn";
	back.style.width = w;
	back.style.height = h;
}

function showFullLayerImgWallResize(obj){
	if(activeWallFlag == 1) {
		var obj = document.getElementById(obj);
		var back = document.getElementById("backDim");
		var w = document.body.clientWidth;
		var h = document.body.scrollHeight;

		obj.className = "showLayer";
		obj.style.top = ((h/3) - (600/2)) + "px";
		obj.style.left = ((w/2) - (970/2)) + "px";

		back.className = "backDimOn";
		back.style.width = w;
		back.style.height = h;
	}
}

//faq
function faq(n) {
	for(i=1; i<10; i++) {
		var obj = document.getElementById("answer_" + i).style;
		var bullet = document.getElementById("bullet_" + i);
		if(i == n) {
			if(obj.display == "block") {
				obj.display = "none";
				bullet.className = "open";
			} else {
				obj.display = "block";
				bullet.className = "close";
			}
		}
	}
}

function faqAll() {
	var faqAll = document.getElementById("allBtn");
	if(faqAll.className == "close") {
		for(i=1; i<10; i++) {
			var obj = document.getElementById("answer_" + i);
			var bullet = document.getElementById("bullet_" + i);
			var faq = document.faq;
			obj.style.display = "block";
			faqAll.className = "open";
			bullet.className = "close";
			faq.src = faq.src.replace(/expand/,"collapse");
			faq.alt = "Collapse All";
		}
	} else {
		for(i=1; i<10; i++) {
			var obj = document.getElementById("answer_" + i);
			var bullet = document.getElementById("bullet_" + i);
			var faq = document.faq;
			obj.style.display = "none";
			faqAll.className = "close";
			bullet.className = "open";
			faq.src = faq.src.replace(/collapse/,"expand");
			faq.alt = "Expand All";
		}
	}
}

// GNB SCRIPT
var GNBVars = {
	initMenu:0
	,previousMenu:0
	,selectedMenu:0
	,timeoutId2:null
	,timeoutId3:null
	,isOver:false
	,strEmail:'E-mail Address'
	,strPasswd:'Password'
};
var GNBEase = 100;
function init_gnb(initMenu)/*{{{*/
{
	if(typeof(initMenu)=='undefined')
		GNBVars.initMenu = -1;
	else
		GNBVars.initMenu = initMenu;
	$('#GnbMenusUnder .m'+GNBVars.initMenu).stop().css({display:'block'}).animate({opacity:1.0}, GNBEase);
	var tLeft = -10+(GNBVars.initMenu*121);
	if(GNBVars.initMenu==-1)
		$('#Border').stop().hide();
	else
		$('#Border').stop().animate({left:tLeft}, GNBEase);
}/*}}}*/
function focus_gnb_menu(idx, obj)/*{{{*/
{
	GNBVars.selectedMenu = idx;
	GNBVars.isOver = true;
	window.clearTimeout(GNBVars['timeoutId']);
	if(GNBVars['timeoutId'+idx]!=null)
	{
		window.clearTimeout(GNBVars['timeoutId'+idx]);
		GNBVars['timeoutId'+idx] = null;
	}
	$('#GnbMenusUnder .m'+idx).stop().css({display:'block'}).animate({opacity:1.0}, GNBEase);
	var tLeft = -10+(idx*121);
	if(idx>1)
	{
		$('#Border').stop().show().animate({left:tLeft}, GNBEase,function(){
			$('#subCont'+idx).show();
			var trate = (Math.abs(parseInt($('#subMenu'+idx).css('top'))))/137*200;
			$('#subMenu'+idx).stop().animate({top:0}, trate);
		});
	}
	else
	{
		$('#Border').stop().show().animate({left:tLeft}, GNBEase);
	}
}/*}}}*/
function return_gnb_menu(idx)/*{{{*/
{
	if(idx!=GNBVars.initMenu)
		$('#GnbMenusUnder .m'+idx).stop().animate({opacity:0.0}, GNBEase);
	if(idx>1)
	{
		if(idx==4)
		{
			var trate = Math.abs(parseInt($('#subMenu'+idx).css('top'))+69)/69*300;
			$('#subMenu'+idx).stop().animate({top:-100}, trate, function(){ $('#subCont'+idx).hide();});
		}
		else
		{
			var trate = Math.abs(parseInt($('#subMenu'+idx).css('top'))+137)/137*300;
			$('#subMenu'+idx).stop().animate({top:-200}, trate, function(){ $('#subCont'+idx).hide();});
		}
	}
	if(!GNBVars.isOver)
		init_gnb(GNBVars.initMenu);
 
}/*}}}*/
function bind_gnb_events(login)/*{{{*/
{
/* menu event attach {{{*/
	$('#GnbMenusCover > div.GMenuWrap > a').each(function(idx, obj){
		$(this).mouseenter(function(){
			GNBVars.isOver = true;
			focus_gnb_menu(idx, obj);
		});
		$(this).mouseleave(function(){
			GNBVars.previousMenu = idx;
			GNBVars.isOver = false;
			GNBVars['timeoutId'+idx] = setTimeout(function(){return_gnb_menu(idx);}, GNBEase*2);
		});
		if(idx>1)
		{
			$('#subCont'+idx).mouseenter(function(){
				GNBVars.isOver = true;
				GNBVars.selectedMenu = idx;
				focus_gnb_menu(idx, obj);
			});
			$('#subCont'+idx).mouseleave(function(){
				GNBVars.previousMenu = idx;
				GNBVars.isOver = false;
				GNBVars['timeoutId'+idx] = setTimeout(function(){return_gnb_menu(idx);}, GNBEase);
			});
 
		}
	});
/*}}}*/
/* submenu event attach{{{*/
	$('#GnbMenusUnder > div.GMenuWrap > div.depth2 > div.depth2List > ul > li').each(function(idx, obj){
		var oSpan = $(obj.getElementsByTagName('span')[0])
		$(oSpan).css({opacity:0,display:'block'});
		$(obj).mouseenter(function(){
			$(oSpan).stop().animate({opacity:1}, GNBEase);
		});
		$(obj).mouseleave(function(){
			$(oSpan).stop().animate({opacity:0}, GNBEase*2);
		});
	});
/*}}}*/
/* login event attach {{{*/
if(!login) {
	$('#GnbLogin').mouseenter(function(){
			$('#GnbLoginUnder').slideDown('fast');
	});
	$('#GnbLogin').mouseleave(function(){
			$('#GnbLoginUnder').slideUp('fast');
	});
	var cookedEmail = GetCookie('setemail');
	if(cookedEmail)
		$('#loginId').val(cookedEmail);
	if($('#loginId').val()=='')
		$('#loginId').val(GNBVars.strEmail);
		
	$('#loginId').focus(function(){
		if(this.value==GNBVars.strEmail)
			this.value = '';
	});
	$('#loginId').blur(function(){
		if(this.value.replace(/\s/g, '')=='')
			this.value = GNBVars.strEmail;
	});
	$('#fakePwd').focus(function(){
		$(this).hide();
		$('#loginPwd').show().focus();
	});
	$('#loginPwd').blur(function(){
		if(this.value=='')
		{
			$(this).hide();$('#fakePwd').show();
		}	
	});
	$('#fakeRemember').click(function(){
		document.getElementById('loginRemember').checked = document.getElementById('fakeRemember').checked;
	});
}
/*}}}*/
}/*}}}*/
function GnbLogin(oForm)
{
	var email = oForm.loginId.value;
	var pwd = oForm.loginPwd.value;
	var sessid = GetCookie('PHPSESSID');
	pwd = hex_md5(sessid+hex_md5(pwd));
	var ajxData = {email:email,pwd:pwd,from:'gnb'};
	if(document.getElementById('fakeRemember').checked)
		saveEmail(email);
	GoAjaxLogin(ajxData);
	$('#GnbLoginUnderMsg').slideUp('fast');
}
function PageLogin(oForm)
{
	if(chkValue())
	{
		var email = oForm.email.value;
		var pwd = oForm.pwd.value;
		var sessid = GetCookie('PHPSESSID');
		pwd = hex_md5(sessid+hex_md5(pwd));
		var ajxData = {'email':email,'pwd':pwd,'reload':true,from:'page'};
		if(document.getElementById('fakeRemember').checked)
			saveEmail(email);
		GoAjaxLogin(ajxData);
	}
}
function GoAjaxLogin(ajxData)
{
	$.ajax({
		url:'/ajax/gnb_login.php',
		dataType:'json',
		cache:false,
		data:ajxData,
		type:'POST',
		success:function(data){OnGnbLogin(data);}
	});
}
function OnGnbLogin(data)
{
	if(data.result)
	{
		//login show
		$('#LoginUserId').html(data.user_id);
		$('#GnbLoginForm').hide();
		$('#GnbLoginInfo').fadeIn();
		$('#GnbLoginUnderWrap').hide();
		$('#GnbLogin').unbind();

			$('#GnbLoginUnder').slideUp('fast');
			$('#GnbLoginUnderMsg').slideUp('fast');
		//flogin
		var ajxData = {'do':'login','vb_login_username':data.user_id,'vb_login_password':data.vpd,'securitytoken':data.securitytoken,'from':'GNB','reUrl':'/blank.html'};
		$.ajax({
			url:'/forums/login.php?do=login',
			dataType:'html',
			cache:false,
			data:ajxData,
			type:'POST',
			success:function(data){
				var arrLocation = document.location.href.split('/');
				var oFrm = document.getElementById('frm');
				if(oFrm && oFrm.t_url)
				{
					if(oFrm.t_url.value!='')
						location.href = oFrm.t_url.value;
					else
						location.reload();
				}
				else if(arrLocation[3] == 'forums' || (arrLocation.length>3 && arrLocation[4]=='account.php'))
					location.reload();
			}
		});
	}
	else
	{
		if(data.from=='gnb')
		{
			if(data.type=='VERIFY')
			{
				window.location.href = '/utility/verify_alert.php?email='+encodeURIComponent(data.email);
			}
			else
			{
				$('#GnbLoginUnder').slideUp('fast');
				$('#GnbLoginUnderMsg').html(data.message).slideDown('fast');
				$(document.getElementById('GnbLoginUnderMsg').getElementsByTagName('a')).click(function(){ResendVerifyMail(data.email, 'gnb');return false;});
				setTimeout(function(){ $('#GnbLoginUnderMsg').slideUp('fast'); }, 2000);
			}
		}
		else
		{
			if(data.type=='VERIFY')
			{
				window.location.href = '/utility/verify_alert.php?email='+encodeURIComponent(data.email);
				//$('#msgPageLogin').html(data.message).fadeIn('fast');
				//$(document.getElementById('msgPageLogin').getElementsByTagName('a')).click(function(){ResendVerifyMail(data.email, 'page');return false;});
			}
			else if(data.type=='PWD')
			{
				$('#msgPwdFail').html(data.message).fadeIn('fast');
			}
			else
			{
				$('#msgEmailFail').html(data.message).fadeIn('fast');
			}
		}
	}
}
function ResendVerifyMail(email, from)
{

	var ajxData = {'email':email,'from':from,'do':'verify'}
	
		$.ajax({
			url:'/ajax/send_verify_email.php',
			dataType:'json',
			cache:false,
			data:ajxData,
			type:'POST',
			success:function(data){
				OnResendVerifyMail(data);
			}
		});
}
function OnResendVerifyMail(data)
{
//	if(data.result)
	{
		document.getElementById('titLoginFail').setAttribute('src', '/images/tit/t_emailresent.png');
		$('#beforeSend').hide();
		$('#afterSend').show();
	}
}
