function create_th(content) {
    var cell = document.createElement("th");
    if (typeof content == "object") {
        cell.appendChild(content);
    } else {
        var cellValue = document.createTextNode(content);
        cell.appendChild(cellValue);
    }
    return cell;
}

function create_td(content) {
    var cell = document.createElement("td");
    if (typeof content == "object") {
        cell.appendChild(content);
    } else {
        var cellValue = document.createTextNode(content);
        cell.appendChild(cellValue);
    }
    return cell;
}

function getElementPosition(obj) {
    var curleft = 0;
    var curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft;
        curtop = obj.offsetTop;
        while ((obj = obj.offsetParent)) {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
    }
    return {curleft:curleft, curtop:curtop};
}

var parElem;
function oddHistory(element, odd_id) {
    parElem = element;
    create_xhr();
    xhr.onreadystatechange = _oddHistory;
    xhr.open("GET", "/odds-inplay/oddhistory.php?id=" + odd_id);
    xhr.send(null);
}

function _oddHistory() {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var xml = xhr.responseXML;
            var nodata = xml.getElementsByTagName("nodata");
            if (nodata.length == 0) {
                del_odd_history();
                var elem = xml.getElementsByTagName("element");
                var div = document.createElement("div");
                div.setAttribute("id", "odd_history");
                div.setAttribute("title", "Click to close");
                var closer = document.createElement("div");
                if (navigator.userAgent.match(/MSIE/)) {
                    closer.setAttribute("className", "oh_capt");
                } else {
                    closer.setAttribute("class", "oh_capt");
                }
                var strong = document.createElement("span");
                strong.appendChild(document.createTextNode("BEST ODD HISTORY"));
                closer.appendChild(strong);
                var link = document.createElement("a");
                if (navigator.userAgent.match(/MSIE/)) {
                    link.onclick = new Function("env", "del_odd_history()");
                    div.onclick = new Function("env", "del_odd_history()");
                } else {
                    link.setAttribute("onclick", "del_odd_history()");
                    div.setAttribute("onclick", "del_odd_history()");
                }
                link.appendChild(document.createTextNode("X"));
                closer.appendChild(link);
                div.appendChild(closer);
                var cont = document.createElement("div");
                if (navigator.userAgent.match(/MSIE/)) {
                    cont.setAttribute("className", "oh_content");
                } else {
                    cont.setAttribute("class", "oh_content");
                }
                var table = document.createElement("table");
                var tbody = document.createElement("tbody");
                for (var i = 0; i < elem.length; i++) {
                    var tr = document.createElement("tr");
                    var dat = elem.item(i).getElementsByTagName("odd");
                    dat = dat.item(0).firstChild.nodeValue;
                    var course = elem.item(i).getElementsByTagName("date");
                    course = course.item(0).firstChild.nodeValue;
                    tr.appendChild(create_th(dat));
                    tr.appendChild(create_td(course));
                    tbody.appendChild(tr);
                }
                table.appendChild(tbody);
                cont.appendChild(table);
                div.appendChild(cont);
                var pos = getElementPosition(parElem);
                var posMain = getElementPosition(document.getElementById("main"));
                var div_main = document.getElementById("main");

								var limit = top == self ? 110 : 140
                var correction = 0;
                if (pos.curleft + limit + 5 >= posMain.curleft + div_main.offsetWidth) {
                    correction = pos.curleft - (posMain.curleft + div_main.offsetWidth - limit);
                }
                
                var l = pos.curleft - 7 - correction;
                var t = pos.curtop + 17;
                if (navigator.userAgent.match(/MSIE/)) {
                    div.style.cssText = "display:block;top:" + t + "px;left:" + l + "px;";
                } else {
                    div.setAttribute("style", "display:block;top:" + t + "px;left:" + l + "px;");
                }
                document.body.appendChild(div);
            }
        } else {
            show_xhr_error();
        }
    }
}

function del_odd_history() {
    var box = document.getElementById("odd_history");
    if (box != undefined) {
        document.body.removeChild(box);
    }
}

