var elements = new Array();
var cursor = 0;
var timer = null;
var min_seconds = 5;

function setEncadre() {
    if (cursor>=(elements.length-1)) {
        cursor = 0;
    }
/**
    var id_suivant = cursor;
    if (id_suivant>=(elements.length-1)) {
        id_suivant = 0;
    }
    var emplacement_suivant = document.createElement('div');
    emplacement_suivant.setAttribute('id', 'laviepaschere_suivant');
    emplacement_suivant.style.display = 'none';
    var Image_suivant = document.createElement('img');
    Image_suivant.setAttribute('src', elements[id_suivant][0]);
    Image_suivant.setAttribute('alt', elements[id_suivant][2]);
    if (elements[id_suivant][4]) {
    	var fo = new SWFObject(elements[id_suivant][4], "laviepaschere_flash_suivant", "250", "250", "7", '');
    	// fo.addParam("wmode","transparent");
    	fo.addVariable("lien", elements[id_suivant][1]);
    	fo.write("laviepaschere_suivant");
    }
*/

    var emplacement = document.getElementById("laviepaschere");

    //
    var Lien = document.createElement('a');
    var nom = elements[cursor][5];
    var nom_fichier = elements[cursor][5]+'.gif';
    var url = 'http://www.toulouseweb.com/count_pubs.php?mini=0&nom='+nom;
    url =url+ '&nom_fichier='+nom_fichier+'&sous_menu=accueil&url='+elements[cursor][1];
    Lien.setAttribute('href', url);
    Lien.setAttribute('title', elements[cursor][2]);
    Lien.setAttribute('target', '_blank');

    //
    var Image = document.createElement('img');
    Image.setAttribute('src', elements[cursor][0]);
    Image.setAttribute('alt', elements[cursor][2]);

    Lien.appendChild(Image);

    emplacement.innerHTML = '';
    emplacement.appendChild(Lien);

    if (elements[cursor][4]) {
    	var fo = new SWFObject(elements[cursor][4], "laviepaschere_flash", "250", "250", "7", '');
    	// fo.addParam("wmode","transparent");
    	fo.addVariable("lien", elements[cursor][1]);
    	fo.write("laviepaschere");
    	plus_time = 2000;
    }

    countAffichage(nom, nom_fichier);

    ++ cursor;

    timer = setTimeout("setEncadre()", (elements[cursor][3]*1000));
}

function getArrayPubliciteByXML(reponseXML) {
    var Publicite = new Array();
    Publicite[0] = reponseXML.getElementsByTagName("picture")[0].firstChild.nodeValue;
    Publicite[1] = reponseXML.getElementsByTagName("lien")[0].firstChild.nodeValue;
    Publicite[2] = reponseXML.getElementsByTagName("alt")[0].firstChild.nodeValue;
    if (reponseXML.getElementsByTagName("seconds").length>0) {
        Publicite[3] = reponseXML.getElementsByTagName("seconds")[0].firstChild.nodeValue;
    } else {
        Publicite[3] = min_seconds;
    }
    if (reponseXML.getElementsByTagName("flash").length>0) {
        Publicite[4] = reponseXML.getElementsByTagName("flash")[0].firstChild.nodeValue;
    }
    Publicite[5] = reponseXML.getElementsByTagName("id")[0].firstChild.nodeValue;
    return Publicite;
}

function countAffichage(nom, nom_fichier) {
    var xhr = getXhr();
    xhr.open("GET", "count_affich_pubs.php?nom="+nom+"&amp;nom_fichier="+nom_fichier+"&amp;mini=0&amp;sous_menu=accueil", true);
    xhr.send(null);
}

function loadViePasChere() {
    var xhr = getXhr();
    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4) {
            reponse = clean(xhr.responseXML.documentElement);
            if (xhr.status == 200) {
                var mesPublicites = reponse.getElementsByTagName("publicite");
                for(i=0; i<mesPublicites.length; ++i) {
                    elements[i] = getArrayPubliciteByXML(mesPublicites[i]);
                }
                setEncadre();
            } else {
            }
        }
    }
    xhr.open("GET", "laviepaschere/promos.xml?"+Math.round(Math.random()*100000000), true);
    xhr.send(null);
}

function backPub() {
    cursor = cursor - 2;
    if (cursor<0) {
        cursor = 0;
    }
    clearTimeout(timer);
    setEncadre();
}

function nextPub() {
    clearTimeout(timer);
    setEncadre();
}

window.addEvent('domready', function() {
    loadViePasChere()
});

