
function initBubble() {
	// Tenemos el enlace
	if(document.getElementById('ico-flecha')==null)
		return false;
		
	var a = document.getElementById('ico-flecha').getElementsByTagName('a')[0];
	
	var imagen = document.createElement('img');
	imagen.setAttribute('src', globals_web_eroski.relPath + '/imgs/ico.boton-flecha.png');
	imagen.setAttribute('alt', '');
	imagen.style.marginLeft = '0.5em';
	a.appendChild(imagen);

	if(a.addEventListener)
		a.addEventListener('click', toggleBubble, false);
	else
		a.attachEvent('onclick', toggleBubble);

	var capa = document.createElement('ul');
	capa.setAttribute('id', 'capa-tiendas-online');

	createBubbleOption(globals_web_eroski.bubble[0], 'http://www.compraonline.grupoeroski.com/ecoventa/', capa);
	//createBubbleOption(globals_web_eroski.bubble[1], 'http://www.compraonline.grupoeroski.com/electroventa/tecno/index.jsp', capa);
	//createBubbleOption(globals_web_eroski.bubble[2], 'http://www.compraonline.grupoeroski.com/electroventa/electro/index.jsp', capa);
	createBubbleOption(globals_web_eroski.bubble[1], 'http://www.viajeseroski.es/', capa);
	createBubbleOption(globals_web_eroski.bubble[2], 'http://www.eroskimovil.es/geomv/c/pub/es/index.jsp', capa);
	//createBubbleOption(globals_web_eroski.bubble[5], 'http://www5.eroski.es/asegura/visor/pm_controller.jsp', capa);
	createBubbleOption(globals_web_eroski.bubble[3], 'http://www.eroski.es/seguros', capa);
	createBubbleOption(globals_web_eroski.bubble[4], 'http://www5.eroski.es/prestamos/index.html', capa);

	document.body.appendChild(capa);
}

function getPosition(element) {
	var el = element, left = 0, top = 0;
        do {
                left += el.offsetLeft || 0;
                top += el.offsetTop || 0;
                el = el.offsetParent;
        } while (el);

	return {'x': left, 'y': top};
}

function createBubbleOption(nombre, enlace, dest) {
        var li = document.createElement('li');

        var opcion = document.createElement('a');
        opcion.setAttribute('href', enlace);
        opcion.appendChild(document.createTextNode(nombre));
        li.appendChild(opcion);

        dest.appendChild(li);
}

function toggleBubble(e) {
		var e = new Event(e);
		e.stop();

        var bubble = document.getElementById('capa-tiendas-online');

		var pos = getPosition(document.getElementById('ico-flecha').getElementsByTagName('img')[0]);

		// Calculamos la posición del icono
		bubble.style.top = (pos.y + 13) + 'px';
		bubble.style.left = (pos.x - 93) + 'px';

        if(bubble.style.display == 'block')
                bubble.style.display = 'none';
        else
                bubble.style.display = 'block';
		bubble.blur();
}

window.addEvent('load',initBubble);
