/*
	:: E A S T E R  E G G  J a v a S c r i p t ::
	
	By [big_fury]SiZiOUS
	04 nov 2006
	Version 0.1
	
	You can use this code for your website if you want.
	Sorry comments in french. If you need some help please mail me at  sizious  (at)  dc-france  [dot]  com with subject "easter egg javascript".
	
	start method : easterEgg()
	end method : finalizeEasterEgg()
*/

/* --- CUSTOMIZE HERE --- */

// constantes
var FINAL_SCREEN_WIDTH = 320; // largeur finale de l'écran (la div egg_window)
var FINAL_SCREEN_HEIGTH = 240; // hauteur finale de l'écran (la div egg_window)
var FINAL_LEFT = 300; // position finale gauche
var FINAL_TOP = 53; // position finale haut

var NO_SIGNAL_DELAY = 2000; // le no signal sera affiché pendant POWER_ON_TIME millisecondes
var HORIZONTAL_OPEN_SPEED = 8; // vitesse d'ouveture horizontale
var VERTICAL_OPEN_SPEED = 16; // vitesse d'ouveture verticale

var OUTRO_QUAKE_DURATION = 4000; // 1 sec
var OUTRO_HORIZONTAL_CLOSE_SPEED = 18;
var OUTRO_VERTICAL_CLOSE_SPEED = 18;

/* --- PRIVATE SECTION DON'T CHANGE IF YOU DON'T KNOW WHAT YOU ARE DOING --- */

// variables temporaires
var easter_egg_timer = null; // timer qui permet de tout faire à peu près (animation du début et de fin)

// éléments HTML
var egg_window = null; // div principale (la fenêtre quoi)
var img_component = null; // image à afficher au final !

// variables temporaires de calcul
var show_screen_during = 0; // temps pendant lequel l'image sera affichée (lue depuis la page : input hidden id = easter_egg_animation_duration)
var current_width = 0; // largeur courante
var current_heigth = 0; // hauteur courante

var start_left = 0; // point de départ gauche
var start_top = 0; // point de départ haut

var outro_left = 0; // point de départ gauche pour l'outro
var outro_top = 0; // point de départ haut pour l'outro
var outro_quake_time = 0; // temps pour l'outro

// commencer l'animation
function runEasterEgg() {
	if (easter_egg_timer != null) { // déjà en cours
		alert('We get signal ! Main screen turn on !'); // IE arrête le script si l'alert est affiché. Connard d'IE, quoi, comme d'hab.
		return; 
	}
	
	initEgg();
	
	egg_window = document.getElementById('egg_win');
	img_component = document.getElementById('egg_screen');
	
	if (egg_window == null) {
		/* merde dommage y'a un blem... on affiche juste une alert à la con ^^ */
		alert('How are you gentlemen !! All you base are belong to us.'); 
		return; 
	}
	if (img_component == null) return;
	
	// récuperer la durée de l'animation dans la page (en milli secondes)
	var anim_duration = document.getElementById("easter_egg_animation_duration");
	if (anim_duration == null) return;
	show_screen_during = anim_duration.value; // temps pendant lequel l'animation doit être affichée
	
	img_component.style.display = 'none';
	egg_window.style.display = "block";
	
	start_left = FINAL_LEFT + (FINAL_SCREEN_WIDTH / 2);
	start_top = FINAL_TOP + (FINAL_SCREEN_HEIGTH / 2);
	
	// init de la fenêtre (point central d'ou partira l'animation)
	egg_window.style.left = start_left + 'px';
	egg_window.style.width = '0px';
	egg_window.style.top = start_top + 'px';
	egg_window.style.height = '0px';

	// commencer l'animation
	easter_egg_timer = setInterval("doIntroHorizontalScreenScroll()", 1); //nouveau code avec ralentissement du scroll
}

// finir l'animation
function stopEasterEgg() {
	if (easter_egg_timer != null) {
		clearTimeout(easter_egg_timer);
		easter_egg_timer = null;
	}
	
	if (egg_window == null || img_component == null) return; // problème
	
	changeImageState(img_component, false); // on élimine l'image ... on la fera réapparaitre quand le mec demande de l'afficher ^^
	
	noSignal(true);
	easter_egg_timer = setInterval("doOutroQuake();", 1);
}

// --------- PRIVATE FUNCTIONS --------- //

// reinit Egg
// fonction spécial ANTI CASSE COUILLE IE : y'a pas moyen que ça marche si c'est remis à 0 dans doOutroFinalize... IE DE MERDE !
function initEgg() {
	current_width = 0;
	current_heigth = 0;
	start_left = 0;
	start_top = 0;
	outro_left = 0;
	outro_top = 0;
	outro_quake_time = 0;
	show_screen_during = 0;
	easter_egg_timer = null;
}

// cacher ou afficher une image
function changeImageState(image, state) {
	if (state) { // on affiche 
		image.src = image.src.substring(0, image.src.length - 1); // charger l'image pour la faire commencer au début	
		image.style.display = 'block'; // on affiche l'image
	} else {
		image.src += "_"; // on met un trait à la fin du fichier pour rendre l'image invalide
		image.style.display = 'none'; // on cache l'image
	}
}

// allumer l'écran : ça fait un joli effet "nosignal" :D
function noSignal(state) {
	var nosignal = document.getElementById("nosignal");
	if (nosignal == null) return; // visiblement y'a un problème... j'espère pas !
	changeImageState(nosignal, state);
}

// animation horizontale d'ouverture écran (1 ère fonction appellée dans easterEgg)
function doIntroHorizontalScreenScroll() {	
	//alert(current_width);
	
	// doIntroHorizontalScreenScroll terminée !
	if (current_width >= FINAL_SCREEN_WIDTH) {
		clearTimeout(easter_egg_timer); // ok on arrête ce timer
		easter_egg_timer = setInterval("doIntroVerticalScreenScroll()", 1); //lancer pour le top
	}
	
	egg_window.style.width = current_width + 'px';
	egg_window.style.left = start_left - (current_width / 2) + 'px';
	
	current_width += HORIZONTAL_OPEN_SPEED; // vitesse d'ouverture
}

// animation verticale d'ouverture écran (2ième fonction lancée après dans easterEgg, lancée par doHorizontalScreenScroll)
function doIntroVerticalScreenScroll() {
	
	// doIntroVerticalScreenScroll terminé !
	if (current_heigth >= FINAL_SCREEN_HEIGTH) {
		clearTimeout(easter_egg_timer);
		
		// s'assurer que c'est bien la taille attendue
		egg_window.style.width = FINAL_SCREEN_WIDTH + 'px';
		egg_window.style.heigth = FINAL_SCREEN_HEIGTH + 'px';
	
		noSignal(true); //afficher nosignal
		easter_egg_timer = setTimeout("doIntroFinalize();", NO_SIGNAL_DELAY); // afficher le "nosignal" pendant NO_SIGNAL_DELAY
	}
	
	egg_window.style.height = current_heigth + 'px';
	egg_window.style.top = start_top - (current_heigth / 2) + 'px';
		
	current_heigth += VERTICAL_OPEN_SPEED; // vitesse d'ouverture
}

// terminer l'animation de début (afficher la vraie image quoi), 3ième fonction appellée après easterEgg, lancée par doVerticalScreenScroll
function doIntroFinalize() {
	clearTimeout(easter_egg_timer); //on arrête le timer
		
	// on enlève le no signal
	noSignal(false);
	
	// on va afficher notre image finale
	if (img_component == null) {
		alert('Oh no !... problem when finishing animation :\'(');
		return; // merde ! problème !
	}
	
	// afficher l'image
	changeImageState(img_component, true);
	
	/* 
	egg_window.style.left = '50%';
	egg_window.style.top = '50%';
	egg_window.style.marginTop = '-120px'; //moitié de la hauteur
	egg_window.style.marginLeft = '-160px'; //moitié de la largeur 
	*/
	
	// lancer le tempo pendant laquelle l'image sera affichée
	easter_egg_timer = setTimeout("stopEasterEgg();", show_screen_during);
}

// trembler l'écran
function doOutroQuake() {
	// fini !
	if (outro_quake_time >= OUTRO_QUAKE_DURATION) {
		clearTimeout(easter_egg_timer);
		
		// revenir en position initiale
		egg_window.style.left = FINAL_LEFT + 'px';
		egg_window.style.top = FINAL_TOP + 'px';
		
		noSignal(false); // on éteind l'écran
		easter_egg_timer = setInterval("doOutroVerticalScroll();", 1); // on ferme verticalement
	}
	
	outro_left = FINAL_LEFT + (outro_left % 5);
	outro_top = FINAL_TOP + (outro_top % 5);
	outro_left++;
	outro_top++;
	
	egg_window.style.left = outro_left + 'px';
	egg_window.style.top = outro_top + 'px';
	
	outro_quake_time += 60; // une seconde
}

// fermer verticalement
function doOutroVerticalScroll() {

	// doOutroVerticalScroll terminé !
	if (current_heigth <= 0) {
		clearTimeout(easter_egg_timer);
		egg_window.style.height = '0px';
		easter_egg_timer = setInterval("doOutroHorizontalScroll();", 1); // on ferme horizontalement
	} else { // on empêche les valeurs négatives... étant donné que le timer est plus rapide que l'execution de la fonction on affectait des valeurs négatives...
		egg_window.style.height = current_heigth + 'px';
		egg_window.style.top = start_top - (current_heigth / 2) + 'px';
		current_heigth -= OUTRO_VERTICAL_CLOSE_SPEED; // vitesse de fermeture
	}	
	
}

// fermer horizontalement
function doOutroHorizontalScroll() {

	// doOutroHorizontalScroll terminée !
	if (current_width <= 0) {
		clearTimeout(easter_egg_timer); // ok on arrête ce timer
		doOutroFinalize(); // c'est FINI !
	}
	
	egg_window.style.width = current_width + 'px';
	egg_window.style.left = start_left - (current_width / 2) + 'px';
	
	current_width -= OUTRO_HORIZONTAL_CLOSE_SPEED; // vitesse de fermeture
}

// c'est fini !
function doOutroFinalize() {
	egg_window.style.display = 'none'; // on cache la fenêtre easter egg
	
	// re-init
	initEgg();
	
	// terminé !
}

/*
	// ancien code intro permettant de scroller mais sans delay donc on voit pas le scroll !
	
	for(var w = 0 ; w < FINAL_SCREEN_WIDTH ; w++) {
		egg.style.width = w + 'px';
		egg.style.left = start_left - (w / 2) + 'px';
	}

	for(var h = 0 ; h < FINAL_SCREEN_HEIGTH ; h++) {
		egg.style.height = h + 'px';
		egg.style.top = start_top - (h / 2) + 'px';
	} 
*/