Commits

Serj Zavadsky committed fe213d9

typo fix + options init refactored

Comments (0)

Files changed (2)

js/flipcounter.js

 /**
  * Apple-Style Flip Counter
- * Version 0.5.3 - May 7, 2011 
+ * Version 0.5.3 - May 7, 2011
  *
  * Copyright (c) 2010 Chris Nanney
  * http://cnanney.com/journal/code/apple-style-counter-revisited/
  * Licensed under MIT
  * http://www.opensource.org/licenses/mit-license.php
  */
- 
+
 var flipCounter = function(d, options){
 
 	// Default values
 		fW: 53,
 		bOffset: 390
 	};
-	
-	var o = options || {},
-	doc = window.document,
+
+	var	doc = window.document,
 	divId = typeof d !== 'undefined' && d !== '' ? d : 'flip-counter',
 	div = doc.getElementById(divId);
-	
-	for (var opt in defaults) o[opt] = (opt in o) ? o[opt] : defaults[opt];
+
+    var o = {};
+    for (var opt in defaults) {
+        o[opt] = (opt in options) ? options[opt] : defaults[opt];
+    }
 
 	var digitsOld = [], digitsNew = [], subStart, subEnd, x, y, nextCount = null, newDigit, newComma,
 	best = {
 		pace: 0,
 		inc: 0
 	};
-	
+
 	/**
 	 * Sets the value of the counter and animates the digits to new value.
-	 * 
+	 *
 	 * Example: myCounter.setValue(500); would set the value of the counter to 500,
 	 * no matter what value it was previously.
 	 *
 		}
 		return this;
 	};
-	
+
 	/**
 	 * Sets the increment for the counter. Does NOT animate digits.
 	 */
 		o.inc = isNumber(n) ? n : defaults.inc;
 		return this;
 	};
-	
+
 	/**
 	 * Sets the pace of the counter. Only affects counter when auto == true.
 	 *
 		o.pace = isNumber(n) ? n : defaults.pace;
 		return this;
 	};
-	
+
 	/**
 	 * Sets counter to auto-incrememnt (true) or not (false).
 	 *
 	 *   Should counter auto-increment, true or false
 	 */
 	this.setAuto = function(a){
-		if (a && ! o.atuo){
+		if (a && ! o.auto){
 			o.auto = true;
 			doCount();
 		}
 		}
 		return this;
 	};
-	
+
 	/**
 	 * Increments counter by one animation based on set 'inc' value.
 	 */
 		if (! o.auto) doCount();
 		return this;
 	};
-	
+
 	/**
 	 * Adds a number to the counter value, not affecting the 'inc' or 'pace' of the counter.
 	 *
 		}
 		return this;
 	};
-	
+
 	/**
 	 * Subtracts a number from the counter value, not affecting the 'inc' or 'pace' of the counter.
 	 *
 		}
 		return this;
 	};
-	
+
 	/**
 	 * Increments counter to given value, animating by current pace and increment.
 	 *
 	 */
 	this.incrementTo = function(n, t, p){
 		if (nextCount) clearNext();
-		
+
 		// Smart increment
 		if (typeof t != 'undefined'){
 			var time = isNumber(t) ? t * 1000 : 10000,
 			diff = typeof n != 'undefined' && isNumber(n) ? n - o.value : 0,
 			cycles, inc, check, i = 0;
 			best.q = null;
-			
+
 			// Initial best guess
 			pace = (time / diff > pace) ? Math.round((time / diff) / 10) * 10 : pace;
 			cycles = Math.floor(time / pace);
 			inc = Math.floor(diff / cycles);
-			
+
 			check = checkSmartValues(diff, cycles, inc, pace, time);
-			
+
 			if (diff > 0){
-				while (check.result === false && i < 100){				
+				while (check.result === false && i < 100){
 					pace += 10;
 					cycles = Math.floor(time / pace);
 					inc = Math.floor(diff / cycles);
-					
-					check = checkSmartValues(diff, cycles, inc, pace, time);					
+
+					check = checkSmartValues(diff, cycles, inc, pace, time);
 					i++;
 				}
-				
+
 				if (i == 100){
 					// Could not find optimal settings, use best found so far
 					o.inc = best.inc;
 					o.inc = inc;
 					o.pace = pace;
 				}
-				
+
 				doIncrement(n, true, cycles);
 			}
-		
+
 		}
 		// Regular increment
 		else{
 			doIncrement(n);
 		}
-		
+
 	}
-	
+
 	/**
 	 * Gets current value of counter.
 	 */
 	this.getValue = function(){
 		return o.value;
 	}
-	
+
 	/**
 	 * Stops all running increments.
 	 */
 		if (nextCount) clearNext();
 		return this;
 	}
-	
+
 	//---------------------------------------------------------------------------//
-	
+
 	function doCount(){
 		x = o.value;
 		o.value += o.inc;
 		digitCheck(x,y);
 		if (o.auto === true) nextCount = setTimeout(doCount, o.pace);
 	}
-	
+
 	function doIncrement(n, s, c){
 		var val = o.value,
 		smart = (typeof s == 'undefined') ? false : s,
 		cycles = (typeof c == 'undefined') ? 1 : c;
-		
+
 		if (smart === true) cycles--;
-		
+
 		if (val != n){
 			x = o.value,
 			o.auto = true;
 
 			if (val + o.inc <= n && cycles != 0) val += o.inc
 			else val = n;
-			
+
 			o.value = val;
 			y = o.value;
-			
+
 			digitCheck(x,y);
 			nextCount = setTimeout(function(){doIncrement(n, smart, cycles)}, o.pace);
 		}
 		else o.auto = false;
 	}
-	
+
 	function digitCheck(x,y){
 		digitsOld = splitToArray(x);
 		digitsNew = splitToArray(y);
 			}
 		}
 	}
-	
+
 	function animateDigit(n, oldDigit, newDigit){
 		var speed, step = 0, w, a,
 		bp = [
 		}
 		// Cap on slowest animation can go
 		speed = (speed > 80) ? 80 : speed;
-		
+
 		function animate(){
 			if (step < 7){
 				w = step < 3 ? 't' : 'b';
 				else animate();
 			}
 		}
-		
+
 		animate();
 	}
-	
+
 	// Creates array of digits for easier manipulation
 	function splitToArray(input){
 		return input.toString().split("").reverse();
 		newDigit.className = 'cd';
 		newDigit.id = divId + '_d' + li;
 		newDigit.innerHTML = '<li class="t" id="' + divId + '_t_d' + li + '"></li><li class="b" id="' + divId + '_b_d' + li + '"></li>';
-		
+
 		if (li % 3 == 0){
 			newComma = doc.createElement("ul");
 			newComma.className = 'cd';
 			newComma.innerHTML = '<li class="s"></li>';
 			div.insertBefore(newComma, div.firstChild);
 		}
-		
+
 		div.insertBefore(newDigit, div.firstChild);
 		doc.getElementById(divId + "_t_d" + li).style.backgroundPosition = '0 -' + (digit * o.tFH) + 'px';
 		doc.getElementById(divId + "_b_d" + li).style.backgroundPosition = '0 -' + (digit * o.bFH + o.bOffset) + 'px';
 	}
-	
+
 	// Removes digit
 	function removeDigit(id){
 		var remove = doc.getElementById(divId + "_d" + id);
 		// Do first animation
 		if (o.auto === true) nextCount = setTimeout(doCount, o.pace);
 	}
-	
+
 	// Checks values for smart increment and creates debug text
 	function checkSmartValues(diff, cycles, inc, pace, time){
 		var r = {result: true}, q;
 		r.cond4 = (Math.abs(cycles * pace - time) <= 100) ? true : false;
 		// 5: Calculated time should not be over target time
 		r.cond5 = (cycles * pace <= time) ? true : false;
-		
+
 		// Keep track of 'good enough' values in case can't find best one within 100 loops
 		if (r.cond1 && r.cond2 && r.cond4 && r.cond5){
 			q = Math.abs(diff - (cycles * inc)) + Math.abs(cycles * pace - time);
 				best.inc = inc;
 			}
 		}
-		
+
 		for (var i = 1; i <= 5; i++){
 			if (r['cond' + i] === false){
 				r.result = false;
-			}			
+			}
 		}
 		return r;
 	}
-	
+
 	// http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric/1830844
 	function isNumber(n) {
 		return !isNaN(parseFloat(n)) && isFinite(n);
 	}
-	
+
 	function clearNext(){
 		clearTimeout(nextCount);
 		nextCount = null;
 	}
-	
+
 	// Start it up
 	initialDigitCheck(o.value);
-};
+};

js/flipcounter.min.js

-/**
- * Apple-Style Flip Counter
- * Version 0.5.3 - May 7, 2011 
- *
- * Copyright (c) 2010 Chris Nanney
- * http://cnanney.com/journal/code/apple-style-counter-revisited/
- *
- * Licensed under MIT
- * http://www.opensource.org/licenses/mit-license.php
- */
-var flipCounter=function(y,C){function v(){m=a.value;a.value+=a.inc;h=a.value;s(m,h);a.auto===!0&&(o=setTimeout(v,a.pace))}function z(d,f,b){var c=a.value,e=typeof f=="undefined"?!1:f,g=typeof b=="undefined"?1:b;e===!0&&g--;c!=d?(m=a.value,a.auto=!0,c+a.inc<=d&&g!=0?c+=a.inc:c=d,a.value=c,h=a.value,s(m,h),o=setTimeout(function(){z(d,e,g)},a.pace)):a.auto=!1}function s(d,f){w=d.toString().split("").reverse();t=f.toString().split("").reverse();var b,c=w.length,e=t.length;if(e>c)for(b=e-c;b>0;){var g=
-t[e-b],n=Number(e-b+1)-1;i=k.createElement("ul");i.className="cd";i.id=j+"_d"+n;i.innerHTML='<li class="t" id="'+j+"_t_d"+n+'"></li><li class="b" id="'+j+"_b_d"+n+'"></li>';if(n%3==0)p=k.createElement("ul"),p.className="cd",p.innerHTML='<li class="s"></li>',l.insertBefore(p,l.firstChild);l.insertBefore(i,l.firstChild);k.getElementById(j+"_t_d"+n).style.backgroundPosition="0 -"+g*a.tFH+"px";k.getElementById(j+"_b_d"+n).style.backgroundPosition="0 -"+(g*a.bFH+a.bOffset)+"px";b--}if(e<c)for(b=c-e;b>
-0;){e=k.getElementById(j+"_d"+(c-b));l.removeChild(e);e=l.firstChild.firstChild;if((" "+e.className+" ").indexOf(" s ")>-1)e=e.parentNode,l.removeChild(e);b--}for(b=0;b<c;b++)t[b]!=w[b]&&D(b,w[b],t[b])}function D(d,f,b){function c(){if(g<7){n=g<3?"t":"b";if(h=k.getElementById(j+"_"+n+"_d"+d))h.style.backgroundPosition=i[g];g++;g!=3?setTimeout(c,e):c()}}var e,g=0,n,h,i=["-"+a.fW+"px -"+f*a.tFH+"px",a.fW*-2+"px -"+f*a.tFH+"px","0 -"+b*a.tFH+"px","-"+a.fW+"px -"+(f*a.bFH+a.bOffset)+"px",a.fW*-2+"px -"+
-(b*a.bFH+a.bOffset)+"px",a.fW*-3+"px -"+(b*a.bFH+a.bOffset)+"px","0 -"+(b*a.bFH+a.bOffset)+"px"];if(a.auto===!0&&a.pace<=300)switch(d){case 0:e=a.pace/6;break;case 1:e=a.pace/5;break;case 2:e=a.pace/4;break;case 3:e=a.pace/3;break;default:e=a.pace/1.5}else e=80;e=e>80?80:e;c()}function B(a,f,b,c,e){var g={result:!0};g.cond1=a/f>=1?!0:!1;g.cond2=f*b<=a?!0:!1;g.cond3=Math.abs(f*b-a)<=10?!0:!1;g.cond4=Math.abs(f*c-e)<=100?!0:!1;g.cond5=f*c<=e?!0:!1;if(g.cond1&&g.cond2&&g.cond4&&g.cond5){a=Math.abs(a-
-f*b)+Math.abs(f*c-e);if(q.q===null)q.q=a;if(a<=q.q)q.pace=c,q.inc=b}for(b=1;b<=5;b++)if(g["cond"+b]===!1)g.result=!1;return g}function r(a){return!isNaN(parseFloat(a))&&isFinite(a)}function A(){clearTimeout(o);o=null}var x={value:0,inc:1,pace:1E3,auto:!0,tFH:39,bFH:64,fW:53,bOffset:390},a=C||{},k=window.document,j=typeof y!=="undefined"&&y!==""?y:"flip-counter",l=k.getElementById(j),u;for(u in x)a[u]=u in a?a[u]:x[u];var w=[],t=[],m,h,o=null,i,p,q={q:null,pace:0,inc:0};this.setValue=function(d){if(r(d))m=
-a.value,h=d,a.value=d,s(m,h);return this};this.setIncrement=function(d){a.inc=r(d)?d:x.inc;return this};this.setPace=function(d){a.pace=r(d)?d:x.pace;return this};this.setAuto=function(d){if(d&&!a.atuo)a.auto=!0,v();if(!d&&a.auto)o&&A(),a.auto=!1;return this};this.step=function(){a.auto||v();return this};this.add=function(d){if(r(d))m=a.value,a.value+=d,h=a.value,s(m,h);return this};this.subtract=function(d){if(r(d))m=a.value,a.value-=d,a.value>=0?h=a.value:(h="0",a.value=0),s(m,h);return this};this.incrementTo=
-function(d,f,b){o&&A();if(typeof f!="undefined"){var f=r(f)?f*1E3:1E4,b=typeof b!="undefined"&&r(b)?b:a.pace,c=typeof d!="undefined"&&r(d)?d-a.value:0,e,g,h,i=0;q.q=null;b=f/c>b?Math.round(f/c/10)*10:b;e=Math.floor(f/b);g=Math.floor(c/e);h=B(c,e,g,b,f);if(c>0){for(;h.result===!1&&i<100;)b+=10,e=Math.floor(f/b),g=Math.floor(c/e),h=B(c,e,g,b,f),i++;i==100?(a.inc=q.inc,a.pace=q.pace):(a.inc=g,a.pace=b);z(d,!0,e)}}else z(d)};this.getValue=function(){return a.value};this.stop=function(){o&&A();return this};
-(function(d){var f=d.toString(),d=f.length,b=1,c;for(c=0;c<d;c++){i=k.createElement("ul");i.className="cd";i.id=j+"_d"+c;i.innerHTML=i.innerHTML='<li class="t" id="'+j+"_t_d"+c+'"></li><li class="b" id="'+j+"_b_d"+c+'"></li>';l.insertBefore(i,l.firstChild);if(b!=d&&b%3==0)p=k.createElement("ul"),p.className="cd",p.innerHTML='<li class="s"></li>',l.insertBefore(p,l.firstChild);b++}f=f.toString().split("").reverse();for(c=0;c<d;c++)k.getElementById(j+"_t_d"+c).style.backgroundPosition="0 -"+f[c]*a.tFH+
-"px",k.getElementById(j+"_b_d"+c).style.backgroundPosition="0 -"+(f[c]*a.bFH+a.bOffset)+"px";a.auto===!0&&(o=setTimeout(v,a.pace))})(a.value)};
+/**
+ * Apple-Style Flip Counter
+ * Version 0.5.3 - May 7, 2011
+ *
+ * Copyright (c) 2010 Chris Nanney
+ * http://cnanney.com/journal/code/apple-style-counter-revisited/
+ *
+ * Licensed under MIT
+ * http://www.opensource.org/licenses/mit-license.php
+ */
+var flipCounter=function(E,c){var p={value:0,inc:1,pace:1000,auto:true,tFH:39,bFH:64,fW:53,bOffset:390};var G=window.document,h=typeof E!=="undefined"&&E!==""?E:"flip-counter",s=G.getElementById(h);var w={};for(var a in p){w[a]=(a in c)?c[a]:p[a]}var f=[],F=[],C,i,n,l,m=null,j,B,b={q:null,pace:0,inc:0};this.setValue=function(d){if(q(d)){n=w.value;l=d;w.value=d;k(n,l)}return this};this.setIncrement=function(d){w.inc=q(d)?d:p.inc;return this};this.setPace=function(d){w.pace=q(d)?d:p.pace;return this};this.setAuto=function(d){if(d&&!w.auto){w.auto=true;e()}if(!d&&w.auto){if(m){t()}w.auto=false}return this};this.step=function(){if(!w.auto){e()}return this};this.add=function(d){if(q(d)){n=w.value;w.value+=d;l=w.value;k(n,l)}return this};this.subtract=function(d){if(q(d)){n=w.value;w.value-=d;if(w.value>=0){l=w.value}else{l="0";w.value=0}k(n,l)}return this};this.incrementTo=function(y,M,x){if(m){t()}if(typeof M!="undefined"){var H=q(M)?M*1000:10000,d=typeof x!="undefined"&&q(x)?x:w.pace,L=typeof y!="undefined"&&q(y)?y-w.value:0,J,I,o,K=0;b.q=null;d=(H/L>d)?Math.round((H/L)/10)*10:d;J=Math.floor(H/d);I=Math.floor(L/J);o=D(L,J,I,d,H);if(L>0){while(o.result===false&&K<100){d+=10;J=Math.floor(H/d);I=Math.floor(L/J);o=D(L,J,I,d,H);K++}if(K==100){w.inc=b.inc;w.pace=b.pace}else{w.inc=I;w.pace=d}A(y,true,J)}}else{A(y)}};this.getValue=function(){return w.value};this.stop=function(){if(m){t()}return this};function e(){n=w.value;w.value+=w.inc;l=w.value;k(n,l);if(w.auto===true){m=setTimeout(e,w.pace)}}function A(I,d,H){var y=w.value,x=(typeof d=="undefined")?false:d,o=(typeof H=="undefined")?1:H;if(x===true){o--}if(y!=I){n=w.value,w.auto=true;if(y+w.inc<=I&&o!=0){y+=w.inc}else{y=I}w.value=y;l=w.value;k(n,l);m=setTimeout(function(){A(I,x,o)},w.pace)}else{w.auto=false}}function k(o,K){f=u(o);F=u(K);var J,H=f.length,d=F.length;if(d>H){J=d-H;while(J>0){r(d-J+1,F[d-J]);J--}}if(d<H){J=H-d;while(J>0){v(H-J);J--}}for(var I=0;I<H;I++){if(F[I]!=f[I]){g(I,f[I],F[I])}}}function g(y,L,o){var I,H=0,K,J,d=["-"+w.fW+"px -"+(L*w.tFH)+"px",(w.fW*-2)+"px -"+(L*w.tFH)+"px","0 -"+(o*w.tFH)+"px","-"+w.fW+"px -"+(L*w.bFH+w.bOffset)+"px",(w.fW*-2)+"px -"+(o*w.bFH+w.bOffset)+"px",(w.fW*-3)+"px -"+(o*w.bFH+w.bOffset)+"px","0 -"+(o*w.bFH+w.bOffset)+"px"];if(w.auto===true&&w.pace<=300){switch(y){case 0:I=w.pace/6;break;case 1:I=w.pace/5;break;case 2:I=w.pace/4;break;case 3:I=w.pace/3;break;default:I=w.pace/1.5;break}}else{I=80}I=(I>80)?80:I;function x(){if(H<7){K=H<3?"t":"b";J=G.getElementById(h+"_"+K+"_d"+y);if(J){J.style.backgroundPosition=d[H]}H++;if(H!=3){setTimeout(x,I)}else{x()}}}x()}function u(d){return d.toString().split("").reverse()}function r(o,x){var d=Number(o)-1;j=G.createElement("ul");j.className="cd";j.id=h+"_d"+d;j.innerHTML='<li class="t" id="'+h+"_t_d"+d+'"></li><li class="b" id="'+h+"_b_d"+d+'"></li>';if(d%3==0){B=G.createElement("ul");B.className="cd";B.innerHTML='<li class="s"></li>';s.insertBefore(B,s.firstChild)}s.insertBefore(j,s.firstChild);G.getElementById(h+"_t_d"+d).style.backgroundPosition="0 -"+(x*w.tFH)+"px";G.getElementById(h+"_b_d"+d).style.backgroundPosition="0 -"+(x*w.bFH+w.bOffset)+"px"}function v(x){var d=G.getElementById(h+"_d"+x);s.removeChild(d);var o=s.firstChild.firstChild;if((" "+o.className+" ").indexOf(" s ")>-1){d=o.parentNode;s.removeChild(d)}}function z(I){var d=I.toString(),x=d.length,H=1,o;for(o=0;o<x;o++){j=G.createElement("ul");j.className="cd";j.id=h+"_d"+o;j.innerHTML=j.innerHTML='<li class="t" id="'+h+"_t_d"+o+'"></li><li class="b" id="'+h+"_b_d"+o+'"></li>';s.insertBefore(j,s.firstChild);if(H!=(x)&&H%3==0){B=G.createElement("ul");B.className="cd";B.innerHTML='<li class="s"></li>';s.insertBefore(B,s.firstChild)}H++}var y=u(d);for(o=0;o<x;o++){G.getElementById(h+"_t_d"+o).style.backgroundPosition="0 -"+(y[o]*w.tFH)+"px";G.getElementById(h+"_b_d"+o).style.backgroundPosition="0 -"+(y[o]*w.bFH+w.bOffset)+"px"}if(w.auto===true){m=setTimeout(e,w.pace)}}function D(J,I,H,K,y){var o={result:true},x;o.cond1=(J/I>=1)?true:false;o.cond2=(I*H<=J)?true:false;o.cond3=(Math.abs(I*H-J)<=10)?true:false;o.cond4=(Math.abs(I*K-y)<=100)?true:false;o.cond5=(I*K<=y)?true:false;if(o.cond1&&o.cond2&&o.cond4&&o.cond5){x=Math.abs(J-(I*H))+Math.abs(I*K-y);if(b.q===null){b.q=x}if(x<=b.q){b.pace=K;b.inc=H}}for(var d=1;d<=5;d++){if(o["cond"+d]===false){o.result=false}}return o}function q(d){return !isNaN(parseFloat(d))&&isFinite(d)}function t(){clearTimeout(m);m=null}z(w.value)};