var sindex=0;
var dindex=0;
var lindex=0;

jQuery(function($){
//-----------------------------------------------------------------------------------------------------------------------------
$('#tomoBox')
.animate({opacity:1},500)
.scrollFollow({
			  speed:500,
			  easing:'easeOutBounce',
			  offset:16,
			  container:'mimiko',
			  killSwitch:'killSwitch',
			  onText:'Turn Slide Off',
			  offText:'Turn Slide On',
			  relativeTo:'bottom',
			  delay:500
			  });

$('#tomoToggler')
.click(function(){
if(parseInt($('#tomoBox').css('width'),10)==36){
$('#tomoToggler')
.html('X');
$('#tomoBox')
.animate({width:window.innerWidth*0.96-16},200,function(){
$('#tomoVer')
.show()
$('#tomoAvatar')
.show()
});
}else{
$('#tomoToggler')
.html('O');
$('#tomoAvatar')
.hide()
$('#tomoVer,#tomoDialogBox')
.hide()
$('#tomoBox')
.animate({width:36},200);
};
});

$('#tomoAvatar')
.click(function(){
if($('#tomoDialogBox').css('display')=='none'){
$('#tomoDialogBox')
.show();
tomoDramaRecite('common');
}else{
$('#tomoDialogBox')
.hide();
$('#tomoDialog').html('');
$('#tomoDialogBox').hide();
};
});

tomoCssSetting();
$(window).resize(function(){
tomoCssSetting();
});

$(window).scroll(function(){
if(parseInt($('#tomoBox').css('width'),10)>36){
$('#tomoToggler').stop(false,true)
.html('O');
$('#tomoAvatar').stop(false,true)
.hide();
$('#tomoVer,#tomoDialogBox').stop(false,true)
.hide();
$('#tomoBox').stop(false,true)
.css({width:36});
};
});

$('#tomoInputer').change(function(){
tomoThinking();
});

$('#tomoDialog').click(function(){
tomoNext();
});
//-----------------------------------------------------------------------------------------------------------------------------
});
//-----------------------------------------------------------------------------------------------------------------------------
function tomoCssSetting(){
//-----------------------------------------------------------------------------------------------------------------------------
var tomoBoxWidth=window.innerWidth-16*2-16;

var tomoAvatarHeight=window.innerHeight*0.95;
if(tomoAvatarHeight>640){tomoAvatarHeight=640;};
var tomoAvatarWidth=tomoAvatarHeight/16*9;

var tomoDialogBoxWidth=window.innerWidth*0.6;
var tomoDialogBoxHeight=window.innerHeight*0.6;

var tomoDialogFontSize=tomoDialogBoxHeight*0.05;
if(tomoDialogFontSize<12){tomoDialogFontSize=12;};
//-----------------------------------------------------------------------------------------------------------------------------
/*Tomo Avatar*/
$('#tomoAvatar')
.attr({'src':'/yukidoll/tomo/image/tomo.png'})
.css({width:tomoAvatarWidth,height:tomoAvatarHeight})
.css({left:tomoBoxWidth-tomoAvatarWidth,top:-tomoAvatarHeight+36+16});
//-----------------------------------------------------------------------------------------------------------------------------
/*Tomo Box*/
if(parseInt($('#tomoBox').css('width'),10)>36){
	$('#tomoBox')
	.css({width:tomoBoxWidth});
};
//-----------------------------------------------------------------------------------------------------------------------------
/*Tomo Dialog*/
$('#tomoDialogBox')
.css({width:tomoDialogBoxWidth, height:tomoDialogBoxHeight})
.css({left:tomoBoxWidth-tomoAvatarWidth-tomoDialogBoxWidth,top:-tomoAvatarHeight+36+16});
$('#tomoDialog')
.css({'font-size':tomoDialogFontSize,left:tomoDialogBoxWidth*0.1,top:tomoDialogBoxHeight*0.1, width:tomoDialogBoxWidth*0.8,height:tomoDialogBoxHeight*0.8-32-4});
//-----------------------------------------------------------------------------------------------------------------------------
/*Tomo Input*/
$('#tomoInputer')
.css({left:tomoDialogBoxWidth*0.05,top:tomoDialogBoxHeight*0.9-32, width:tomoDialogBoxWidth*0.9,height:32});
//-----------------------------------------------------------------------------------------------------------------------------
};

function tomoTalk(dialog){
if(dialog){}else{dialog='啊咧咧？';};
	
	var tomoDialog=dialog;

	$('#tomoDialog')
	.animate({opacity:0},100,function(){$('#tomoDialog').html(tomoDialog).msgPlay();})
	.animate({opacity:1},100);
};

function tomoThinking(){
//-----------------------------------------------------------------------------------------------------------------------------
/*获得语句*/
var sentence=$('#tomoInputer').val().replace(/\n/g,"").replace(/\"/g,"").replace(/\'/g,"");
var dialog='';
//-----------------------------------------------------------------------------------------------------------------------------
/*日常功能判定*/
var customFunction=new Array;
if( sentence.search(/主页|首页/) != -1 ){
customFunction.push("<a href=''>[主页]</a>");
};
if( sentence.search(/邮箱|郵箱|信件|信息|短信|消息|邮件|收信|写信/) != -1 ){
customFunction.push("<a href='message.php'>[邮箱]</a>");
};
if( sentence.search(/银行|銀行|存款|取款|存钱|取钱/) != -1 ){
customFunction.push("<a href='hack.php?H_name=bank'>[银行]</a>");
};
if( sentence.search(/搜索|查找|查询|查看/) != -1 ){
customFunction.push("<a href='search.php'>[搜索]</a>");
};
if( sentence.search(/股市|股票|炒股/) != -1 ){
customFunction.push("<a href='hack.php?H_name=stock'>[股市]</a>");
};
if( sentence.search(/农场|牧场|偷菜|种菜|收菜/) != -1 ){
customFunction.push("<a href='hack.php?H_name=newfarm'>[农场]</a>");
};

if(customFunction.length==1){
dialog='您想要前往'+dialog+customFunction[0]+'吗？';
tomoTalk(dialog);
}else if(customFunction.length>1){
for(i=0;i<customFunction.length-1;i++){
dialog=dialog+customFunction[i]+'、';
};
dialog='您想要前往'+dialog.slice(0,-1)+'和'+customFunction[customFunction.length-1]+'吗？';
tomoTalk(dialog);
/*日常功能判定结束*/
//-----------------------------------------------------------------------------------------------------------------------------
}else{
//-----------------------------------------------------------------------------------------------------------------------------
/*装傻*/
dialog='啊哈哈，琉璃不清楚。';
tomoTalk(dialog);
/*装傻结束*/
//-----------------------------------------------------------------------------------------------------------------------------
};

};

function tomoDramaRecite(dramaName){
switch(dramaName){
	case "public":
	sindex=0;
	break;
	case "common":
	sindex=1;
	break;
	case "menu":
	sindex=2;
	break;
	case "game":
	sindex=3;
	break;
	case "link":
	sindex=4;
	break;
	case "ero":
	sindex=5;
	break;
	default:
	sindex=6;
};
dindex=Math.round((Math.random()*(drama[sindex].scene.length-1)));
lindex=0;
tomoTalk( drama[sindex].scene[dindex].dialog[lindex].lines );
};

function tomoNext(){

lindex=lindex+1;
if(lindex>(drama[sindex].scene[dindex].dialog.length-1)){lindex=0;dindex=Math.round((Math.random()*(drama[sindex].scene.length-1)));};
tomoTalk( drama[sindex].scene[dindex].dialog[lindex].lines );
if(lindex<(drama[sindex].scene[dindex].dialog.length-1)){
$('#tomodialog').html( $('#tomodialog').html()
+"<br /><br />"
+"<a class='pointer mbutton tomonext'>[继续]</a>"
);}
else{
$('#tomodialog').html( $('#tomodialog').html()
+"<br /><br />"
+"<a class='pointer mbutton tomomenu'>[返回]</a>"
);};
//tomoeffect();
};