var rotator=new Class({containerEl:null,foreImg:null,foreLink:null,backImg:null,backLink:null,promoJSON:null,currentRotations:0,currentPromoIdx:0,fwOpacity:null,options:{rotationLimit:240,duration:1000,linger:3000},initialize:function(containerEl,promoJsonURL,options){this.setOptions(options);this.containerEl=containerEl;result=new Json.Remote(promoJsonURL,{method:'get',onComplete:function(jsonObj){this.promoJSON=jsonObj;this.initPromos()}.bind(this),onFailure:function(jsonObj){alert("There was difficulty in retrieving the promo data.\n"+"transport.readyState = "+result.transport.readyState+" / transport.status "+result.transport.status)}}).send()},initPromos:function(){this.foreLink=$('topLayer');this.foreImg=$('topLayerImg');this.backLink=$('bottomLayer');this.backImg=$('bottomLayerImg');var pn=$('promoNav').empty();for(i=0;i<this.promoJSON.promoList.length;i++){new Element('a',{'class':'promo'+(i+1),'id':'promo'+(i+1),'events':{'click':(function(x,obj){return function(){obj.view(x)}})(i,this)}}).injectTop(pn)}new Element('a',{'class':'pause','id':'playToggle','events':{'click':(function(obj){return function(){obj.playToggle()}})(this)}}).injectTop(pn);this.currentPromoIdx=0;new Asset.image(this.promoJSON.promoList[this.currentPromoIdx].image,{onload:function(){this.present();if(this.promoJSON.startOnInit){this.start()}}.bind(this)})},present:function(){$$('#promoNav a').removeClass('on');$('promo'+(this.currentPromoIdx+1)).addClass('on');this.foreImg.src=this.promoJSON.promoList[this.currentPromoIdx].image;switch(this.promoJSON.promoList[this.currentPromoIdx].type){case'blank':this.foreLink.href="javascript:var newwin = window.open('"+this.promoJSON.promoList[this.currentPromoIdx].destination+"', '_blank'); newwin.focus();";break;case'link':this.foreLink.href=this.promoJSON.promoList[this.currentPromoIdx].destination;break;case'javascript':this.foreLink.href="javascript:"+this.promoJSON.promoList[this.currentPromoIdx].destination;break}},step:function(){this.currentPromoIdx=(this.currentPromoIdx+1)%this.promoJSON.promoList.length;this.backImg.src=this.foreImg.src;this.foreImg.setStyle('opacity',0);var tmp=function(){new Asset.image(this.promoJSON.promoList[this.currentPromoIdx].image,{onload:function(){this.present();this.fwOpacity=this.foreImg.effect('opacity',{duration:this.options.duration}).start(0,1)}.bind(this)})}.delay(20,this);this.currentRotations+=(this.currentPromoIdx==0)?1:0;if(this.currentRotations>this.options.rotationLimit){this.currentRotations=0;this.playToggle()}},start:function(){this.currentPromoIdx=0;this.currentRotations=0;this.timer=this.step.periodical(this.options.linger+this.options.duration,this);$('playToggle').className='pause'},playToggle:function(){if($('playToggle').className=='pause'){$('playToggle').className='play';$clear(this.timer)}else{$('playToggle').className='pause';this.timer=this.step.periodical(this.options.linger+this.options.duration,this)}},view:function(idx){$clear(this.timer);if(this.fwOpacity)this.fwOpacity.stop().set(1);$('playToggle').className='play';this.currentPromoIdx=idx;this.present()}});rotator.implement(new Options);