///////// YAYA@S[Xgev[g
///////// j[

//****  ********************************************************************
// ̎̓S[Xgev[gɕKvȋʏ܂Ƃ߂̂łB
// ʏ͂Kv͂܂B
//****  ********************************************************************


//******************************************************************************
//NEI
//******************************************************************************

OnSystemLoad.AYATEMPLATE
{
	AYATEMPLATE.installedplugin_name = IARRAY
	AYATEMPLATE.installedplugin_id = IARRAY
	AYATEMPLATE.stroke = IARRAY
	AYATEMPLATE.strokeid = IARRAY
	AYATEMPLATE.stroketimer = IARRAY
	AYATEMPLATE.mikireflag = 0
	AYATEMPLATE.LastTalk = ''
}

OnSystemUnload.AYATEMPLATE
{
	//ERASEVAR('AYATEMPLATE.installedplugin_name')
	//ERASEVAR('AYATEMPLATE.installedplugin_id')
	ERASEVAR('AYATEMPLATE.stroke')
	ERASEVAR('AYATEMPLATE.strokeid')
	ERASEVAR('AYATEMPLATE.stroketimer')
	ERASEVAR('AYATEMPLATE.mikireflag')
	ERASEVAR('AYATEMPLATE.LastTalk')
}

//******************************************************************************
//_g[N
//******************************************************************************

OnAITalk
{
	OnAiTalk
}

//------------------------------------------------------------------------------
//OnAiTalkCxg
//------------------------------------------------------------------------------
OnAiTalk
{
	//---- ʏ̃_g[NA`FC̓`FCg[N
	if SHIORI3FW.IsChain {
		AYATEMPLATE.LastTalk = ChainTalk
	}
	else {
		AYATEMPLATE.LastTalk = RandomTalk
	}
	AYATEMPLATE.LastTalk
}


//******************************************************************************
//j[֌W
//******************************************************************************

//------------------------------------------------------------------------------
//Cxg
//IID/AJ[ID̂܂܊֐Ƃď悤ɂȂ܂B
//------------------------------------------------------------------------------

OnChoiceSelect
{
	if ISFUNC(reference[0]) {
		EVAL(reference[0])
	}
}

OnAnchorSelect
{
	if SUBSTR(reference[0],0,7) == 'http://' {
		if basewarename == 'SSP' {
			"\C"
		}
		--
		"\j[%(AYATEMPLATE.EscapeText(reference[0]))]"
	}
	else {
		if ISFUNC(reference[0]) {
			EVAL(reference[0])
		}
	}
}

//------------------------------------------------------------------------------
//j[1s]
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//ʏ탁j[
AYATEMPLATE.MenuItem
{
	_linefeed = 100
	_n = _argc
	if _n >= 3 {
		if GETTYPE(_argv[_n-1]) == 1 { //
			_linefeed = _argv[_n-1]
			_n -= 1
		}
	}

	_lftext = ''
	while _linefeed > 0 {
		if _linefeed >= 100 {
			_lftext += '\n'
		}
		else {
			_lftext += '\n[half]'
		}
		_linefeed -= 100
	}
	
	_lftext
	--
	'\![*]\q['
	--
	AYATEMPLATE.EscapeText( SHIORI3FW.MakeJustText(_argv[0],46) )
	--
	_s = ''
	for _i = 1 ; _i < _n ; _i++ {
		_s += ',' + AYATEMPLATE.EscapeText(_argv[_i])
	}
	_s
	--
	']'
}

//------------------------------------------------------------------------------
//GXP[v
AYATEMPLATE.EscapeText
{
	_r = _argv[0]
	if RE_SEARCH(_r,'["\[\]]') {
		'"' + REPLACE(_r,'"','""') + '"'
	}
	else {
		_r
	}
}

//******************************************************************************
//Nj[
//******************************************************************************
AYATEMPLATE.LinkMenuConvert
{
    _text = ''
    _data = IARRAY
    for _i = 0 ; _i < _argc ; _i++ {
        _data = (RE_SPLIT(_argv[_i],'[ \t]*\|[ \t]*'),'','') //_~[
        _text += "%(_data[0])%(CHR(1))%(_data[1])%(CHR(1))%(_data[2])%(CHR(1))%(CHR(2))"
    }
    _text;
}

//******************************************************************************
//؂ւRA
//******************************************************************************

AYATEMPLATE.ExecuteChangeCallTalk
{
	AYATEMPLATE.ExecuteGhostNameFunc(_argv[0],reference[0],_argv[1])
}


//******************************************************************************
//؂ւ/R~ʃRAiS[Xgt֐Ăяoj
//_argv[0]	֐x[X
//_argv[1]	S[XgiIɃVXeGXP[vj
//			S[Xg̏ꍇ_argv[2]̕]
//			u֐x[X{S[Xgv̊֐ꍇ́ux[XOtherv
//_argv[2]	L̊֐݂ȂꍇAĂԊ֐iOptional)
//******************************************************************************
AYATEMPLATE.ExecuteGhostNameFunc
{
	_ghostname = SHIORI3FW.TranslateSystemChar(_argv[1])

	if _ghostname != '' {
		_funcname = "%(_argv[0])_%(_ghostname)"
		if ISFUNC(_funcname) {
			_script = EVAL(_funcname)
			if STRLEN(_script) != 0 {
				_script
				return
			}
		}

		_funcname = "%(_argv[0])Other"
			if ISFUNC(_funcname) {
			_script = EVAL(_funcname)
			if STRLEN(_script) != 0 {
				_script
				return
			}
		}
	}

	if _argc >= 2 {
		_script = EVAL(_argv[2])
		if STRLEN(_script) != 0 {
			REPLACE(_script,"\-","")
			return
		}
	}
}

//******************************************************************************
//R~֌W
//******************************************************************************

//b
AYATEMPLATE.ExecuteTalkToTalk
{
	//܂b鑊߂
	_ghostname=AYATEMPLATE.GetRandomSakuraName()
	res_reference0=_ghostname
	
	DUMPVAR()
	
	//ʃRAgĊ֐Ăяo
	AYATEMPLATE.ExecuteGhostNameFunc('TalkTo',_ghostname,'TalkToNobody')
}

//bɉ
AYATEMPLATE.ExecuteReplyToTalk
{
	AYATEMPLATE.ExecuteGhostNameFunc('ReplyTo',_argv[0])
}

//b鑊_Ɍ
AYATEMPLATE.GetRandomSakuraName
{
	_ghostlist=IARRAY
	//FMOgꍇFMOǂ݂܂
	if ISFUNC('SHIORI3FW.RefreshFMOTable') {
		SHIORI3FW.RefreshFMOTable
		_ghostlist=SHIORI3FW.SakuraNameList
	}else{
		_ghostlist=ghostexlist
	}
	if ARRAYSIZE(_ghostlist)>0 {
		ANY(_ghostlist)
	}else{
		''
	}
}

//NS[XgXg擾
On_otherghostname
{
	// ݋ÑS[Xg̃Xg쐬
	ghostexlist = reference
	ghostexcount = ARRAYSIZE(ghostexlist)
}



//******************************************************************************
//}EXCxgRA
//******************************************************************************
AYATEMPLATE.MouseEventExec
{
	_fname = _argv[0] + TOSTR(reference[3]) + TOSTR(reference[4])
	if ISFUNC(_fname) {
		EVAL(_fname)
		return
	}
	
	_fname = _argv[0] + TOSTR(reference[3])
	if ISFUNC(_fname) {
		EVAL(_fname)
		return
	}
}

AYATEMPLATE.MouseMoveWheelFunc
{
	_side = TOINT(reference[3])
	if _argv[0] {
		_side += 10
	}
	
	if AYATEMPLATE.strokeid[_side] == reference[4] {
		if (GETTICKCOUNT - TOINT(AYATEMPLATE.stroketimer[_side])) > 1500 {
			AYATEMPLATE.stroke[_side] = 1
		}
		AYATEMPLATE.stroketimer[_side] = GETTICKCOUNT
		
		_stroke = TOINT(AYATEMPLATE.stroke[_side])
		_stroke += 1
		AYATEMPLATE.stroke[_side] = _stroke
		
		if _stroke >= _argv[1] {
			if 'talking' !_in_ status {
				AYATEMPLATE.MouseEventExec(_argv[2]);
				AYATEMPLATE.stroke[_side] = 1
			}
		}
	}
	else {
		AYATEMPLATE.stroke[_side] = 1
		AYATEMPLATE.stroketimer[_side] = GETTICKCOUNT
		AYATEMPLATE.strokeid[_side] = reference[4]
	}
}

//------------------------------------------------------------------------------
//vOC
//------------------------------------------------------------------------------
On_installedplugin : void
{
	_n = ARRAYSIZE(reference)
	AYATEMPLATE.installedplugin_name = IARRAY
	AYATEMPLATE.installedplugin_id = IARRAY
	
	for _i = 0 ; _i < _n ; _i++ {
		_a = RE_SPLIT(reference[_i],'[,\x1]')
		
		AYATEMPLATE.installedplugin_name ,= _a[0]
		AYATEMPLATE.installedplugin_id ,= _a[1]
	}
}

AYATEMPLATE.IsPluginExist
{
	if _argc == 0 {
		0
		return
	}
	
	if ASEARCH(_argv[0],AYATEMPLATE.installedplugin_id) >= 0 {
		1
		return
	}
	if ASEARCH(_argv[0],AYATEMPLATE.installedplugin_name) >= 0 {
		1
		return
	}
	0
}
