﻿var uid = 0;
var maxMessageId = 0;
var cultureCode = "";
var articleId = 0;
var userNameDiv = "";
var dishttp = false;
var sending = false;
var discurlprefix = ""; //"http://"+document.domain+"/";

var sendString = "SEND";
var cancelString = "CANCEL";
var replyString = "REPLY";


function rewriteclass(nod, val) { var att = nod.getAttributeNode("class"); if (!att) { att = document.createAttribute("class"); att.nodeValue = val; nod.setAttributeNode(att); } else att.nodeValue = val; }
function insertChild(newElement, parent) {if (parent.hasChildNodes()) parent.insertBefore(newElement, parent.firstChild);else parent.appendChild(newElement);}
function _utf8_encode(t) { t = t.replace(/\r\n/g, "\n"); var utftext = ""; for (var n = 0; n < t.length; n++) { var c = t.charCodeAt(n); if (c < 128) { utftext += String.fromCharCode(c); } else if ((c > 127) && (c < 2048)) { utftext += String.fromCharCode((c >> 6) | 192); utftext += String.fromCharCode((c & 63) | 128); } else { utftext += String.fromCharCode((c >> 12) | 224); utftext += String.fromCharCode(((c >> 6) & 63) | 128); utftext += String.fromCharCode((c & 63) | 128); } } return utftext; }
function encode(t) { return escape(_utf8_encode(t)).replace(/\+/g, "%2B"); }


function sendComment(textId, parentComment) {
    var sendText = document.getElementById("sendText" + textId);
    if (!sendMessage(sendText.value, parentComment)){}
    sendText.value = "";

    sendText.parentNode.parentNode.removeChild(sendText.parentNode);
}
function sendCommentCancel(textId) {
    var sendText = document.getElementById("sendText" + textId);
    sendText.parentNode.parentNode.removeChild(sendText.parentNode);
}

function createSendForm(parentComment) {
    var sendPanelId = "sendPanel" + parentComment;
    var sendPanel = document.getElementById(sendPanelId);
    if (sendPanel) {
        sendPanel.parentNode.removeChild(sendPanel);
        return true;
    }
    

    ++uid;
    var comments = document.getElementById("comments_" + parentComment);

    sendPanel = document.createElement("div"); rewriteclass(sendPanel, "sendPanel");
    sendPanel.id = sendPanelId;

    // user name
    var userNameDiv = document.createElement("div");
    userNameDiv.appendChild(document.createTextNode(userNameText + ":"));
    rewriteclass(userNameDiv, "userName");
    sendPanel.appendChild(userNameDiv);
    
    // text to send
    var sendText = document.createElement("textarea");
    sendText.rows = 5;
    sendText.id = "sendText" + uid;
    sendPanel.appendChild(sendText);

    // send button
    var sendButton = document.createElement("input");
    sendButton.type = "button";
    sendButton.value = sendString;
    sendButton.onclick = new Function("sendComment(" + uid + ",\"" + parentComment + "\");");
    sendPanel.appendChild(sendButton);

    // cancel button
    var sendCancelButton = document.createElement("input");
    sendCancelButton.type = "button";
    sendCancelButton.value = cancelString;
    sendCancelButton.onclick = new Function("sendCommentCancel(" + uid + ");");
    sendPanel.appendChild(sendCancelButton);

    // insert panel into the comments panel
    insertChild(sendPanel, comments);

    sendText.focus();

    return true;
}

function sendMessage(text, parentComment) {
    if (!dishttp) dishttp = gethttp();
    if (sending) return false;
    if (text.value == "") return false;

    var params = "l=" + cultureCode + "&action=send&articleid=" + articleId + "&parentcomment=" + parentComment + "&text=" + encode(text);
    dishttp.open("POST", discurlprefix + "components/general/comments/comments.aspx", true);
    dishttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    dishttp.setRequestHeader("Content-length", params.length);
    dishttp.setRequestHeader("Connection", "close");
    dishttp.onreadystatechange = function() {
    if (dishttp.readyState == 4) {
            sending = false;
            if (dishttp.status == 200) { refreshdiscussion(); }
        } 
    }
    sending = true;
    dishttp.send(params);

    return true;
}
function refreshdiscussion() {
    if (!dishttp) dishttp = gethttp();
    dishttp.open("GET", discurlprefix + "components/general/comments/comments.aspx?action=get&articleid=" + articleId + "&maxmessageid=" + maxMessageId + "&l=" + cultureCode, true);
    dishttp.onreadystatechange = function() {
        if (dishttp.readyState == 4 && dishttp.status == 200) {
            var local = new Function("return " + dishttp.responseText)();
            addMessage(local[0].messageid, local[0].username, local[0].text, local[0].date, local[0].parent);
            refreshdiscussion();
        } else { }
    }
    dishttp.send(null);
}
var smilies = [[":)", "smilie"], [":-)", "smilie"], [":d", "smilie2"], [":-d", "smilie2"], ["xd", "smilie2"], [";)", "wink"], [";-)", "wink"], ["|)", "what"], ["|-)", "what"], [":$", "styd"], [":o", "ooo"], [":-o", "what"], [":s", "oh"], [":-s", "oh"], [":(", "no"], [":-(", "no"], [":p", "mnam"], [":-p", "mnam"], [":@", "grrr"], ["&lt;|&gt;", "kundicka"], ["8--", "pyj"], ["(i)", "light"], ["(6)", "devil"], ["(k)", "kiss"], ["(l)", "heart"]];
function splitMsgText(celltexts, old, newtagfce, tagfceparam) {
    for (var m = 0; m < celltexts.length; ++m) if (celltexts[m].nodeValue != null) {
        var p = celltexts[m].nodeValue.toLowerCase().indexOf(old);
        if (p >= 0) celltexts.splice(m, 1, document.createTextNode(celltexts[m].nodeValue.substr(0, p)), newtagfce(tagfceparam), document.createTextNode(celltexts[m].nodeValue.substr(p + old.length)));
    }
    return celltexts;
}
function insertMsgText(msg, textPanel) {
    var celltexts = splitMsgText(new Array(document.createTextNode(msg)), "<br/>", function(param) { return document.createElement("br"); }, -1);
    for (var s = 0; s < smilies.length; ++s) celltexts = splitMsgText(celltexts, smilies[s][0], function(param) { var img = document.createElement("img"); var src = document.createAttribute("src"); src.nodeValue = "images/smilies/" + smilies[param][1] + ".gif"; img.setAttributeNode(src); return img; }, s);
    for (var m = 0; m < celltexts.length; ++m) textPanel.appendChild(celltexts[m]);
}
function addMessage(messageid, username, text, date, parentComment) {
    if (maxMessageId < messageid) maxMessageId = messageid;
    var comments = document.getElementById("comments_" + parentComment);

    var commentPanel = document.createElement("div"); rewriteclass(commentPanel, "comment");
    commentPanel.id = "comment_" + messageid;

    var commentHeader = document.createElement("div"); rewriteclass(commentHeader, "commentHeader");
    var replyLink = document.createElement("a");
    replyLink.href = "#";
    replyLink.onclick = new Function("return !createSendForm(" + messageid + ");");
    replyLink.appendChild(document.createTextNode(replyString));
    commentHeader.appendChild(replyLink);
    commentHeader.appendChild(document.createTextNode(username));
    var dateSpan = document.createElement("span");
    dateSpan.appendChild(document.createTextNode(date));
    commentHeader.appendChild(dateSpan);


    var commentBody = document.createElement("div"); rewriteclass(commentBody, "commentBody");
    insertMsgText(text, commentBody);

    var subcomments = document.createElement("div"); rewriteclass(subcomments, "subcomments");
    subcomments.id = "comments_" + messageid;

    commentPanel.appendChild(commentHeader);
    commentPanel.appendChild(commentBody);
    commentPanel.appendChild(subcomments);
    
    // insert panel into the comments panel
    insertChild(commentPanel, comments);

    return true;
}
function deleteMessage(messageid) {
    if (!confirm("Are you sure?"))
        return true;
        
    var msgPanel = document.getElementById("comment_" + messageid);

    if (!dishttp) dishttp = gethttp();
    dishttp.open("GET", discurlprefix + "components/general/comments/comments.aspx?action=delete&messageid=" + messageid, true);
    dishttp.onreadystatechange = function() {
        if (dishttp.readyState == 4 && dishttp.status == 200) {
            
        } else { }
    }
    dishttp.send(null);

    //
    msgPanel.style.position = "absolute";
    msgPanel.style.visibility = "hidden";

    return true;
}
