var xmlhttp = null;
/*var dataPath = "../xml/display.1614.xsl";*/
var dataPath = "../SyndFullLeaderboard.xml"

function setupXMLHTTP(path, changeHandler) {
    if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); }
    else if (window.ActiveXObject) { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
    if (xmlhttp) {
        xmlhttp.onreadystatechange = eval(changeHandler);
        xmlhttp.open("GET", path, true);
        xmlhttp.send(null);
    }
}
function xmlError(e) { /* there was an error, show the user */alert(e); } /* end function xmlError */

function getNumEntries(xml, tag) { return (xml.split(tag).length) - 1; }

var maxPlayers = null;
var playersPerPage = 20;
var pageNum = location.hash !== "" ? parseInt(location.hash.replace('#page', ''), 10) : 1;
var startPos = (pageNum * playersPerPage) - playersPerPage;
function displayLeaderboardXML(xmlData) {
    var xml = xmlData;
    var XMLObj = new XMLDoc(xml, xmlError);

    maxPlayers = getNumEntries(xml, "<Player ");

    var lbTable = $('leader_board_table');
    var rowClass = "odd", nextTBody = null, nextRow = null, nextCell = null, nextCellData = null, totalScore = null;

    for (t = 0; t < (maxPlayers / playersPerPage); t++) {
        nextTBody = document.createElement('tbody');
        if (t + 1 !== pageNum) {
            nextTBody.className = "hideTBody";
        } else {
            nextTBody.className = "showTBody";
        }

        rowClass = "odd";

        for (p = (t * playersPerPage); p < Math.min(((t * playersPerPage) + playersPerPage), maxPlayers); p++) {
            nextRow = document.createElement('tr');

            nextCell = document.createElement('td');
            nextCell.className = "first " + rowClass;
            nextCellData = document.createTextNode(XMLObj.selectNode('/Players/Player[' + p + ']').getAttribute("MnyRnk"));
            nextCell.appendChild(nextCellData);
            nextRow.appendChild(nextCell);

            nextCell = document.createElement('td');
            nextCell.className = rowClass;
            nextCellData = document.createTextNode(XMLObj.selectNode('/Players/Player[' + p + ']').getAttribute("CurPos"));
            nextCell.appendChild(nextCellData);
            nextRow.appendChild(nextCell);

            nextCell = document.createElement('td');
            nextCell.className = rowClass;
            nextCellData = document.createTextNode(XMLObj.selectNode('/Players/Player[' + p + ']').getAttribute("StrPos"));
            nextCell.appendChild(nextCellData);
            nextRow.appendChild(nextCell);

            nextCell = document.createElement('td');
            nextCell.className = rowClass;
            nextCellData = document.createElement('a');
            playerID = "0" + XMLObj.selectNode('/Players/Player[' + p + ']').getAttribute("PID");
            playerID1 = playerID.substring(0, 2);
            playerID2 = playerID.substring(2, 4);
            playerID3 = playerID.substring(4, 6);
            nextCellData.href = "http://www.pgatour.com/players/" + playerID1 + "/" + playerID2 + "/" + playerID3 + "/";
            popUrl = "http://www.pgatour.com/players/" + playerID1 + "/" + playerID2 + "/" + playerID3 + "/";
            nextCellData.onclick = function() { return popWinCust(this.href, 'pop', 'no', 'no', 'no', 'no', 'no', 'yes', 'yes', 1024, 768, 0, 0); };
            var nextCellDataText = document.createTextNode(XMLObj.selectNode('/Players/Player[' + p + ']').getAttribute("Fname") + " " + XMLObj.selectNode('/Players/Player[' + p + ']').getAttribute("Lname"));
            nextCellData.appendChild(nextCellDataText);
            nextCell.appendChild(nextCellData);
            nextRow.appendChild(nextCell);

            nextCell = document.createElement('td');
            nextCell.className = rowClass;
            nextCellData = document.createTextNode(XMLObj.selectNode('/Players/Player[' + p + ']').getAttribute("CurParRel"));
            nextCell.appendChild(nextCellData);
            nextRow.appendChild(nextCell);

            nextCell = document.createElement('td');
            nextCell.className = rowClass;
            nextCellData = document.createTextNode(XMLObj.selectNode('/Players/Player[' + p + ']').getAttribute("Thru"));
            nextCell.appendChild(nextCellData);
            nextRow.appendChild(nextCell);

            nextCell = document.createElement('td');
            nextCell.className = rowClass;
            nextCellData = document.createTextNode(XMLObj.selectNode('/Players/Player[' + p + ']').getAttribute("TournParRel"));
            nextCell.appendChild(nextCellData);
            nextRow.appendChild(nextCell);

            totalScore = 0;

            for (r = 0; r < 4; r++) {
                nextCell = document.createElement('td');
                nextCell.className = rowClass;
                if (XMLObj.selectNode('/Players/Player[' + p + ']/Rnd[' + r + ']')) {
                    roundScore = XMLObj.selectNode('/Players/Player[' + p + ']/Rnd[' + r + ']').getAttribute("Stroke");
                    totalScore += parseInt(roundScore, 10);
                    if (!isNaN(parseInt(roundScore, 10))) {
                        nextCellData = document.createTextNode(roundScore);
                    } else {
                        nextCellData = document.createTextNode("--");
                    }
                } else {
                    nextCellData = document.createTextNode("--");
                }
                nextCell.appendChild(nextCellData);
                nextRow.appendChild(nextCell);
            }

            nextCell = document.createElement('td');
            nextCell.className = rowClass;
            if (!isNaN(totalScore)) {
                nextCellData = document.createTextNode(totalScore);
            } else {
                nextCellData = document.createTextNode("--");
            }
            nextCell.appendChild(nextCellData);
            nextRow.appendChild(nextCell);

            nextCell = document.createElement('td');
            nextCell.className = rowClass;
            nextCellData = document.createTextNode(XMLObj.selectNode('/Players/Player[' + p + ']').getAttribute("ProjMny"));
            nextCell.appendChild(nextCellData);
            nextRow.appendChild(nextCell);

            nextTBody.appendChild(nextRow);

            rowClass = rowClass == "odd" ? "even" : "odd";
        }

        lbTable.insertBefore(nextTBody, getChildElementsByType('tfoot', $('leader_board_table'))[0]);
    }

    writePagination(pageNum);

    lbTable.removeChild(getChildElementsByType('tbody', lbTable)[0]);

    getElementsByClassName('timestamp', 'span', $('leader_board_table_lastrow'))[0].innerHTML = "Information Updated: " + XMLObj.docNode.getAttribute("CurTime");
}

function writePagination(currPage) {
    var pagesDiv = getElementsByClassName('blank', 'div', getElementsByClassName('hat_inner', 'div', $('main'))[0])[0];
    currPage = parseInt(currPage, 10);
    pagesDiv.innerHTML = "Players: " + (((currPage * playersPerPage) - playersPerPage) + 1) + " - " + Math.min((currPage * playersPerPage), maxPlayers) + " of " + maxPlayers + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Page:&nbsp;";
    for (p = 1; p < (maxPlayers / playersPerPage) + 1; p++) {
        if (p == currPage) {
            pagesDiv.innerHTML += '&nbsp;' + p + '&nbsp;';
        } else {
            pagesDiv.innerHTML += '&nbsp;<a href="#page' + p + '" onclick="navigatePage(' + p + ',location.hash)">' + p + '</a>&nbsp;';
        }
    }
}

function navigatePage(destPage, currPage) {
    currPage = currPage !== "" ? parseInt(currPage.replace('#page', ''), 10) : 1;
    getChildElementsByType('tbody', $('leader_board_table'))[currPage - 1].className = 'hideTBody';
    getChildElementsByType('tbody', $('leader_board_table'))[destPage - 1].className = 'showTBody';
    writePagination(destPage);
}

function processData() {
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            displayLeaderboardXML(xmlhttp.responseText);
        } else { /* updateStatus(invalidText); */ }
    }
}
function loadData() {
    setupXMLHTTP(dataPath, "processData");
    return false;
}
WindowOnload(function() { loadData(); });

