var sb_event_doc = new Array();


var eTip = null;

function sb_event_over(thisId, text) 
{
    if(eTip != null) {
        sb_node_delete(eTip);
        eTip = null;
    }

    var pos = getPos(thisId.parentNode);
    eTip = sb_node(document.body, "div", text);
    eTip.className = "tooltip";
    eTip.style.position = 'absolute';
    eTip.style.zIndex = 1000;
    eTip.style.top = pos.top - eTip.offsetHeight - 5 +  "px";
    eTip.style.left = pos.left + thisId.parentNode.offsetWidth / 2  - eTip.offsetWidth / 2  + "px";
}

function sb_event_out(thisId) 
{
    if (eTip != null) {
        sb_node_delete(eTip);
        eTip = null;
    }
}

var sb_eventCache = {};
var sb_eventTimeout = null;
function sb_event_viewer(thisId, date, page, layout, module, rollover) 
{
    var Months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

    var eDoc;

    var split = date.split("-");
    var year  = split[0];
    var month = parseInt(split[1], 10) - 1;
    var day   = split[2];

    for(var p = thisId; p.nodeName != 'TABLE'; p = p.parentNode);

    var table = p;

    var win = new sbwin('events');
    thisId.win = win;
    win.cont.className += ' calendar_overlay';
    win.align = 'relativeCenter';
    win.title =  Months[month]+" "+day+", "+year;
    win.relativeNode = thisId;
    win.ref = {
        mouseout : sb_eventOut,
        node : thisId
    };

    var listing = sb_node(win.body, "div", "Retrieving Data...");
    listing.className = "listing";
    document.body.style.cursor = 'wait';

    var str = SB_FILES+"/modules/events.php?date="+date+"&page="+page+"&module="+module+"&layout="+layout

    if (!sb_eventCache[str]) {
        sb_eventCache[str] = sb_get_xml(str);
    }
    eDoc = sb_eventCache[str];

    if (eDoc) {
        sb_node_clear(listing);   
        var eInfo = eDoc.getElementsByTagName("document").item(0);
        var ul = sb_node(listing, "ul");

        for(var i = 0; i < eInfo.childNodes.length; i++) {
            var tag = eInfo.childNodes[i];

            var li = sb_node(ul, "li");
   
            var div = sb_node(li, "div");
            div.className = 'title';

            a = sb_node(div, "a", tag.getAttribute("title"));
            a.href = tag.getAttribute("href");
        }
    }
    else {
//  window.location.href = SB_FILES+"/modules/events.php?date="+date+"&page="+page+"&module="+module+"&layout="+layout;

        sb_node_clear(listing);   
        sb_node(listing, "div", "Error retrieving data from server.");
    }

    document.body.style.cursor = '';
    if (!rollover) {
        win.open();
    }
    else {
        sb_eventClear();
        sb_eventTimeout = setTimeout(function() {sb_eventOpen(win)}, 500);
        thisId.onmouseout = sb_eventOut;
    }
}

function sb_eventOut(evt) 
{
    var relTarget = evt.relatedTarget || evt.toElement;
            
    var obj = this;
    sb_eventClear();
    if (this.node && this.node == relTarget) {
        return;
    }
    setTimeout(function() {sb_eventOutDelay(obj.win)}, 400);
}

function sb_eventOutDelay(win)
{ 
    if (win.mouseIsOver == false) {
        win.close();
    }
}

function sb_eventClear() {
    if (sb_eventTimeout) {
        clearTimeout(sb_eventTimeout);
        sb_eventTimeout = null;
    }
}

var sb_eventWin = null;
function sb_eventOpen(win) 
{
    if (sb_eventWin) {
        sb_eventWin.close();
        sb_eventWin = null;
    }
    sb_eventWin = win;
    sb_eventTimeout = null;
    win.open();
}

