/*
 * 항상 사용하는 스크립트 들.
 * 
 * by jonathan
 */

// 쿠키 사용불가시 메시지 출력
if('undefined'==typeof(window.dialogArguments) && !navigator.cookieEnabled)
{
  if(confirm("쿠키 설정이 되어 있지 않습니다.\n쿠키 설정 없이는 세이클럽 사용이 불가능합니다.\n쿠키 설정 도움말을 보시겠습니까?"))
  {
    idx = (navigator.appVersion.indexOf("MSIE"));
    if (idx > -1)
      IEV = parseInt(navigator.appVersion.substr(idx+4));
    if (IEV<=4)
      document.location.href = 'http://www.sayclub.com/cookie/cookie_ie4.nwz';
    else
      document.location.href = 'http://www.sayclub.com/cookie/cookie_ie5.nwz';
  }
}

// 네비게이터 및 버전을 체크한다.
/*
IE_Version = new Number(((window.navigator.appVersion.split('; '))[1].split(' '))[1]);
if (window.navigator.appName != "Microsoft Internet Explorer")
{
  document.location.href = "http://www.sayclub.com/global/index_oldbrowser.nwz";
}
*/

/*
 * ItemHandler
 */
if("undefined" == typeof ItemHandler)
{
  ItemHandler = {
    imageRootConverter : function (imgfile,prefix) { //이미지파일 경로 변경 ( item_real2 => 등록날짜별 세부폴더 )
      var str_prefix = '';
      if (prefix) str_prefix = prefix+"_";
      var reg_str = /^([0-9]{4}-[0-9]{2})/;
      if (!(imgfile.match(reg_str))) 
        return "http://filei.sayclub.kr/charimg/item_real2/"+str_prefix+imgfile;
      else 
        return "http://filei.sayclub.kr/charimg/item_real2/"+imgfile.substr(0,4)+"/"+imgfile.substr(5,2)+"/"+str_prefix+imgfile;
    },
    getAvatarZindex : function ()
    {
		  var z_index = new Array;		
		  z_index[1]=  8;
		  z_index[2]=  7;
		  z_index[3]=  6;
		  z_index[4]=  9;
		  z_index[5]=  5;
		  z_index[6]=  14;
		  z_index[7]=  15;
		  z_index[8]=  22;
		  z_index[9]=  0;
		  z_index[10]= 1;
		  z_index[11]= 2;
		  z_index[12]= 3;
		  z_index[13]= 4;
		  z_index[14]= 13;
		  z_index[15]= 10;
		  z_index[16]= 11;
		  z_index[17]= 12;
		  z_index[18]= 16;
		  z_index[19]= 17;
		  z_index[20]= 18;
		  z_index[21]= 19;
		  z_index[22]= 20;
		  z_index[23]= 21;
      return z_index;
    },
    getAvatarStr : function (icon, from)
    {
	  		
      var z_index = ItemHandler.getAvatarZindex();
		  var arrIcon = icon.split(',');
		  var retstring = "<DIV id=\"character\" style=\"position:relative; width:62px; height:62px; overflow:hidden\">";
		  var cntLayer = arrIcon.length;
		  var imgfile = '';
		
		  for (i = 0; i < cntLayer; i++)
		  {
		    if( 'spotlight' == from )
		    {
		      if( i == 8 )
		      {
		        break;
		      }
		    }
		    imgfile = ItemHandler.imageRootConverter(arrIcon[i]);
		    retstring += "<DIV ID=\"Layer"+(i+1)+"\" STYLE=\"position:absolute; left:0; top:0; z-index:"+z_index[i+1]+"\">"+
		    "<img id=\"ImgChar"+(i+1)+"\" src="+imgfile+" border=0></DIV>";
		  }
		  retstring += "</DIV>";
		  
		  return retstring;
    },
    getAvatarFromFilestr : function (filestr,mood,width,height)
    {
      if (jQuery.inArray(mood,[2,3,4,5]) == -1) mood = 1;
      var z_index = ItemHandler.getAvatarZindex();
      var leftPosition = - (100-width) / 2;
      var charimg = "<!-- CHARACTER START --><div>";
      charimg += "<div id=\"character\" style=\"position:relative; top:0; left:0; width:"+width+"px; height:"+height+"px; overflow:hidden\">";

      var arrChar = filestr.split("!");
      if (arrChar.length > 1)
      {
        var arrTemp = "";
        if (typeof(arrChar[4]) != "undefined" && arrChar[4] != "*")
        {
          arrTemp = arrChar[4].split(",");
          arrChar[4] = arrTemp[0];
        }
        if (typeof(arrChar[17]) != "undefined" && arrChar[17] != "*")
        {
          arrTemp = arrChar[17].split(",");
          arrChar[17] = arrTemp[0];
        }
        for (var j=0; j<23; j++)
        {
          if (typeof(arrChar[j]) == "undefined" || arrChar[j] == "" || arrChar[j] == "*")
            arrChar[j] = "img_blank.gif";
        }

        var layer = 0;
        var tempStr = "";
        for (var i=0;i<arrChar.length;i++)
        {
          layer = i+1;
          if (i==0 || i==4 || i==12 || i==17 || i==18)
          {
            if (mood >= 2 && mood <= 5 && arrChar[i] != "img_blank.gif" && arrChar[i].search("default") == -1)
            {
              arrChar[i] = arrChar[i].replace("01.gif","0"+mood+".gif");
            }
          }
          charimg += "<div id=\"Layer"+layer+"\" style=\"position:absolute; left:"+leftPosition+"px; top:0px; z-index:"+z_index[layer]+"\">";
          charimg += "<img id=\"ImgChar"+layer+"\" src=\""+ItemHandler.imageRootConverter(arrChar[i])+"\" border=\"0\"></div>";
        }
      }
      else
      {
        charimg += "<div id=\"Layer0\" style=\"position:absolute; left:"+leftPosition+"px; top:0px; z-index:1\">";
        charimg += "<img src=\""+ItemHandler.imageRootConverter(filestr)+"\" border=\"0\"></div>";
      }
      charimg += "</div></div><!-- CHARACTER END -->";

      return charimg;
    },
    avatarReset : function (avatarHtml)
    {
    	// 필요시 추가
    }
  }
}

/*
 * Ajax
 */
if("undefined" == typeof Ajax)
{
  Ajax = {
    syncGET : function (pUrl)
    {
      var retval = $.ajax({
        url: pUrl,
        async: false
       }).responseText;
      return retval;
    },
    asyncGET : function (pUrl,paramJson,callbackFunc)
    {
      $.get(pUrl,paramJson,callbackFunc);
    }
  }
}

/*
 * PopupHandler
 */
if("undefined" == typeof PopupHandler){  
  PopupHandler = {        
    getScreenCenter : function(w, h)
    {
      var sleft = parseInt(($(window).width()/2) - (w/2));
      var stop = parseInt(($(window).height()/2) - (h/2));
      sleft = (sleft < 0 ? 0 : sleft);
      stop = (stop < 0 ? 0 : stop);
      return "top=" + stop + ", left=" + sleft;
    },    
    openWin : function(url, name, w, h)
    {
      var vleft = ($(window).width() - w)/2;
      var vtop = ( $(window).height() - h)/2;
      var oWin = window.open(url, name, "scrollbars=no,resizable=yes,width="+w+",height="+h+", top="+vtop+", left="+vleft );
      try{ oWin.focus(); }catch(Exception){}
      return oWin;
    },    
    openWinPosition : function (url, name, w, h, x, y)
    {
      var oWin = window.open(url, name, "scrollbars=no,resizable=yes,width="+w+",height="+h+", top="+x+", left="+y );
      try{ oWin.focus(); }catch(Exception){}
      return oWin;
    },    
    openWinNoresizable : function(url, name, w, h, scenter)
    {
      var strPos = "";
      if (scenter)
        strPos = ", " + PopupHandler.getScreenCenter(w, h);
    
      var oWin = window.open(url, name, "scrollbars=no,resizable=no,width="+w+",height="+h+strPos);
      try{ oWin.focus(); }catch(Exception){}
      return oWin;
    },
    openWinScroll : function (url, name , w,  h)
    {
      var oWin = window.open(url, name , "scrollbars=yes,resizable=yes,width="+w+",height="+h);
      try{ oWin.focus(); }catch(Exception){}
      return oWin;
    },
    resize : function (pWidth, pHeight)
    {
      try {
        if(pWidth && pHeight) {
          window.resizeTo( pWidth, pHeight );
		    }
      } catch(e) {
        return false;
      }
    }
	}  
}


/**
 *  SSL 
 */
if("undefined" == typeof SSLHandler )
{
  SSLHandler = {    
    sslRequestUrl : function(url)
    {
      url = unescape(url.replace(/\+/g," "));
      url.replace(/%2F/g, "\/");
      return "https://secure.sayclub.com/?surl=" + url;
    },
    isSSLRequest : function()
    {
      var cururl = window.location + "";
      if (cururl.substr(0, 8) == "https://")
        return true;
      return false;
    },
    getSSLURL : function(url)
    {
      var sslbase = "https://secure.sayclub.com/?surl=";
      if (SSLHandler.isSSLRequest())
      {
        url = url.replace(/:/g, "%3A");
        url = url.replace(/\//g, "%2F");
        url = url.replace(/\?/g, "?&");
        return sslbase+url;
      }
      return url;
    },
    getSSLResourceURL : function(url)
    {
      var sslbase = "https://secure.sayclub.com/resource.nwz?src=";
      if (SSLHandler.isSSLRequest())
        return sslbase+url;
      return url;
    }
  }
}

function openSendRose(pMsrl) {
	var roseUrl = _staticSaycastUrl + "/saycast/rosegift/send/" + pMsrl;
	PopupHandler.openWinScroll(roseUrl, "giftrose", 445, 250);
}

function window_name (name1, name2)/*{{{*/
{
  var fname = "";
  var name0 = escape("_say"+"_"+name1+"_"+name2);
  for (i=0; i<name0.length; i++) {
    if (name0.charAt(i)=='%' || name0.charAt(i)=='/')
      fname += "_";
    else
      fname += name0.charAt (i);
  }
  return fname;
}/*}}}*/

function openHompy(srl,str)
{
  if(srl == '-1' || srl == '')
      return;
  var url='http://hompy.sayclub.com/hompy.nwz?targetmsrl='+srl+'&pop=pop';
  if(str)
    url = url + '&' + str;
  hompy_win = window.open(url, '_hompy', 'scrollbars=no,resizable=no,width=995,height=565');
  try{ hompy_win.focus(); }catch(Exception){}
}

function openBuyRose()
{
	url = "http://saymall.sayclub.com/saymall_buy_product.nwz?product_id=73380&cat_id=1000009303";
  window.open (url,"_buyrose","scrollbars=no,resizable=no,width=400,height=300");
}

function openCjTalk(cjmsrl)
{
	// 추후 삭제하자.
	//PopupHandler.openWinScroll("http://saychatting.sayclub.com/cjtalk/index.nwz?cjmsrl="+cjmsrl,"cjtalk",620,670);
}
function openProfile(cjid)
{
	window.open("http://me.sayclub.com/profile/home/view/"+cjid);
}
function openCJtalk(cjmsrl) // sayclub interface
{
	openCjTalk(cjmsrl);
}

// 정수형 세션 값을 읽어온다.
// 값을 읽어오는데 실퍼하거나 값이 없을 경우에는 0 을 반환한다.
function getSessionValueInt(varname)
{
  if (typeof(AppletSession) == 'object' &&
      AppletSession.getvalue(varname) != '' &&
      AppletSession.getvalue(varname) > 0)
    return parseInt(AppletSession.getvalue(varname));
  else
    return 0;
}

// 로그인페이지로 이동
function goLoginForm(nextPage)
{
	location.href = _staticUrl+"/common/login/index";
}

// 충전창 띄우기
function chargeSaycash( param, from )
{
  var url = 'http://saymall.sayclub.com/saymall_create_token.nwz';
  var fullurl = url + ( param ? "?"+param : "" );

  if ( from )
    window.open( fullurl, '_rds', 'width=470,height=210,scrollbars=no,scrolling=auto' );
  else
    document.location.replace( fullurl );

  return;
}

// 마우스 위치 잡기
var _mouse = {'x' : 0 , 'y' : 0};
$(document).bind('mousedown',function(e)
{
	_mouse.x = e.pageX;
	_mouse.y = e.pageY;
});

/************************************************
 * SayAx , Notify & sayclub 에서 날라오는 script
 ************************************************/
function OnQuit_SayClub()
{
  alert("서버와의 접속이 끊어졌습니다.");
	document.location.href = "http://saycast.sayclub.com";
}
function saycast_cmd(cmd)
{
	try{
		eval(cmd);
	}catch(e){}
}
function document_eval(msg0)
{
  if (msg0.search("document.write") != -1 || msg0.search("FlashPlay") != -1)
    return false;

  try {
    eval(msg0);
  } catch (e) { }
}
function open_win_noresizable(){
  eval(" PopupHandler.openWinNoresizable("+ toStringArgs(arguments) + ")");
}
function open_win_scroll(url,name,w,h)
{
  eval(" PopupHandler.openWinScroll("+ toStringArgs(arguments) + ")");
}
function open_window (url, name, extra)
{
  var oWin = window.open(url, name, extra);
  try{ oWin.focus(); }catch(Exception){}
}
function tachy_go_handler(a)
{
  eval(AppletSession.codec(50,a,''));
}
function tachy_go(url,target,options)
{
  if (url.indexOf("http://tachy.sayclub.com/join")>=0)
    AppletSession.executeHTTP(url,'','tachy_go_handler','');
  else
  {
    if(url.indexOf("http://hompy.sayclub.com/hompy.nwz?targetid=") >0)
    {
        hompy_win = window.open(url, '_hompy', options);
        try{ hompy_win.focus(); }catch(Exception){}
    }
    else
      window.open(url,target,options);
  }
}
function passwd_window()
{
  var url = "https://secure.sayclub.com/?surl="+escape("https://www.sayclub.com/forget_passwd.nwz");
  var wndNew = window.open(url, "passwd_window", "width=400, height=275, toolbar=no, location=no");

  if (wndNew)
    wndNew.focus();
}

/************************************************
 * END SayAx , Notify 
 ************************************************/

/**
 * 팝업해제
 */
$(document).ready(function(){
	
	if(typeof(AppletSession) == 'object' && typeof(AppletSession.allowpopup) != "undefined")
	{
		AppletSession.allowpopup("*.saycast.com","f52d5c483db5614629c8b1fc9564bafa");
	}
	
	//_dwiPID="d-FUd-255";
  //dauminside();
		
});		

function dauminside()
{
	try{
		var send = false;
		var ck = document.cookie;
		var cpre = "_analytics=";
		var sidx = ck.indexOf(cpre);
		if(sidx == -1){
			if(Util.rand(1,10) == 7){
				document.cookie = cpre + "1";
				send = true;
			}else{
				document.cookie = cpre + "0";
			}
		}else{
			var eidx = ck.indexOf(";", sidx + cpre.length);
			if(eidx == -1){
				var avalue = ck.substring(sidx + cpre.length, ck.length);
			}else{
				var avalue = ck.substring(sidx + cpre.length, eidx);
			}
			if(avalue == "1"){
				send = true;
			}
		}
		if(send == true){
			if(typeof(_dwiCatch) == "function") { _dwiCatch();}
		}
	}catch(e){}
}

// Flash 
function FlashPlay(Url, tWidth, tHeight, Wmode, swfId) // 경로,가로,세로,투명,ID
{
  var tmpStr = "";
  tmpStr += "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,12,36' width='"+ tWidth +"' height='"+ tHeight +"' id='"+ swfId +"'>"
  tmpStr += "<param name='movie' value='"+ Url +"'>"
  tmpStr += "<param name='quality' value='high'>"
  tmpStr += "<param name='wmode' value='"+ Wmode +"'>"
  tmpStr += "<param name='allowScriptAccess' value='always'>"
  tmpStr += "<embed src='"+ Url +"' width='"+ tWidth +"' height='"+ tHeight +"' wmode='"+ Wmode +"' id='"+ swfId +"' allowScriptAccess='always' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash'></embed>"
  tmpStr += "</object>"
                             
  document.write (tmpStr);
} 

//친구 접속시 notify
function gnbFriendIsLogout(){}
function gnbFriendIsLogin(){}

// img over out
function imgOver(obj) { obj.src = obj.src.replace("Off.gif", "On.gif");}
function imgOut(obj) { obj.src = obj.src.replace("On.gif", "Off.gif");}


