function create() { return true; }

var step;
var charactersWritten;
var mouseCoords = new Array(0,0);

function addEventListener(element, event, callback)
{
	if(element.addEventListener)
		element.addEventListener(event, callback, false);
	else
		element.attachEvent('on' + event, callback);
}

function start()
{
	document.body.style.cursor = 'wait';
	
	charactersWritten = 0;
	step = document.getElementById('step');
	step.style.backgroundColor = '#FFF';
	step.style.color = '#000';
	step.style.fontSize = '16px';
	
	step.innerHTML = "Étape 1";
	
	var colorAnimation = new Animation(step, 400);
	colorAnimation.onfinishDelegate = writeQuery;
	colorAnimation.style.backgroundColor = '#EC0';
	colorAnimation.onfinish = revertColorAnimation;
	colorAnimation.start(Animation.LINEAR);
	
	var sizeAnimation = new Animation(step, 100);
	sizeAnimation.style.fontSize = '60px';
	sizeAnimation.onfinish = revertSizeAnimation;
	sizeAnimation.start();
	
	addEventListener(document, 'mousemove', followMouse);
}

function followMouse(event)
{
	if(event == undefined)
		event = window.event;
	mouseCoords[0] = event.clientX;
	mouseCoords[1] = event.clientY;
}

function revertColorAnimation(ani)
{
	var animation = new Animation(step, 400)
	animation.style.backgroundColor = '#FFF';
	if(ani.onfinishDelegate)
		animation.onfinish = ani.onfinishDelegate;
	animation.start();
}

function revertSizeAnimation(ani)
{
	var sizeAnimation = new Animation(step, 200);
	sizeAnimation.style.fontSize = '16px';
	sizeAnimation.start();
}

function writeQuery(ani)
{
	step.innerHTML += '<br>Entrez les termes de votre recherche.';
	var q = document.getElementById('q');
	q.style.backgroundColor = '#FFF';
	q.focus();
	
	var animation = new Animation(q, 400);
	animation.style.backgroundColor = '#EC0';
	animation.onfinish = function() {
		var animation = new Animation(q, 400);
		animation.style.backgroundColor = '#FFF';
		animation.start();
		setTimeout(writeCharacter, 500);
	};
	animation.start();
}

function writeCharacter()
{
	if(charactersWritten >= queryString.length)
	{
		setTimeout(next, 500);
		return;
	}
	
	document.getElementById('q').value += queryString.charAt(charactersWritten);
	charactersWritten++;
	setTimeout(writeCharacter, 150);
}

function next()
{
	step.innerHTML = "Étape 2";
	
	var colorAnimation = new Animation(step, 400);
	colorAnimation.onfinishDelegate = simulateMouse;
	colorAnimation.style.backgroundColor = '#EC0';
	colorAnimation.onfinish = revertColorAnimation;
	colorAnimation.start(Animation.LINEAR);
	
	var sizeAnimation = new Animation(step, 100);
	sizeAnimation.style.fontSize = '60px';
	sizeAnimation.onfinish = revertSizeAnimation;
	sizeAnimation.start();
}

function simulateMouse()
{
	step.innerHTML += "<br>Cliquer sur « Rechercher »";
	
	var cursor = document.createElement('img');
	// let's assume anyone not using a Mac is using Windows; after all Linux users have a tendency to be able
	// to use Google by themselves ;)
	if(navigator.platform.indexOf("Mac") == -1)
		cursor.src = 'images/windows_cursor.png';
	else
		cursor.src = 'images/apple_cursor.png';
	cursor.style.position = "absolute";
	cursor.style.left = mouseCoords[0] - 4 + "px";
	cursor.style.top = mouseCoords[1] - 4 + "px";
	document.body.appendChild(cursor);
	
	var search = document.getElementById('search');
	
	var clickAnimation = new Animation(cursor, 2000);
	clickAnimation.style.left = search.offsetLeft + search.offsetWidth / 2 + "px";
	clickAnimation.style.top = search.offsetTop + search.offsetHeight / 2 + "px";
	clickAnimation.onfinish = function(ani)
	{
		search.focus();
		step.innerHTML = "Bravo !";
	
		var colorAnimation = new Animation(step, 400);
		colorAnimation.onfinishDelegate = function() { setTimeout(google, 1000); };
		colorAnimation.style.backgroundColor = '#EC0';
		colorAnimation.onfinish = revertColorAnimation;
		colorAnimation.start(Animation.LINEAR);
		
		var sizeAnimation = new Animation(step, 100);
		sizeAnimation.style.fontSize = '60px';
		sizeAnimation.onfinish = revertSizeAnimation;
		sizeAnimation.start();
	}
	clickAnimation.start();
}

function google(ani)
{
	step.innerHTML = "Alors, tu y arriveras sans nous la prochaine fois ?";
	
	var colorAnimation = new Animation(step, 400);
	colorAnimation.onfinishDelegate = function()
	{
		setTimeout(function() { document.getElementById('form').submit(); }, 2000);
	};
	colorAnimation.style.backgroundColor = '#EC0';
	colorAnimation.onfinish = revertColorAnimation;
	colorAnimation.start(Animation.LINEAR);
	
	var sizeAnimation = new Animation(step, 100);
	sizeAnimation.style.fontSize = '20px';
	sizeAnimation.onfinish = revertSizeAnimation;
	sizeAnimation.start();
	
	return false;
}
