/*	gold_pod.js
	copyleft Thomas Baspeyras 2005

requires:
	dom.js
	js_lib.js
	
index:
	GoldPod()
		add(text, image)
		addGoldPod(id)
		start()
*/

function GoldPod(path) {

/* 	start -> randomRecord -> showImage & showText

	showImage -> slide -> pause
	showText -> type -> pause

	pause -> unShowImage -> unSlide -> restart
*/

	var records = new Array;
	var podText;
	var podImage;

	function Record(text, image) {
		this.text = text;
		this.image = image;
	}

	this.add = function(text, image) {
		records.push(new Record(text, path + image))
	}

	this.addGoldPod = function(id) {
		var container = document.getElementById(id);
		podText = div();
		podText.id = "podText";
		podImage = div();
		podImage.id = "podImage";
		appendTo(container, podText, podImage);
	}

	var toSlide;
	var height;
	var left;
	var top;
	var width;
	var mix;

	this.slide = function() {
		function interpolate(from, to) {
			return Math.round(Math.sqrt(mix) * (to - from) + from);
		}
		mix += 0.025;
		toSlide.style.height = interpolate(height, 100) + "px";
		toSlide.style.left = interpolate(left, 0) + "px";
		toSlide.style.top = interpolate(top, 0) + "px";
		toSlide.style.width = interpolate(width, 100) + "px";
		if (mix >= 1) setTimeout("goldPod.pause('doneImage')", 2000);
		else setTimeout("goldPod.slide()", 50);
	}

	this.unSlide = function() {
		function interpolate(from, to) {
			return Math.round(mix * mix * (to - from) + from);
		}
		mix += 0.025;
		toSlide.style.height = interpolate(100, height) + "px";
		toSlide.style.left = interpolate(0, left) + "px";
		toSlide.style.top = interpolate(0, top) + "px";
		toSlide.style.width = interpolate(100, width) + "px";
		if (mix >= 1) setTimeout("goldPod.restart('doneImage')", 500);
		else setTimeout("goldPod.unSlide()", 40);
	}

	this.showImage = function(image) {
		toSlide.src = image;
		height = 99 * Math.round(Math.random()) + 1;
		left = 100 * (Math.round(2 * Math.random()) - 1);
		top = 100 * (Math.round(2 * Math.random()) - 1);
		width = 99 * Math.round(Math.random()) + 1;
		mix = 0;
		/*	Give 500 ms to image loading
			Donne 500 ms au chargement de l'image
		*/
		setTimeout("goldPod.slide()", 500);
	}

	this.unShowImage = function() {
		height = 99 * Math.round(Math.random()) + 1;
		left = 100 * (Math.round(2 * Math.random()) - 1);
		top = 100 * (Math.round(2 * Math.random()) - 1);
		width = 99 * Math.round(Math.random()) + 1;
		mix = 0;
		this.unSlide();
	}

	var typingIndex = 0;
	var toType;

	this.type = function() {
		if (typingIndex < toType.length) {
			if (toType.charAt(typingIndex) == ";") appendTo(podText, br());
			else appendTo(podText, textNode(toType.charAt(typingIndex)));
			typingIndex += 1;
			setTimeout("goldPod.type()", 100);
		}
		else setTimeout("goldPod.pause('doneText')", 2000);
	}

	this.showText = function(text) {
		typingIndex = 0;
		toType = text;
		this.type();
	}

	var previous;

	this.randomRecord = function() {
		for (var i = 0; i < 10; i++) {
			var record = records.random();
			if (record != previous) { previous = record; break; }
		}
		this.doneImage = false;
		this.doneText = false;
		this.showImage(record.image);
		this.showText(record.text);
	}

	this.pause = function(doneThat) {
		this[doneThat] = true;
		if (this.doneImage && this.doneText) {
			setTimeout("goldPod.unShowImage()", 500);
		}
	}

	this.restart = function(doneThat) {
		this[doneThat] = true;
		if (this.doneImage && this.doneText) {
			while(podText.hasChildNodes()) podText.removeChild(podText.childNodes[0]);
			setTimeout("goldPod.randomRecord()", 500);
		}
	}

	this.start = function() {
		toSlide = img("", "Pochette de disque", "Un disque Gold");
		appendTo(podImage, toSlide);
		this.randomRecord();
	}
}

var goldPod = new GoldPod("gold_pod/");

addLoadEvent(
	function() {
		goldPod.add("Alabina;L'essentiel;;Réalisation, arrangements et enregistrement : Ronny Gold", "alabina_l_essentiel.jpg");
		goldPod.add("Alabina;The Album;;Réalisation, arrangements et enregistrement : Ronny Gold", "alabina_the_album.jpg");
		goldPod.add("Francis Lalanne;Face cachée;;Réalisation, arrangements et enregistrement : Ronny Gold", "francis_lalanne_face_cachee.jpg");
		goldPod.add("Francis Lalanne;Flash-back;;Réalisation, arrangements et enregistrement : Ronny Gold", "francis_lalanne_flash_back.jpg");
		goldPod.add("Gloria Gaynor;I Will Survive;;Réalisation, arrangements et enregistrement : Ronny Gold", "gloria_gaynor_i_will_survive.jpg");
		goldPod.add("Ishtar;;La voix d'Alabina", "ishtar.jpg");
		goldPod.add("Kaoma;A La Media Noche;;Réalisation, arrangements et enregistrement : Ronny Gold", "kaoma_a_la_media_noche.jpg");
		goldPod.add("Larusso;Il suffira;;Far Away (Larusso / D. Bueno / R. Gold)", "larusso_il_suffira.jpg");
		goldPod.add("Latifa;Inchallah;;Réalisation et arrangements :;Ronny Gold", "latifa_inchallah.jpg");
		goldPod.add("Los Ninõs de Sara;La Cubanita;;Réalisation, arrangements et enregistrement : Ronny Gold", "los_ninos_de_sara_la_cubanita.jpg");
		goldPod.add("Luc Cherki;Mazal;;Réalisation, arrangements et enregistrement : Ronny Gold", "luc_cherki_mazal.jpg");
		goldPod.add("Mariage Mixte;;Bande originale du film d'Alexandre Arcady", "mariage_mixte.jpg");
		goldPod.add("Reines de Saba;Dorbia;;Bande originale du film Mariage Mixte", "reines_de_saba_dorbia.jpg");
		goldPod.add("Ricardo Torres y su grupo Huracan;De dos hemisferios;;Une coproduction Studio Gold", "ricardo_torres_de_dos_hemisferios.jpg");
		goldPod.add("Samy Goz;French Love Songs;;Enregistré et mixé au Studio Gold", "samy_goz_french_love_songs.jpg");
		goldPod.add("Samy Goz;Love Songs;;Enregistré et mixé au Studio Gold", "samy_goz_love_songs.jpg");
		goldPod.add("Samy Goz;Salsa Rica;;Enregistré et mixé au Studio Gold", "samy_goz_salsa_rica.jpg");
		goldPod.add("Shake;Peace in the Eastern Town;;Mixage : Ronny Gold", "shake.jpg");
		goldPod.addGoldPod("goldPod");
		goldPod.start();
	}
)

