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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/* file: carousel.js
date: oct 2008
author: jeremydw,smain
info: operates the carousel widget for announcements on
the android developers home page. modified from the
original market.js from jeremydw. */
/* -- video switcher -- */
var oldVid = "multi"; // set the default video
var nowPlayingString = "Now playing:";
var assetsRoot = "assets/";
/* -- app thumbnail switcher -- */
var currentDroid;
var oldDroid;
// shows a random application
function randomDroid(){
// count the total number of apps
var droidListLength = 0;
for (var k in droidList)
droidListLength++;
// pick a random app and show it
var j = 0;
var i = Math.floor(droidListLength*Math.random());
for (var x in droidList) {
if(j++ == i){
currentDroid = x;
showPreview(x);
centerSlide(x);
}
}
}
// shows a bulletin, swaps the carousel highlighting
function droid(appName){
oldDroid = $("#droidlink-"+currentDroid);
currentDroid = appName;
var droid = droidList[appName];
$("#"+appName).show().siblings().hide();
if(oldDroid)
oldDroid.removeClass("selected");
$("#droidlink-"+appName).addClass("selected");
}
// -- * build the carousel based on the droidList * -- //
function buildCarousel() {
var appList = document.getElementById("app-list");
for (var x in droidList) {
var droid = droidList[x];
var icon = droid.icon;
var name = droid.name;
var a = document.createElement("a");
var img = document.createElement("img");
var br = document.createElement("br");
var span = document.createElement("span");
var text = document.createTextNode(droid.name);
a.setAttribute("id", "droidlink-" + x);
a.className = x;
a.setAttribute("href", "#");
a.onclick = function() { showPreview(this.className); return false; }
img.setAttribute("src", toRoot + assetsRoot + "images/home/" + droid.icon);
img.setAttribute("alt", "");
span.appendChild(text);
a.appendChild(img);
a.appendChild(br);
a.appendChild(span);
appList.appendChild(a);
/* add the bulletins */
var layout = droid.layout;
var div = document.createElement("div");
var imgDiv = document.createElement("div");
var descDiv = document.createElement("div");
div.setAttribute("id", x);
div.setAttribute("style", "display:none");
imgDiv.setAttribute("class", "bulletinImg");
descDiv.setAttribute("class", "bulletinDesc");
if (layout == "imgLeft") {
$(imgDiv).addClass("img-left");
$(descDiv).addClass("desc-right");
} else if (layout == "imgTop") {
$(imgDiv).addClass("img-top");
$(descDiv).addClass("desc-bottom");
} else if (layout == "imgRight") {
$(imgDiv).addClass("img-right");
$(descDiv).addClass("desc-left");
}
imgDiv.innerHTML = "<img src='" + toRoot + assetsRoot + "images/home/" + droid.img + "'>";
descDiv.innerHTML = (droid.title != "") ? "<h3>" + droid.title + "</h3>" + droid.desc : droid.desc;
$(div).append(imgDiv);
$(div).append(descDiv);
$("#carouselMain").append(div);
}
}
// -- * slider * -- //
// -- dependencies:
// (1) div containing slides, (2) a "clip" div to hide the scroller
// (3) control arrows
// -- * config below * -- //
var slideCode = droidList; // the dictionary of slides
var slideList = 'app-list'; // the div containing the slides
var arrowRight = 'arrow-right'; // the right control arrow
var arrowLeft = 'arrow-left'; // the left control arrow
function showPreview(slideName) {
centerSlide(slideName);
if (slideName.indexOf('selected') != -1) {
return false;
}
droid(slideName); // do this function when slide is clicked
}
var thumblist = document.getElementById(slideList);// the div containing the slides
var slideWidth = 144; // width of a slide including all margins, etc.
var slidesAtOnce = 3; // no. of slides to appear at once (requires odd number to have a centered slide)
// -- * no editing should be needed below * -- //
var originPosition = {};
var is_animating = 0;
var currentStripPosition = 0;
var centeringPoint = 0;
var rightScrollLimit = 0;
// makeSlideStrip()
// - figures out how many slides there are
// - determines the centering point of the slide strip
function makeSlideStrip() {
var slideTotal = 0;
centeringPoint = Math.ceil(slidesAtOnce/2);
for (var x in slideCode) {
slideTotal++;
}
var i = 0;
for (var code in slideCode) {
if (i <= centeringPoint-1) {
originPosition[code] = 0;
} else {
if (i >= slideTotal-centeringPoint+1) {
originPosition[code] = (slideTotal-slidesAtOnce)*slideWidth;
} else {
originPosition[code] = (i-centeringPoint+1)*slideWidth;
}
}
i++;
}
rightScrollLimit = -1*(slideTotal-slidesAtOnce)*slideWidth;
}
// slides with acceleration
function slide(goal, id, go_left, cp) {
var div = document.getElementById(id);
var animation = {};
animation.time = 0.5; // in seconds
animation.fps = 60;
animation.goal = goal;
origin = 0.0;
animation.origin = Math.abs(origin);
animation.frames = (animation.time * animation.fps) - 1.0;
var current_frame = 0;
var motions = Math.abs(animation.goal - animation.origin);
function animate() {
var ease_right = function (t) { return (1 - Math.cos(t * Math.PI))/2.0; };
var ease = ease_right;
if (go_left == 1) {
ease = function(t) { return 1.0 - ease_right(t); };
}
var left = (ease(current_frame/animation.frames) * Math.abs(animation.goal - animation.origin)) - cp;
if(left < 0) {
left = 0;
}
if(!isNaN(left)) {
div.style.left = '-' + Math.round(left) + 'px';
}
current_frame += 1;
if (current_frame == animation.frames) {
is_animating = 0;
window.clearInterval(timeoutId)
}
}
var timeoutId = window.setInterval(animate, animation.time/animation.fps * 1000);
}
//Get style property
function getStyle(element, cssProperty){
var elem = document.getElementById(element);
if(elem.currentStyle){
return elem.currentStyle[cssProperty]; //IE
} else{
var style = document.defaultView.getComputedStyle(elem, null); //firefox, Opera
return style.getPropertyValue(cssProperty);
}
}
// Left and right arrows
function page_left() {
var amount = slideWidth;
animateSlide(amount, 'left');
}
function page_right() {
var amount = slideWidth;
animateSlide(amount, 'right');
}
// animates the strip
// - sets arrows to on or off
function animateSlide(amount,dir) {
var currentStripPosition = parseInt(getStyle(slideList,'left'));
var motionDistance;
if (amount == slideWidth ) {
motionDistance = slideWidth;
} else {
motionDistance = amount;
}
var rightarrow = document.getElementById(arrowRight);
var leftarrow = document.getElementById(arrowLeft);
function aToggle(state,aDir) {
if (state == 'on') {
if (aDir =='right') {
rightarrow.className = 'arrow-right-on';
rightarrow.href = "javascript:page_right()";
} else {
leftarrow.className = 'arrow-left-on';
leftarrow.href = "javascript:page_left()";
}
} else {
if (aDir =='right') {
rightarrow.href = "javascript:{}";
rightarrow.className = 'arrow-right-off';
} else {
leftarrow.href = "javascript:{}";
leftarrow.className = 'arrow-left-off';
}
}
}
function arrowChange(rP) {
if (rP >= rightScrollLimit) {
aToggle('on','right');
}
if (rP <= rightScrollLimit) {
aToggle('off','right');
}
if (rP <= slideWidth) {
aToggle('on','left');
}
if (rP >= 0) {
aToggle('off','left');
}
}
if (dir == 'right' && is_animating == 0) {
arrowChange(currentStripPosition-motionDistance);
is_animating = 1;
slide(motionDistance, slideList, 0, currentStripPosition);
} else if (dir == 'left' && is_animating == 0) {
arrowChange(currentStripPosition+motionDistance);
is_animating = 1;
rightStripPosition = currentStripPosition + motionDistance;
slide(motionDistance, slideList, 1, rightStripPosition);
}
}
function centerSlide(slideName) {
var currentStripPosition = parseInt(getStyle(slideList,'left'));
var dir = 'left';
var originpoint = Math.abs(currentStripPosition);
if (originpoint <= originPosition[slideName]) {
dir = 'right';
}
var motionValue = Math.abs(originPosition[slideName]-originpoint);
animateSlide(motionValue,dir);
}
function initCarousel(def) {
buildCarousel();
showPreview(def);
makeSlideStrip();
}
|