var compteurSlide = 0;
var tabSlide      = new Array();
var objectLnk     = null;

function C_handlerEvenement(self){
    if(self.xmlhttp.readyState == 4) {
        reponse = self.xmlhttp.responseText;
        var r = reponse.parseJSON();
        if(r['actif'] == '1')
            afficherPromo(r);
    }
}
function ClientPromo(){
    this.parent = ClientService;
    this.parent();
    this.handlerEvenement = C_handlerEvenement;
}
function imageUrl(img, url) {
	this.img = img; 
	this.url = url;
}
function lancerPromo(){
    var url = document.getElementsByTagName('base')[0].href;
    client = new ClientPromo();
    client.setUrl(url+'/achatFichiers/server_ajax.php?service=getPromo');
    client.executer();
}
function afficherPromo(p_infos){
    var bib = new Bibliotheque();
    var nomCookie = 'cpt_promo';
    var cookie = bib.getCookie(nomCookie);
 
    if(cookie == "")
        cookie = 1;
    else
        cookie = eval(cookie) + 1;
    bib.setCookie(nomCookie, cookie);
    if(cookie <= p_infos['nbAffichagePromo']){ 
        var left = (screen.width - eval(p_infos['largeurPromo']))/2;
        var top = (screen.height - eval(p_infos['hauteurPromo']))/2;
        
        var promo = window.open(p_infos['URLPromo'],"promo",
            "width="+p_infos['largeurPromo']+
            ",height="+p_infos['hauteurPromo']+
            ",left="+left+",top="+top+",location=no,menubar=no,status=no,toolbar=no");
        promo.focus();
        //promo.setTimeout('close()', 5000);
    }
}
function slideStart(param) {
	compteurSlide = 0;
	
	tabSlide      = param.split(';');
	objectLnk     = document.getElementById('pub_autres');
	
	slideElement();
}

function slideElement() {
	tabElementSlide = tabSlide[compteurSlide].split('!');
	delaySlide      = parseInt(tabElementSlide[0]);
	elementSlide    = tabElementSlide[1];
	
	ajoutElement(elementSlide);
	
	compteurSlide   = (compteurSlide + 1) % tabSlide.length;
	setTimeout("slideElement()", delaySlide);
}

function ajoutElement(listAffichage) {
	divPub = objectLnk;
	if (divPub) {
		// On supprime les noeuds enfant de la div
		supprimerNoeudsElement(divPub);
		
		// Création des noeuads image
		framesAffichage = listAffichage.split(',');
		nbFrames        = framesAffichage.length;
		for (i=0; i<nbFrames; i++) {
			frame  = framesAffichage[i].split('|');
			src    = frame[0];
			ext    = src.substring(src.lastIndexOf('.')+1, src.length);
			css    = frame[1];
			width  = frame[2];
			height = frame[3];
			url    = frame[4];
			cible  = frame[5];

			if (ext == 'swf') {
				//flash
				ajouterFlash(divPub, src, css, width, height, cible)
			} else {
				// images
				ajouterLienImage(divPub, src, css, width, height, url, cible);
			}
		}
	}
}

function supprimerNoeudsElement(noeud) {
	if ((noeud != undefined) && (noeud != null)) {
		while (noeud.hasChildNodes()) {
			noeud.removeChild(noeud.firstChild);
		}
	}
}

function ajouterLienImage(objConteneur, src, style, width, height, url, cible) {
	if (url != '') {
		objLnk           = document.createElement('a');
		objLnk.href      = url;
        objLnk.target    = cible;
		objLnk.className = style;
		objConteneur.appendChild(objLnk);
		
		objImg           = document.createElement('img');
		objImg.src       = src;
		objImg.border    = 0;
		objImg.width     = width;
		objImg.height    = height;
		objLnk.appendChild(objImg);
	} else {
		objImg           = document.createElement('img');
		objImg.src       = src;
		objImg.className = style;
		objImg.border    = 0;
		objImg.width     = width;
		objImg.height    = height;
		objConteneur.appendChild(objImg);
	}
}

function ajouterFlash(objConteneur, src, style, width, height, cible) {
	objDiv           = document.createElement('div');
	objDiv.className = style;
	objConteneur.appendChild(objDiv);
	
	objFlash         = document.createElement('embed');
	objFlash.src     = src;
    objFlash.target  = cible;
	objFlash.width   = width;
	objFlash.height  = height;
	objFlash.quality = 'high';
	objFlash.scale   = 'exactfit';
	objFlash.wmode   = 'opaque';
	objDiv.appendChild(objFlash);
}

