show a generic loading icon when we are doing work making an oauth request, then...
[tritium.git] / script / tritium4.js
blobb778aab7e6257ab5f5c7ef6a4432e03c2153eb44
1 var out = "tritium4-content";
2 function json_request(url,method,params,output,type)
4         new Ajax.Request("./?show="+url,
5         {
6                 method: method,
7                 parameters: params,
8                 evalScripts: true,
9                 onSuccess: function(data)
10                 {
11                         var json = data.responseText.evalJSON();
12                         if (type == 1)
13                         {
14                                 var content = '<ul class="comment-list">';
15                                 for (i = 0; i < json.length; ++i)
16                                 {
17                                         if (!json[i]["retweeted_status"])
18                                         {
19                                                 var chunk = "user";
20                                                 var author = json[i][chunk].screen_name;
21                                                 var avatar = json[i][chunk].profile_image_url;
22                                                 var context = parse_tweet(json[i].text);
23                                                 var date = json[i].created_at;
24                                                 var id = json[i].id;
25                                                 var name = json[i][chunk].name;
26                                                 var retweeted = false;
27                                                 var source = json[i].source;
28                                                 var userid = json[i][chunk].id;
29                                         }
30                                         else
31                                         {
32                                                 var chunk = "retweeted_status";
33                                                 var author = json[i][chunk]["user"].screen_name;
34                                                 var avatar = json[i][chunk]["user"].profile_image_url;
35                                                 var context = parse_tweet(json[i][chunk].text);
36                                                 var date = json[i][chunk].created_at;
37                                                 var id = json[i][chunk]["user"].id;
38                                                 var name = json[i][chunk]["user"].name;
39                                                 var retweeted = "<strong>RT</strong> by "+name;
40                                                 var source = json[i][chunk].source;
41                                                 var userid = json[i][chunk]["user"].id;
42                                         }
43                                         date = relative_time(date);
44                                         content += '<li class="comment comment-parent" id="comment-'+id+'">';
45                                                 content += '<div class="comment-profile-wrapper left">';
46                                                         content += '<div class="comment-profile">';
47                                                                 content += '<div class="comment-gravatar">';
48                                                                         content += '<img src="'+avatar+'" height="30px" width="30px" alt="" />';
49                                                                 content += '</div>';
50                                                                 content += '<div class="comment-author">'+author+'</div>';
51                                                         content += '</div>';
52                                                 content += '</div>';
53                                                 content += '<div class="comment-content-wrapper right">';
54                                                         content += '<div class="comment-content-wrapper-2">';
55                                                                 content += '<div class="comment-body">';
56                                                                         content += '<div class="comment-arrow"></div>';
57                                                                         content += '<div id="comment-'+id+'-date" class="post-date">';
58                                                                                 content += '<div class="left">'+date+'</div>';
59                                                                                 /*content += '<div class="right"></div>';*/
60                                                                                 content += '<div class="clearer">&nbsp;</div>';
61                                                                         content += '</div>';
62                                                                         content += '<div class="hr"></div>';
63                                                                         content += '<div class="comment-text">';
64                                                                                 content += '<p id="comment-'+id+'-text">'+context+'</p>';
65                                                                         content += '</div>';
66                                                                         content += '<div class="clearer">&nbsp;</div>';
67                                                                 content += '</div>';
68                                                         content += '</div>';
69                                                 content += '</div>';
70                                                 content += '<div class="clearer">&nbsp;</div>';
71                                         content += '</li>';
72                                 }
73                                 content += '</ul>';
74                                 $(output).update(content);
75                         }
76                         else
77                         {
78                                 return json;
79                         }
80                 }
81         });
83 function loadhome(params)
85         new Effect.Appear("loading",
86         {
87                 afterFinish: function()
88                 {
89                         var content = json_request("home","GET",params,out,1);
90                         new Effect.Fade("loading",
91                         {
92                                 afterFinish: function()
93                                 {
94                                         $("loading").setStyle("display:none;");
95                                         return content;
96                                 },
97                                 duration: .4
98                         });
99                 },
100                 duration: .2
101         });
103 function loadmore(what,method,params,type)
105         
107 //from the official twitter widget, (C) 2010 twitter
108 var relative_time = function (a)
110         var K = function ()
111         {
112                 var a = navigator.userAgent;
113                 return{
114                         ie: a.match(/MSIE\s([^;]*)/)
115                 }
116         }();
117         var b = new Date();
118         var c = new Date(a);
119         if (K.ie)
120         {
121                 c = Date.parse(a.replace(/( \+)/, ' UTC$1'))
122         }
123         var d = b - c;
124         var e = 1000, minute = e * 60, hour = minute * 60, day = hour * 24, week = day * 7, month = day * 30, year = month * 12;
125         if (isNaN(d) || d < 0)
126         {
127                 return "";
128         }
129         if (d < e * 7)
130         {
131                 return "just now";
132         }
133         if (d < minute)
134         {
135                 return Math.floor(d / e) + " secs ago";
136         }
137         if (d < minute * 2)
138         {
139                 return "a min ago";
140         }
141         if (d < hour)
142         {
143                 return Math.floor(d / minute) + " mins ago";
144         }
145         if (d < hour * 2)
146         {
147                 return "an hr ago";
148         }
149         if (d < day)
150         {
151                 return Math.floor(d / hour) + " hrs ago";
152         }
153         if (d > day && d < day * 2)
154         {
155                 return "a day ago";
156         }
157         if (d < week)
158         {
159                 return Math.floor(d / day) + " days ago";
160         }
161         if (d > week && d < week * 2)
162         {
163                 return "last week";
164         }
165         if (d > week && d < month)
166         {
167                 return Math.floor(d / week) + " weeks ago";
168         }
169         if (d > month && d < month * 2)
170         {
171                 return "last month";
172         }
173         if (d > month)
174         {
175                 return Math.floor(d / month) + " months ago";
176         }
177         if (d > year && d < year * 2)
178         {
179                 return "last year";
180         }
181         if (d > year)
182         {
183                 return Math.floor(d / year) + " years ago";
184         }
187 function parse_tweet(text)
189         if(!text)
190         {
191                 return text;
192         }
193         else
194         {
195                 text = text.replace(/((https?\:\/\/)|(www\.))([^ ]+)/g,function(url)
196                 {
197                         return '<a target="_blank" rel="nofollow" href="'+ url +'">'+url.replace(/^www./i,'')+'</a>';
198                 });
199                 text = text.replace(/@([\w*]+)/g,function(user)
200                 {
201                         return '<a target="_blank" rel="nofollow" href="http://twitter.com/'+user+'">'+user+'</a>';
202                 })+" ";
203                 text = text.replace(/#([\w*]+)/g,function(tag)
204                 {
205                         return '<a target="_blank" rel="nofollow" href="http://search.twitter.com/search?q='+tag.replace(/#/i,'%23')+'">'+tag+'</a>';
206                 })+" ";
207                 return text;
208         }