/*
 * gallery/countdown
 */
KISSY.add('gallery/countdown',function(S){var D=S.DOM,EVENT_AFTER_PAINT='afterPaint';function Countdown(container,config){if(!(this instanceof Countdown)){return new Countdown(container,config);}container=S.get(container);if(!container){return;}this.container=container;this.config=S.merge(Countdown.Config,config);this._init();}Countdown.Config={effect:'normal',_varReg:/\$\{([\-\w]+)\}/g,_clock:['d',100,2,'h',24,2,'m',60,2,'s',60,2,'u',10,1]};var watchman=(function(){var fns=[],commands=[];function sound(){var time=+new Date(),duration=100-time+sound.lastTime,i,len;while(commands.length){commands.shift()();}for(i=0,len=fns.length;i<len;i+=2){fns[i+1]+=2;if(fns[i+1]===21||(fns[i+1]&1)===0){fns[i](time);fns[i+1]&=1;}}sound.lastTime=time;duration=duration>0?duration:0;setTimeout(sound,duration);}sound.lastTime=+new Date();setTimeout(sound,100);return{add:function(fn,frequency){commands.push(function(){fns.push(fn);fns.push(frequency===1000?1:0);});},remove:function(fn){commands.push(function(){var i=S.indexOf(fn,fns);if(i!==-1){fns.splice(S.indexOf(fn,fns),2);}});}};}());S.augment(Countdown,S.EventTarget,{_init:function(){var me=this,cfg=me.config,varReg=cfg._varReg,clockCfg=cfg._clock,container=me.container,hands=[];this._notify=[];me.hands=hands;me.frequency=1000;me.startAt=(new Date()).getTime();var end=(new Date(D.attr(container,'end'))).getTime();me.total=end-me.startAt;varReg.lastIndex=0;container.innerHTML=container.innerHTML.replace(varReg,function(str,type){if(type==='u'||type==='s-ext'){me.frequency=100;}var content='';if(type==='s-ext'){hands.push({type:'s'});hands.push({type:'u'});content=me._html('','s','handlet')+me._html('.','','digital')+me._html('','u','handlet');}else{hands.push({type:type});}return me._html(content,type,'hand');});S.each(hands,function(hand){var type=hand.type,base=100,i;hand.node=S.one(container).one('.hand-'+type);for(i=clockCfg.length-3;i>-1;i-=3){if(type===clockCfg[i]){break;}base*=clockCfg[i+1];}hand.base=base;hand.radix=clockCfg[i+1];hand.bits=clockCfg[i+2];});me._reflow(me.startAt);var _reflow=me._reflow;me._reflow=function(time){return _reflow.call(me,time);};watchman.add(me._reflow,me.frequency);D.css(container,'display','block');},_reflow:function(time){var left=this.total+this.startAt-time;if(left<this.frequency){watchman.remove(this._reflow);}S.each(this.hands,function(hand){hand.lastValue=hand.value;hand.value=Math.floor(left/hand.base)%hand.radix;});this._repaint();if(this._notify[left]){S.each(this._notify[left],function(callback){callback();});}return this;},_repaint:function(){var me=this,effect=me.config.effect;Countdown.Effects[effect].paint.apply(me);me.fire(EVENT_AFTER_PAINT);},_toDigitals:function(value,bits){value=value<0?0:value;var digitals=[];while(bits--){digitals[bits]=value%10;value=Math.floor(value/10);}return digitals;},_html:function(content,className,type){if(S.isArray(content)){content=content.join('');}switch(type){case'hand':className=type+' hand-'+className;break;case'handlet':className=type+' hand-'+className;break;case'digital':if(content==='.'){className=type+' '+type+'-point '+className;}else{className=type+' '+type+'-'+content+' '+className;}break;}return'<s class="'+className+'">'+content+'</s>';},notify:function(time,callback){var notifies=this._notify[time]||[];notifies.push(callback);this._notify[time]=notifies;return this;}});S.Gallery=S.Gallery||{};S.Gallery.Countdown=Countdown;});
/*
 * gallery/countdown/effects
 */
KISSY.add('gallert/countdown/effects',function(S){var D=S.DOM,Countdown=S.Gallery.Countdown;Countdown.Effects={normal:{paint:function(){var me=this,content;S.each(me.hands,function(hand){if(hand.lastValue!==hand.value){content='';S.each(me._toDigitals(hand.value,hand.bits),function(digital){content+=me._html(digital,'','digital');});hand.node.html(content);}});}},slide:{paint:function(){var me=this,content,bits,digitals,oldDigitals;S.each(me.hands,function(hand){if(hand.lastValue!==hand.value){content='';bits=hand.bits;digitals=me._toDigitals(hand.value,bits);if(hand.lastValue===undefined){oldDigitals=digitals;}else{oldDigitals=me._toDigitals(hand.lastValue,bits);}
while(bits--){if(oldDigitals[bits]!==digitals[bits]){content=me._html([me._html(digitals[bits],'','digital'),me._html(oldDigitals[bits],'','digital')],'slide-wrap')+content;}else{content=me._html(digitals[bits],'','digital')+content;}}
hand.node.html(content);}});Countdown.Effects.slide.afterPaint.apply(me);},afterPaint:function(){S.each(this.hands,function(hand){if(hand.lastValue!==hand.value&&hand.lastValue!==undefined){var node=hand.node,height=node.one('.digital').height();node.css('height',height);node.all('.slide-wrap').css('top',-height).animate('top: 0',0.5,'easeIn');}});}},flip:{paint:function(){var me=this,m_mask,m_new,m_old;S.each(me.hands,function(hand){if(hand.lastValue!==hand.value){m_mask='';m_new='';m_old='';S.each(me._toDigitals(hand.value,hand.bits),function(digital){m_new+=me._html(digital,'','digital');});if(hand.lastValue===undefined){hand.node.html(m_new);}else{m_new=me._html(m_new,'handlet');S.each(me._toDigitals(hand.lastValue,hand.bits),function(digital){m_old+=me._html(digital,'','digital');});m_mask=me._html(m_old,'handlet mask');m_old=me._html(m_old,'handlet');hand.node.html(m_new+m_old+m_mask);}}});Countdown.Effects.flip.afterPaint.apply(me);},afterPaint:function(){S.each(this.hands,function(hand){if(hand.lastValue!==hand.value&&hand.lastValue!==undefined){var node=hand.node,ns=node.all('.handlet'),n_new=ns.item(0),n_old=ns.item(1),n_mask=ns.item(2),width=node.width(),height=node.height(),h_top=Math.floor(height/2),h_bottom=height-h_top;n_old.css({clip:'rect('+h_top+'px, '+width+'px, '+height+'px, 0)'});n_mask.css({overflow:'hidden',height:h_top+'px'});n_mask.animate({top:h_top+'px',height:0},0.15,'easeNone',function(){n_mask.html(n_new.html());n_mask.css({top:0,height:h_top+'px',clip:'rect('+h_top+'px, '+width+'px, '+height+'px, 0)'});n_mask.animate('height: '+height+'px',0.3,'bounceOut');});}});}}};});

