﻿var rev_popupRevationServer = "";
var rev_popupGroup = "";
var rev_popupPresenceId = "";
var rev_statusIntervalId = null;

function getStyleObject(objectId) {
    if (document.getElementById && document.getElementById(objectId)) {
        return document.getElementById(objectId).style;
    }
    else if (document.all && document.all(objectId)) {
        return document.allrev_(objectId).style;
    }
    else if (document.layers && document.layers[objectId]) {
        return getObjNN4(document, objectId);
    }
    else {
        return false;
    }
}

function changeObjectVisibility(objectId, newVisibility) {
    var styleObject = getStyleObject(objectId, document);
    if (styleObject) {
        styleObject.visibility = newVisibility;
        return true;
    }
    return false;
}

function show(objectId) {
    changeObjectVisibility(objectId, "visible");
}

function hide(objectId) {
    changeObjectVisibility(objectId, "hidden");
}

function rwc_statusUpdated(pid, status) {
    // called when status check returns with the status
    if (status == "online") {
        rev_positionPopupLayer();
        show("rev_popup_layer");
    }
    else {
        hide("rev_popup_layer");
    }
}

function rev_closePopupLayer() {
    // called if they click to close or click to chat
    clearInterval(rev_statusIntervalId);
    hide("rev_popup_layer");
}

function rev_positionPopupLayer() {
    // called to center the popup window
    var dsocleft = document.all ? document.body.scrollLeft : pageXOffset
    var dsoctop = document.all ? document.body.scrollTop : pageYOffset
    var window_width = document.all ? document.body.clientWidth : window.innerWidth
    var window_height = document.all ? document.body.clientHeight : window.innerHeight
    var popupobj = document.getElementById("rev_popup_layer");
    var popup_width = popupobj.clientWidth;
    var popup_height = popupobj.clientHeight;
    var obj = getStyleObject("rev_popup_layer");
    if (obj) {
        //if the user is using IE 4+ or NS6+
        if (document.all || document.getElementById) {
            if (parseInt(window_width) <= parseInt(popup_width)) {
                obj.left = parseInt(dsocleft);
            }
            else {
                obj.left = parseInt(dsocleft) + ((parseInt(window_width) - parseInt(popup_width)) / 2);
            }
            if (parseInt(window_height) <= parseInt(popup_height)) {
                obj.top = parseInt(dsoctop);
            }
            else {
                obj.top = parseInt(dsoctop) + ((parseInt(window_height) - parseInt(popup_height)) / 2);
            }
        }

        //else if the user is using NS 4
        else if (document.layers) {
            obj.left = dsocleft
            obj.top = dsoctop
        }
    }
}

function rev_updateStatus() {
    // called by timer the kick off a status query
    var head = document.getElementsByTagName('head').item(0);
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.id = 'myscript';
    script.defer = true;
    script.src = 'http://' + rev_popupRevationServer + '/status/js/?im=' + rev_popupPresenceId + '&group=' + rev_popupGroup;
    head.appendChild(script);
}

function rev_chatPopupWindow() {
    // called if they click to chat
    rev_closePopupLayer();
    var chatUrl = "https://" + rev_popupRevationServer + "/?im=" + rev_popupPresenceId + "&group=" + rev_popupGroup + "&pres=0";
    window.open(chatUrl, "rev_chatPopupWindow", "scrollbars=no,menubar=no,status=no,resizable=no,location=no,width=450,height=452,left=200,top=100");
}

function rev_initializePopupLayer(server, group, im, seconds) {
    // called by page to set the timeout
    rev_popupRevationServer = server;
    rev_popupGroup = group;
    rev_popupPresenceId = im;
    rev_statusIntervalId = setInterval("rev_updateStatus()", seconds * 1000);
}
