function getAbsoluteNonMVCPath(relativePath) {
    var url = window.location.href.split('/');
    return url[0] + '/' + url[1] + '/' + url[2] + '/' + relativePath;
}

function invite(conversationKey,invite,accept,decline,wait,answer,alertPollTime){
	this.conversationKey = conversationKey;
	this.invite =document.getElementById(invite);
	this.accept=document.getElementById(accept);
	this.decline =document.getElementById(decline);
	this.wait=document.getElementById(wait);
	this.answer=document.getElementById(answer);
	this.accept.onclick = this.acceptOffer.bind(this);
	this.decline.onclick = this.declineOffer.bind(this);
	this.trigger = null;
	this.alertPollTime = alertPollTime;
}

invite.prototype.declineOffer=function(name,conversationkey ){
	var u = new updater();
	u.sendARequest(getAbsoluteNonMVCPath('AjaxRequest.aspx?wci=declineInvitation&ConversationKey=') + this.conversationKey);
	layeroff(this.invite);
};

invite.prototype.acceptOffer=function(name,conversationkey ){
	layeroff(this.invite);
	layeron(this.wait);
	var u = new updater();
	u.sendARequest(getAbsoluteNonMVCPath('AjaxRequest.aspx?wci=AcceptInvitation&ConversationKey=') + this.conversationKey);
	this.startRequesting();
};


invite.prototype.startRequesting=function(){
	try{
		var u = new updater();
		u.sendARequest(getAbsoluteNonMVCPath('AjaxRequest.aspx?wci=PaymentAccepted&ConversationKey=') + this.conversationKey,this.paymentListener.bind(this));
	}catch(err){}
};


invite.prototype.paymentListener=function(req){
	if(req){
		try{
			var r = eval('(' +req.responseText+ ')');
			if(r.chattingStatus=="1"){
				top.location.href= getAbsoluteNonMVCPath('friendsmessenger.aspx?wci=friendsmessenger&wce=enterConversation&ConversationKey=') + this.conversationKey;
				return;
			}
			if(r.chattingToStatus=="7"){
				layeroff(this.invite);
				layeroff(this.wait);
				layeron(this.answer);
			}
		}catch(err){}
	}
	var timer = setTimeout(this.startRequesting.bind(this),this.alertPollTime);
};

