var j = 0, m = 0, q = 0, n = 1, v = 1, coeff;
var ta = new Array(), tb = new Array(), td = new Array();
for (i = 0; i < 11; i++) {
	img = new Image(8, 16); img.src = "img/a" + i + ".gif"; ta[i] = img;
	if (i < 10 ) { img = new Image(10, 15); img.src = "img/b" + i + ".gif"; tb[i] = img; }
}
for (i = 0; i < 5; i++) {img = new Image(35, 13); img.src = "img/d" + i + ".gif"; td[i] = img;}
var tempo = new Array(250, 130, 80, 40, 1);
var o1, o2, x0, y0;

$(document).ready(function() {
	o1 = $("#obj1")[0].style;
	o2 = $("#obj2")[0].style;
	placeObjets();
	o1.display = "block";
	o2.display = "block";
});

$(window).resize(function() { placeObjets(); });

$(window).scroll(function() { placeObjets(); });

function placeObjets() {
	var pos = calculOffset(w.getElementById("plan"));
	x0 = pos[0] - 4;
	y0 = pos[1] - 4;
	o2.left = (x[j] + x0 - 3) + "px";
	o2.top = (y[j] + y0 - 2) + "px";
	o1.left = (x[j] + x0) + "px";
	o1.top = (y[j] + y0) + "px";
}

function affHeure(x) {
	var d, u, h = Math.floor(x/60);
	x = x - 60*h; d = Math.floor(x/10); u = x - 10*d;
	w.images['hr1'].src = tb[h].src;
	w.images['hr2'].src = tb[d].src;
	w.images['hr3'].src = tb[u].src;
}

function affDist(x) {
	var d, u, c = Math.floor(x/100);
	x = x - 100*c; d = Math.floor(x/10); u = x - 10*d; if (!c) c = 10;
	w.images['cpt1'].src = ta[c].src;
	w.images['cpt2'].src = ta[d].src;
	w.images['cpt3'].src = ta[u].src;
}

function affMsg(x, t, n) {
	var s = '', c, h;
	if (t) {
		x = Math.round(x*(1 - 1/coeff));
		c = Math.floor(x/10); x = x - 10*c;
		s = n ? 'Les premiers sont arrivés' : 'Le premier est arrivé';
		s += '. Il vous reste encore ' + c;
		if (x) s += ',' + x;
		s += ' km à parcourir en ';
		t = (coeff - 1)*t; h = Math.floor(t/60); t = Math.round(t - 60*h);
		if (h) s += h + 'h';
		if (t) {c = Math.floor(t/10); t = t - 10*c; if (h || c) s += c; s += t + "'";}
		s += ".";
	}
	$("#msg").text(s);
}

function Go() {
	if (m) return;
	coeff = w.forms['simul'].temps.value;
	if (!q) affMsg(0, 0, 0);
	m = 1;
	Bouge();
}

function Stop() {m = 0;}

function Depart() {j = m = q = 0; Go(); m = 0;}

function affPhoto(p) {
	if (!m) chgPhoto(p);
}

function chgPhoto(p) {
	n = p;
	w.images['photo'].src = ti[n].src;
	$("#divphoto a").attr("href", ti[n].src.replace('_',''));
}

function Modif(x) {
	v += x;
	if (v < 0) v = 0; else if (v > 4) v = 4;
	w.images['jauge'].src = td[v].src;
}

function calculOffset(obj) {
	var x = 0, y = 0;
	while (obj) {
		x += obj.offsetLeft;
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return [x, y];
}

