blob: dee3130b09396b2fd48fc76f3d60e5e01696537f [file] [log] [blame]
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001/* file: carousel.js
2 date: oct 2008
3 author: jeremydw,smain
4 info: operates the carousel widget for announcements on
5 the android developers home page. modified from the
6 original market.js from jeremydw. */
7
8/* -- video switcher -- */
9
10var oldVid = "multi"; // set the default video
11var nowPlayingString = "Now playing:";
12var assetsRoot = "/assets/";
13
14
15/* -- app thumbnail switcher -- */
16
17var currentDroid;
18var oldDroid;
19
20// shows a random application
21function randomDroid(){
22
23 // count the total number of apps
24 var droidListLength = 0;
25 for (var k in droidList)
26 droidListLength++;
27
28 // pick a random app and show it
29 var j = 0;
30 var i = Math.floor(droidListLength*Math.random());
31 for (var x in droidList) {
32 if(j++ == i){
33 currentDroid = x;
34 showPreview(x);
35 centerSlide(x);
36 }
37 }
38
39}
40
41// shows a bulletin, swaps the carousel highlighting
42function droid(appName){
43
44 oldDroid = $("#droidlink-"+currentDroid);
45 currentDroid = appName;
46
47 var droid = droidList[appName];
48 var layout = droid.layout;
49 var imgDiv = document.getElementById("bulletinImg");
50 var descDiv = document.getElementById("bulletinDesc");
51
52 if (layout == "imgLeft") {
53 imgDiv.className = "img-left";
54 descDiv.className = "desc-right";
55 } else if (layout == "imgTop") {
56 imgDiv.className = "img-top";
57 descDiv.className = "desc-bottom";
58 } else if (layout == "imgRight") {
59 imgDiv.className = "img-right";
60 descDiv.className = "desc-left";
61 }
62
63 imgDiv.innerHTML = "<img src='" + assetsRoot + "images/home/" + droid.img + "'>";
64 descDiv.innerHTML = (droid.title != "") ? "<h3>" + droid.title + "</h3>" + droid.desc : droid.desc;
65
66 if(oldDroid)
67 oldDroid.removeClass("selected");
68
69 $("#droidlink-"+appName).addClass("selected");
70}
71
72
73// -- * build the carousel based on the droidList * -- //
74function buildCarousel() {
75 var appList = document.getElementById("app-list");
76 for (var x in droidList) {
77 var droid = droidList[x];
78 var icon = droid.icon;
79 var name = droid.name;
80 var a = document.createElement("a");
81 var img = document.createElement("img");
82 var br = document.createElement("br");
83 var text = document.createTextNode(droid.name);
84
85 a.setAttribute("id", "droidlink-" + x);
86 a.className = x;
87 a.setAttribute("href", "#");
88 a.onclick = function() { showPreview(this.className); return false; }
89 img.setAttribute("src", assetsRoot + "images/home/" + droid.icon);
90 img.setAttribute("alt", "");
91
92 a.appendChild(img);
93 a.appendChild(br);
94 a.appendChild(text);
95 appList.appendChild(a);
96 }
97}
98
99// -- * slider * -- //
100
101// -- dependencies:
102// (1) div containing slides, (2) a "clip" div to hide the scroller
103// (3) control arrows
104
105// -- * config below * -- //
106
107var slideCode = droidList; // the dictionary of slides
108var slideList = 'app-list'; // the div containing the slides
109var arrowRight = 'arrow-right'; // the right control arrow
110var arrowLeft = 'arrow-left'; // the left control arrow
111
112
113function showPreview(slideName) {
114// centerSlide(slideName);
115 if (slideName.indexOf('selected') != -1) {
116 return false;
117 }
118 droid(slideName); // do this function when slide is clicked
119}
120
121var thumblist = document.getElementById(slideList);// the div containing the slides
122
123var slideWidth = 144; // width of a slide including all margins, etc.
124var slidesAtOnce = 3; // no. of slides to appear at once (requires odd number to have a centered slide)
125
126// -- * no editing should be needed below * -- //
127
128var originPosition = {};
129var is_animating = 0;
130var currentStripPosition = 0;
131var centeringPoint = 0;
132var rightScrollLimit = 0;
133
134// makeSlideStrip()
135// - figures out how many slides there are
136// - determines the centering point of the slide strip
137function makeSlideStrip() {
138 var slideTotal = 0;
139 centeringPoint = Math.ceil(slidesAtOnce/2);
140 for (var x in slideCode) {
141 slideTotal++;
142 }
143 var i = 0;
144 for (var code in slideCode) {
145 if (i <= centeringPoint-1) {
146 originPosition[code] = 0;
147 } else {
148 if (i >= slideTotal-centeringPoint+1) {
149 originPosition[code] = (slideTotal-slidesAtOnce)*slideWidth;
150 } else {
151 originPosition[code] = (i-centeringPoint+1)*slideWidth;
152 }
153 }
154 i++;
155 }
156 rightScrollLimit = -1*(slideTotal-slidesAtOnce)*slideWidth;
157}
158
159// slides with acceleration
160function slide(goal, id, go_left, cp) {
161 var div = document.getElementById(id);
162 var animation = {};
163 animation.time = 0.5; // in seconds
164 animation.fps = 60;
165 animation.goal = goal;
166 origin = 0.0;
167 animation.origin = Math.abs(origin);
168 animation.frames = (animation.time * animation.fps) - 1.0;
169 var current_frame = 0;
170 var motions = Math.abs(animation.goal - animation.origin);
171 function animate() {
172 var ease_right = function (t) { return (1 - Math.cos(t * Math.PI))/2.0; };
173 var ease = ease_right;
174 if (go_left == 1) {
175 ease = function(t) { return 1.0 - ease_right(t); };
176 }
177 var left = (ease(current_frame/animation.frames) * Math.abs(animation.goal - animation.origin)) - cp;
178 if(left < 0) {
179 left = 0;
180 }
181 if(!isNaN(left)) {
182 div.style.left = '-' + Math.round(left) + 'px';
183 }
184 current_frame += 1;
185 if (current_frame == animation.frames) {
186 is_animating = 0;
187 window.clearInterval(timeoutId)
188 }
189 }
190 var timeoutId = window.setInterval(animate, animation.time/animation.fps * 1000);
191}
192
193//Get style property
194function getStyle(element, cssProperty){
195 var elem = document.getElementById(element);
196 if(elem.currentStyle){
197 return elem.currentStyle[cssProperty]; //IE
198 } else{
199 var style = document.defaultView.getComputedStyle(elem, null); //firefox, Opera
200 return style.getPropertyValue(cssProperty);
201 }
202}
203
204// Left and right arrows
205function page_left() {
206 var amount = slideWidth;
207 animateSlide(amount, 'left');
208}
209
210function page_right() {
211 var amount = slideWidth;
212 animateSlide(amount, 'right');
213}
214
215
216// animates the strip
217// - sets arrows to on or off
218function animateSlide(amount,dir) {
219 var currentStripPosition = parseInt(getStyle(slideList,'left'));
220 var motionDistance;
221 if (amount == slideWidth ) {
222 motionDistance = slideWidth;
223 } else {
224 motionDistance = amount;
225 }
226
227 var rightarrow = document.getElementById(arrowRight);
228 var leftarrow = document.getElementById(arrowLeft);
229
230 function aToggle(state,aDir) {
231 if (state == 'on') {
232 if (aDir =='right') {
233 rightarrow.className = 'arrow-right-on';
234 rightarrow.href = "javascript:page_right()";
235 } else {
236 leftarrow.className = 'arrow-left-on';
237 leftarrow.href = "javascript:page_left()";
238 }
239 } else {
240 if (aDir =='right') {
241 rightarrow.href = "javascript:{}";
242 rightarrow.className = 'arrow-right-off';
243 } else {
244 leftarrow.href = "javascript:{}";
245 leftarrow.className = 'arrow-left-off';
246 }
247 }
248 }
249
250 function arrowChange(rP) {
251 if (rP >= rightScrollLimit) {
252 aToggle('on','right');
253 }
254 if (rP <= rightScrollLimit) {
255 aToggle('off','right');
256 }
257 if (rP <= slideWidth) {
258 aToggle('on','left');
259 }
260 if (rP >= 0) {
261 aToggle('off','left');
262 }
263 }
264
265 if (dir == 'right' && is_animating == 0) {
266 arrowChange(currentStripPosition-motionDistance);
267 is_animating = 1;
268 slide(motionDistance, slideList, 0, currentStripPosition);
269 } else if (dir == 'left' && is_animating == 0) {
270 arrowChange(currentStripPosition+motionDistance);
271 is_animating = 1;
272 rightStripPosition = currentStripPosition + motionDistance;
273 slide(motionDistance, slideList, 1, rightStripPosition);
274 }
275}
276
277function centerSlide(slideName) {
278 var currentStripPosition = parseInt(getStyle(slideList,'left'));
279 var dir = 'left';
280 var originpoint = Math.abs(currentStripPosition);
281 if (originpoint <= originPosition[slideName]) {
282 dir = 'right';
283 }
284 var motionValue = Math.abs(originPosition[slideName]-originpoint);
285 animateSlide(motionValue,dir);
286}
287
288
289function initCarousel(def) {
290 buildCarousel();
291 showPreview(def);
292 makeSlideStrip();
293}