OSDN Git Service

embrj
[embrj/master.git] / js / public.js
1 var UPDATE_INTERVAL;
2 var PAUSE_UPDATE = false;
3 var PAUSE_TIMELINE = false;
4 //form function
5 function updateSentTip(message,duration,className){
6         var sentTip = $("#sentTip");
7         var bgColor = $("body").css("background-color");
8         sentTip.html(message).removeClass().addClass(className)
9                 .css({
10                         "border-style": "solid",
11                         "border-width": "1px",
12                         "border-color": "transparent"
13                 }).slideDown("fast");
14         setTimeout(function (){
15                         sentTip.slideUp('fast');
16                 },duration);
17         return sentTip;
18 };
19 function leaveWord(num){
20         if(!num){
21                 num = 140;
22         }
23         var leave = num-$("#textbox").val().length;
24         var sent_id = $("#sent_id").val();
25         var $tb = $("#tip b");
26         if (sent_id){
27                 leave -= sent_id.length+3;
28         }
29         if (leave < 0){
30                 $tb.text(leave).css("color","#C00");
31                 $("#tweeting_button").addClass('btn-disabled');
32         }else{
33                 $tb.text(leave);
34                 $("#tweeting_button").removeClass('btn-disabled');
35                 if (leave > 40){
36                         $tb.css("color","#CCC");
37                 }else if(leave > 20){
38                         $tb.css("color","#CAA");
39                 }else if(leave > 10){
40                         $tb.css("color","#C88");
41                 }else{
42                         $tb.css("color","#C44");
43                 }
44         }
45         if(leave === 140){
46                 $("#in_reply_to").val("");
47                 $("#tweeting_button").addClass('btn-disabled');
48         }
49 }
50 var formHTML = '<span id="tip"><b>140</b></span><form action="index.php" method="post"><textarea name="status" id="textbox"></textarea><input type="hidden" id="in_reply_to" name="in_reply_to" value="0" /><div id="tweeting_controls"><a class="a-btn a-btn-m btn-disabled" id="tweeting_button" tabindex="2" href="#" title="Ctrl/⌘+Enter also works!"><span>Send</span></a></div></form>';
51
52 var embrTweet=function(objs){
53         if(typeof objs === 'undefined'){
54                 var objs = $('#statuses .timeline .source a');
55         }else{
56                 var objs = objs.find('.source a');
57         }
58         objs.each(function (){
59                 var $this = $(this);
60                 if (/embr/i.test($this.text())) $this.addClass('embr');
61         });
62         if($("span.date a").length > 0){
63                 $("span.date a,#latest_meta a,#full_meta a").timeago();
64         }else{
65                 $("span.date,#latest_meta a,#full_meta a").timeago();
66         }
67 }
68 var formFunc = function(){
69         leaveWord();
70         $("#textbox").keyup(function (e){
71                         leaveWord();
72                         $(e.target).unbind('keydown');
73                         if ((e.ctrlKey || e.metaKey) && e.which == 13){
74                                 if (PAUSE_UPDATE !== true){
75                                         updateStatus();
76                                 }else{
77                                         return 0;
78                                 }
79                         }else{
80                                 if($.inArray(e.which,[91,93,224,17]) > -1){
81                                         $(e.target).keydown(function(e){
82                                                 if(e.which == 13){
83                                                         if (PAUSE_UPDATE !== true){
84                                                                 updateStatus();
85                                                         }else{
86                                                                 return 0;
87                                                         }
88                                                         e.stopPropagation();
89                                                 }
90                                         });
91                                 }
92                         }
93                 });
94         $("#tweeting_button").click(function (e){
95                 e.preventDefault();
96                 if ($("#textbox").val().length >0 ){
97                         updateStatus();
98                 }
99         });             
100 };
101         var updateStatus = function(){
102         PAUSE_UPDATE = true;
103         var text = $("#textbox").val();
104         var sent_id = $("#sent_id").val();
105         if(sent_id){
106                 text = "D "+sent_id+' '+text; 
107         }
108         var wordsCount = text.length;
109         if (wordsCount > 140){
110                 $.cookie('recover',text,{'expire': 30});
111                 if(window.confirm("Your tweet is longer than 140 words! truncated? (you can restore later using restore button.)")){
112                         text = text.substr(0,137)+'...' ;
113                 }
114         }
115         if (wordsCount == 0 || $.trim(text).length == 0){
116                 updateSentTip("Your cannot send an empty tweet!",3e3,"failure");
117                 return false;
118         }else{
119                 $('#tip').addClass('loading').find('b').css('color','transparent');
120                 $.cookie('recover',text,{'expire': 30});
121                 $.ajax({
122                         url: "ajax/update.php",
123                         type: "POST",
124                         data:{
125                                 "status": text,
126                                 "in_reply_to": $("#in_reply_to").val()
127                         },
128                         success: function (msg){
129                                 if ($.trim(msg).indexOf("</li>") > 0){
130                                         $('#tip').removeClass('loading').find("b").text("140").show();
131                                         if ( (text.substring(0,2)).toUpperCase() == "D "){ //exclude the DMs. the exam of user_name is omitted.
132                                                 updateSentTip("Your DM has been sent!",3e3,"success");
133                                                 $("#sent_id,#textbox").val("");
134                                                 leaveWord();
135                                         }else{
136                                                 updateSentTip("Your status has been updated!",3e3,"success");
137                                                 $("#textbox").val("");
138                                                 leaveWord();
139                                                 if(typeof INTERVAL_COOKIE !== 'undefined'){
140                                                         var source = $(msg).prependTo($("#allTimeline"));
141                                                         source.hide().slideDown('fast');
142                                                         var statusid = $.trim($(msg).find('.status_id').text());
143                                                         var statusText = $.trim($(msg).find('.tweet').html());
144                                                         var statusDate = $.trim($(msg).find('span.date a').attr('id'));
145                                                         embrTweet(source);
146                                                         $(".mine").slideDown("fast");
147                                                         $("#full_status").fadeIn("fast");
148                                                         $("#currently .status-text").hide().text(limitation(text)).fadeIn("fast");
149                                                         $("#latest_meta").hide().html("<a target=\"_blank\" href=\"status.php?id="+statusid+"\" id=\""+statusDate+"\">less than 5 seconds ago</a>").fadeIn("fast");
150                                                         $("#currently .full-text").hide().html(statusText);
151                                                         $("#full_meta").hide().html("<a target=\"_blank\" href=\"status.php?id="+statusid+"\" id=\""+statusDate+"\">less than 5 seconds ago</a>");
152                                                         $("#full_meta a,.full-text a").click(function (e){e.stopPropagation();});
153                                                         previewMedia(source);
154                                                         freshProfile();
155                                                 }
156                                         }
157                                 }else{
158                                         $('#tip').removeClass('loading');
159                                         leaveWord();
160                                         updateSentTip("Update failed. Please try again.",3e3,"failure");
161                                         $('#tweeting_button').removeClass('btn-disabled');
162                                 }
163                                 PAUSE_UPDATE = false;
164                         },
165                         error: function (msg){
166                                 $('#tip').removeClass('loading');
167                                 leaveWord();
168                                 updateSentTip("Update failed. Please try again.",3e3,"failure");
169                                 $('#tweeting_button').removeClass('btn-disabled');
170                                 PAUSE_UPDATE = false;
171                         }
172                 });
173         }
174 };
175 function shortUrlDisplay(){
176         var stringVar = $("#textbox").val();
177         if (stringVar.length === 0){
178                 updateSentTip("There's no URL in your tweet to shorten!",3e3,"failure");
179         }else{
180                 var str = '';
181                 var regexp = /http(s)?:\/\/([\w\-]+\.)+[\w\-]+(\/[\w\-\.\/?\%\!\&=\+\~\:\#\;\,]*)?/ig;
182                 var l_urls = '';
183                 str = stringVar.match(regexp);
184                 if (str !== null){
185                         unshorten = 0;
186                         for (idx = 0; idx < str.length; idx++){
187                                 regexp2 = /(http:\/\/j.mp\/[\S]+)|(http:\/\/bit.ly\/[\S]+)|(http:\/\/goo.gl\/[\S]+)|(http:\/\/t.co\/[\S]+)/gi;
188                                 if (!str[idx].match(regexp2)){
189                                         l_urls += str[idx]+"|";
190                                 }else{
191                                         unshorten++;
192                                 }
193                         }
194                         if (unshorten){
195                                 updateSentTip(unshorten+" URL(s) are maintained!",3e3,"failure");
196                         }
197                         if (l_urls != ""){
198                                 $('#tip').addClass('loading').find('b').css('color','transparent');
199                                 $.post("ajax/shorturl.php",{
200                                         long_urls: l_urls
201                                         },function (data){
202                                         getShortUrl(data);
203                                 });
204                         }
205                 }
206         }
207 }
208 function getShortUrl(res){
209         var $textbox = $('#textbox');
210         var url_arry,s_url,l_url,part;
211         var err_cnt = 0;
212         url_arry = res.split('^');
213         for (i = 0; i < url_arry.length; i++){
214                 part = url_arry[i].split('|');
215                 if (part.length == 2){
216                         s_url = part[0];
217                         l_url = part[1];
218                 }
219                 if (s_url){
220                         $textbox.val($textbox.val().replace(l_url,s_url)+"");
221                         leaveWord();
222                         $('#tip').removeClass('loading');
223                         updateSentTip("Successfully shortened your URLs!",3e3,"success");
224                 }       else{
225                         err_cnt++;
226                 }
227         }
228         if (err_cnt > 0){
229                 updateSentTip("Failed to shorten URLs,please try again.",3e3,"failure");
230         }
231 }
232
233 function shortenTweet(){
234         var tweet = $.trim($("#textbox").val());
235         if (tweet.length === 0){
236                 updateSentTip("There's nothing to shorten!",3e3,"failure");
237         }else{
238                 $('#tip').addClass('loading');
239                 $.ajax({
240                         url: "ajax/shortenTweet.php",
241                         type: "POST",
242                         data: "text="+tweet,
243                         success: function(msg){
244                                 if(msg !== 'error'){
245                                         $("#textbox").val(msg);
246                                         leaveWord();
247                                         $('#tip').removeClass('loading');
248                                         updateSentTip("Your tweet has been shortened!",5e3,"success");
249                                 }else{
250                                         updateSentTip("Failed to shorten your tweet.",5e3,"failure");
251                                 }
252                         },
253                         error: function(msg){
254                                 updateSentTip("Failed to shorten your tweet.",5e3,"failure");
255                         }
256                 });
257         }
258 }
259 $(function (){
260         $("#latest_status").toggle(
261                 function (){
262                         $("#currently .status-text,#latest_meta").css("display","none");
263                         $("#currently .full-text,#full_meta").css("display","inline");
264                 },function (){
265                         $("#currently .status-text,#latest_meta").css("display","inline");
266                         $("#currently .full-text,#full_meta").css("display","none");
267                 });
268         $("#full_meta a,.full-text a").click(function (e){
269                 e.stopPropagation();
270         });
271         var $temp = $("#currently .status-text");
272         $temp.text(limitation($temp.text()));
273         $("#translateMy").live("click",function(){
274                 var orig = $("#textbox").val();
275                         ORIG_TEXT = orig;
276                 var lang = $.cookie('myLangs')
277                 if(lang === null){
278                         lang = 'en';
279                 }
280                 $('#tip').addClass('loading');
281                 translate(orig,'',lang,'transMyCallback');
282         });
283 });
284 var limitation = function (text){
285         if (text.length > 60){
286                 text = text.substr(0,60)+" ...";
287         }
288         return text;
289 };
290 function ajax_reply($this){
291         var $that = $this.parent().parent().parent().parent();
292         var thread = $that.find(".ajax_form");
293         if (thread.length > 0){
294                 thread.slideToggle("fast");
295         }else{
296                 $that.addClass("loading");
297                 $.ajax({
298                         url: $this.attr("href"),
299                         type: "GET",
300                         dataType: "text",
301                         success: function(msg){
302                                 $that.removeClass("loading");
303                                 if ($.trim(msg).indexOf("</li>") > 0){
304                                         var source = $(msg).appendTo($that);
305                                         embrTweet(source);
306                                 }else{
307                                         updateSentTip('Get thread failed.',5e3,'failure');
308                                 }
309                         },
310                         error: function(msg){
311                                 updateSentTip('Get thread failed.',5e3,'failure');
312                                 $that.removeClass("loading");
313                         }
314                 });
315         }
316 }
317 //tweet function
318
319 function rminit($this){
320         var $that = $this.parent().parent().parent();
321         var $rm = $that.find(".right_menu");
322         if($rm.length > 0){
323                 $rm.fadeIn('fast');
324         }else{
325                 var id = $that.find(".status_word").find(".user_name").attr("id");
326                 $that.addClass("loading");
327                 $.ajax({
328                         url: 'ajax/relation.php',
329                         type: "POST",
330                         data: "action=show&id="+id,
331                         success: function(msg){
332                                 var html = '<ul class="right_menu round"><li><a class="rm_mention" href="#"><i></i>Mention</a></li>';
333                                 var r = parseInt(msg);
334                                 switch(r){
335                                         case 1:
336                                         html += '<li><a class="rm_dm" href="#"><i></i>Message</a></li>';
337                                         case 2:
338                                         html += '<li><a class="rm_unfollow" href="#"><i></i>Unfollow</a></li><li><a class="ul_block" href="#"><i></i>Block</a></li>';
339                                         break;
340                                         case 3:
341                                         html += '<li><a class="rm_dm" href="#"><i></i>Message</a></li>';
342                                         case 9:
343                                         html += '<li><a class="rm_follow" href="#"><i></i>Follow</a></li><li><a class="rm_block" href="#"><i></i>Block</a></li>';
344                                         break;
345                                         case 4:
346                                         html += '<li><a class="rm_follow" href="#"><i></i>Follow</a></li><li><a class="rm_unblock" href="#"><i></i>UnBlock</a></li>';
347                                         break;
348                                 }
349                                 html += '<li><a class="rm_spam" href="#"><i></i>Report Spam</a></li><li><a href="user.php?id='+id+'">View Full Profile</a></ul>';
350                                 $this.parent().parent().after(html);
351                                 $(html).fadeIn('fast');
352                                 $that.removeClass();
353                         },
354                         error: function(){
355                                 updateSentTip('Loading Avatar Menu Failed,Please Retry!',3e3,"failure");
356                                 $that.removeClass();
357                         }
358                 });
359         }
360 }
361
362 function rmmention($this,e){
363         var replie_id = $this.parent().parent().parent().find(".status_word").find(".user_name").attr("id");
364         var in_reply_id = $this.parent().parent().parent().find(".status_id").text();
365         var text = "@"+replie_id;
366         var mode = "In reply to ";
367         if ($("#textbox").length <= 0) {
368                 $("#info_head").after('<h2>In reply to ' + replie_id + '</h2>' + formHTML);
369                 formFunc();
370         }
371         scroll(0,0);
372         $("#textbox").focus().val($("#textbox").val()+text+' ');
373         $("#in_reply_to").val(in_reply_id);
374         $("#full_status,#latest_meta,#full_meta,#currently .full-text").hide();
375         $("#currently .status-text").html(mode+text);
376         leaveWord();
377 }
378 function rmdm($this,e){
379         var replie_id = $this.parent().parent().parent().find(".status_word").find(".user_name").attr("id");
380         var text = "D "+replie_id;
381         if ($("#textbox").length <= 0) {
382                 $("#info_head").after('<h2>Send message to ' + replie_id + '</h2>' + formHTML);
383                 formFunc();
384         }
385         scroll(0,0);
386         $("#textbox").focus().val($("#textbox").val()+text+' ');;
387         $("#in_reply_to").val(e.target.parent().parent().parent().find(".status_id").text());
388         $("#full_status,#latest_meta,#full_meta,#currently .full-text").hide();
389         $("#currently .status-text").html("Reply direct message to @"+replie_id);
390         leaveWord();
391 }
392 function rmfollow($this){
393         var id = $this.parent().parent().parent().find(".status_word").find(".user_name").attr("id");
394         updateSentTip("Following "+id+"...",5e3,"ing");
395         $.ajax({
396                 url: "ajax/relation.php",
397                 type: "POST",
398                 data: "action=create&id="+id,
399                 success: function (msg){
400                         if (msg.indexOf("success") >= 0){
401                                 updateSentTip("You have followed "+id+"!",3e3,"success");
402                         }else{
403                                 updateSentTip("Failed to follow "+id+",please try again.",3e3,"failure");
404                         }
405                 },
406                 error: function (msg){
407                         updateSentTip("Failed to follow "+id+",please try again.",3e3,"failure");
408                 }
409         });
410 }
411 function rmunfollow($this){
412         var id = $this.parent().parent().parent().find(".status_word").find(".user_name").attr("id");
413         if (confirm("Are you sure to unfollow "+id+" ?")){
414                 updateSentTip("Unfollowing "+id+"...",5e3,"ing");
415                 $.ajax({
416                         url: "ajax/relation.php",
417                         type: "POST",
418                         data: "action=destory&id="+id,
419                         success: function (msg){
420                                 if (msg.indexOf("success") >= 0){
421                                         updateSentTip("You have unfollowed "+id+"!",3e3,"success");
422                                 }else{
423                                         updateSentTip("Failed to unfollow "+id+",please try again.",3e3,"failure");
424                                 }
425                         },
426                         error: function (msg){
427                                 updateSentTip("Failed to unfollow "+id+",please try again.",3e3,"failure");
428                         }
429                 });
430         }
431 }
432 function rmblock($this){
433         var id = $this.parent().parent().parent().find(".status_word").find(".user_name").attr("id");
434         if (confirm("Are you sure to block "+id+" ?")){
435                 updateSentTip("Blocking "+id+"...",5e3,"ing");
436                 $.ajax({
437                         url: "ajax/relation.php",
438                         type: "POST",
439                         data: "action=block&id="+id,
440                         success: function (msg){
441                                 if (msg.indexOf("success") >= 0){
442                                         updateSentTip("You have blocked "+id+"!",3e3,"success");
443                                 }else{
444                                         updateSentTip("Failed to block "+id+",please try again.",3e3,"failure");
445                                 }
446                         },
447                         error: function (msg){
448                                 updateSentTip("Failed to block "+id+",please try again.",3e3,"failure");
449                         }
450                 });
451         }
452 }
453 function rmunblock($this){
454         var id = $this.parent().parent().parent().find(".status_word").find(".user_name").attr("id");
455         if (confirm("Are you sure to unblock "+id+" ?")){
456                 updateSentTip("Unblocking "+id+"...",5e3,"ing");
457                 $.ajax({
458                         url: "ajax/relation.php",
459                         type: "POST",
460                         data: "action=unblock&id="+id,
461                         success: function (msg){
462                                 if (msg.indexOf("success") >= 0){
463                                         updateSentTip("You have unblocked "+id+"!",3e3,"success");
464                                 }else{
465                                         updateSentTip("Failed to unblock "+id+",please try again.",3e3,"failure");
466                                 }
467                         },
468                         error: function (msg){
469                                 updateSentTip("Failed to unblock "+id+",please try again.",3e3,"failure");
470                         }
471                 });
472         }
473 }
474 function rmspam($this){
475         var id = $this.parent().parent().parent().find(".status_word").find(".user_name").attr("id");
476         if (confirm("Are you sure to report "+id+" ?")){
477                 updateSentTip("Reporting "+id+" as a spammer...",5e3,"ing");
478                 $.ajax({
479                         url: "ajax/reportSpam.php",
480                         type: "POST",
481                         data: "spammer="+id,
482                         success: function (msg){
483                                 if (msg.indexOf("success") >= 0){
484                                         updateSentTip("Successfully reported!",3e3,"success");
485                                 }else{
486                                         updateSentTip("Failed to report "+id+",please try again.",3e3,"failure");
487                                 }
488                         },
489                         error: function (msg){
490                                 updateSentTip("Failed to report "+id+",please try again.",3e3,"failure");
491                         }
492                 });
493         }
494 }
495 //tweet actions
496 function onFavor($this){
497         var status_id = $.trim($this.parent().parent().find(".status_id").text());
498         updateSentTip("Adding this tweet to your favorites...",5e3,"ing");
499         $.ajax({
500                 url: "ajax/addfavor.php",
501                 type: "POST",
502                 data: "status_id="+status_id,
503                 success: function (msg){
504                         if (msg.indexOf("success") >= 0){
505                                 updateSentTip("Favorite added successfully.",3e3,"success");
506                                 $this.parent().parent().parent().append('<i class="faved"></i>');
507                                 $this.removeClass().addClass("unfav_btn").attr("title","UnFav").text("UnFav");
508                         }else{
509                                 updateSentTip("Add failed. Please try again.",3e3,"failure");
510                         }
511                 },
512                 error: function (msg){
513                         updateSentTip("Add failed. Please try again.",3e3,"failure");
514                 }
515         });
516 }
517 function onReplie($this,e){
518         var $word = $this.parent().parent().find(".status_word");
519         var replie_id = $word.find(".user_name").attr("id");
520         var in_reply_id = $this.parent().parent().find(".status_id").text();
521         var text = "@"+replie_id;
522         var start = text.length+1;
523         var mode = "In reply to ";
524         if (!e.ctrlKey && !e.metaKey){
525                 var temp=[];
526                 temp[text] = true;
527                 var self = '@'+$("#side_name").text();
528                 temp[self] = true;
529                 var mentionArray = [text];
530                 var mentions = $word.find('.tweet').find('a[href^="user.php"]');
531                 $.each(mentions,function (){
532                         var t = this.text;
533                         if (!(t in temp)){
534                                 temp[t] = true;
535                                 mentionArray.push(t);
536                         }
537                         text = mentionArray.join(' ');
538                 });
539                 if (mentionArray.length > 1){
540                         mode = "Reply to all: ";
541                 }
542         }
543         if (e.altKey){
544                 mode = "Non-conversational reply to ";
545                 in_reply_id = "";
546         }
547         scroll(0,0);
548         var end = text.length;
549         $("#textbox").focus().val($("#textbox").val()+text+' ').caret(start,end);
550         $("#in_reply_to").val(in_reply_id);
551         $("#full_status,#latest_meta,#full_meta,#currently .full-text,#latest_meta").hide();
552         $("#currently .status-text").html(mode+text);
553         leaveWord();
554 }
555 function onRT($this){
556         var replie_id = $this.parent().parent().find(".status_word").find(".user_name").attr("id");
557         scroll(0,0);
558         var status_word = $this.parent().parent().find(".status_word").clone();
559         status_word.find('.tweet a[rel=noreferrer]').each(function(){
560                 var imgsrc = $(this).attr('href');
561                 if (imgsrc.indexOf('img.php') > -1) {
562                         imgsrc = imgsrc.substr(15);
563                 }
564                 $(this).text(imgsrc);
565         });
566         $("#textbox").focus().val(" RT @"+replie_id+":"+status_word.find('.tweet').text()).caret(0);
567         $("#full_status,#latest_meta,#full_meta,#currently .full-text,#latest_meta").hide();
568         $("#currently .status-text").html("Retweet @"+replie_id+"'s tweet with comment.");
569         leaveWord();
570 }
571 function onReplieDM($this){
572         var replie_id = $this.parent().parent().find(".status_word").find(".user_name").attr("id");
573         var text = "D "+replie_id;
574         scroll(0,0);
575         $("#textbox").focus().val($("#textbox").val()+text+' ');
576         $("#full_status,#latest_meta,#full_meta,#currently .full-text,#latest_meta").hide();
577         $("#currently .status-text").html("Reply direct message to @"+replie_id);
578         leaveWord();
579 }
580 function onNwRT($this){
581         if (confirm("Are you sure to retweet this?")){
582                 var statusBody = $this.parent().parent();
583                 var status_id = statusBody.find(".status_id").text();
584                 var div = "#"+statusBody.parent().parent().attr('id');
585                 var btnDiv = div+"Btn";
586                 updateSentTip("Retweeting tweet...",5e3,"ing");
587                 $.ajax({
588                         url: "ajax/retweet.php",
589                         type: "post",
590                         data: "status_id="+status_id,
591                         success: function (msg){
592                                 if (msg.length >= 0){
593                                         statusBody.parent().addClass("retweet");
594                                         statusBody.find(".source").hide();
595                                         statusBody.find(".status_info").append("<span class=\"rt_source\">Retweeted by you.").fadeIn("fast");
596                                         statusBody.find(".date").hide();
597                                         statusBody.find(".status_info").append("<span class=\"rt_undos\" title=\"Your followers will no longer see the tweet as retweeted by you.\">&nbsp;<a class=\"rt_undo\" href=\"#\">(Undo)</a><span class=\"rt_id\" style=\"display: none;\">"+msg+"</span></span>").fadeIn("fast");
598                                         updateSentTip("This tweet has been retweeted!",3e3,"success");
599                                         $(".rt_undos").tipsy({
600                                                         gravity: 's'
601                                                 });
602                                 }else{
603                                         if (msg === "duplicated"){
604                                                 updateSentTip("You have retweeted this tweet!",3e3,"failure");
605                                         }else{
606                                                 updateSentTip("Failed to retweet!",3e3,"failure");
607                                         }
608                                 }
609                         },
610                         error: function (msg){
611                                 updateSentTip("Retweet failed. Please try again.",3e3,"failure");
612                         }
613                 });
614         }
615 }
616 function UnFavor($this){
617         if (window.confirm("Are you sure to unfavor this tweet?")){
618                 var $that=$this.parent().parent();
619                 var status_id = $.trim($that.find(".status_id").text());
620                 $that.parent().css("background-color","#FF3300");
621                 updateSentTip("Unfavoring tweet...",5e3,"ing");
622                 $.ajax({
623                         url: "ajax/delete.php",
624                         type: "POST",
625                         data: "favor_id="+status_id,
626                         success: function (msg){
627                                 if (msg.indexOf("success") >= 0){
628                                         if (location.href.indexOf('favor.php')>0){
629                                                 $that.parent().fadeOut("fast");
630                                         }else{
631                                                 $that.parent().find(".faved").fadeOut("fast");
632                                                 $this.removeClass().addClass("favor_btn").attr("title","Fav").text("Fav");
633                                         }
634                                         updateSentTip("This tweet has been unfavored!",3e3,"success");
635                                 }else{
636                                         updateSentTip("Unfavor failed. Please try again.",3e3,"failure");
637                                 }
638                                 $that.parent().css("background-color","");
639                         },
640                         error: function (msg){
641                                 updateSentTip("Unfavor failed. Please try again.",3e3,"failure");
642                                 $that.parent().css("background-color","");
643                         }
644                 });
645         }
646 }
647 function onDelete($this){
648         if (window.confirm("Are you sure to delete this tweet?")){
649                 var $this=$this.parent().parent();
650                 var status_id = $.trim($this.find(".status_id").text());
651                 $this.parent().css("background-color","#FF3300");
652                 updateSentTip("Deleting tweet...",5e3,"ing");
653                 $.ajax({
654                         url: "ajax/delete.php",
655                         type: "POST",
656                         data: "status_id="+status_id,
657                         success: function (msg){
658                                 if (msg.indexOf("success") >= 0){
659                                         $this.parent().fadeOut("fast");
660                                         updateSentTip("Your tweet has been destroyed!",3e3,"success");
661                                 }else{
662                                         updateSentTip("Delete failed. Please try again.",3e3,"failure");
663                                 }
664                                 $this.parent().css("background-color","");
665                         },
666                         error: function (msg){
667                                 updateSentTip("Delete failed. Please try again.",3e3,"failure");
668                                 $this.parent().css("background-color","");
669                         }
670                 });
671         }
672 }
673 function onUndoRt($this){
674         if (window.confirm("Are you sure to undo this retweet?")){
675                 var status_id = $.trim($this.parent().find(".rt_id").text());
676                 var statusBody = $this.parent().parent().parent();
677                 statusBody.css("background-color","#FF3300");
678                 updateSentTip("Undoing retweet...",5e3,"ing");
679                 $.ajax({
680                         url: "ajax/delete.php",
681                         type: "POST",
682                         data: "status_id="+status_id,
683                         success: function (msg){
684                                 if (msg.indexOf("success") >= 0){
685                                         statusInfo = $this.parent().parent();
686                                         if (statusInfo.find(".rt_source").size() === 1){
687                                                 statusInfo.find(".source").show().find(".date").show();
688                                                 statusInfo.find(".rt_source").remove()
689                                                 statusInfo.find(".rt_undos").remove();
690                                                 statusBody.removeClass("retweet");
691                                         }else{
692                                                 statusBody.fadeOut("fast");
693                                         }
694                                         updateSentTip("Your retweet has been undo!",3e3,"success");
695                                 }else{
696                                         updateSentTip("Undo failed. Please try again.",3e3,"failure");
697                                 }
698                                 statusBody.css("background-color","");
699                         },
700                         error: function (msg){
701                                 updateSentTip("Undo failed. Please try again.",3e3,"failure");
702                                 statusBody.css("background-color","");
703                         }
704                 });
705                 
706         }
707 }
708 function onDeleteMsg($this){
709         if (window.confirm("Are you sure to delete this message?")){
710                 var $this=$this.parent().parent();
711                 var message_id = $.trim($this.find(".status_id").text());
712                 $this.parent().css("background-color","#FF3300");
713                 updateSentTip("Deleting message...",5e3,"ing");
714                 $.ajax({
715                         url: "ajax/delete.php",
716                         type: "POST",
717                         data: "message_id="+message_id,
718                         success: function (msg){
719                                 if (msg.indexOf("success") >= 0){
720                                         $this.parent().fadeOut("fast");
721                                         updateSentTip("Message deleted.",3e3,"success");
722                                 }else{
723                                         updateSentTip("Failed to delete this message!",3e3,"failure");
724                                 }
725                                 $this.parent().css("background-color","");
726                         },
727                         error: function (msg){
728                                 updateSentTip("Failed to delete this message!",3e3,"failure");
729                                 $this.parent().css("background-color","");
730                         }
731                 });
732         }
733 }
734 $(function (){
735         $("#statuses .trans_close").live('click',function(e){
736                 e.preventDefault();
737                 $(e.target).parent().parent().parent().parent().find(".translated").remove();
738         });
739         $("#transRecover").click(function(e){
740                 $("#textbox").val(ORIG_TEXT);
741                 $(e.target).fadeOut('fast');
742                 });
743         });
744 var translate = function(text,context,lang,callback){
745         
746         var a = "http://www.google.com/uds/Gtranslate";
747         a += "?callback="+callback;
748         a += "&context="+context;
749         a += "&q="+encodeURIComponent(text);
750         a += "&key=notsupplied&v=1.0&nocache=1240207680396&langpair=%7C";
751         a += lang;
752         $.getScript(a);
753 };
754 var transMyCallback = function(content,translation){
755         if(translation.translatedText !== null){
756                 $('#tip').removeClass('loading');
757                 $("#transArea").hide();
758                 $("#textbox").val(translation.translatedText);
759                 $("#transRecover").fadeIn('fast');
760         }
761 };
762 var transCallback = function(content,translation){
763         if(translation.translatedText !== null){
764                 var lang = $.cookie('transLang')
765                 if(lang === null){
766                         lang = 'zh';
767                 }
768                 var langTxt = $.cookie('fullLang');
769                 if(langTxt === null){
770                         langTxt = $('#transArea select[name=langs] option[value='+lang+']').text();
771                 }
772                 var html = '<div class="translated"><a href="#" title="Hide Translation" class="trans_close">(Hide)</a><span class="trans_header"><strong>Translation <small>(from '+translation.detectedSourceLanguage;
773                 html += ' to '+langTxt+')</small> : </strong></span>';
774                 html += '<span class="trans_body">'+translation.translatedText+'</span></div>';
775                 var li,target;
776                 if(typeof INTERVAL_COOKIE !== 'undefined'){
777                         li = $("#statuses ol:visible li:has(.status_id)").filter(":contains("+content+")");
778                         target = li.find(".status_word").filter(":first");
779                 }else{
780                         li = $("#statuses li:has(.status_id)").filter(":contains("+content+")");
781                         target = li.find(".status_word").filter(":first");
782                 }
783                 $(html).appendTo(target);
784                 li.removeClass("loading");
785         }
786 };
787
788 $(function (){
789         $('body').click(function (){
790                 $('ul.right_menu').fadeOut('fast');
791         });
792         $('ol.timeline').click(function(e){
793                 var $this = $(e.target);
794                 switch(e.target.id){
795                                 //avatar menu
796                         case 'avatar':
797                                 e.preventDefault();
798                                 rminit($this);
799                                 e.stopPropagation();
800                         break;
801                 }
802                 switch(e.target.className){
803                                 //ajax_reply
804                         case 'ajax_reply':
805                                 e.preventDefault();
806                                 ajax_reply($this);
807                         break;
808                         //avatar_menu_action
809                         case 'rm_mention':
810                                 e.preventDefault();
811                                 rmmention($this,e);
812                         break;
813                         case 'rm_dm':
814                                 e.preventDefault();
815                                 rmdm($this,e);
816                         break;
817                         case 'rm_follow':
818                                 e.preventDefault();
819                                 rmfollow($this);
820                         break;
821                         case 'rm_unfollow':
822                                 e.preventDefault();
823                                 rmunfollow($this);
824                         break;
825                         case 'rm_block':
826                                 e.preventDefault();
827                                 rmblock($this);
828                         break;
829                         case 'rm_unblock':
830                                 e.preventDefault();
831                                 rmunblock($this);
832                         break;
833                         case 'rm_spam':
834                                 e.preventDefault();
835                                 rmspam($this);
836                         break;
837                         //translate
838                         case 'trans_btn':
839                                 e.preventDefault();
840                                 var tBody = $this.parent().parent();
841                                 if(tBody.find(".trans_body").length !== 0){
842                                         return;
843                                 }
844                                 var id = $.trim(tBody.find('.status_id').text());
845                                 var text = $.trim(tBody.find('.tweet').text());
846                                 var lang = $.cookie('transLang');
847                                 if(lang === null){
848                                         lang = 'zh';
849                                 }
850                                 tBody.parent().addClass('loading');
851                                 translate(text,id,lang,'transCallback');
852                         break;
853                         // unshorturl 
854                         case 'tweet_url':
855                                 var tp = $this.text().split('/');
856                                 var d = tp[0];
857                                 if(d == 't.cn' || d == 'goo.gl' || d == 'bit.ly' || d == 'j.mp' || d == 'is.gd' || d == '163.fm') {
858                                         e.preventDefault();
859                                         updateSentTip('Unshorting the URL...',3e3,'ing');
860                                         $.getJSON('ajax/expand.php?url=' +encodeURIComponent($this.attr('href')), function(data) {
861                                                 if('expanded_url' in data) {
862                                                         var url = data['expanded_url'];
863                                                         if (url != $this.attr('href')) {
864                                                                 var tmp = url.split("://");
865                                                                 $this.text(tmp[1]);
866                                                                 $this.attr('href',url);
867                                                                 updateSentTip('Successfully unshort the URL!',3e3,'success');
868                                                                 if ($.cookie('showpic') === 'true') previewImg($this);
869                                                                 if ($.cookie('mediaPre') === 'true') previewFlash($this);
870                                                                 $this.data('previewed',true);
871                                                         }
872                                                 } else {
873                                                         updateSentTip('Fail to unshort the URL! Please try again later!',3e3,'failure');
874                                                 }
875                                         });
876                                 }
877                         break;
878                 }
879         });
880         $('ol.timeline').on("dbclick", 'a.tweet_url', function(e){
881                 $this = $(e.target);    
882                 
883         });
884 });
885
886 //sidebar function
887 var scroller = function(){
888         var $sidebar = $("#side");
889         var $window = $(window);
890         var top = $sidebar.data("top");
891         if ($window.scrollTop() > top){
892                 $sidebar.css({
893                         marginTop: '',
894                         top: 0,
895                         position: 'fixed'
896                 });
897         }else{
898                 $sidebar.css({
899                         marginTop:'',
900                         top:'',
901                         position:'relative'
902                 });
903         }
904 }
905 var sidebarscroll = function (msg){
906         if($.cookie('sidebarscroll') != 'false' && location.href.indexOf('profile.php')< 0 && location.href.indexOf('setting.php') < 0){
907                 var $sidebar = $("#side");
908                 var $window = $(window);
909                 if(!$sidebar.data("top")){
910                         var offset = $sidebar.offset();
911                         $sidebar.data("top",offset.top);
912                 }
913                 if(msg == undefined){
914                         $window.scroll(scroller);
915                 }
916                 if (msg == 'pause'){
917                         var top = $window.scrollTop() - $sidebar.data('top');
918                         if (top <= 0) top = 0;
919                         $sidebar.css({
920                                 marginTop:top,
921                                 position:'relative'
922                         });
923                         $window.unbind('scroll',scroller);
924                 }       
925         }
926 };
927 $(function (){
928         if($.cookie('autoscroll') != 'false' && $("#more").length > 0 && $("ol.timeline").length > 0){
929                 $("ol.timeline").infinitescroll({
930                         nextSelector:"#more:last",
931                         navSelector:"#pagination",
932                         itemSelector:"ol.timeline li",
933                         callback: function(obj){
934                                 embrTweet(obj);
935                                 previewMedia(obj);
936                         },
937                 });
938         }
939         $("#indicator").toggle(
940                 function (){
941                         $('#sidebarTip_more').slideDown('fast');
942                         $('#indicator').html('[-]');
943                 },function (){
944                         $('#sidebarTip_more').slideUp('fast');
945                         $('#indicator').html('[+]');
946                 });
947         $("#sidebarTip [contenteditable]").live('focusout',function(){
948                 var $this = $("#sidebarTip [contenteditable]");
949                 $.post(
950                         'ajax/setTip.php',
951                         {Tip_Title: $this.eq(0).text()+' ',Tip_Content:$this.eq(1).text()+' ',Tip_More:$this.eq(2).text()+' '},
952                         function (msg){
953                                 if(msg == 'unsecured'){
954                                         updateSentTip('Fail to save your tip for the security issues!',3e3,'failure');
955                                 }else if(msg == 'error'){
956                                         updateSentTip('Fail to save your tip! Please try again later!',3e3,'failure');
957                                 }else{
958                                         updateSentTip('Successfully save your tip!',3e3,'success');
959                                 }
960                         }
961                 );
962         });
963         $("#sidebarTip #tip_reset").live('click',function(e){
964                 e.preventDefault();
965                 if(window.confirm('Are you sure to restore to default tips?')){
966                         $.post(
967                                 'ajax/setTip.php',
968                                 {reset: 'true'},
969                                 function (msg){
970                                         if(msg == 'reset'){
971                                                 updateSentTip('Successfully restore to the default tips!',3e3,'success');
972                                                 location.reload();
973                                         }else{
974                                                 updateSentTip('Fail to save your tip! Please try again later!',3e3,'failure');
975                                         }
976                                 }
977                         );
978                 }
979         })
980         $("#profileRefresh").click(function(e){
981                 e.preventDefault();
982                 var that = $(this);
983                 if (!that.hasClass('refreshing')){
984                         that.addClass('refreshing');
985                         $.ajax({
986                                 url: "ajax/updateProfile.php",
987                                 type: "GET",
988                                 dataType: "json",
989                                 success: function(msg){
990                                         if (msg.result == 'success'){ 
991                                                 freshProfile();
992                                                 updateSentTip("Profile updated successfully!",3e3,"success");
993                                         }
994                                         else{
995                                                 updateSentTip("Failed to update your profile!",3e3,"failure");
996                                         }
997                                 },
998                                 error: function (msg){
999                                         updateSentTip("Failed to update your profile!",3e3,"failure");
1000                                 },
1001                                 complete: function(){
1002                                         that.removeClass('refreshing');
1003                                 }
1004                         });
1005                 }
1006         });
1007 });
1008
1009 // sidepost function
1010 $(function (){
1011         $("#trends_title").toggle(
1012                 function (){
1013                         $("#trends_title").removeClass().addClass("loading");
1014                         updateTrends();
1015                 },function (){
1016                         $("#trends_title").removeClass();
1017                         $("#trend_entries").slideUp("fast");
1018                         sidebarscroll();
1019                 });
1020         $("#following_title").toggle(
1021                 function (){
1022                         $("#following_title").removeClass().addClass("loading");
1023                         sidebarscroll('pause');
1024                         updateFollowing();
1025                 },function (){
1026                         $("#following_title").removeClass();
1027                         $("#following_list").slideUp("fast");
1028                         sidebarscroll();
1029                 });
1030         $("#apiquota_title").toggle(
1031                 function (){
1032                         $("#apiquota_title").removeClass().addClass("loading");
1033                         sidebarscroll('pause');
1034                         updateAPIQuota();
1035                 },function (){
1036                         $("#apiquota_title").removeClass();
1037                         $("#apiquota_list").slideUp("fast");
1038                         sidebarscroll();
1039                 });
1040 });
1041 function updateTrends(){
1042         sidebarscroll('pause');
1043         if (navigator.geolocation) {
1044                 if ($.cookie('woeid') == undefined) {
1045                         navigator.geolocation.getCurrentPosition(function (pos, error) {
1046                                 $.ajax({
1047                                         url: "ajax/updateTrends.php?lat=" + pos.coords.latitude + "&long=" + pos.coords.longitude,
1048                                         type: "GET",
1049                                         success: function (msg){
1050                                                 if ($.trim(msg).indexOf("</ul>" > 0)){
1051                                                         $("#trend_entries").html(msg);
1052                                                 }
1053                                                 $("#trends_title").removeClass().addClass("open");
1054                                                 $("#trend_entries").slideDown("fast");
1055                                         }
1056                                 })
1057                         });
1058                 } else {
1059                         $.ajax({
1060                                 url: "ajax/updateTrends.php",
1061                                 type: "GET",
1062                                 success: function (msg){
1063                                         if ($.trim(msg).indexOf("</ul>" > 0)){
1064                                                 $("#trend_entries").html(msg);
1065                                         }
1066                                         $("#trends_title").removeClass().addClass("open");
1067                                         $("#trend_entries").slideDown("fast");
1068                                 }
1069                         });
1070                 }
1071         }
1072 }
1073 function updateFollowing(){
1074         $.ajax({
1075                 url: "ajax/updateFollowing.php",
1076                 type: "GET",
1077                 success: function (msg){
1078                         if ($.trim(msg).indexOf("</span>" > 0)){
1079                                 $("#following_list").html(msg);
1080                         }
1081                         $("#following_title").removeClass().addClass("open");
1082                         $("#following_list").slideDown("fast");
1083                 }
1084         });
1085 }
1086 function updateAPIQuota(){
1087         $.ajax({
1088                 url: "ajax/apiQuota.php",
1089                 type: "GET",
1090                 success: function (msg){
1091                         if ($.trim(msg).indexOf("</span>" > 0)){
1092                                 $("#apiquota_list").html(msg);
1093                         }
1094                         $("#apiquota_title").removeClass().addClass("open");
1095                         $("#apiquota_list").slideDown("fast");
1096                 }
1097         });
1098 }
1099 $(window).load(function(){
1100         var scrollTo = function (top,duration,callback){
1101         var w = $(window);
1102         var FPS = 50;
1103         var currentTop = w.scrollTop();
1104         var offset = (currentTop - top) / (duration * FPS / 1000);
1105         var n = 0;
1106         var prevTop = currentTop;
1107         var t = setInterval(function (){
1108                         if ((prevTop - top) * (currentTop - top) <= 0){
1109                                 clearInterval(t);
1110                                 currentTop = prevTop = top;
1111                                 w.scrollTop(top);
1112                                 if (callback) callback();
1113                         }else{
1114                                 prevTop = currentTop;
1115                                 w.scrollTop(currentTop -= offset);
1116                         }
1117                 },1e3 / FPS);
1118         }
1119         var scrollToTop = function(){
1120                 scrollTo(0,200,function (){
1121                                 scrollTo(30,50,function (){
1122                                                 scrollTo(0,50);
1123                                         });
1124                         });
1125         };
1126         var scrollToBottom = function(){
1127                 var height = document.body.clientHeight;
1128                 scrollTo(height,200,function (){
1129                         scrollTo(height+30,50,function (){
1130                                         scrollTo(height,50);
1131                                 });
1132                 });
1133         };
1134         $('body').dblclick(function (){
1135                 scrollToTop();
1136                 $("#textbox").focus();
1137         });
1138         $('#content').dblclick(function (e){
1139                 e.stopPropagation();
1140         });
1141         var hkFadeIn = function(text){
1142                 $("#shortcutTip").fadeIn("fast").html(text);
1143         };
1144         var hkFadeOut = function(){
1145                 setTimeout(function (){$("#shortcutTip").fadeOut("fast");},2000);
1146         };
1147         // hotkeys
1148         var hotkeyHandler = function(code){
1149                 switch(code){
1150                 case 82: // R - refresh
1151                         hkFadeIn("Refresh");
1152                         update();
1153                         hkFadeOut();
1154                         break;
1155                 case 67: // C - focus textbox
1156                 case 85: // U
1157                         hkFadeIn("Compose");
1158                         scrollTo(0,1,function (){
1159                                         $("#textbox").focus();
1160                                 });
1161                         hkFadeOut();
1162                         break;
1163                 case 66: // B - scroll to bottom
1164                         hkFadeIn("Boom!");
1165                         scrollToBottom();
1166                         hkFadeOut();
1167                         break;
1168                 case 84: // T - scroll to top
1169                         hkFadeIn("Whiz!");
1170                         scrollToTop();
1171                         hkFadeOut();
1172                         break;
1173                 case 83: // S - search
1174                         hkFadeIn("Search");
1175                         $("#sidepost").animate({backgroundColor: "#FF6347"},500,function(){
1176                                         $("#header_search_query").focus();
1177                                         $("#sidepost").animate({backgroundColor: $("#side_base").css("background-color")},1000);
1178                                 });
1179                         hkFadeOut();
1180                         break;
1181                 }
1182         };
1183         $(document).keydown(function(e){
1184                 var tag = e.target.tagName;
1185                 if(tag === "BODY" || tag === "HTML"){
1186                         if(!e.altKey && !e.shiftKey && !e.ctrlKey && !e.metaKey){
1187                                 hotkeyHandler(e.keyCode);
1188                         }
1189                 }
1190         });
1191         $("#statuses .big-retweet-icon,#func_set .func_btn,#profileRefresh").tipsy({
1192                 gravity: 's'
1193         });
1194         $('#symbols span').tipsy({
1195                 gravity: $.fn.tipsy.autoNS
1196         });
1197         $("#statuses .mine").live("mouseout",function (e){
1198                 $(e.target).removeClass("mine").addClass("myTweet");
1199         });
1200 });
1201 //init global functions
1202 $(document).ready(function (){
1203         embrTweet();
1204         $("#primary_nav li a").click(function (e){
1205                         $(e.target).each(function (){
1206                                         if ($(this).hasClass("active")){
1207                                                 $(this).removeClass()
1208                                         }
1209                                 });
1210                         $(this).removeClass().addClass("active").css("background","transparent url('../img/spinner.gif') no-repeat scroll 173px center")
1211                 });
1212         $("#avatar,#sideimg").lazyload({threshold:100,effect:"fadeIn",placeholder:"img/blank.gif"});
1213         sidebarscroll();
1214 });
1215 var freshProfile = function(){
1216         $("#side_name").text($.cookie('name'));
1217         $.cookie('name',null);
1218         $("span.count").eq(0).text($.cookie('friends_count')).end()
1219                 .eq(1).text($.cookie('followers_count')).end()
1220                 .eq(2).text($.cookie('listed_count'));
1221         $("#update_count").text($.cookie('statuses_count'));
1222         $('#sideimg').attr("src",$.cookie('imgurl'));
1223 };
1224 var markReply = function(obj){
1225         var sidename = "@"+$("#side_name").text().toLowerCase();
1226         obj.each(function (i,o){
1227                 if ($(this).find("> span").find('.tweet').text().toLowerCase().indexOf(sidename) > -1){
1228                         $(this).addClass("reply");
1229                 }
1230         });
1231 };