﻿var T = {
	img_src: 'images/turtle-nofoot.png',
	img_foot_src: 'images/turtle.png',
	img_blink_src: 'images/turtle-blink.png',
	width: 156,
	height: 100,
	foot_height: 47, // foot hidden from top:47px
	
	px_per_em: document.getElementById('header_banner').offsetHeight / 6, // #header_banner should be 6 em
};

document.writeln('<div style="position:absolute; left:0; top:-'+(T.foot_height+1)+'px; padding-top:1em; width:100%;"><div style="margin:0 auto; width:'+T.width+'px;"><div style="position:absolute;">' +
		'<img id="turtle_foot" src="'+T.img_foot_src+'" style="width:'+T.width+'px; height:'+T.height+'px; border:none; position:absolute; top:-1000px;">' +
		'<img id="turtle" src="'+T.img_src+'" style="width:'+T.width+'px; height:'+T.height+'px; border:none; position:absolute; top:-1000px; cursor:pointer;">' +
		'</div></div></div>');
T.img_foot = document.getElementById('turtle_foot');
T.img = document.getElementById('turtle');

T.timeout = function(js, secs) { window.setTimeout('T.'+js, secs*1000); }
T.img.onclick = function() { T.blink(); }
T.img.ondblclick = function() { window.location = (window.location.href.indexOf('?file=')!=-1?'?file=':'') + 'Go_turtle%21'; }
T.move = function(h) {
	if (h < 0) {
		h += 1 / 5;
		h = Math.floor(0.9*h*5) / 5;
	} else if (h == 0) { // pause
		T.timeout('move(' + (1/5) + ')', 10);
		T.blink(5);
		return; // pause
	} else if (h < 60) {
		h += (60-h)*h/(30*30) + 1/5;
		h = Math.floor(h*5) / 5;
		bottom = (60-h)/10 * T.px_per_em + T.foot_height;
		T.img_foot.style.clip = 'rect(0, ' + T.width + 'px, ' + bottom + 'px, 0)';
	} else {
		T.timeout('blink(2)', 2);
		return; // quit
	}
	T.img.style.top  = T.img_foot.style.top  = (h/10) + 'em';
	T.img.style.left = T.img_foot.style.left = Math.sin(h/10)*1 + 'em';
	T.timeout('move('+h+')', 0.06);
}

T.blink = function(loops, reset) {
	if (!reset) {
		T.img.src = T.img_blink_src;
		T.timeout('blink(0, 1)', 0.2);
	} else
		T.img.src = T.img_src;
	if (loops && loops>1)
		T.timeout('blink(' + (loops-1) + ')', 1 + Math.random());
}

T.timeout('move(-' + (T.height-1) + ')', 10);
