
function DatingoChat() {
    this.polltime = 10000; // 10 sekunden
    this.partner = null;
    this.partnerOnline = null;
    this.username = null;
    this.chatdialog = null;
    this.chatinput = null;
    this.xmlhttp = null;
};

DatingoChat.prototype.startMessagePolling = function() {
    if (!this.username) return;  //nicht eingelogged, kein polling
    this.getNewChatData();
    var _this = this;
    window.setTimeout(function(){_this.startMessagePolling()}, this.polltime);
};

DatingoChat.prototype.sendNewChatData = function() {
    textDiv = window.document.getElementById(this.chatinput);
    text = textDiv.value;
    if (text == '') return;
    textDiv.value = "";
    try {
        this.getNewChatData(text);
    } catch(err) {
    }
};

DatingoChat.prototype.getXMLHttpRequest = function() {
    if (this.xmlhttp && this.xmlhttp.readyState==4) {
        return this.xmlhttp;
    } else {
        this.xmlhttp = newXMLHttpRequest();
        return this.xmlhttp;
    };
};

DatingoChat.prototype.getNewChatData = function(text) {
    var params = "";
    if (this.partner) {
        params += "partner=" + this.partner;}
    if (text) {
        params += "&text=" + text.replace(/&/g, "%26");}
    var xmlhttp = this.getXMLHttpRequest();
    xmlhttp.open('POST', '/user/chatjson/', true);
    var _this = this;
    xmlhttp.onreadystatechange = getReadyStateHandler(xmlhttp, function(response) {_this.handleResponse(response)});
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded") ;
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.send(params);
};
    

DatingoChat.prototype.handleResponse = function(response) {
    eval("var result = " + response.responseText);
    this.handleNewChatData(result);
};

DatingoChat.prototype.handleNewChatData = function(chatData) {
    if (chatData["isOnline"] != this.partnerOnline) {
        this.displayOnlineStatus(this.partner, chatData["isOnline"]);
        this.partnerOnline = chatData["isOnline"];
    }
    for (var msgIndex in chatData["newMessages"]) {
        msg = chatData["newMessages"][msgIndex]
        this.displayPartnerMessage(msg[0], msg[1]);
        };
    for (var msgIndex in chatData["expiredMessages"]) {
        msg = chatData["expiredMessages"][msgIndex]
        this.displayExpiredMessage(msg[0], msg[1]);
        };
    if (chatData["messageEcho"]) {
        this.displayOwnMessage(this.username, chatData["messageEcho"]);
    }
    if (chatData["error"]) {
        this.displayErrorMessage(chatData["error"]);
    }
};

DatingoChat.prototype.displayPartnerMessage = function(name, text) {alert(name+text)};

DatingoChat.prototype.displayOwnMessage = function(name, text) {this.displayPartnerMessage(name, text)};

DatingoChat.prototype.displayErrorMessage = function(text) {};

DatingoChat.prototype.displayExpiredMessage = function(name, text) {};

DatingoChat.prototype.displayOnlineStatus = function(name, isOnline) {};



var notifierOpen = false;
var currentPartner = null;
var notifier = null; //seite ist hier noch nicht geladen, kann also nicht hier gesetzt werden
var hasFocus = true;

function newMessage(name, message) {
    if (notifierOpen) return;
    currentPartner = name;
    notifier = window.document.getElementById('chatnotifier');
    window.document.getElementById('chat_notify_name').innerHTML="<a href='/member/" + name + "/'>" + name + "</a>";
    window.document.getElementById('chat_notify_message').innerHTML=message;
    notifier.style.display = "block";
    self.focus();
    window.focus();
    if (!hasFocus) alert("Chat!");
    moveNotifier();
}

self.addEventListener("focus",CheckFocus,true);
self.addEventListener("blur",CheckBlur,true);

function CheckFocus(event)
{
  hasFocus = true;
}

function CheckBlur(event)
{
  hasFocus = false;
}


function moveNotifier(pos) {
    if (notifierOpen) return;
    if(typeof(pos) == 'undefined') pos = -100;
    notifier.style.bottom = pos + "px";
    if (pos < 0) {
        window.setTimeout(function(){moveNotifier(pos+2)},15);
    } else {
        notifierOpen = true;
        window.setTimeout(function(){removeNotifier()},25000);
    }
}

function removeNotifier(pos) {
    if (!notifierOpen) return;
    if(typeof(pos) == 'undefined') pos = 0;
    notifier.style.bottom = pos + "px";
    if (pos > -100) {
        window.setTimeout(function(){removeNotifier(pos-2)},15);
    } else {
        notifierOpen = false;
        //datingoChat.getNewChatData();
    }
}

function openChatWindow() {
    open('/user/chatbox/' + currentPartner + '/','chat_' + currentPartner,'width=400,height=350');
    removeNotifier();
}

 function blockPartner() {
    window.location.href= "/user/contactParameter/addBlockUser/" + currentPartner + "/?next=/user/contactParameter/";
    removeNotifier();
}