// {{{ shoutchat_getXmlRequest()

function shoutchat_getXmlRequest()
{
    var obj_xmlRequest;
    if (document.all) {
        try {
            obj_xmlRequest = new ActiveXObject("Microsoft.XMLHTTP")
        }
        catch (e) {
            try {
                obj_xmlRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                obj_xmlRequest = false;
            }
        }
    }
    else if (document.getElementById) {
        obj_xmlRequest = new XMLHttpRequest();
    }
    else {
        obj_xmlRequest = false;
    }
    
    return obj_xmlRequest;
}

// }}}
// {{{ shoutchat_shout()

function shoutchat_shout(in_form) {
    // don't allow blank messages
    if (!in_form.messageTyping.value && in_form.messageTyping.value !== 0) {
        return;
    }
    
    var tmp_matches = in_form.messageTyping.value.match(/^\/([a-z]+)( |$)(.*)?/);
    if (tmp_matches) {
        switch (tmp_matches[1]) {
            case 'join':
            case 'j':
                tmp_matches[3] = tmp_matches[3].replace(/[#\n\r\t ]/g, '');
                if (tmp_matches[3] && tmp_matches[3] != in_form.channel.value) {
                    in_form.channel.value = tmp_matches[3];
                    in_form.channelDisplay.style.display = '';
                    in_form.channelDisplay.value = '#' + tmp_matches[3];
                    document.getElementById('shoutchat').src = 'shoutchat.php?action=retrieve&channel=' + escape(tmp_matches[3]) + '&fresh=' + Math.random();
                }
            break;
            
            case 'leave':
            case 'part':
            case 'p':
                in_form.channel.value = '';
                in_form.channelDisplay.value = '';
                in_form.channelDisplay.style.display = 'none';
                document.getElementById('shoutchat').src = 'shoutchat.php?action=retrieve&fresh=' + Math.random();
            break;

            case 'list':
                document.getElementById('shoutchat').src = 'shoutchat.php?action=list&fresh=' + Math.random();
            break;

            case 'nick':
                tmp_matches[3] = tmp_matches[3].replace(/[ \n\r\t]/g, '');
                if (tmp_matches[3]) {
                    in_form.screenname.value = tmp_matches[3];
                }
            break;

            case 'help':
                window.open('shoutchat.php?action=helpWindow', 'shoutchatHelp', 'width=300,height=400,toolbar=0,location=0,menubar=0,directories=0,scrollbars=1');
            break;

            case 'history':
                window.open('shoutchat.php?action=historyWindow', 'shoutchatHistory', 'width=154,height=550,toolbar=0,location=0,menubar=0,directories=0,scrollbars=0');
            break;
            
            case 'lines':
                tmp_matches[3].replace(/[^\d]/g, '');
                if (tmp_matches[3]) {
                    in_form.numLines.value = tmp_matches[3];
                }
                else {
                    in_form.numLines.value = '';
                }

                document.getElementById('shoutchat').src = 'shoutchat.php?action=retrieve&numLines=' + in_form.numLines.value + '&channel=' + escape(in_form.channel.value) + '&fresh=' + Math.random();
            break;
            
            case 'topic':
            break;
            
            case 'access':
            break;
        }
    }
    else {
        in_form.action.value = 'shout';
        in_form.idleFactor.value = 0;
        in_form.message.value = in_form.messageTyping.value;
        in_form.submit();
    }

    in_form.messageTyping.value = '';
    return false;
}

// }}}
// {{{ shoutchat_loadChatBox()

function shoutchat_loadChatBox()
{
    var tmp_date = new Date();
    shoutchat_setCookie('tzOffset', tmp_date.getTimezoneOffset() * 60);
    document.forms.shoutchatForm.channel.value = '';
    document.getElementById('shoutchat').src = 'shoutchat.php?action=retrieve&fresh=' + Math.random();
    document.forms.shoutchatForm.messageTyping.focus();
}

// }}}
// {{{ shoutchat_setCookie()

function shoutchat_setCookie(in_cookieName, in_cookieValue)
{
    var cookieExpiry = (arguments.length >= 3 && arguments[2] != 0) ? arguments[2] : 31536000000;
    var cookiePath = (arguments.length >= 4) ? arguments[3] : '/';
    var expDate = new Date();
    expDate.setTime(expDate.getTime() + cookieExpiry);
    document.cookie = in_cookieName + '=' + window.escape(in_cookieValue) + '; expires=' + expDate.
toGMTString() + '; path=' + cookiePath;
}

// }}}
// {{{ shoutchat_getInterval()

function shoutchat_getInterval()
{
    shoutchat_idleFactor++;
    return Math.max(shoutchat_minCheckInterval, Math.pow(shoutchat_checkIntervalBase, shoutchat_idleFactor)) * 1000;
}

// }}}
// {{{ shoutchat_clearIdle()

function shoutchat_clearIdle()
{
    if (shoutchat_idleFactor > 3) {
        shoutchat_idleFactor = 0;
        clearTimeout(shoutchat_timeout);
        shoutchat_timeout = setTimeout('shoutchat_listen();', shoutchat_getInterval());
    }
    else {
        shoutchat_idleFactor = 0;
    }
}

// }}}
// {{{ shoutchat_listen()

function shoutchat_listen() {
    if (shoutchat_xmlRequest) {
        shoutchat_xmlRequest.open('GET', 'shoutchat.php?action=check&channel=' + escape(shoutchat_channel) + '&numLines=' + shoutchat_numLines + '&fresh=' + Math.random(), true);
        shoutchat_xmlRequest.onreadystatechange = function() {
            if (shoutchat_xmlRequest.readyState == 4) {
                var lastRecordedShout = shoutchat_xmlRequest.getResponseHeader('X-Last-Shout');
                if (lastRecordedShout > shoutchat_lastRenderedShout) {
                    window.location = 'shoutchat.php?action=retrieve&channel=' + shoutchat_channel + '&idleFactor=' + shoutchat_idleFactor + '&fresh=' + Math.random();
                }
                else { 
                    shoutchat_timeout = setTimeout('shoutchat_listen();', shoutchat_getInterval());
                }
            }
        }
        
        shoutchat_xmlRequest.send(null)
    }
}

// }}}
// {{{ shoutchat_initChannel()

function shoutchat_initChannel() {
    // correct for scrollbar in IE in iframe
    if (navigator.userAgent.toLowerCase().indexOf('msie') != -1) {
        document.body.style.width = (document.body.clientWidth - 20) + 'px';
    }

    // scroll to bottom
    window.scrollTo(0, 1000000);
    window.parent.document.title = window.parent.document.title.replace(/^(\* )?/, '* ');
    shoutchat_timeout = setTimeout('shoutchat_listen();', shoutchat_getInterval());
}

// }}}
