function chat(room,base_url,h_entries_scroll,h_entries,h_send,h_input,h_people_container,h_people,h_people_arrows,expand) {

	/*--INITIAL LOADING--*/

		//Define vars
			var latest_entry = 0;
			var ping_count = 1;
			var prev_ping_count = 1;
	
		//Create element vars
			h_entries_scroll = $(h_entries_scroll);
			h_entries = $(h_entries);
			h_send = $(h_send);
			h_input = $(h_input);
			h_people_container = $(h_people_container);
			h_people = $(h_people);
			h_people_arrows = $(h_people_arrows);
			
	
		//Initial update
		updateChat();
		
		//Commence interval updates
		setInterval(function () {
			updateChat();
		},2000);
		
		//Initial ping
		ping();
		
		//Commence interval pings
		setInterval(function () {
			ping();
		},10000);

		
		
		
	/*--ELEMENT CONTROLS--*/

		/*--Entry input box and button--*/

			//Send button
			h_send.click(function(ev) {
				ev.preventDefault();
				submitEntry();
			});
			
			//Send via keyboard enter
			$(h_input).keydown(function(e) {
				
				if(e.keyCode == 13) {
					e.preventDefault();
					submitEntry();
				}
				
			});
	



	/*--PRIMARY FUNCTIONS--*/
		
		/*--UPDATECHAT: updates the chat room via Ajax and JSON--*/
		
			function updateChat() {
				
				//Calculate timestamp
				ts = Math.round(new Date().getTime() / 1000);
				
				//Generate JSON url
				ts_json_url = base_url + 'chat/latest/' + room + '/' + latest_entry + '/' + ts;
				
				//Send Ajax request to specified url
				$.getJSON(ts_json_url, function(data) {			
					
					//If there are no recent entries, add a welcome message and then autoscroll
					if (latest_entry == 0 && !data.entries) {
						welcome_message = '<div class="chat_entry welcome" id="chat_entry_' + room + '_welcome">Welcome to the chat room! Type your message below and press enter to get started.</div>';
						h_entries.append(welcome_message);
						autoScroll(h_entries_scroll);
						latest_entry = data.room.latest_entry;
					
					//If there are recent entries, append them and then autoscroll
					} else if (data.entries) {
						for (var e in data.entries) {
							addEntry(room,data.entries[e]['entry_id'],data.entries[e]['human_time'],data.entries[e]['first_name'],data.entries[e]['last_name'],data.entries[e]['entry']);
						}
						autoScroll(h_entries_scroll);
					}
					
				});
			}




		/*--AUTOSCROLL: automatically scrolls the chat room entries pane--*/
			
			function autoScroll(element) {
				var autoScroll = element.data('jScrollPanePosition') == element.data('jScrollPaneMaxScroll');
				element.jScrollPane({showArrows:true, animateTo:true, animateInterval:25});
				if (autoScroll) {
					element[0].scrollTo(element.data('jScrollPaneMaxScroll'));
				}
			}




		/*--ADD ENTRY: add an entry to the chat room--*/
		
			function addEntry(room,entry_id,human_time,first_name,last_name,message) {
				
				var entry_div_id = '#chat_entry_' + room + '_' + entry_id;
				if (! $(entry_div_id).length) {
					var entry_div = '<div class="chat_entry" id="chat_entry_' + room + '_' + entry_id + '" title="' + human_time + '"><div class="chat_entry_info">' + first_name + ' ' + last_name + '</div><div class="chat_entry_message">' + message + '</div></div>';
					h_entries.append(entry_div);
					latest_entry = entry_id;
				}
			}




		/*--ADD USER: add a user to the chat room--*/
		
			function addUser(room,user_id,first_name,last_name,photo_small,page_name) {
				var person_div_id = '#chat_person_' + room + '_' + user_id;
				var person_div_class = 'online_' + ping_count;
				var person_div_class_prev = 'online_' + prev_ping_count;
				if ($(person_div_id).length) {
					$(person_div_id).addClass(person_div_class);
					$(person_div_id).removeClass(person_div_class_prev);
				} else {
					var person_div = '<a href="' + base_url + 'connect/users/' + page_name + '" target="_blank" class="chat_person hide ' + person_div_class + '" id="chat_person_' + room + '_' + user_id + '" title="' + first_name + ' ' + last_name + '">' + first_name + ' ' + last_name + '</a>';
					if (expand != 0) { 
						var increase = h_people.width() + expand;
						h_people.width(increase);
					}
					h_people.prepend(person_div);
					if (photo_small) { photo_bg = "url("+ photo_small + ")"; $(person_div_id).css({'background-image' : photo_bg}); }
					$(person_div_id).fadeIn("slow");
					if (expand != 0) {
						setTimeout(function() {
							scrollArrowState();
						},1000);
					}
				}
			}




		/*--REMOVE USER: remove expired users from the chat room--*/
		function removeUsers() {
			var prev_div_class = ".online_" + prev_ping_count;
			$(prev_div_class).each(function() {
				$(this).fadeOut(1000, function() {
					$(this).remove();
					if (expand != 0) { 
						var decrease = h_people.width() - expand;
						h_people.width(decrease);
					}
					setTimeout(function() {
						scrollArrowState();
					},1000);
				});
			});
		}




		/*--SUBMIT ENTRY: submit an entry from the user--*/
		
			function submitEntry() {
				if (h_input.val()) {
					
					var entry = h_input.val();
					h_input.attr('disabled', true);
					h_send.attr('disabled', true);
					//h_input.val('');
					
					//Calculate timestamp
					ts = Math.round(new Date().getTime() / 1000);
					
					//Generate JSON url
					ts_json_url = base_url + 'chat/submit/' + ts;
					
					var post = "room_id=" + room + "&entry=" + entry;
					
					//Send Ajax request to specified url
					$.post(ts_json_url, post, function(data) {
						if (data.status == true) {
							h_input.val('');
							h_send.removeAttr("disabled");
							h_input.removeAttr("disabled");
							updateChat();
							h_input.keydown();
							h_input.focus();
						}
					}, 'json');
				}
			}




		/*--PING: pings the user and updates the status of other users--*/
		
			function ping() {
				prev_ping_count = ping_count;
				ping_count = ping_count + 1;

				//Calculate timestamp
				ts = Math.round(new Date().getTime() / 1000);
				
				//Generate JSON url
				ts_json_url = base_url + 'chat/ping/' + ts;
				
				var post = "room_id=" + room;
				
				//Send Ajax request to specified url
				$.post(ts_json_url, post, function(data) {
					if (data.users) {
						for (var u in data.users) {
							addUser(room,data.users[u]['user_id'],data.users[u]['first_name'],data.users[u]['last_name'],data.users[u]['photo_thumb'],data.users[u]['page_name']);
						}
						removeUsers();
					} else { 
						removeUsers();
					}
				}, 'json');
			}

}