

function alertItem(name,id,parent){
	this.name = name;
	this.id = id;
	this.parent =parent;
	this.container =null;
};


alertItem.prototype.createAlert= function(){
	this.container = document.createElement('h1');
	this.parent.div.appendChild (this.container);
	this.container.innerHTML = '<img src="../WCContent/image/icons/chat30x25.png" class="middle" /> Instant message alert: <b>' + this.name + '</b> has sent you a message'
	var a = document.createElement('a');
	a.classname = "linkAlert";
	a.href= 'friendsmessenger.aspx?wci=friendsmessenger&wce=enterConversation&ConversationKey=' + this.id
	a.innerHTML = 'Accept chat';
	a.className = 'accept';
	this.parent.div.appendChild(a);
	var aClose = document.createElement('a');
	aClose.classname = "decline";
	aClose.onclick= this.dismissAlert.bind(this);
	aClose.innerHTML = 'Decline chat'
	aClose.href= '';
	this.parent.div.appendChild(aClose);

};

alertItem.prototype.dismissAlert=function(){
	var u = new updater;
	u.sendARequest('AjaxRequest.aspx?wci=DismissAlert&conversationkey=' + this.id);
	this.container.parentNode.removeChild(this.container);
	if(this.parent.div.innerHTML =='')
		this.parent.clearAlert();
};

//##########################################################################################################################


function alerter(id,path,alertPollTime){
	this.div = document.getElementById(id);
	this.path = path;
	this.alertPollTime = alertPollTime;
	this.stop = false;
}

alerter.prototype.addMessageAlert=function(name,conversationkey ){
	try{
		var alertitem = new alertItem(name,conversationkey,this);
		alertitem.createAlert();
	}catch(err){}

};

alerter.prototype.clearAlert=function(name){
	this.div.style.visibility="hidden";
	this.div.style.display="none";
	this.div.innerHTML = "";
};

alerter.prototype.stopAlerting=function(){
	this.clearAlert();
	var u = new updater;
	u.sendARequest('AjaxRequest.aspx?wci=stopAlerting');
	this.stop =true;
};
alerter.prototype.startAlerting=function(){
	this.stop = false;
	var u = new updater;
	u.sendARequest('AjaxRequest.aspx?wci=startAlerting');
	this.alertListener()
};


alerter.prototype.startRequesting=function(){
	try{
		var u = new updater;
		this.alertListener.bind(new nullObject());
		u.sendARequest(this.path,this.alertListener.bind(this));
	}catch(err){}
};

alerter.prototype.alertListener = function(req) {
    if (!this.stop) {
        if (req) {
            this.clearAlert();
            try {
                var r = eval('(' + req.responseText + ')');
                for (j = 0; j < r.messages.length; j++) {
                    if (this.div.style.visibility) this.div.style.visibility = "visible";
                    this.div.style.display = "block";
                    this.addMessageAlert(r.messages[j].name, r.messages[j].conversationkey);
                };

            } catch (err) {
                //alert(err);
            }
        }
        this.startRequesting.bind(new nullObject());
        var timer = setTimeout(this.startRequesting.bind(this), this.alertPollTime);
    }
};

