var timers =  [];
var urls = {
	'img1' : { 'time': 3500, 'urls' :	[ 
											{ 'src' : '/images/publicites/pub1.jpg',	'link' : 'produitdetail.php?id=158' },
											{ 'src' : '/images/publicites/pub1bis.jpg',	'link' : 'produitdetail.php?id=20' }
										]
			},
	'img2' : { 'time': 4500, 'urls' :	[ 
											{ 'src' : '/images/publicites/pub2.jpg',	'link' : 'produitdetail.php?id=93' },
											{ 'src' : '/images/publicites/pub2bis.jpg',	'link' : 'produitdetail.php?id=114' }
										]
			},
	'img3' : { 'time': 5000, 'urls' :	[ 
											{ 'src' : '/images/publicites/pub3.jpg',	'link' : 'produitdetail.php?id=129' },
											{ 'src' : '/images/publicites/pub3bis.jpg', 'link' : 'produitdetail.php?id=107' }
										]
			}
};
var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		pre_lapharmacie1 = newImage('images/menus/lapharmacie1.gif');
		pre_historique1 = newImage('images/menus/historique1.gif');
		pre_lequipe1 = newImage('images/menus/lequipe1.gif');
		pre_conseils1 = newImage('images/menus/conseils1.gif');
		pre_plantes1 = newImage('images/menus/plantes1.gif');
		pre_symptomes1 = newImage('images/menus/symptomes1.gif');
		pre_commandes1 = newImage('images/menus/commandes1.gif');
		pre_produitsnat1 = newImage('images/menus/produitsnat1.gif');
		pre_cosmetiques1 = newImage('images/menus/cosmetiques1.gif');
		pre_medicaments1 = newImage('images/menus/medicaments1.gif');
		pre_caddy1 = newImage('images/menus/caddy1.gif');
		pre_actualites1 = newImage('images/menus/actualites1.gif');
		pre_nouveautes1 = newImage('images/menus/nouveautes1.gif');
		pre_offres1 = newImage('images/menus/offres1.gif');
		pre_contact1 = newImage('images/menus/contact1.gif');
		pre_adresse1 = newImage('images/menus/adresse01.gif');
		preloadFlag = true;
	}
}

window.onload = function(){
	preloadImages();
	loadTimers();
}

function loadTimers() {
	var index = 0;
	for (var name in urls) {
		timers[index] = new Ticker(name, urls[name]['time'], urls[name]['urls']);
		timers[index].tick();
		index += 1;
	}
}


//----------------------------------------------------------------
function Ticker(imgName, tempo, params){
	this.imgName = imgName;
	var linkName = imgName+"_link";
	if (document.images) {
		this.preload = [];
		for (var i=0; i<params.length;i++) {
			this.preload[i] = new Image();
			this.preload[i].src = params[i]['src'];
		}
	} else 
		this.preload = null;
	
	this.image = document.getElementById(this.imgName) ? document.getElementById(this.imgName) : null;
	this.image = document.getElementById(this.imgName) ? document.getElementById(this.imgName) : null;
	this.link = document.getElementById(linkName) ? document.getElementById(linkName) : null;
	this.tempo = tempo;
	this.params = params;
	this.current = -1;
    this.timer = new Timer(this);
}

Ticker.prototype.tick = function(){
	this.current += 1;
	if (this.image)
		this.image.src = this.params[this.current]['src'];
	if (this.link) {
		this.link.href = this.params[this.current]['link'];
	}
	var len = this.params.length - 1;
	if (this.current == len) {
		this.current = -1;
	}
    this.timer.setTimeout("tick", this.tempo);
}
                   

//----------------------------------------------------------------
function Timer(){
    this.obj = (arguments.length)?arguments[0]:window;
    return this;
}

Timer.prototype.setInterval = function(func, msec){
    var i = Timer.getNew();
    var t = Timer.buildCall(this.obj, i, arguments);
    Timer.set[i].timer = window.setInterval(t,msec);
    return i;
}
Timer.prototype.setTimeout = function(func, msec){
    var i = Timer.getNew();
    Timer.buildCall(this.obj, i, arguments);
    Timer.set[i].timer = window.setTimeout("Timer.callOnce("+i+");",msec);
    return i;
}

Timer.prototype.clearInterval = function(i){
    if(!Timer.set[i]) return;
    window.clearInterval(Timer.set[i].timer);
    Timer.set[i] = null;
}
Timer.prototype.clearTimeout = function(i){
    if(!Timer.set[i]) return;
    window.clearTimeout(Timer.set[i].timer);
    Timer.set[i] = null;
}

Timer.set = new Array();
Timer.buildCall = function(obj, i, args){
    var t = "";
    Timer.set[i] = new Array();
    if(obj != window){
        Timer.set[i].obj = obj;
        t = "Timer.set["+i+"].obj.";
    }
    t += args[0]+"(";
    if(args.length > 2){
        Timer.set[i][0] = args[2];
        t += "Timer.set["+i+"][0]";
        for(var j=1; (j+2)<args.length; j++){
            Timer.set[i][j] = args[j+2];
            t += ", Timer.set["+i+"]["+j+"]";
    }}
    t += ");";
    Timer.set[i].call = t;
    return t;
}
Timer.callOnce = function(i){
    if(!Timer.set[i]) return;
    eval(Timer.set[i].call);
    Timer.set[i] = null;
}
Timer.getNew = function(){
    var i = 0;
    while(Timer.set[i]) i++;
    return i;
}