OSDN Git Service

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