function addLoadEvent(func){
    var oldonload = window.onload;
    if (typeof window.onload != 'function'){
        window.onload = func;
    } else{
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function faviconize(){ 
    if((window.location.hash) && (window.location.hash) != '#')
        viewcomm(parseInt(window.location.hash.substring(1)));
    var links = document.getElementsByTagName("a");
    //    var links = document.getElementsByClassName("zapLink"); //works on firefox et more speed :'(
    var hoststring = /^http:/;
    for (var j=0; j<links.length; j++){
        if(links[j].className != "zapLink") continue; //because non ByClassName :(
        var hrefvalue = links[j].getAttribute("href",2);
        if (hrefvalue.search(hoststring) != -1) {
            var domain = hrefvalue.match(/(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)/);
            domain = RegExp.$2;
            var cue = links[j].previousSibling;
            cue.title = domain;
            cue.src = "http://"+domain+"/favicon.ico";
            cue.onerror = function () {
                this.src = "/i/external.gif";
            }
        }
    }
    return true;
}

addLoadEvent(faviconize);

function viewcomm(i){
    document.getElementById('a'+i).firstChild.src="/i/commclose.png";
    document.getElementById('a'+i).firstChild.title="Fermer les commentaires";
    document.getElementById('a'+i).onclick = function () {closecomm(i);};
    var mynode = document.getElementById('id'+i);
    //var elem = document.createElement('tr');
    var trs = document.getElementsByTagName("tr");
    var z = -1;
    for (j=0; j < trs.length; j++)
        if(trs[j].id == "id"+i) {z = j; break;}
    
    if(z != -1) {
        var el= mynode.parentNode.insertRow(z+1);
        var elem = el.insertCell(0);
        elem.id ='comm'+i;
        elem.colSpan=5;
        elem.className = 'comm';
        //mynode.parentNode.insertBefore(elem, mynode.nextSibling);
        asyncRequest('getComm.php', function(o) {responseComm(o.responseText, i);}, 'id='+i);
    }
}

function responseComm(t, i){
    document.getElementById('comm'+i).innerHTML = t;
}

function closecomm(i){
    document.getElementById('a'+i).firstChild.src="/i/comm.png";
    document.getElementById('a'+i).firstChild.title="Voir les commentaires";
    document.getElementById('a'+i).onclick = function () {viewcomm(i)};
    var obj = document.getElementById('comm'+i);
    obj.parentNode.removeChild(obj);
}

var asyncRequest = function() {
    function handleReadyState(o, callback) {
        if (o && o.readyState == 4) (o.status==200)?callback(o):affError();
        //     if(o.status==200) {
        //                 if(callback) callback(o);
        //                 if(requestTimer) clearTimeout(requestTimer);
        //             }
        //             else affError(); 
    }
    var getXHR=function(){return (window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Msxml2.XMLHTTP")};
    return function(uri, callback, postData) {
        var xhr = getXHR();
        xhr.open('POST', uri, true);
        xhr.onreadystatechange=function(){handleReadyState(xhr, callback)};
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhr.send('http=1&'+postData);
        //  requestTimer = setTimeout(function(){xhr.abort();},10);// MAXIMUM_WAITING_TIME);
        return xhr};
}();

function affError(){
    ;
}

