//load( "components.asc" );
application.onAppStart = function()
{
	now_date = new Date();
	// Get the server shared object 'users_so' and 'list_so'
	application.users_so = SharedObject.get("users_so", false);
	application.nextId = 100;
	application.history = "<font color='#990000'>("+now_date+ ") Application Started</font>";
	trace(application.history)
}

//when someone connects to the application(a room)
application.onConnect = function(newClient,name,money,purpose)
{	

			if (name=="guest")
			{
				newClient.name ="guest"+application.nextId
  				money=0;
				application.nextId++;
			}else{
				newClient.name=name;
			}

			newClient.id=application.nextId;
			
			if (purpose!="record")
			{
				var PeopleObj = new Object();
				PeopleObj.name = newClient.name;
				PeopleObj.money = money;
				PeopleObj.status = "offline";
				//PeopleObj.status = "online";
				PeopleObj.id = newClient.id;
				application.users_so.setProperty(PeopleObj.id,PeopleObj);
			}

		application.nextId++;
	
		// Accept the client's connection		
	 	application.acceptConnection(newClient);
		
		//Update the history log
		if(newClient.name!="administrator")
		{
			now_date = new Date();
			application.history += "<br>("+ now_date + ") "+ newClient.name + " joined the chat." 
		}

		//send the log to the admin
		newClient.call("setHistory",null,application.history);		

		//function below
		newClient.requestPrivateSrvr = function(username, pmode)
		{	
			now_date = new Date();
			application.history += "<br>("+ now_date + ") "+ username + " requested private chat, mode:" + pmode + "."
			application.users_so.send("requestPrivate", username, pmode);
		};
			
		newClient.leavePrivateSrvr = function (username)
		{
			now_date = new Date();
			application.history += "<br>("+ now_date + ") "+ username + " left private chat."
			application.users_so.send("leavePrivate", username);
		};
		
		newClient.sendTip = function(ammount,from) 
		{	
			now_date = new Date();
			application.history += "<br>("+ now_date + ") "+ from + " tipped the model with " + ammount/100 + "$. "
			application.users_so.send("recieveTip",ammount,from);
		};
		
		newClient.msgFromClient = function(msg) 
		{	
			trace("messagefrom client");

			msg="<b>"+newClient.name+"</b>!!"+msg;
		    arContents = new Array();
			arContents = msg.split("!!");
			
			now_date = new Date();
		 	application.history += "<br>("+ now_date + ") "+arContents[0]+":"+arContents[2];
			application.users_so.send("msgFromSrvr", msg);
		};
		
		newClient.changeStatus = function(status) 
		{	
			PeopleObj.status = status;
			trace(status);
			application.users_so.setProperty(PeopleObj.id,PeopleObj);
			trace();
		};
	
}

application.onDisconnect = function(client)
{		
	var personObj= application.users_so.getProperty(client.id);
	if (personObj.name!="administrator")
	{
		now_date = new Date();
		application.history += "<br>("+ now_date + ") "+ personObj.name + " left the chat."
	}
	application.users_so.setProperty(client.id, null);
		
}
