Advertisement
SergioRP

global.js

Sep 7th, 2014
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function titulo(title) {
  2.     var titulo = $('#site-title').html();
  3.    
  4.     if (title !== undefined) {
  5.         $('#site-title').html(titulo['split']('SergioPiza')[0] + 'SergioPiza - ' + title);
  6.     } else {
  7.         $('#site-title').html('SergioPiza' + titulo['split']('SergioPiza')[1]);
  8.     }
  9. }
  10.  
  11. function TitleNotifyPrepend(n) {
  12.     var titulo = $('#site-title').html();
  13.     $('#site-title').html('(' + $.trim(n) + ') SergioPiza' + titulo['split']('SergioPiza')[1]);
  14. }
  15.  
  16. function goTo(href) {
  17.     location.href='#!/' + href + '/';
  18. }
  19.  
  20. var lr = {
  21.     f: function(a) {
  22.         Messi.load('lib/'+a+'.php', {
  23.             title: a.capitalize(),
  24.             modal: true,
  25.             titleClass: 'info anim'
  26.         });
  27.     }
  28. }
  29.  
  30. function MyHash() {
  31.     var mh = location.hash['split']('#!/')[1]['split']('/');
  32.     mh[0] = mh[0].toLowerCase();
  33.     return mh;
  34. }
  35.  
  36. function LoadPage(query) {
  37.     if (location.hash == null || location.hash == '') {
  38.         return false;
  39.     }
  40.     $('.loading-page').slideDown();
  41.     $.ajax({
  42.         url: 'files/' + MyHash()[0] + '.php',
  43.         type: 'POST',
  44.         data: (query? query : ''),
  45.         success: function (content) {
  46.             $('#page-content').html(content);
  47.         },
  48.         error: function(xhr, textStatus, errorThrown){
  49.             if (xhr.status == '404') {
  50.                 $('#page-content').html('<div class="erro"><h1>404 - Not Found</h1><p>Esta url não existe</p></div>');
  51.             } else {
  52.                 $('#page-content').html('<div class="erro"><h1>'+errorThrown+'</h1><p>Ocorreu um erro inexperado. Código do erro: <b>'+xhr.status+'</b></p></div>');
  53.             }
  54.         },
  55.         complete: function(){
  56.             $('.loading-page').slideUp();
  57.         }
  58.     });
  59. }
  60.  
  61. function OnHashChange() {
  62.    
  63.     messidebug(0);
  64.     HideSearch();
  65.    
  66.     if (MyHash()[1] == undefined) {
  67.         location.hash = location.hash + '/';
  68.     }
  69.    
  70.     if (MyHash()[1].length > 0) {
  71.         switch (MyHash()[0]) {
  72.            
  73.             case 'mensagens':
  74.                 if (MyHash()[1] == 'new' && MyHash()[2].length > 0) {
  75.                     LoadPage('new=' + encodeURIComponent( MyHash()[2] ));
  76.                     break;
  77.                 } else
  78.                 if (MyHash()[1] == 'view' && MyHash()[2].length > 0) {
  79.                     LoadPage('view=' + encodeURIComponent( MyHash()[2] ));
  80.                     break;
  81.                 } else
  82.                 if (MyHash()[1] == 'list') {
  83.                     LoadPage('list=1');
  84.                     break;
  85.                 } else {
  86.                     LoadPage();
  87.                     break;
  88.                 }
  89.            
  90.             case 'news':
  91.                 LoadPage('id=' + encodeURIComponent( MyHash()[1] ));
  92.                 break;
  93.                
  94.             case 'user':
  95.                 LoadPage('user=' + encodeURIComponent( MyHash()[1] ));
  96.                 break;
  97.                
  98.             case 'edit':
  99.                 LoadPage('action=edit&id=' + encodeURIComponent( MyHash()[1] ));
  100.                 break;
  101.                
  102.             case 'forgot':
  103.                 if (MyHash()[2] !== undefined && MyHash()[3] !== undefined) {
  104.                     LoadPage('change=1&h1=' + MyHash()[1] + '&h2=' + MyHash()[2] + '&email=' + MyHash()[3]);
  105.                     break;
  106.                 } else {
  107.                     LoadPage();
  108.                     break;
  109.                 }
  110.            
  111.             case 'contato':
  112.                 LoadPage('query=' + encodeURIComponent( MyHash()[1] ));
  113.                 break;
  114.            
  115.             case 'welcome':
  116.                 LoadPage('p=' + encodeURIComponent( MyHash()[1] ));
  117.                 break;
  118.            
  119.             default:
  120.                 LoadPage();
  121.                 break;
  122.         }
  123.                
  124.     } else {
  125.         LoadPage();
  126.     }
  127.    
  128. }
  129.  
  130. function OnResize() {
  131.     var elem = [$('#content'), $('#right'), $('#news-bottom'), $('.news-bottom')];
  132.     for (i=0; i<elem.length; i++) {
  133.         if (document.width <= 1135) {
  134.             elem[i].addClass('small');
  135.         } else {
  136.             elem[i].removeClass('small');
  137.         }
  138.     }
  139. }
  140.  
  141. $(document).ready(function () {
  142.     if (location.hash == null || location.hash == '') {
  143.         location.hash = '!/home/';
  144.     } else {
  145.         OnHashChange();
  146.     }
  147. })
  148.  
  149. window.onhashchange = OnHashChange;
  150. window.onresize = OnResize;
  151.  
  152. // Debug Messi
  153. function messidebug(messisleft) {
  154.     var elements = $('[messi=true]');
  155.     var arr = jQuery.makeArray(elements);
  156.     $(arr).each(function (index, value) {
  157.         for (i = messisleft; i < arr.length; ++i) {
  158.             $(arr[i]).remove();
  159.         }
  160.     })
  161. }
  162.  
  163. // Functions
  164. function LoadingSuspensionPts() {
  165.     var sp = $('.loading-sp-pts');
  166.     switch (sp.html()) {
  167.         case '.':
  168.             sp.html('..');
  169.             break;
  170.         case '..':
  171.             sp.html('...');
  172.             break;
  173.         case '...':
  174.             sp.html('');
  175.             break;
  176.         default:
  177.             sp.html('.');
  178.             break;
  179.     }
  180. }
  181.  
  182. function NotificationDiv(type) {
  183.  
  184.     var not = $('#notification-div');
  185.    
  186.     if (type == 'open') {
  187.         if (not.attr('class') == '') {
  188.             window.setTimeout(function() {not.attr('class', 'open');}, 50);
  189.         }
  190.     } else
  191.     if (type == 'close') {
  192.             not.attr('class', '');
  193.             $.ajax({
  194.                 url: "notifications.php",
  195.                 type: 'POST',
  196.                 data: 'clear=1'
  197.             });
  198.     }
  199.    
  200. }
  201.  
  202. function GSDiv() {
  203.     $('#global-search-div').toggle();
  204.     $('#global-search-div input').focus();
  205. }
  206.  
  207. function GlobalSearch() {
  208.        
  209.     _gs_val = $.trim($('#global-search').val());
  210.     _gs_content = $('#global-search-content');
  211.     _pg_content = $('#page-content');
  212.    
  213.     function __GS__SHOW() {
  214.         _gs_content.load('globalsearch.php?term=' + encodeURIComponent(_gs_val), function() {
  215.             _pg_content.fadeOut(500, function() {
  216.                 _gs_content.fadeIn(500, function() {
  217.                     if ($(_pg_content).is(':visible')) {
  218.                         _pg_content.fadeOut(500, function() {
  219.                             if (_gs_val == '') {
  220.                                 __GS__HIDE();
  221.                             }
  222.                         });
  223.                     }
  224.                 });
  225.             });
  226.         });
  227.     }
  228.    
  229.     function __GS__HIDE() {
  230.         _gs_content.fadeOut(500, function() {
  231.             _pg_content.fadeIn();
  232.         });
  233.     }
  234.    
  235.     if (_gs_val !== '') {
  236.         __GS__SHOW();
  237.     } else if (_gs_val == '') {
  238.         __GS__HIDE();
  239.     }
  240.    
  241. }
  242.  
  243. function HideSearch() {
  244.     $('#global-search-content').fadeOut(500, function() {
  245.         $('#global-search').val('');
  246.         $('#page-content').fadeIn();
  247.     });
  248. }
  249.  
  250. $(document).ready(function() {
  251.    
  252.     $(document).click(function() {
  253.         if ($('#notification-div').attr('class') == '' || $('#notification-div').attr('class') == undefined) {
  254.             //return false;
  255.         } else {
  256.             NotificationDiv('close');
  257.         }
  258.     });
  259.    
  260.     $('a').click(function() {
  261.         if ($(this).attr('target') !== "_blank") {
  262.             window.location.href = $(this).attr('href');
  263.         }
  264.     })
  265.    
  266. })
  267.  
  268. function preview(textarea, div) {
  269.     var content = $(textarea).val();
  270.     $(div).load('lib/preview_msg.php?preview=' + encodeURIComponent(content));
  271. }
  272.  
  273. function go2home(nick) {
  274.     if (nick.length > 0) {
  275.         location.hash = '!/user/' + nick + '/';
  276.     } else {
  277.         return false;
  278.     }
  279. }
  280.  
  281. function phpTime() {
  282.     var mydate = new Date().getTime() + '';
  283.     return mydate.replace(/[0-9]{3}$/, '');
  284. }
  285.  
  286. function notifications() {
  287.     $.ajax({
  288.         url: "notifications.php",
  289.         type: 'POST',
  290.         data: 'n=1',
  291.         success: function (lol) {
  292.             if (lol > 0) {
  293.                 TitleNotifyPrepend(lol);
  294.                 $('.nb-counter').html('(' + $.trim(lol) + ')');
  295.             } else {
  296.                 titulo();
  297.                 $('.nb-counter').html('');
  298.             }
  299.         },
  300.         complete: function() {
  301.             setTimeout(notifications, 10000);
  302.         }
  303.     });
  304.     $('#notification-div').load('notifications.php');
  305. }
  306.  
  307. // Tab na <textarea>
  308. function TextAreaTab(id) {
  309.     $(document).delegate('textarea#' + id, 'keydown', function (e) {
  310.         var keyCode = e.keyCode || e.which;
  311.  
  312.         if (keyCode == 9) {
  313.             e.preventDefault();
  314.             var start = $(this).get(0).selectionStart;
  315.             var end = $(this).get(0).selectionEnd;
  316.  
  317.             // set textarea value to: text before caret + tab + text after caret
  318.             $(this).val($(this).val().substring(0, start) + "\t" + $(this).val().substring(end));
  319.  
  320.             // put caret at right position again
  321.             $(this).get(0).selectionStart = $(this).get(0).selectionEnd = start + 1;
  322.         }
  323.     });
  324. }
  325. TextAreaTab('conteudo');
  326.  
  327. $.get("js/BBCodeFunc.js");
  328.  
  329. //Ctrl Shortcuts
  330. function shortcut(key, link) {
  331.     Mousetrap.bind(['command+'+key+'', 'ctrl+'+key+''], function(e) {
  332.         location.href = '#!/' + link + '/';
  333.         return false;
  334.     });
  335. }
  336. shortcut(1, 'home');
  337. shortcut(2, 'news');
  338. shortcut(3, 'account');
  339. shortcut(4, 'mensagens');
  340. shortcut(5, 'mensagens/list');
  341. shortcut(6, 'user');
  342. shortcut(7, 'contato');
  343. shortcut(8, 'loopsounds');
  344. shortcut(9, 'termosdeuso');
  345. shortcut(0, 'welcome');
  346. Mousetrap.bind(['command+r', 'ctrl+r'], function(e) {
  347.     OnHashChange();
  348.     return false;
  349. });
  350.  
  351. // [Votos]
  352. function StarHover(n, txt, color) {
  353.     var d = $('.voting-dialog');
  354.     var img_p = 'images/voting/star_gold_';
  355.     for (i=1;i<=n;i++) {
  356.         $('.v-star.' + i).attr('src', img_p + 'full_hover.png');
  357.     }
  358.     for (i=5; i>n; i--) {
  359.         $('.v-star.' + i).attr('hovered', 'false');
  360.         $('.v-star.' + i).attr('src', img_p + $('.v-star.' + i).attr('star') + '.png');
  361.     }
  362.     $('.voting-dialog').attr('style', 'color:' + color).html(txt);
  363. }
  364.  
  365. function LoadRating(element, type, id) {
  366.     $.ajax({
  367.         url: 'lib/voting.php',
  368.         type: "POST",
  369.         data: "view=1&type=" + type + "&id=" + id,
  370.         success: function(r) {
  371.             $(element).html(r).fadeIn();
  372.         }
  373.     });
  374. }
  375.  
  376. function SendVote(element, type, id, value, session) {
  377.     $(element).fadeOut();
  378.     $.ajax({
  379.         url: 'lib/voting.php',
  380.         type: "POST",
  381.         data: "vote=1&type=" + type + "&id=" + id + "&value=" + value + "&cookie=" + session,
  382.         success: function(r) {
  383.             LoadRating(element, type, id);
  384.         }
  385.     });
  386. }
  387. // [/Votos]
  388.  
  389. //ShoutBox
  390. function mountmsg(nick, color, msg, time, date, id) {
  391.     if ($.trim(msg) !== '') {
  392.         $('#shoutbox-msgs').append('<div class="msg-'+id+'" style="width:350px;padding:1px;"><span style="float:left;margin-right:2px;"><a href="#!/user/'+nick+'/" style="color:'+color+';"><b>'+nick+':</b></a></span><span>'+msg+'</span><span title="'+time+' '+date+'" style="color:#666;text-align:right;display:block;margin-bottom:1px;">['+time+']</span>');
  393.        
  394.     }
  395. }
  396. function ShoutBox(load) {
  397.     var lastmsg = $.cookie("msgid");
  398.     if (load == true) {
  399.         lastmsg = 'false';
  400.     }
  401.     $.ajax({
  402.         url: "lib/shoutbox.php",
  403.         type: "POST",
  404.         data: 'getlastmsg=' + lastmsg,
  405.         success: function(lm) {
  406.             if (parseInt(lastmsg) !== parseInt(lm)) {
  407.                 $.ajax({
  408.                     url: 'lib/shoutbox.php',
  409.                     type: 'POST',
  410.                     data: 'json=' + lastmsg,
  411.                     dataType: 'json',
  412.                     success: function(json) {
  413.                         for (o = 0; o <= Object.size(json) - 1; o++) {
  414.                         /*if (load !== true) {
  415.                             alert('lastmsg(' + (parseInt(lastmsg) + 1) + ') lm(' + lm + ')' );
  416.                             console.log(json);
  417.                         }*/
  418.                             if (!$('.msg-' + json[o].id).length) {
  419.                                 if ($('.msg-ex').length) {
  420.                                     $('.msg-ex').slideUp(250, function() {
  421.                                         $('.msg-ex').remove();
  422.                                         mountmsg(json[o].nick, json[o].color, json[o].msg, json[o].time, json[o].date, json[o].id);
  423.                                     });
  424.                                 } else {
  425.                                     mountmsg(json[o].nick, json[o].color, json[o].msg, json[o].time, json[o].date, json[o].id);
  426.                                 }
  427.                             }
  428.                            
  429.                             setTimeout('ShoutBoxScroll(1000)', 100);
  430.                            
  431.                         }
  432.                         if (load == true) {
  433.                             ShoutBoxScroll(500);
  434.                         }
  435.                     }
  436.                 });
  437.                 $.cookie("msgid", lm - 1);
  438.             }
  439.             window.setTimeout(ShoutBox, 2500);
  440.         },
  441.         error: function(jqXHR, textStatus, errorThrown){
  442.             //alert("Ocorreu um erro.\nCertifique-se de que você está conectado à  internet.");
  443.         }
  444.     });
  445. }
  446.  
  447. function ShoutBoxScroll(time) {
  448.     $('#shoutbox-msgs').stop().animate({
  449.         scrollTop: $("#shoutbox-msgs")[0].scrollHeight
  450.     }, time);
  451. }
  452. function ShoutBoxEnviar() {
  453.    
  454.     var sessao = $.cookie("sessionid");
  455.     var msg = encodeURIComponent($('#shoutbox-msg').val());
  456.  
  457.     $.ajax({
  458.         url: "lib/shoutbox.php",
  459.         type: 'POST',
  460.         data: 'cookie=' + sessao + '&msg=' + msg,
  461.         success: function() {
  462.             ShoutBox();
  463.         }
  464.     });
  465.     $('#shoutbox-msg').val('');
  466. }
  467.  
  468. function OnlineUsers(n) {
  469.     var o = n;
  470.     var msg = '';
  471.     $.ajax({
  472.         url: "lib/onlineusers.php",
  473.         type: 'POST',
  474.         data: 'o=' + o,
  475.         success: function (ou) {
  476.            
  477.             switch(ou) {
  478.                 case '0':
  479.                     msg += 'Nenhum usuário';
  480.                     break;
  481.                 case '1':
  482.                     msg += 'Um usuário';
  483.                     break;
  484.                 default:
  485.                     msg += ou + ' usuários';
  486.                     break;
  487.             }
  488.            
  489.             msg += ' online';
  490.            
  491.             $('#online-users').html('<a class="a" onclick="WhoIsOnline()" href="javascript:void(\'Online Users\')" style="text-decoration:none !important;">' + msg + '</a>');
  492.            
  493.             o = ou;
  494.         },
  495.         complete: function() {
  496.             setTimeout(OnlineUsers, 10000);
  497.         }
  498.     });
  499. }
  500.  
  501. function WhoIsOnline() {
  502.     Messi.load('lib/onlineusers.php?v=list', {
  503.         buttons: [{
  504.             id: 0,
  505.             label: 'Fechar',
  506.             val: ''
  507.         }],
  508.         modal: true,
  509.         width: '300px'
  510.     });
  511. }
  512.  
  513. function sbDragging(toggle) {
  514.     d = $('#bottom');
  515.     if (toggle == 'disable') {
  516.         d.draggable("destroy")
  517.     } else {
  518.         d.draggable({
  519.             handle: '#d-sb-a',
  520.             containment: 'document',
  521.             start: function(event, ui) {
  522.                 $('#sb-b-reload').show();
  523.             }
  524.         });
  525.     }
  526. }
  527. function sbReplace() {
  528.     $('#bottom').attr('style', '');
  529.     $('#sb-b-reload').hide();
  530. }
  531. function sbDragToggle() {
  532.     var sb = $('#sb-b-drag');
  533.     switch (sb.attr('dragging')) {
  534.         case "true":
  535.             sb.attr('dragging', 'false').attr('class', 'ui-icon ui-icon-locked');
  536.             sbDragging('disable');
  537.             break;
  538.         case "false":
  539.             sb.attr('dragging', 'true').attr('class', 'ui-icon ui-icon-unlocked');
  540.             sbDragging('enable');
  541.             break;
  542.     }
  543. }
  544.  
  545. String.prototype.capitalize = function() {
  546.     return this.charAt(0).toUpperCase() + this.slice(1);
  547. }
  548. Object.size = function(obj) {
  549.     var size = 0, key;
  550.     for (key in obj) {
  551.         if (obj.hasOwnProperty(key)) size++;
  552.     }
  553.     return size;
  554. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement