1 $(document).ready(function() {
  2 
  3 /**
  4  * Creates the ct namespace for the chatapp widget
  5  * @name ct
  6  * @namespace ct
  7  */
  8 var ct = {}; 
  9 
 10 /**
 11  * An array of possible names for the user. Randomly generated.
 12  */
 13 ct.names = ["meatPopsicle", "pilez", "madeMan", "wiseGuy"];
 14 
 15 /**
 16  * Randomly choose a user name 
 17  */
 18 ct.user = ct.names[Math.floor(Math.random()*4)];
 19 
 20 /**
 21  * Instantiate the widgets in the DOM dynamically
 22  */
 23 ct.init = function() {
 24    $('<h2 id="title"></h2><div id="chat"></div><textarea id="chat_input"></textarea><div id="users"></div>').appendTo('body');
 25    $('#title').html("Your username is: " + ct.user);
 26 
 27 }();
 28 
 29 /**
 30  * Grab the init funciton chat widget
 31  */
 32 ct.scroll = document.getElementById("chat");
 33 
 34 /**
 35  * Store the token
 36  */
 37 ct.token = "fe60596f247178f2e94ea9ec0edde4f0c423b77c96e1c074e91de6fa"; 
 38 
 39 /**
 40  * Get the current app name
 41  * lends ct
 42  * @param {string} a url representing a controller method server-side
 43  * @param {object} data sent along with the ajax request
 44  * @return {object} current app name
 45  */
 46 ct.get = function(url, data) {
 47 
 48    return $.getJSON("http://cutetypo.com" + url + "?callback=?&token=" + ct.token, data);
 49 
 50 };
 51 
 52 /**
 53  * Send the server a message
 54  * 
 55  * @param {string} a url representing a controller method server-side
 56  * @param {object} data sent along with the ajax request
 57  * @return {boolean} success
 58  */
 59 ct.send = function(url, data) {
 60 
 61    return $.getJSON("http://cutetypo.com" + url + "?message=" + ct.msg + "&username=" + ct.user + "&callback=?&token=" + ct.token, data);
 62 
 63 };
 64 
 65 /**
 66  * Initial interval for how far back to retrieve messages
 67  */
 68 ct.int = "-1days";
 69 
 70 /**
 71  * Initial grab of all the messages on load
 72  * 
 73  * @param {string} a url representing a controller method server-side
 74  * @param {object} data sent along with the ajax request
 75  * @return {object} a list of all returned messages
 76  */
 77 ct.read = function(url, data) {
 78 
 79    return $.getJSON("http://cutetypo.com" + url + "?since=" + ct.int + "&callback=?&token=" + ct.token, data);
 80 
 81 };
 82 
 83 /**
 84  * Look for all the active users in the last 5 minutes to list in the sidebar
 85  * 
 86  * @param {string} a url representing a controller method server-side
 87  * @param {object} data sent along with the ajax request
 88  * @return {object} a list of all active users
 89  */
 90 ct.userCheck = function(url, data) {
 91 
 92    return $.getJSON("http://cutetypo.com" + url + "?since=-5minutes" + "&callback=?&token=" + ct.token, data);
 93 
 94 };
 95 
 96 /**
 97  * Refresh the chat window at an interval
 98  * 
 99  * @param {string} a url representing a controller method server-side
100  * @param {object} data sent along with the ajax request
101  * @return {object} a list of all returned messages
102  */
103 ct.ref = function(url, data) {
104 
105    return $.getJSON("http://cutetypo.com" + url + "?since=-1seconds" + "&callback=?&token=" + ct.token, data);
106 
107 };
108 
109 ct.refresh = function() {
110 
111    ct.ref("/message/get")
112      .then(function(data) {});
113 
114 }
115 
116 /*ct.get("/name/get")
117    .then(function(data) {  });
118 */
119 
120 /**
121  * Call the read method of ct and add all the messages returned to the chat
122  * window, then scroll to the bottom of the window
123  */
124 ct.read("/message/get")
125   .then(function(data) {
126 
127      for(var msg in data) {
128       
129          $("#chat").append("<p>user: " + data[msg].username + " | " + data[msg].message + "</p>");
130          ct.scroll.scrollTop = 500;
131 
132      }
133 
134 });
135 
136 /**
137  * Call the usercheck function and add the returned users to the sidebar
138  */
139 ct.userlist = ct.userCheck("/message/get")
140  .then(function(data) {
141     for(var user in data) {
142 
143        $("#users").append("<p>" + data[user].username + "</p>");
144 
145     }
146 
147 
148  });
149 
150 /**
151  * Listen for a keydown event on the chatbar, then call the write functions
152  * sending in the set object properties to the server and append the messages
153  * to the chat window
154  */
155 $("#chat_input").keydown(function(event) {
156 
157   if(event.keyCode == '13') {
158       event.preventdefault;
159       ct.msg = $("#chat_input").val();
160       ct.send("/message/send")
161         .then(function(data) {
162             
163            $("#chat_input").val("");
164 
165            ct.ref("/message/get")
166              .then(function(data) {
167 
168                ct.userlist;
169                for(var msg in data) {
170    
171                   $("#chat").append("<p>user: " + data[msg].username + " | " + data[msg].message + "</p>");
172                   ct.scroll.scrollTop = ct.scroll.scrollHeight;
173 
174                }
175 
176             });
177 
178       });
179 
180   };
181 
182 });
183 /**
184  * Set a recurring interval to refresh the chat window
185  */
186 setInterval(function() {ct.ref("/message/get").then(function(data) {console.log(data);});}, 1000);
187 
188 });
189 
190