/*
Warning:  Cannot modify header information - headers already sent by (output started at /www/htdocs/w00d192c/js/chatbox.js:1) in /www/htdocs/w00d192c/js/chatbox.js on line 4
*/
function updateChatBox(context) {
    var blub = $(context);
    blub = blub.parent("").parent("").parent("");
    var hmm = blub.children(".scroll");
    //alert(hmm);
    //    var hmm = $('#toBeHidden');
    var chatBoxString = blub.attr("id");
    var id = chatBoxString.substring(7,chatBoxString.length);
    var hiddenValue = 1;
    if (hmm.hasClass("hidden")) {
        hmm.removeClass("hidden");
        hiddenValue = 0;
    }
    else hmm.addClass("hidden");
    
    $.post("https://tausch-buecher.de/index.php?ajax=changeMinimized", {
        hidden: ""+hiddenValue, 
        sessionID: ""+id
        },
    function(data) {
        //alert(data);
        });
}
function closeChatBox (sessionID) {
   
    $.post("https://tausch-buecher.de/index.php?ajax=closeChatBox", {
        sID: ""+sessionID
        },
    function(data) {
        $("#chatBox"+sessionID).remove();
    });
}
function sendChatMsg (formContext) {
   
    var jContext = $(formContext);
    var input = jContext.children("#chat-text").attr("value");
    var input2 = jContext.children("#sessionID").attr("value");
    $.post("https://tausch-buecher.de/index.php?ajax=sendChatMsg", {
        chattext: ""+input, 
        sessionID: ""+input2, 
        ajaxsent: "yes"
    },
    function(data) {
        //$("#chatBox"+sessionID).remove();
        updateChatMsgs(input2);
         
         clearInput(jContext.children("#chat-text"));
    });
    return false;
}
function updateChatMsgs(sessionID) {
    var chatBox = $('#chatBox' + sessionID);
    var msgDiv = chatBox.children("#toBeHidden");
    var lastMsg = msgDiv.children().get(1);
    if (!lastMsg) {
        return;
    }
    var msgIDString = lastMsg.getAttribute("id");
    var msgID = msgIDString.substring(11, msgIDString.length);
    $.post("https://tausch-buecher.de/index.php?ajax=getNewChatMsgs", {
        lpid: ""+msgID ,
        sID: ""+sessionID
    },
    function(xml) {
        //alert (xml);
        xml = $.parseXML(xml);
        if ($('post',xml).length != 0) {
            
            $('post',xml).each(function(i) {
                var postID = $(this).find("postID").text();
                var user = $(this).find("user").text();
                //var msg = $(this).find("message")[0].innerHTML; // undefined im IE
				var msg = $(this).find("message")[0].textContent;
				//alert(msg);
                var time = $(this).find("time").text();
                    
                var newDiv = document.createElement('div');
                newDiv.setAttribute("id", "chatMessage"+postID);
                newDiv.setAttribute("class", "chatMessage");
                newDiv.setAttribute("className", "chatMessage");
                
                var divAutor = document.createElement('div');
                divAutor.setAttribute("class", "chatAuthor");
                divAutor.setAttribute("className", "chatAuthor");
                var newLink = document.createElement('a');
                newLink.setAttribute("href", "https://tausch-buecher.de/" + "user/" + user + ".html");
                var newLinkTN = document.createTextNode(user);
                newLink.appendChild(newLinkTN);
                divAutor.appendChild(newLink);
                
                var newTimeTN = document.createTextNode(" um "+time+":");
                divAutor.appendChild(newTimeTN);
                var divText = document.createElement('div');
                divText.setAttribute("class", "chatText");
                divText.setAttribute("className", "chatText");
                
				var span = document.createElement('span');
				span.innerHTML = msg;
				divText.appendChild(span);
                
                newDiv.appendChild(divAutor);
                newDiv.appendChild(divText);
                
                msgDiv[0].insertBefore(newDiv, lastMsg);
                
            });
            
            if (msgDiv.hasClass("hidden")) {
                msgDiv.removeClass("hidden");
            }
        }
    }
    );
}
function clearInput(context) {
    var that = context;
    //    $(context).select();
    /*setTimeout(function(){
        $(that).select();
    },100);*/
    $(that).val("");
}