Event.observe(window, "load", initM4Box);
var toTest = [];
function initM4Box(e)
{
	$$("a").each(function(a)
	{
		if(a.rel == "")
			return;
		var r = a.rel, t;
		r.sub(/M4Box\[([a-z0-9\_\-]+)\]/,function (match){return t = match[1];});
		if(t)
		{
			a.rel = t;
            toTest.push(t);
            M4Box.create(t);
            Event.observe(a, "click", aClickDisplayBox);
		}
	});
    Event.observe(window.document, "M4Box:loaded", function(e)
    {
        $$("a").each(function(a)
        {
	        if(!a.getAttribute("rel"))
	            return;
            if(toTest.indexOf(a.getAttribute("rel"))>-1
                &&!$(a).hasClassName("M4Close"))
            {
	            a.onclick = function()
	            {
		            var e = {target:this, preventDefault:function(){}, cancelable:true, stopPropagation:function(){}, test:true};
		            aClickDisplayBox(e);
		            return false;
	            };
//                Event.observe(a, "click", aClickDisplayBox);
            }
        })
    });
}

function aClickDisplayBox(e)
{
	var d, rel;
	rel = e.test||e.target["rel"]?e.target.rel:$(e.target).up("a").rel;
	d = document.getElementById(rel);
	M4Box.display(d.id,d.innerHTML);
	Event.stop(e);
}

function M4Box(){}
M4Box.created = false;
M4Box.display = function(id,pHtml)
{
	var a = $("M4BoxHide"+id);
	var b = $("M4Box"+id);

    if(!a || !b)
        return;
	b.innerHTML = "";
	b.width = $(id).width+"px";
	b.innerHTML = pHtml;
    if(id.indexOf("map") > -1) {
        b.innerHTML =  b.innerHTML.replace("<!--","").replace("-->","");
    }
    b.setStyle({display:"block", "opacity":0});
    a.setStyle({display:"block", "opacity":0});
	M4Tween.to(a, .2, {"opacity":.65});
	M4Tween.to(b, .3, {"opacity":1});
	b.style.left = "50%";
	b.style.marginLeft = "-"+((""+b.offsetWidth).replace(/(px|\%)/,"") * .5)+"px";
	var close = document.createElement("div");
	a = document.createElement("a");
	a.href="#";
	a.appendChild(document.createTextNode("Fermer"));
	close.appendChild(a);
	a.setAttribute("rel",id);
    a.className = "M4Close";
	b.appendChild(close);
	$(close).className = "close";
	Event.observe(a, "click", CloseM4BoxHandler);
	Event.fire(window.document, "M4Box:loaded");
};

M4Box.hide = function(id)
{
	var a = $("M4BoxHide"+id);
	var b = $("M4Box"+id);
	M4Tween.to(a, .3, {"opacity":0}).onComplete(function(){a.style.display="none";b.style.display="none";});
	M4Tween.to(b, .2, {"opacity":0});
};

M4Box.create = function(id)
{
    var type = id.substring(0,id.indexOf("_"));
    var hide = document.createElement("div");
	//hide.setAttribute("class", "M4BoxHide "+id);
	hide.setAttribute("id", "M4BoxHide"+id);
	hide.setAttribute("rel", id);
    hide.className = "M4BoxHide";
	document.body.appendChild(hide);
	var box = document.createElement("div");
	//box.setAttribute("class","M4Box "+id+" "+type);
	box.setAttribute("id","M4Box"+id);
    box.className = "M4Box "+type;
	document.body.appendChild(box);
	M4Box.created = true;
	Event.observe(hide, "click", CloseM4BoxHandler);
};

function CloseM4BoxHandler(e)
{
    M4Box.hide(e.target.getAttribute("rel"));
	Event.stop(e);
}
