function refreshOnline(room) {
	window.room_id = room;
	getOnlineUsers(room);
	window.onlineId = setInterval(function() {
		getOnlineUsers(room);
	}, 30000);
}

function getOnlineUsers(room) {
	var url = '/chat/online/room/'+room;
	$.ajax({
	   url: url,
	   success: function(response){
	    	if(response == 'ban' || response == 'logout') {
			    	window.location.reload();
	   		} else if(response != 'empty') {		   		
		        $('#online-users').html(response);
		    } 
	   }
	 });
}

function publishWall(alias, name)
{
  var data = {"images":[{"src":"http://www.savorchat.com/images/icons/fb_publish_icon.jpg", "href":"http://www.savorchat.com/chat/"+alias}], "room":name, "alias":alias};

  FB.Connect.showFeedDialog(
  92589892765, data, null, null, null, FB.RequireConnect.promptConnect, null, 'Share this chat room!');
}

function toggleChat(type) {
	if($("#toggle").html() == "Pause Chat" || type == 'pause') {
	
		clearInterval(window.refreshId);
		clearInterval(window.onlineId);
		$("#toggle").html("Resume Chat");
		$("#notice").html('CHAT IS PAUSED').show();
		
	} else if($("#toggle").html() == "Resume Chat") {
		window.refresh = 0;
		refreshChat();
		refreshOnline(window.room_id);
		$("#toggle").html("Pause Chat");
		$("#notice").html('').hide();
		
	}
}



function banUser(user_id, room_id) {

	$("#dialog3").dialog({
		bgiframe: true,
		resizable: false,
		height:140,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			Yes: function() {
			  $.ajax({
			   url: '/user/ban/id/'+user_id+'/room/'+room_id,
			   success: function(response){
			   		if(response == 'success') {	
			   			getOnlineUsers(room_id);
				    }
				    
				    $("#dialog3").dialog('close');
				    return false;
			   }
			 });
			},
			Cancel: function() {
				$(this).dialog('close');
				return false;
			}
		}
	});
	
	$("#dialog3").dialog('open');
	return false;
}

function unbanUser(id) {
	$.ajax({
	   url: '/user/unban/id/'+id,
	   success: function(response){
	   		if(response == 'success') {	
	   			$('#ban-'+id).slideUp(200);
		    }

		    return false;
	   }
	 });
}
