function getAbsoluteNonMVCPath(relativePath) {
	var url = window.location.href.split('/');
	return url[0] + '/' + url[1] + '/' + url[2] + '/' + relativePath;
}

function getAbsoluteMVCPath(relativePath) {
	var url = window.location.href.split('/');
	return url[0] + '/' + url[1] + '/' + url[2] + relativePath + "/";
}

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='" + getAbsoluteNonMVCPath('WCContent/image/icons/default/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 = getAbsoluteNonMVCPath('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(getAbsoluteNonMVCPath('AjaxRequest.aspx?wci=DismissAlert&conversationkey=') + this.id);
	this.container.parentNode.removeChild(this.container.nextSibling);
	this.container.parentNode.removeChild(this.container.nextSibling);
	this.container.parentNode.removeChild(this.container);
	if (this.parent.div.innerHTML == '') {
		this.parent.clearAlert();
	}
	return false;
};

//##########################################################################################################################


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.alertStatus = function () {
	return this.stop;
};

alerter.prototype.stopAlerting=function(){
	this.clearAlert();
	jQuery.ajax({
		url: "/messenger/stopalerting/",
		context: document.body,
		type: "POST",
		success: function () {
		}
	});
	this.stop =true;
};
alerter.prototype.startAlerting = function () {
	this.stop = false;
	jQuery.ajax({
		url: "/messenger/startalerting/",
		context: document.body,
		type: "POST",
		success: function (data) {
			
		}
	});
	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 + ')');
				var j = 0;
				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);
	}
};


