/* Ventrilo Stats */
var vFrame;
function showVentriloStats() {
    var newdiv = document.createElement('div');
    if (newdiv.id === "") newdiv.id = 'ventriloBox';
    else newdiv.setAttribute('id', 'ventriloBox');
    newdiv.style.position = 'absolute';
    newdiv.style.left = '760px';
    newdiv.style.top = "20px";
    newdiv.style.border = "1px white solid";
    //newdiv.setAttribute('style', 'position: fixed; left: 760px; top: 20px; border: 1px white solid; ');
    newdiv.innerHTML = '<iframe id="vFrame" style="height: 292px; width: 204px; border: none;" src="http://cache.www.gametracker.com/components/html0/?host=66.151.55.167:24497&amp;showCurrPlayers=1&amp;bgColor=000000" />';
    document.body.appendChild(newdiv);
    vFrame = document.getElementById("vFrame");
    // Set a refresh every 5 minutes
    setInterval("vFrame.src = vFrame.src;", 1000 * 60 * 5);
}


/* Information Display */
var infoDiv;
function setNickInfo() {
    // Creating infoDiv
    infoDiv = document.createElement('div');
    if (infoDiv.id === "") infoDiv.id = 'infoDiv';
    else infoDiv.setAttribute('id', 'infoDiv');
    infoDiv.style.border = '1px white solid';
    infoDiv.style.backgroundColor = 'Black';
    //infoDiv.setAttribute('style', 'border: 1px white solid; background-color: Black;');

    // Hooking it up to all the avatars
    for (var i = 0; i < document.images.length; i ++) {
        var im = document.images[i];
        if (im.className == 'avatar') {
            im.onmouseover = function(e) {
                im = (e) ? e.currentTarget : this;
                var x = (e) ? e.pageX : event.clientX + ((document.body.scrollTop) ? 
                    document.body.scrollLeft : document.documentElement.scrollLeft);
                var y = (e) ? e.pageY : event.clientY + ((document.body.scrollTop) ? 
                    document.body.scrollTop : document.documentElement.scrollTop);
                showInfo(im.alt, x, y);
            }
        }
    }
    
    // Hooking it up to all the nicks
    var nicks = document.getElementsByClassName("nick");
    for(var i = 0; i < nicks.length; i ++) {
        nicks[i].onmouseover = function(e) {
            nickname = (e) ? e.currentTarget : this;
            var x = (e) ? e.pageX : event.clientX + ((document.body.scrollLeft) ? 
                document.body.scrollLeft : document.documentElement.scrollLeft);
            var y = (e) ? e.pageY : event.clientY + ((document.body.scrollTop) ? 
                document.body.scrollTop : document.documentElement.scrollTop);
            showInfo(nickname.innerHTML, x, y);
        }
    }
    
}

var nickData = {};
function getNickData() {
    var data;
    var req = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
    req.open("GET", "http://hellcatz.net/info.php", true);
    req.onreadystatechange = function () {
        if (req.readyState == 4 && req.status == 200){
            data = eval('(' + req.responseText + ')').d; 
            for(id in data) {
                nickData[data[id].nick] = data[id];
            } } };
    req.send({});    
}

var infoBox;
function createInfoBox() {
    infoBox = document.createElement('div');
    if (infoBox.id === "")
        infoBox.id = 'infoBox';
    else
        infoBox.setAttribute('id', 'infoBox');
    infoBox.style.position = 'absolute';
    infoBox.style.border = '1px white solid';
    infoBox.style.backgroundColor = 'Black';
    infoBox.style.display = 'none';
    infoBox.style.padding = '3px';
    infoBox.style.fontFace = 'Calibri, Tahoma, san-serif';
    infoBox.style.fontSize = '12px';
    infoBox.style.width = '250px';
    document.body.appendChild(infoBox);
    infoBox.onmouseout = function(event) {
        infoBoxHover = false;
        infoBoxTimeout = setTimeout('hideInfo();', 1000);
    }
    infoBox.onmouseover = function(event) {
        infoBoxHover = true;
    }
}

var infoBoxTimeout;
var infoBoxHover;

function hideInfo() {
    if (infoBoxHover) {
        infoBoxTimeout = setTimeout('hideInfo();', 1000);
    } else {
        infoBox.style.display = "none";
    }
}

function showInfo(who, x, y) {
    clearTimeout(infoBoxTimeout);
    var info = nickData[who];    
    infoBox.style.position = 'absolute';
    infoBox.style.top = (y - 3) + 'px';
    infoBox.style.left = (x - 3) + 'px';
    infoBox.style.color = 'White';
    var citylink = '<a href="http://maps.google.com/maps?z=9&amp;q=' + ucwords(info.city) + '">' + ucwords(info.city) + '</a>';
    var city = (info.city) ? 'City: ' + citylink + '<br />' : '';
    var nicklink = '<a href="http://www.gamersfirst.com/warrock/?q=Player&amp;nickname=' + info.nick + '">' + info.nick + '</a>';
    infoBox.innerHTML = '<img class="fullavatar" src="/avatar.php?' + info.nick + '" />' + 
        'RealLife: ' + ucwords(info.name) + ' (' + info.age + ')<br />' + 
        'WarRock: ' + nicklink + ' (' + info.level + ')<br />' +
        city +
        'Posts: ' + info.posts;
    infoBox.style.display = 'block';   
}

/* Got somewhere */
function ucwords(str) {  
    return (str + '').replace(/^(.)|\s(.)/g, function ($1) { return $1.toUpperCase(); });
}

/* Got somewhere... by some Dustin */
if (!document.getElementsByClassName) {
    document.getElementsByClassName = function(searchClass,node,tag) {
        var classElements = new Array();
        if (node == null) node = document;
        if (tag == null) tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++) {
            if (pattern.test(els[i].className) ) {
                classElements[j] = els[i];
                j++;
            }
        }
        return classElements;
    }
}


/* Body Onload */
window.onload = function() {
    showVentriloStats();
    createInfoBox();
    getNickData();
    setNickInfo();
};
