﻿
ChatRoom = new function()
{
	this._serviceUrl = "/ESHOP/ChatService.ashx";
	this._getUserListInterval = 30000; // 5 seconds
	this._getMessagesInterval = 2000; // 1 seconds
	this._updateInterval = 300000; // 5 minutes
	this._getPopupInterval =5000; //5 seconds
	this._getonlineuserInterval = 2500;
	this._getMyFriendCountInterval =300000;
	
	//辅助方法
	this._createDelegate = function(instance, method)
	{
		return function()
		{
			method.apply(instance, arguments);
		}
	}
	
	this._ajaxUpdate = function(params, elementId, callback)
	{
		new Ajax.Updater(
			elementId,
			this._serviceUrl,
			{
				method: "post",
				evalScript: true,
				parameters: params,
				onFailure: callback,
				onException: callback,
				onComplete: callback
			});
	}

	
	this._ajaxRequest = function(params, callback)
	{
		new Ajax.Request(
			this._serviceUrl,
			{
				method: 'post',
				evalScript: true,
				parameters: params,
				onFailure: callback,
				onException: callback,
				onComplete: callback    
			});
	}
	
	this.getUserList = function()
	{
		this._ajaxUpdate(
			"action=users",
			"UserList",
			this._createDelegate(this, this._getUserListCallback));
	}
	this._getUserListCallback = function()
	{
		setTimeout(
			this._createDelegate(this, this.getUserList),
			this._getUserListInterval);
	}
	

	this.getPopup = function()
	{
		this._ajaxUpdate(
			"action=getPopup",
			"divform",
			this._createDelegate(this, this._getPopupCallback));
	}
	this._getPopupCallback = function()
	{
		setTimeout(
			this._createDelegate(this, this.getPopup),
			this._getPopupInterval);
	}
	/*
	获得用户聊天的内容		
	*/
	this.getMessages = function()
	{		
		this._ajaxUpdate(
			"action=getMs&fromuserid="+$("spanfromuserid").innerHTML+"&touserid="+$("spanuserid").innerHTML,
			"ChatContent",
			this._createDelegate(this, this._getMessagesCallback));
	}
	this._getMessagesCallback = function()
	{
		var ele = $("ChatContent");
		ele.scrollTop = ele.scrollHeight;
		setTimeout(
			this._createDelegate(this, this.getMessages),
			this._getMessagesInterval);
	}	
	
	/*
	用户update自己，不让自己过期
	*/
	this.update = function()
	{
		this._ajaxRequest(
			"action=update",
			this._createDelegate(this, this._updateCallback));
	}
	this._updateCallback = function()
	{
		setTimeout(
			this._createDelegate(this, this.update),
			this._updateInterval);
	}
	/*
	在线用户
	*/
	this.getonlineuser = function()
	{
		this._ajaxUpdate(
			"action=onlineuser&userid="+$("spanfromuserid").innerHTML,
			"divOnlineUserList",
			this._createDelegate(this, this._onlineuserCallback));
	}
	this._onlineuserCallback = function()
	{
		setTimeout(
			this._createDelegate(this, this.getonlineuser),
			this._getonlineuserInterval);
	}
	/*
	*得到所有好友列表
	*/
	this.GetMyFriend=function()
	{
	    this._ajaxUpdate(
	        "action=GetMyFriend",
	        "divMyFriend",
	        this._createDelegate(this,this._GetMyFriendCallback));
	}
	this._GetMyFriendCallback =function()
	{
	    setTimeout(
	        this._createDelegate(this,this.GetMyFriend),
	        this._getonlineuserInterval);
	}
	
	/*
	*得到所有好友的数量
	*/
	this.GetMyFriendCount=function()
	{
	    this._ajaxUpdate(
	        "action=GetMyFriendCount",
	        "spanMyFriend",
	        this._createDelegate(this,this._GetMyFriendCountCallback));
	}
	this._GetMyFriendCountCallback =function()
	{
	    setTimeout(
	        this._createDelegate(this,this.GetMyFriendCount),
	        this._getMyFriendCountInterval);
	}
	/*
	*得到所有在线用户的数量
	*/
	this.GetOnlineuserCount=function()
	{
	    this._ajaxUpdate(
	        "action=GetOnlineuserCount",
	        "spanOnlineUser",
	        this._createDelegate(this,this._GetOnlineuserCountCallback));
	}
	this._GetOnlineuserCountCallback =function()
	{
	    setTimeout(
	        this._createDelegate(this,this.GetOnlineuserCount),
	        this._getonlineuserInterval);
	}
	/*
	发送消息
	*/
	this.sendMessage = function(msTo)
	{
		var message = $("UserInputTextBox").value;
		if (message)
		{
			$("UserInputTextBox").value = "";
			var encodedMsg = encodeURIComponent(message);
			var encodedmsTo = encodeURIComponent(msTo);
			
			this._ajaxRequest(
				"action=send&msg=" + encodedMsg+"&msTo="+encodedmsTo);
		}
		//getMessageInfo(msTo);
	}
	/*
	*添加我的好友（我的商圈）
	*/
	this.addMyFriend =function()
	{
	    if(confirm("是否确定加入我的商圈？"))
	    {
	        var FriendID =$('spanuserid').innerHTML;
	        if(FriendID.length!=0)
	        {
	            this._ajaxRequest("action=addMyFriend&FriendID="+FriendID);
	            $("spanMyFriend").innerHTML= parseInt($("spanMyFriend").innerHTML)+1;
	        }
	    }
	}

	this.inputBox_KeyPress = function(e,msTo)
	{
		if (e.keyCode == 13)
		{
			this.sendMessage(msTo);		
			try
			{
				e.preventDefault();
			}
			catch(e){}			
			return false;
		}
		
		return true;		
	}	
}
