chat.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. Copyright (c) 2009 Anant Garg (anantgarg.com | inscripts.com)
  3. This script may be used for non-commercial purposes only. For any
  4. commercial purposes, please contact the author at
  5. anant.garg@inscripts.com
  6. Changes and Chamilo Integration: Julio Montoya <gugli100@gmail.com>
  7. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  8. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  9. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  10. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  11. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  12. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  13. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  14. OTHER DEALINGS IN THE SOFTWARE.
  15. */
  16. var windowFocus = true;
  17. var username;
  18. var chatHeartbeatCount = 0;
  19. var minChatHeartbeat = 4000;
  20. var maxChatHeartbeat = 33000;
  21. var chatHeartbeatTime = minChatHeartbeat;
  22. var originalTitle;
  23. var blinkOrder = 0;
  24. var chatboxFocus = new Array();
  25. var newMessages = new Array();
  26. var newMessagesWin = new Array();
  27. var chatBoxes = new Array();
  28. var timer;
  29. var user_status = 0;
  30. //var ajax_url = 'chat.php'; // This variable is loaded in the template/layout/head.tpl file
  31. function set_user_status(status) {
  32. $.ajax({
  33. url: ajax_url+"?action=set_status",
  34. data: "status="+status,
  35. cache: false,
  36. success: function(data) {
  37. }
  38. });
  39. user_status = status;
  40. }
  41. $(document).ready(function() {
  42. originalTitle = document.title;
  43. startChatSession();
  44. $([window, document]).blur(function() {
  45. windowFocus = false;
  46. }).focus(function(){
  47. windowFocus = true;
  48. document.title = originalTitle;
  49. });
  50. /* "On" conditions, divs are created dynamically */
  51. // User name header toogle
  52. $('body').on('click', '#chatboxtitlemain', function() {
  53. if (user_status == 1) {
  54. stopChatHeartBeat();
  55. $('.user_status_main').html(offline_button);
  56. $('#chatboxtitlemain').html(disconnect_lang);
  57. set_user_status(0);
  58. } else {
  59. startChatHeartBeat();
  60. $('.user_status_main').html(online_button);
  61. $('#chatboxtitlemain').html(connect_lang);
  62. set_user_status(1);
  63. }
  64. });
  65. // User name header toogle
  66. $('body').on('click', '.chatboxtitle', function(){
  67. chatbox = $(this).parents(".chatbox");
  68. var chat_id = chatbox.attr('id');
  69. chat_id = chat_id.split('_')[1];
  70. toggleChatBoxGrowth(chat_id);
  71. });
  72. // Minimize button
  73. $('body').on('click', '.chatboxhead .togglelink', function(){
  74. var chat_id = $(this).attr('rel');
  75. toggleChatBoxGrowth(chat_id);
  76. });
  77. // Close button
  78. $('body').on('click', '.chatboxhead .closelink', function(){
  79. var chat_id = $(this).attr('rel');
  80. closeChatBox(chat_id);
  81. });
  82. });
  83. function showChatConnect() {
  84. if (user_status == 1) {
  85. button = online_button;
  86. label = connect_lang;
  87. } else {
  88. button = offline_button;
  89. label = disconnect_lang;
  90. }
  91. $("<div />" ).attr("id","chatmain")
  92. .addClass("chatboxmain")
  93. .html('<div class="chatboxheadmain"><div class="user_status_main">'+button+'</div><div id="chatboxtitlemain">'+label+'</div><div class="chatboxoptions"></div></div>')
  94. .appendTo($( "body" ));
  95. }
  96. /**
  97. * Start chat session
  98. */
  99. function startChatSession() {
  100. /* fix bug BT#5728 whereby users cannot move to the next question in IE9 */
  101. if (typeof ajax_url != 'undefined') {
  102. $.ajax({
  103. url: ajax_url+"?action=startchatsession",
  104. cache: false,
  105. dataType: "json",
  106. success: function(data) {
  107. if (data) {
  108. username = data.me;
  109. user_status = data.user_status;
  110. showChatConnect();
  111. if (user_status == 1) {
  112. startChatHeartBeat();
  113. } else {
  114. stopChatHeartBeat();
  115. }
  116. $.each(data.items, function(my_user_id, user_items) {
  117. my_items = user_items['items'];
  118. $.each(my_items, function(i, item) {
  119. if (item) { // fix strange ie bug
  120. //my_user_id = item.f;
  121. if ($("#chatbox_"+my_user_id).length <= 0) {
  122. createChatBox(my_user_id, user_items.user_info.user_name, 1, user_items.user_info.online);
  123. }
  124. if (item.s == 1) {
  125. //item.f = username;
  126. }
  127. if (item.s == 2) {
  128. $("#chatbox_"+my_user_id+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
  129. } else {
  130. $("#chatbox_"+my_user_id+" .chatboxcontent").append('<div class="chatboxmessage">\n\
  131. <span class="chatboxmessagefrom">'+item.username+':&nbsp;&nbsp;</span>\n\
  132. <span class="chatboxmessagecontent">'+item.m+'</span></div>');
  133. }
  134. }
  135. });
  136. });
  137. for (i=0;i<chatBoxes.length;i++) {
  138. my_user_id = chatBoxes[i];
  139. $("#chatbox_"+my_user_id+" .chatboxcontent").scrollTop($("#chatbox_"+my_user_id+" .chatboxcontent")[0].scrollHeight);
  140. }
  141. }
  142. }});
  143. }
  144. }
  145. function stopChatHeartBeat() {
  146. clearInterval(timer);
  147. timer = null;
  148. }
  149. function startChatHeartBeat() {
  150. timer = setInterval('chatHeartbeat();', chatHeartbeatTime);
  151. }
  152. /*
  153. * Shows the user messages in all windows
  154. *
  155. * Item array structure :
  156. *
  157. * item.s = type of message: 1 = message, 2 = "sent at" string
  158. * item.m = message
  159. * item.f = from_user
  160. *
  161. **/
  162. function chatHeartbeat() {
  163. var itemsfound = 0;
  164. if (windowFocus == false) {
  165. var blinkNumber = 0;
  166. var titleChanged = 0;
  167. for (x in newMessagesWin) {
  168. if (newMessagesWin[x].status == true) {
  169. ++blinkNumber;
  170. if (blinkNumber >= blinkOrder) {
  171. document.title = newMessagesWin[x].username+' says...';
  172. titleChanged = 1;
  173. break;
  174. }
  175. }
  176. }
  177. if (titleChanged == 0) {
  178. document.title = originalTitle;
  179. blinkOrder = 0;
  180. } else {
  181. ++blinkOrder;
  182. }
  183. } else {
  184. for (x in newMessagesWin) {
  185. newMessagesWin[x].status = false;
  186. }
  187. }
  188. for (x in newMessages) {
  189. if (newMessages[x] == true) {
  190. if (chatboxFocus[x] == false) {
  191. //FIXME: add toggle all or none policy, otherwise it looks funny
  192. $('#chatbox_'+x+' .chatboxhead').toggleClass('chatboxblink');
  193. }
  194. }
  195. }
  196. $.ajax({
  197. url: ajax_url+"?action=chatheartbeat",
  198. cache: false,
  199. dataType: "json",
  200. success: function(data) {
  201. $.each(data.items, function(my_user_id, user_items) {
  202. my_items = user_items['items'];
  203. $.each(my_items, function(i, item) {
  204. if (item) { // fix strange ie bug
  205. if ($("#chatbox_"+my_user_id).length <= 0) {
  206. createChatBox(my_user_id, user_items.user_info.user_name, 0, user_items.user_info.online);
  207. }
  208. if ($("#chatbox_"+my_user_id).css('display') == 'none') {
  209. $("#chatbox_"+my_user_id).css('display','block');
  210. restructureChatBoxes();
  211. }
  212. if (item.s == 1) {
  213. //item.f = username;
  214. }
  215. update_online_user(my_user_id, user_items.user_info.online);
  216. if (item.s == 2) {
  217. $("#chatbox_"+my_user_id+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
  218. } else {
  219. newMessages[my_user_id] = {'status':true, 'username':item.username};
  220. newMessagesWin[my_user_id] = {'status':true, 'username':item.username};
  221. $("#chatbox_"+my_user_id+" .chatboxcontent").append('<div class="chatboxmessage">\n\
  222. <span class="chatboxmessagefrom">'+item.username+':&nbsp;&nbsp;</span>\n\
  223. <span class="chatboxmessagecontent">'+item.m+'</span></div>');
  224. }
  225. $("#chatbox_"+my_user_id+" .chatboxcontent").scrollTop($("#chatbox_"+my_user_id+" .chatboxcontent")[0].scrollHeight);
  226. if ($('#chatbox_'+my_user_id+' .chatboxcontent').css('display') == 'none') {
  227. $('#chatbox_'+my_user_id+' .chatboxhead').toggleClass('chatboxblink');
  228. }
  229. itemsfound += 1;
  230. }
  231. });
  232. });
  233. chatHeartbeatCount++;
  234. if (itemsfound > 0) {
  235. chatHeartbeatTime = minChatHeartbeat;
  236. chatHeartbeatCount = 1;
  237. } else if (chatHeartbeatCount >= 10) {
  238. chatHeartbeatTime *= 2;
  239. chatHeartbeatCount = 1;
  240. if (chatHeartbeatTime > maxChatHeartbeat) {
  241. chatHeartbeatTime = maxChatHeartbeat;
  242. }
  243. }
  244. //timer = setTimeout('chatHeartbeat();',chatHeartbeatTime);
  245. }
  246. }); //ajax
  247. }
  248. function closeChatBox(user_id) {
  249. $('#chatbox_'+user_id).css('display','none');
  250. restructureChatBoxes();
  251. $.post(ajax_url+"?action=closechat", {chatbox: user_id} , function(data){
  252. });
  253. }
  254. function restructureChatBoxes() {
  255. align = 0;
  256. for (x in chatBoxes) {
  257. user_id = chatBoxes[x];
  258. if ($("#chatbox_"+user_id).css('display') != 'none') {
  259. if (align == 0) {
  260. $("#chatbox_"+user_id).css('right', '245px');
  261. } else {
  262. width = (align)*(225+7)+20 + 225;
  263. $("#chatbox_"+user_id).css('right', width+'px');
  264. }
  265. align++;
  266. }
  267. }
  268. }
  269. /**
  270. * Function that fires the chat with an user (creates a chat bloclk)
  271. * @param int user id
  272. * @param string user's firstname + lastname
  273. * @param status
  274. *
  275. **/
  276. function chatWith(user_id, user_name, status, userImage) {
  277. createChatBox(user_id, user_name, 0, status, userImage);
  278. $("#chatbox_"+user_id+" .chatboxtextarea").focus();
  279. }
  280. function chatNotYetWith(message) {
  281. $("#js_alerts").html(message);
  282. $("#js_alerts").css('display', 'block');
  283. $("#js_alerts").attr('class', 'alert alert-warning');
  284. $('#js_alerts').alert()
  285. }
  286. /**
  287. * Creates a div
  288. */
  289. function createChatBox(user_id, chatboxtitle, minimizeChatBox, online, userImage) {
  290. if ($("#chatbox_"+user_id).length > 0) {
  291. if ($("#chatbox_"+user_id).css('display') == 'none') {
  292. $("#chatbox_"+user_id).css('display','block');
  293. restructureChatBoxes();
  294. }
  295. $("#chatbox_"+user_id+" .chatboxtextarea").focus();
  296. return;
  297. }
  298. user_is_online = return_online_user(user_id, online, userImage);
  299. var chatbox = $('<div>')
  300. .attr({
  301. id: 'chatbox_' + user_id
  302. })
  303. .addClass('chatbox')
  304. .css('bottom', 0);
  305. var chatboxHead = $('<div>')
  306. .addClass('chatboxhead')
  307. .append(user_is_online);
  308. $('<div>')
  309. .addClass('chatimage')
  310. .append('<img src="'+userImage+'"/>')
  311. .appendTo(chatboxHead);
  312. $('<div>')
  313. .addClass('chatboxtitle')
  314. .append(chatboxtitle)
  315. .appendTo(chatboxHead);
  316. var chatboxoptions = $('<div>')
  317. .addClass('chatboxoptions')
  318. .appendTo(chatboxHead);
  319. if (!!Modernizr.prefixed('RTCPeerConnection', window)) {
  320. $('<a>')
  321. .addClass('btn btn-xs')
  322. .attr({
  323. href: '#'
  324. })
  325. .html('<i class="fa fa-video-camera"></i>')
  326. .on('click', function(e) {
  327. e.preventDefault();
  328. var createForm = $.get(
  329. ajax_url,
  330. {
  331. action: 'start_video',
  332. to: user_id
  333. }
  334. );
  335. $.when(createForm).done(function(response) {
  336. $('#global-modal')
  337. .find('.modal-dialog')
  338. .removeClass('modal-lg');
  339. $('#global-modal')
  340. .find('.modal-body')
  341. .html(response);
  342. $('#global-modal').modal('show');
  343. });
  344. })
  345. .appendTo(chatboxoptions);
  346. }
  347. $('<a>')
  348. .addClass('btn btn-xs togglelink')
  349. .attr({
  350. href: 'javascript:void(0)',
  351. rel: user_id
  352. })
  353. .html('<i class="fa fa-toggle-down"></i>')
  354. .appendTo(chatboxoptions);
  355. $('<a>')
  356. .addClass('btn btn-xs closelink')
  357. .attr({
  358. href: 'javascript:void(0)',
  359. rel: user_id
  360. })
  361. .html('<i class="fa fa-close"></i>')
  362. .appendTo(chatboxoptions);
  363. $('<br>')
  364. .attr('clear', 'all')
  365. .appendTo(chatboxHead);
  366. var chatboxContent = $('<div>')
  367. .addClass('chatboxcontent');
  368. var chatboxInput = $('<div>')
  369. .addClass('chatboxinput');
  370. $('<textarea>')
  371. .addClass('chatboxtextarea')
  372. .on('keydown', function(e) {
  373. return checkChatBoxInputKey(e.originalEvent, this, user_id);
  374. })
  375. .appendTo(chatboxInput);
  376. chatbox
  377. .append(chatboxHead)
  378. .append(chatboxContent)
  379. .append(chatboxInput)
  380. .appendTo('body');
  381. chatBoxeslength = 0;
  382. for (x in chatBoxes) {
  383. if ($("#chatbox_"+chatBoxes[x]).css('display') != 'none') {
  384. chatBoxeslength++;
  385. }
  386. }
  387. if (chatBoxeslength == 0) {
  388. $("#chatbox_"+user_id).css('right', '245px');
  389. } else {
  390. width = (chatBoxeslength)*(225+7)+20 +225;
  391. $("#chatbox_"+user_id).css('right', width+'px');
  392. }
  393. chatBoxes.push(user_id);
  394. if (minimizeChatBox == 1) {
  395. minimizedChatBoxes = new Array();
  396. if ($.cookie('chatbox_minimized')) {
  397. minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
  398. }
  399. minimize = 0;
  400. for (j=0;j<minimizedChatBoxes.length;j++) {
  401. if (minimizedChatBoxes[j] == user_id) {
  402. minimize = 1;
  403. }
  404. }
  405. if (minimize == 1) {
  406. $('.togglelink').html('<i class="fa fa-toggle-up"></i>');
  407. $('#chatbox_'+user_id+' .chatboxcontent').css('display','none');
  408. $('#chatbox_'+user_id+' .chatboxinput').css('display','none');
  409. }
  410. }
  411. chatboxFocus[user_id] = false;
  412. $("#chatbox_"+user_id+" .chatboxtextarea").blur(function(){
  413. chatboxFocus[user_id] = false;
  414. $("#chatbox_"+user_id+" .chatboxtextarea").removeClass('chatboxtextareaselected');
  415. }).focus(function(){
  416. chatboxFocus[user_id] = true;
  417. newMessages[user_id] = false;
  418. $('#chatbox_'+user_id+' .chatboxhead').removeClass('chatboxblink');
  419. $("#chatbox_"+user_id+" .chatboxtextarea").addClass('chatboxtextareaselected');
  420. });
  421. $("#chatbox_"+user_id).click(function() {
  422. if ($('#chatbox_'+user_id+' .chatboxcontent').css('display') != 'none') {
  423. $("#chatbox_"+user_id+" .chatboxtextarea").focus();
  424. }
  425. });
  426. $("#chatbox_"+user_id).show();
  427. }
  428. /**
  429. * Creates the div user status (green/gray button next to the user name)
  430. * @param int user id
  431. * @param int status 1 or 0
  432. */
  433. function return_online_user(user_id, status, userImage) {
  434. var div_wrapper = $("<div />" );
  435. var new_div = $("<div />" );
  436. new_div.attr("id","online_"+user_id);
  437. new_div.attr("class","user_status");
  438. if (status == '1' || status == 1) {
  439. new_div.html(online_button);
  440. } else {
  441. new_div.html(offline_button);
  442. }
  443. div_wrapper.append(new_div);
  444. return div_wrapper.html();
  445. }
  446. /**
  447. * Updates the user status (green/gray button next to the user name)
  448. */
  449. function update_online_user(user_id, status) {
  450. if ($("#online_" +user_id).length > 0) {
  451. if (status == 1) {
  452. $("#online_" +user_id).html(online_button);
  453. } else {
  454. $("#online_" +user_id).html(offline_button);
  455. }
  456. }
  457. }
  458. function toggleChatBoxGrowth(user_id) {
  459. if ($('#chatbox_'+user_id+' .chatboxcontent').css('display') == 'none') {
  460. var minimizedChatBoxes = new Array();
  461. if ($.cookie('chatbox_minimized')) {
  462. minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
  463. }
  464. var newCookie = '';
  465. for (i=0;i<minimizedChatBoxes.length;i++) {
  466. if (minimizedChatBoxes[i] != user_id) {
  467. newCookie += user_id+'|';
  468. }
  469. }
  470. newCookie = newCookie.slice(0, -1);
  471. $.cookie('chatbox_minimized', newCookie);
  472. $('#chatbox_'+user_id+' .chatboxcontent').css('display','block');
  473. $('#chatbox_'+user_id+' .chatboxinput').css('display','block');
  474. $("#chatbox_"+user_id+" .chatboxcontent").scrollTop($("#chatbox_"+user_id+" .chatboxcontent")[0].scrollHeight);
  475. $('.togglelink').html('<i class="fa fa-toggle-down"></i>');
  476. } else {
  477. var newCookie = user_id;
  478. if ($.cookie('chatbox_minimized')) {
  479. newCookie += '|'+$.cookie('chatbox_minimized');
  480. }
  481. $.cookie('chatbox_minimized',newCookie);
  482. $('#chatbox_'+user_id+' .chatboxcontent').css('display','none');
  483. $('#chatbox_'+user_id+' .chatboxinput').css('display','none');
  484. $('.togglelink').html('<i class="fa fa-toggle-up"></i>');
  485. }
  486. }
  487. function checkChatBoxInputKey(event, chatboxtextarea, user_id) {
  488. if(event.keyCode == 13 && event.shiftKey == 0) {
  489. message = $(chatboxtextarea).val();
  490. message = message.replace(/^\s+|\s+$/g,"");
  491. $(chatboxtextarea).val('');
  492. $(chatboxtextarea).focus();
  493. $(chatboxtextarea).css('height','44px');
  494. if (message != '') {
  495. $.post(ajax_url + "?action=sendchat", {to: user_id, message: message} , function(data) {
  496. message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");
  497. $("#chatbox_"+user_id+" .chatboxcontent").append('<div class="chatboxmessage">\n\
  498. <span class="chatboxmessagefrom">'+username+':&nbsp;&nbsp;</span>\n\
  499. <span class="chatboxmessagecontent">'+message+'</span></div>');
  500. $("#chatbox_"+user_id+" .chatboxcontent").scrollTop($("#chatbox_"+user_id+" .chatboxcontent")[0].scrollHeight);
  501. });
  502. }
  503. chatHeartbeatTime = minChatHeartbeat;
  504. chatHeartbeatCount = 1;
  505. return false;
  506. }
  507. var adjustedHeight = chatboxtextarea.clientHeight;
  508. var maxHeight = 94;
  509. if (maxHeight > adjustedHeight) {
  510. adjustedHeight = Math.max(chatboxtextarea.scrollHeight, adjustedHeight);
  511. if (maxHeight)
  512. adjustedHeight = Math.min(maxHeight, adjustedHeight);
  513. if (adjustedHeight > chatboxtextarea.clientHeight)
  514. $(chatboxtextarea).css('height',adjustedHeight+8 +'px');
  515. } else {
  516. $(chatboxtextarea).css('overflow','auto');
  517. }
  518. }
  519. /**
  520. * Cookie plugin
  521. *
  522. * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
  523. * Dual licensed under the MIT and GPL licenses:
  524. * http://www.opensource.org/licenses/mit-license.php
  525. * http://www.gnu.org/licenses/gpl.html
  526. *
  527. */
  528. jQuery.cookie = function(name, value, options) {
  529. if (typeof value != 'undefined') { // name and value given, set cookie
  530. options = options || {};
  531. if (value === null) {
  532. value = '';
  533. options.expires = -1;
  534. }
  535. var expires = '';
  536. if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
  537. var date;
  538. if (typeof options.expires == 'number') {
  539. date = new Date();
  540. date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
  541. } else {
  542. date = options.expires;
  543. }
  544. expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
  545. }
  546. // CAUTION: Needed to parenthesize options.path and options.domain
  547. // in the following expressions, otherwise they evaluate to undefined
  548. // in the packed version for some reason...
  549. var path = options.path ? '; path=' + (options.path) : '';
  550. var domain = options.domain ? '; domain=' + (options.domain) : '';
  551. var secure = options.secure ? '; secure' : '';
  552. document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  553. } else { // only name given, get cookie
  554. var cookieValue = null;
  555. if (document.cookie && document.cookie != '') {
  556. var cookies = document.cookie.split(';');
  557. for (var i = 0; i < cookies.length; i++) {
  558. var cookie = jQuery.trim(cookies[i]);
  559. // Does this cookie string begin with the name we want?
  560. if (cookie.substring(0, name.length + 1) == (name + '=')) {
  561. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  562. break;
  563. }
  564. }
  565. }
  566. return cookieValue;
  567. }
  568. };