/***[AJAX]***/
var _HTTPFactories = [
		      function () { return new XMLHttpRequest(); },
		      function () { return new ActiveXObject("Msxml.XMLHTTP"); },
		      function () { return new ActiveXObject("Microsoft.XMLHTTP"); }
		      ];

var _HTTPFactory = null;

function HTTPRequest() {
    if(_HTTPFactory) return _HTTPFactory();
    for(var i=0; i < _HTTPFactories.length; i++) {
	try {
	    var factory = _HTTPFactories[i];
	    var request = factory();
	    if(request != null) {
		_HTTPFactory = factory;
		return request;
	    }
	}
	catch(e) { continue; }
    }
    alert("NoHttpRequest");
}

//On pourrait dire que si il y a un callback... quoi ?!
//FIXME: rename as get and post
function get(path, cmd, cbk) {
    var req = HTTPRequest();
    cbk = cbk || function(status, text) { alert("Unhandled response(" + status + "): " + text); };
    var opt = new Array(); /* FIXME: obj to URL: {prop:{sub_prop:value}} => prop.sub_prop=value */
    if(cmd) for(key in cmd) { opt.push(key + "=" + cmd[key]); }//FIXME:urlencode
    req.onreadystatechange = function() {
	if(req.readyState == 4) { cbk(req.status, req.responseText); }
	//    else alert(req.readyState);
    };
    var url=cmd?path + "?" + opt.join("&"):path;
    req.open("GET", url, true);
    req.send(null);
    return false;
}

/***[POPUP]***/
var popup={};

function popup_open(header, content, footer) {
    var ie = (document.all) ? 1 : 0;
    var p = (ie) ? "filter" : "MozOpacity";

    /* n is the element node
       v is the opacity value, from 0 to 100. */
    function op(n,v){
	v = (ie) ? "alpha(opacity="+v+")" : v/100;
	n.style[p] = v;
    }


    popup.content=document.getElementById('popup_content');
    popup.footer=document.getElementById('popup_footer');
    popup.bg=document.getElementById('popup_bg');
    popup.win=document.getElementById('popup');
    popup.header = document.getElementById('popup_header');

    function fade(opacity, max) {
	op(popup.bg, opacity);//popup.bg.style.opacity = opacity;
	if(opacity < max)
	    setTimeout(function() { fade(opacity+10, max) }, 30);
    }
    fade(0, 70);

    popup.bg.style.display = popup.win.style.display='block';
    popup.header.innerHTML = header;
    popup.content.innerHTML = content;
    popup.footer.innerHTML = footer;
}

function popup_close() {
    popup.bg.style.display = popup.win.style.display = 'none';
}

function popup_href(href) {
    function cbk(status, body) {
	header='<a style="padding-right: 5px; padding-top: 5px; font-family: Arial; text-decoration: none; font-weight: 700;" href="javascript:popup_close()">X</a>';
	popup_open(header, body, '');
    }
    get(href, null, cbk);
}

