﻿var woPollUrl;
var woInviteUrl;
var woChatUrl;
var closeTimeout;
var getC = 0;
var cStat = 1;
imgPoll = new Image();
imgPoll.onload = chkImg;
function woAfterLoad() {
    setTimeout('getImg()', 1000);
}
function getImg() {
    var rTimer = 5000;
    if (cStat == 1) {
        var pUrl = woPollUrl + (new Date()).getTime();
        if (typeof (sWOSession) != "undefined") { pUrl += "&u=" + sWOSession; }
        imgPoll.src = pUrl;
    }
    getC++;
    if (getC > 6) rTimer = 10000;
    if (getC > 20) rTimer = 15000;
    if (getC > 30) rTimer = 0;
    if (rTimer > 0) setTimeout("getImg()", rTimer);
}
function chkImg() {
    if (imgPoll != null) {
        nStat = imgPoll.width;
        if (nStat != cStat) {
            cStat = nStat;
            if (cStat == 3) startChat();
            if (cStat == 2) startInvite();
            if (cStat == 1) stopInvite();
        }
    }
}

function startInvite() {
    var regInvite = new Image();
    regInvite.src = woInviteUrl + (new Date()).getTime();

    var wrapper = $('#whosOn');
    if (wrapper.length == 0) {
        wrapper = $('<div/>');
        wrapper.attr('id', 'whosOn-wrapper');
        wrapper.html('<div id="whosOn"><div class="header"><a class="close"></a></div></div>');
        wrapper.click(function (event) {
            if ($(event.target).hasClass('close')) {
                declineInvite();
            } else {
                startChat();
            }
        });
    }
    wrapper.appendTo('#content-middle');
    wrapper.show();
    closeTimeout = setTimeout('stopInvite()', 10000);
}
function stopInvite() {
    cStat = 1;
    var wrapper = $('#whosOn-wrapper');
    wrapper.hide();
}
function startChat() {
    this.chatWindow = window.open(woChatUrl, "Chat", "width=484,height=361");
    this.chatWindow.focus();
    this.chatWindow.opener = window;
    stopInvite();
}
function anPause() {
    clearTimeout(closeTimeout);
}
function anResume() {
    closeTimeout = setTimeout('stopInvite()', 10000);
}
function declineInvite() {
    var wrapper = $('#whosOn-wrapper');
    wrapper.hide();
    return false;
}
if (woPollUrl !== '') {
    var woOldOnload = window.onload;
    if (typeof woOldOnload == 'function') {
        if (!(String(woOldOnload).indexOf('woAfterLoad') > 0)) {
            var woRunOnload = woOldOnload;
            window.onload = function () { woRunOnload(); woAfterLoad(); };
        }
    } else {
        window.onload = woAfterLoad;
    }
}
