﻿var neco = new Neco();
var timer_help;

function Neco(){

	// nade item
	this.n_move = 0;
	this.n_beforeX = -1;
	this.n_beforeY = -1;
	
	// help function
	this.help_on = function(text){
		timer_help = setInterval('$("#neco_text2").html("'+text+'");$("#neco_text").hide("fast");$("#neco_text2").show("fast");', 500);
		
	}
	this.help_off = function(){
		clearInterval(timer_help);
		$("#neco_text2").hide('fast');
		$("#neco_text").show('fast');
	}
	
		// say item
	//this.say = (
	//	('escape','abuse','abuse','abuse','abuse'),
	//	('complaint','complaint','complaint','complaint','complaint')
	//	);
	this.say = function(){
		var tranList = {
			"sleep":5,
			"hungry":5,
			"name":5
		}
		clearInterval(timer);
		var out = '';
		if(!$.cookie('name')){
			out = '覚えてやるから、あなたのなまえを教えてほしいのです。<br><input type="text" size="10" id="inName" /><input type="button" value="教える" name="教える" onClick="neco.setName();" />';
		}

		if(!out){
			var tranTbl = new Array();
			$.each(tranList, function(key,val){
					for(i=0; i<val; i++){
						tranTbl.push(key);
					}
			});
			var task = tranTbl[Math.floor( tranTbl.length * Math.random() )];
			switch(task){
			case 'sleep':
				out = 'Zzzzz Zzzzz...';
				break;
			case 'hungry':
				out = '[name]ー、お腹減ったよ。';
				break;
			case 'name':
				out = '[name]!、呼んでみただけ';
				break;
			}
		}
		
		out = out.replace('[name]',$.cookie('name'));
		$("#neco_text").html(out);
	}

	this.setName = function(){
		var name = $("#inName").val();
		if(!name){
			name='もこぽこもこ';
			$("#neco_text").html("おしえてくれないの？<br>じゃあ、"+name+"さんて呼びますよ。");
		}else{
			$("#neco_text").html(name+"さんですか、ちゃっと覚えたよ。");
		}
		$.cookie('name', name, { expires: 30 });
	}
}

function init_neco(){
	// set help
	var helpList = {
		'#menu_photo':'そこをクリックすると「のらねこの写真」にいきます',
		'#menu_about':'「このサイトについて」のページにいきます',
		'#menu_neco':'わたしのことが書かれてるページみたいです。<br>ちょっと照れるのです',
		'#menu_webm':'ウェブマスターの紹介だって<br>つまらないので見ないで良いですよ。',
		'.link_photoList':'写真一覧のページにいきます',
		'.img_size':'写真のサイズを変えます。<br>Sはサバ、Mはマグロ、Lはラクダです。',
		'.nav .other_album':'見たいアルバムを選んでくださいな',
		'.return_list':'写真の一覧に戻るみたい',
		'#photo_prev':'前の写真にいきます。前っていうのは次の逆です。',
		'#photo_next':'次の写真にいきます。'
	};
	
	$.each(helpList, function(target,text){
		$(target).mouseover(function(){neco.help_on(text);});
		$(target).mouseout(function(){neco.help_off();});
	});
	
	// なでる catch
	
	$("#neco_face").mousemove( function(p){
		if(neco.n_beforeX >= 0){
			var move = Math.abs(neco.n_beforeX-p.clientX)+Math.abs(neco.n_beforeY-p.clientY);
			neco.n_move = neco.n_move + move;
		}else{
			move = 0;
		}
		neco.n_beforeX = p.clientX;
		neco.n_beforeY = p.clientY;
		if(neco.n_move >= 500){
			neco.n_move = 0;
			alert("なでた");
		}
	});

	$("#neco_face").mouseout( function(p){
		neco.n_beforeX = -1;
		neco.n_beforeY = -1;
	});
}