
addListenner(window, "load", lessonBubbleLoader, true);


/**
 * Charge les actions sur les liens
 */
function lessonBubbleLoader()
{
	if (!document.getElementsByTagName) {
		return false;
	}
	
	var nomFranchise = '';
	var tmp = document.getElementById('nomFranchiseJs');
	if (tmp) {
	    nomFranchise = tmp.innerHTML;
	}
	
	
	var bubbleHtml = '';
    //bubbleHtml += '   <div id="bigBubble">';
    bubbleHtml += '<div id="bigBubbleFooter">';
    bubbleHtml += '<div id="bigBubbleContent">';
    bubbleHtml += '<h2 class="smallProductTitle">titre à afficher</h2>';
    bubbleHtml += '<div class="container image">';
    bubbleHtml += '<img src="" alt="illustration de l\'atelier" />';
    bubbleHtml += '</div>';
    bubbleHtml += '<div class="classic">exemple de texte</div>';
    bubbleHtml += '<div class="container pubCarte">';
    bubbleHtml += '<img src="img_v1/carte5Cours.gif"';
    bubbleHtml += ' width="50"';
    bubbleHtml += ' alt="acheter la carte 5 cours et bénéficiez de tarifs privilégiés" />';
    bubbleHtml += '<p class="zoneTitle"><br />';
    bubbleHtml += '<a href="offres.php?type=12&mode=1" class="ajax-content-lightbox width:775,noScroll:1 contenuLightBoxBulle"';
    bubbleHtml += ' title="offres et tarifs" rel="external" class="lien-externe">voir tous nos tarifs</a>';
    bubbleHtml += '</p>';
    bubbleHtml += '</div>';
    if (nomFranchise) {
        bubbleHtml += '<p class="cocon pourpre nomFranchise">';
        bubbleHtml += '    <xsl:text>ateliers bricolage lilibricole </xsl:text>';
        bubbleHtml += '    ' + nomFranchise;
        bubbleHtml += '</p>';
    }
    bubbleHtml += '<div class="container horaires">';
    bubbleHtml += '</div>';
    bubbleHtml += '<div class="container alertesInfos">';
    bubbleHtml += '<p>';
    bubbleHtml += 'J\'aimerais être informé par mail des prochaines dates pour ce cours';
    bubbleHtml += '</p>';
    bubbleHtml += '<a href="mes-alertes.php" class="boutonReserver">';
    bubbleHtml += 'Créer une alerte';
    bubbleHtml += '</a>';
    bubbleHtml += '</div>';
    bubbleHtml += '<div class="container alertesInfos dispoSurPlace">';
    bubbleHtml += '<p>';
    bubbleHtml += '';
    bubbleHtml += '</p>';
    bubbleHtml += '</div>';
    bubbleHtml += '<a href="#" id="closeBigBubble">Fermer</a>';
    bubbleHtml += '</div>';
    bubbleHtml += '</div>';
    //bubbleHtml += '   </div> <!-- end of bigBubble -->';
	
    var lightBox = document.createElement('div');
    lightBox.id = 'bigBubble';
    lightBox.innerHTML = bubbleHtml;
    var body = document.getElementsByTagName('body');
    if (body[0]) {
        body[0].appendChild(lightBox);
    }

    if (loadContentLightBox) {
        loadContentLightBox();
    }
    
    
    var linklist = document.getElementsByTagName('a');
    var clicDone = false;
    for (var i = 0; i < linklist.length; i++) {
        if (/bigLessonBubble/.test(linklist[i].className)) {
            linklist[i].onclick = getBubbleData;

            //if (/rollover/.test(linklist[i].className)) {
            	linklist[i].onmouseover = linklist[i].onclick;
            //}

            if (!clicDone && /afficherDefaut/.test(linklist[i].className)) {
            	linklist[i].onclick();
            	clicDone = true;
            }
        }
    }

    var actionLink = document.getElementById('closeBigBubble');
    actionLink.onclick = actionLink.ondblclick = closeBigBubble;

} // end of "lessonBubbleLoader()"






/**
 * Lance la requête de demande des données de l'atelier
 */
function getBubbleData()
{
	closeBigBubble();
	var oData = {};
	var oBubble = document.getElementById('bigBubble');
	
	if (this.className.indexOf('foireDeParis') >= 0) {
	    addClass(oBubble, 'bulleFoireDeParis');
	}
	
	
	oBubble.tryOuts = 1;
	oBubble.infoLink = this.id;
	oData['id'] = this.id.replace('atelier-', '');
	oData['id'] = oData['id'].split('-')[2];
	oData['id'] = parseInt(oData['id']);
	oData['xhrAction'] = 'getLessonData';
	//alert('clic');
    XHR_post(document.location.href, oData, prepareBubble);
    return false;
} // end of "getBubbleData()"




/**
 * Prépare à afficher la bulle
 *
 * lance le remplissage et le préchargement des images
 */
function prepareBubble(data)
{	
	//alert('prepareBubble');
	if (!data['data']) {
		return false;
	}

	var oBubble = document.getElementById('bigBubble');
	fillBubble(data['data']);

	oBubble.preloadImg = new Image();
	oBubble.preloadImg.onload = openBigBubble;

	if (data['data']['atelier']['attachments']
	    && data['data']['atelier']['attachments'][0]
	    && data['data']['atelier']['attachments'][0]['zoom_src']) {
		
		//alert('zoom_src = ' + data['data']['atelier']['attachments'][0]['zoom_src']);
		oBubble.preloadImg.src = data['data']['chemin-upload'] + data['data']['atelier']['attachments'][0]['zoom_src'];

	} else {
		oBubble.preloadImg.src = data['data']['chemin-images'] + 'blank.gif';
	}
	oBubble.timeOutBubble = setTimeout('openBigBubble()', 1000);
	//alert('fin de prepareBubble');

} // end of "prepareBubble()"




/**
 * Place les données dans la bulle
 */
function fillBubble(data)
{
	//alert('fillBubble = "' + data['atelier']['AT_titre'] + '"');
	var oBubbleContent = document.getElementById('bigBubbleContent');

	// titre de la bulle
	var obj = oBubbleContent.firstChild;
	if (data['atelier']['AT_titre']) {
		obj.innerHTML = data['atelier']['AT_titre'];
	} else {
		obj.innerHTML = 'Atelier';
	}

	//alert('titre remplacé ? "' + obj.nodeName + '.' + obj.className + '"');

	// image
	obj = obj.nextSibling.firstChild;
	obj.oldSrc = obj.src;
	if (data['atelier']['attachments']
	    && data['atelier']['attachments'][0]
	    && data['atelier']['attachments'][0]['zoom_src']) {
		obj.src = data['chemin-upload'] + data['atelier']['attachments'][0]['zoom_src'];

	} else {
		obj.src = data['chemin-images'] + 'blank.gif';
	}


	// paragraphe descriptif
	obj = obj.parentNode.nextSibling;
	if (data['atelier']['AT_programme_details']) {
	    try {
	        obj.innerHTML = '';
	        obj.innerHTML = data['atelier']['AT_programme_details'];
	    } catch(e) {
	        
	    }
	    //alert(data['atelier']['AT_programme_details']);
	} else {
		obj.innerHTML = '';
	}

	// partie "pub carte"
	obj = obj.nextSibling;
	if (data['atelier']['ATCA_id']) {
	    //data['atelier']['LISA_id'] != 1
	    if (data['atelier']['ATCA_id'] == 10 || (data['salon'] && data['salon']['LISA_parent_id'] > 0)) {
	        obj.style.display = 'none';
	    } else {
	        obj.style.display = 'block';
	    }
	}
	
	
	var urlReserver = 'reserver.php';
	if (data['salon'] && data['salon']['LISA_parent_id'] > 0) {
	//if (data['atelier']['LISA_id'] != 1) {
	    urlReserver = 'reserver-fdp.php';
	}
	
	// Si la franchise est activée, on passe cette zone
	if (obj.nextSibling.nodeName == 'P') {
	    obj = obj.nextSibling;
	}
	
	// date et horaires
	obj = obj.nextSibling;
	var htmlText = '';
	var limite = 1;
	if (data['atelier']['ATCA_id'] > 0) {
    	if (data['horaires']) {
    		for (var i in data['horaires']) {
    		    if (limite <= 5) {
        			htmlText += '<div class="container">';
        			htmlText += '<p class="classic">';
        			if (data['horaires'][i]['AAH_date']) {
        				htmlText += '<strong>';
        				htmlText += data['horaires'][i]['AAH_date-jour'] + ' ' + data['horaires'][i]['AAH_date'];
        				htmlText += '</strong>';
        			}
        			if (data['horaires'][i]['ATHO_horaire']) {
        				htmlText += ':';
        				htmlText += '   ' + data['horaires'][i]['ATHO_horaire'];
        			}
        			htmlText += '</p>';
        			if (data['horaires'][i]['AAH_nombre_places_dispo'] > 0 && data['horaires'][i]['AAH_forcer_complet'] != 1) {
        				htmlText += '<a class="boutonReserver" href="' + urlReserver + '?atelierHoraire=';
        				htmlText += data['horaires'][i]['AAH_id'] + '">réserver</a>';
        			}
        			htmlText += '</div>';
    		    }
    		    limite++;
    		}
    	} else {
    		htmlText += '<p class="zoneTitle">';
    		htmlText += '<strong>Retrouvez très bientôt les prochaines dates ';
    		htmlText += 'pour cet atelier lilibricole';
    		htmlText += '</strong></p>';
    	}
	}
	obj.innerHTML = htmlText;
	
	
    // alerte
    obj = obj.nextSibling;
	if (data['atelier']['ATCA_id'] > 0) {
	    obj.style.display = 'block';
	} else {
	    obj.style.display = 'none';
	}
	
	
    // salon
	if (data['salon'] && data['salon']['LISA_parent_id'] > 0 && data['salon']['LISA_qui_sommes_nous_intro']) {
	    obj = obj.nextSibling;
	    htmlText = '<p>';
        htmlText += data['salon']['LISA_qui_sommes_nous_intro'];
	    htmlText += '</p>';
        obj.innerHTML = htmlText;
    }
	
	

} // end of "fillBubble()"





/**
 * Affiche la bulle et la positionne
 */
function openBigBubble()
{
	var oBubble = document.getElementById('bigBubble');
	if (oBubble.timeOutBubble) {
		clearTimeout(oBubble.timeOutBubble);
	}
	/*if (oBubble.tryOuts) {
		alert('openBigBubble (' + oBubble.tryOuts + ')');
	} else {
		alert('openBigBubble (no tryouts data)');
	}*/
	
	if (oBubble.preloadImg && !oBubble.preloadImg.complete && oBubble.tryOuts
		&& oBubble.tryOuts < 10) {
		setTimeout('openBigBubble()', 50);
		oBubble.tryOuts++;
		return false;
	}

	var oLink = document.getElementById(oBubble.infoLink);
	var linkPos = getPosition(oLink, false);
	//alert('linkPos.x = ' + linkPos.x + ', linkPos.y =' + linkPos.y);
	var x = 0;
	var y = 0;
	y = linkPos.y - oBubble.clientHeight - 2;
	x = linkPos.x - 50;
    //if (y <= 0) {
        addClass(oBubble, 'reverse');
        y = linkPos.y + oLink.clientHeight + 20;
        x -= 50;
    //}
    if (x <= 0) {
        x = 0;
    }
	oBubble.style.top = (y) + 'px';
	oBubble.style.left = (x) + 'px';
	oBubble.style.visibility = 'visible';
	addClass(oBubble, 'show');
	//alert('clic');

} // end of "openBigBubble()"




/**
 * Ferme la bulle et remet à zéro ses paramètres
 */
function closeBigBubble()
{
	var zoomWin = document.getElementById('bigBubble');
	removeClass(zoomWin, 'reverse');
	removeClass(zoomWin, 'show');
	zoomWin.style.visibility = 'hidden';

	var imgObj = zoomWin.getElementsByTagName('img')[0];
	imgObj.src = imgObj.oldSrc;

	return false;
} // end of "closeBigBubble()"




