forked from jarred/cargo-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic.js
273 lines (263 loc) · 7.89 KB
/
magic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
var n_speed = 200;
var is_index = false;
var a_images = [];
var n_current_image = 0;
var view;
var previous_page = "no";
var next_page = "no";
var is_loading_next = false;
var current_page = 0;
//
function init () {
$(".post_thumb").click(function () {
var id = $(this).attr("id").replace("thumb_","");
window.location = "#post"+id;
});
//
$("#search_field").keydown(function (e) {
if (e_.which == 13) {
$("#search_field").submit();
}
});
$("#loading").fadeTo(n_speed, 0);
if ($.cookie("view") == null) {
view = default_format;
$.cookie("view", view);
} else {
view = $.cookie("view");
default_format = view;
}
if (is_index) {
if (view == "grid") {
$(".grid").removeClass("hide");
$("#view_options").html("<li><a href=\"#list\" id=\"show_list_link\" class=\"rounded\">Display as list.</a></li>");
$("#show_list_link").click(show_list);
getPhotosetImages();
getVideoThumbs();
} else {
$("#entry").addClass("hide");
$(".line").addClass("hide");
$(".list").removeClass("hide");
$("#view_options").html("<li><a href=\"#grid\" class=\"rounded\" id=\"show_grid_link\">Display as grid.</a></li>");
$("#show_grid_link").click(show_grid);
$(window).scroll(check_for_more);
}
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, history);
} else {
// format_notes();
// $("#dsq-options").remove()
// $(".dsq-by").remove()
}
if (!$("body").hasClass("index") && !$("body").hasClass("perma")) {
$("#pages").removeClass("hide");
}
add_credit();
}
//
function history(hash) {
var id = SWFAddress.getValue()+""
if (id == "/") {
//
}else if (id == "about") {
if (view == "list") {
show_grid();
} else {
$(".line").removeClass("hide");
$("#entry").removeClass("hide");
$("#entry").html(""+$("#about").html());
}
}else if (id == "following") {
if (view == "list") {
show_grid();
} else {
$(".line").removeClass("hide");
$("#entry").removeClass("hide");
$("#entry").html(""+$("#following").html());
}
}else if (id == "search") {
if (view == "list") {
show_grid();
} else {
$(".line").removeClass("hide");
$("#entry").removeClass("hide");
$("#entry").html(""+$("#search").html());
}
} else {
// in certain scenarios (IE/Windows?) we get the hash with a leading '/' - strip it off
// thanks to Brian Long — http://newobj.net/
if ( id.charAt(0) == '/' ) {
id = id.substring(1, id.length);
}
goto_post(id);
}
}
//
function goto_post(id) {
if (view == "list") {
show_grid();
}
$("#thumb_"+id+" .loading").fadeTo(0, 0);
$("#thumb_"+id+" .loading").removeClass("hide");
$("#thumb_"+id+" .loading").fadeTo(n_speed, 1);
//$("#entry").fadeTo(n_speed, .1)
$("html body").animate({
scrollTop:0
}, n_speed);
var src = "/post/"+id+"/"
if ($("#entry").hasClass("hide")) {
$("#entry").removeClass("hide");
}
if ($(".line").hasClass("hide")) {
$(".line").removeClass("hide");
}
$("#entry").html($("#"+id).html());
$("#entry").fadeTo(0, 0);
if ($("#entry").hasClass("hide")) {
$("#entry").removeClass("hide");
}
$("#entry").fadeTo(200, 1);
$(".post_thumb").each(function () {
if ($(this).attr("id") == "thumb_"+id) {
if (!$(this).hasClass("selected")) {
$(this).addClass("selected");
}
} else {
if ($(this).hasClass("selected")) {
$(this).removeClass("selected");
}
}
});
return false
}
//
function show_list() {
$.cookie("view", null);
$.cookie("view", "list");
SWFAddress.setValue("");
setTimeout("window.location.reload()", 10);
return false;
}
//
function show_grid() {
$.cookie("view", null);
$.cookie("view", "grid");
setTimeout("window.location.reload()", 10);
return false;
}
//
function getVideoThumbs() {
$(".list .video").each(function () {
var embedCode;
var vimeo_ID;
var youtube_ID;
var vid;
var thumb_url;
var swf;
var wR;
var hR;
var ID = $(this).parent().parent().attr("id");
embedCode = $(this).html();
if (embedCode.indexOf("tumblr.com/swf/") >= 0) {
$("#post_" + ID).addClass("tumblr-video");
var frameR = /poster=(http.+?_frame1.jpg)/g;
var frame = decodeURIComponent(String(frameR.exec(embedCode)[1]));
$("#thumb_" + ID + " .video").prepend("<div class=\"bg rounded\" style=\"background-image:url('" + frame + "');\"></div>");
} else {
if (embedCode.indexOf("iframe") >= 0) {
// iframe
// thumbs
if (embedCode.indexOf("vimeo") >= 0) {
var rID = /\/video\/[0-9]*"/g;
vimeo_ID = String(rID.exec(embedCode)).replace("/video/", "").replace("\"", "");
$.getJSON("http://vimeo.com/api/clip/" + vimeo_ID + ".json?callback=?", function (d) {
var thumb_url = d[0].thumbnail_large;
$("#thumb_" + ID + " .video").prepend("<div class=\"bg rounded\" style=\"background-image:url('" + thumb_url + "');\"></div>");
});
}
} else {
var swf = "";
var id = $(this).parent().parent().attr("id");
$(this).children("object").children("param").each(function () {
if ($(this).attr("name") == "movie") {
swf = $(this).attr("value");
}
});
if (swf.indexOf("youtube.com") >= 0) {
// get youtube thumb
swf = swf.replace("http://www.youtube.com");
swf = swf.replace("http://youtube.com");
var youtube_id = swf.substring(swf.indexOf("/v/")+3, swf.indexOf("&"));
var thumb_url = "http://i.ytimg.com/vi/"+youtube_id+"/0.jpg";
$("#thumb_"+id+" .video").prepend("<div class=\"bg rounded\" style=\"background-image:url('"+thumb_url+"');\"></div>");
}else if (swf.indexOf("vimeo.com") >= 0) {
// get vimeo json, then thumb (SO ANNOYING!)
swf = swf.replace("http://vimeo.com/moogaloop.swf?");
swf = swf.replace("http://www.vimeo.com/moogaloop.swf?");
var vimeo_id = swf.substring(swf.indexOf("clip_id=")+8, swf.indexOf("&"));
$.getJSON("http://vimeo.com/api/clip/"+vimeo_id+".json?callback=?", function (d) {
var thumb_url = d[0].thumbnail_large;
$("#thumb_"+id+" .video").prepend("<div class=\"bg rounded\" style=\"background-image:url('"+thumb_url+"');\"></div>");
});
}
}
}
})
}
//
function getPhotosetImages() {
$(".post_thumb .photoset").each(function () {
var ID = $(this).parent().attr("id").replace("thumb_", "")
$.getJSON("/api/read/json?id="+ID+"&callback=?", {}, function (D) {
var photos = D["posts"][0]["photos"]
for(var i = 0; i<photos.length; i++) {
var thumb = photos[i]["photo-url-250"];
$("#thumb_"+ID+" .photoset .thumbs").append("<div class=\"thumb rounded\" style=\"background-image:url('"+thumb+"')\"></div>");
}
})
});
}
//
function check_for_more() {
if (view == "list") {
var yPos = window.pageYOffset;
var h = $(document).height();
var wH = $(window).height();
//$("#debug").text(yPos+" / "+h)
if ((yPos/(h-wH)) >= .8 && !is_loading_next && next_page != "no") {
//alert("we're trying to load the next shit.")
is_loading_next = true;
$("html body").append("<div id=\"temp\" class=\"hide\"></div>");
$(".list").append("<div id=\"next_"+current_page+"\"><div class=\"post_preloader\">loading next page...</div></div>");
$("#temp").load(next_page, {}, function (d) {
$(".post_preloader").remove();
var content = $(d).children("#main_column .list").html()
$("#next_"+current_page).html(content);
var n_p = $(d).children("#info .next_page").text()
if (n_p != "") {
next_page = n_p;
} else {
next_page = "no";
}
setTimeout(can_check_for_more_now, 2000);
});
}
}
}
//
function can_check_for_more_now() {
$(".post_preloader").remove();
current_page++;
is_loading_next = false;
}
//
function format_notes() {
$(".avatar").each(function () {
$(this).parent().remove();
});
}
//
function add_credit() {
$("html body").append("<div id=\"credit\"><a href=\"http://cargotheme.tumblr.com\">Cargo Theme</a> by <a href=\"http://jarredbishop.tumblr.com\">Jarred Bishop</a>. Inspired by <a href=\"http://cargocollective.com\">Cargo</a></div>")
}
$(document).ready(init);
//$(document).load(loaded)