var stp = 0;
var l = '';
var cp;
var _mp;
var mp;
var newMP;
var p;
var tp;
var iid_teke;

var op_ouchi = false;

$(document).ready(function(){
	preload([
		home+'/img/book_close.gif',
		home+'/img/book_off.gif',
		home+'/img/book_on.gif',
		home+'/img/book_open.gif',
		home+'/img/teke_l.gif',
		home+'/img/teke_stand_l.gif',
		home+'/img/teke_stand.gif',
		home+'/img/teke_stop_l.gif',
		home+'/img/teke_stop.gif',
		home+'/img/teke.gif',
	]);

	$('a[rel^=lightbox]').lightBox();

	iid_teke = setInterval(
		function(){
			if(_mp == newMP) return;
			_mp = newMP;
			p = $('#teke').position();
			cp = $('#body').offset();
			mp = newMP - cp.left;

			if(mp<=10){
				tp = 10;
			}else if(mp>990){
				tp = 990;
			}else{
				tp = mp;
			}

			$('#teke').animate(
				{left: tp+"px"},
				{
					duration: 10000,
					easing: "easeOutCirc"
				}
			);



			if(p.left<mp){
				l = '';
			}else if(p.left>mp){
				l = '_l';
			}else{
				img = 'teke_stop'+l;
				$('#teke .img').load(home+'/img/get_image.php?img='+img);
			}

		},2600);


	$('#main').mousemove(function(e){
		newMP = e.pageX;
	});

	$('#teke .cover').mouseover(function(){
		if(!stp){
			stp=1;
			img = 'teke_stand'+l;
			$('#teke .img').load(home+'/img/get_image.php?img='+img,null,function(){stp=0;});
		}
	});
	$('#teke .cover').mouseout(function(){
		if(!stp){
			stp=1;
			img = 'teke'+l;
			$('#teke .img').load(home+'/img/get_image.php?img='+img,null,function(){stp=0;});
		}
	});


	$('.booka:not(.hover)').append('<div class="book"><div class="img"><img src="'+home+'/img/book_close.gif" alt="" /></div><div class="cover"></div></div>');
	$('.booka.hover').append('<div class="book"><div class="img"><img src="'+home+'/img/book_open.gif" alt="" /></div></div>');
	$('.booka:not(.hover)').mouseover(over);
	$('.booka:not(.hover)').mouseout(out);

	//ouchi
	$('.ouchi').click(function(){
		if(op_ouchi==this){
			op_ouchi=false;
			ouchi_reset(this);
		}else{
			op_ouchi=this;
			ouchi_reset('all');
			$(this).children('.box').children('img').attr({src:home+'/img/ouchi.gif'});
			$(this).children('.box').stop();
			$(this).children('.box').children('img').stop();
			//$(this).children('p').hide();
			$(this).children('.box').animate({width:'460px',height:'300px',top:(180-$(this).position().top)+'px',left:($(this).position().left*-1)+'px'}
				,{
					duration:700,
					easing:'easeOutBack',
					complete:function(){
						$(this).children('.mess').show();
					}
				}
			);
			$(this).children('.box').children('img').animate({width:'181px',height:'152px'}
			,{
				duration:700,
				easing:'easeOutBack',
				complete:function(){
				}
			}
		);
		}
	});

	if($('#contents .singlepost h1').size()>0){
		$('#contents .singlepost h1').wrap('<div id="pagetitle"></div>');
		$('#contents .singlepost h1').before('<b class="l"></b>');
		$('#contents .singlepost h1').after('<b class="r"></b>');
	}

});
function ouchi_reset(ouchi){
	obj = (ouchi=='all') ? $('.ouchi') : $(ouchi) ;
	obj.children('.box').stop();
	obj.children('.box').children('img').stop();
	obj.children('.box').animate(
		{width:'22px',height:'19px',left:'0',top:'0'}
		,{
			duration:500,
			easing:'easeOutBack'
		}
	);
	obj.children('.box').children('img').animate(
			{width:'22px',height:'19px'}
			,{
				duration:500,
				easing:'easeOutBack',
				complete:function(){
					$(this).attr({src:home+'/img/ouchi_s.gif'});
					$(this).parent('.ouchi').children('p').show();
					$(this).children('.mess').hide();
				}
			}
		);
}

function over(){
	img = 'book_on';
	$(this).children('.book').children('.img').load(home+'/img/get_image.php?img='+img);
	$(this).children('b').fadeOut('slow');
}
function out(){
	img = 'book_off';
	$(this).children('.book').children('.img').load(home+'/img/get_image.php?img='+img);
	$(this).children('b').fadeIn('slow');
}
function preload(arrayOfImages) {
	$(arrayOfImages).each(function(){
		$('<img/>')[0].src = this;
	});
}
function deb(txt){
	$('#deb').html($('#deb').html()+txt);
}



