var BgSlideConfig={loadingIndicator:"home-banner-loading-indicator",bgLayer1:"home-banner-slide-1",bgLayer2:"home-banner-slide-2",updateInterval:10,imgDir:"/images/",imgArr:["banner-1.jpg","banner-2.jpg","banner-3.jpg"],debug:false};var BgSlide=Class.create({initialize:function(config){Object.extend(this,config);this.log('init');this.bgSlideSupported=true;this.executer=null;this.slideImg=null;this.lastImg=null;if(this.bgSlideSupported){this.slideStart();}else{this.hideIndicator();}},showIndicator:function(){$(this.loadingIndicator).show();},hideIndicator:function(){Effect.Fade(this.loadingIndicator,{duration:0.5,delay:0.5});},slideUpdate:function(caller){this.log('slideUpdate');this.showIndicator();var imgSrc=this.imgDir+this.getRandomImg();this.preload(imgSrc);},getRandomImg:function(){var newImgs=this.imgArr.without(this.lastImg);var len=newImgs.length;var idx=Math.floor(Math.random()*len);var imgSrc=newImgs[idx];this.lastImg=imgSrc;this.log('select rand img, src='+imgSrc);return imgSrc;},preload:function(imgSrc){this.log('preload, src='+imgSrc);if($(this.bgLayer2).visible()){this.slideImg=$(this.bgLayer1);}else{this.slideImg=$(this.bgLayer2);}
this.slideImg.src=imgSrc;var me=this;if(this.slideImg.complete||(this.slideImg.height!=0)){this.log('preload: image already cached');me.preloadCallback();}else{this.log('preload: observe load event');Event.observe(me.slideImg,'load',function(){me.preloadCallback();Event.stopObserving(me.slideImg);});}},preloadCallback:function(){this.log('preloadCallback');if($(this.bgLayer2).visible()){if(!($(this.bgLayer1).visible())){$(this.bgLayer1).show();}
Effect.Fade(this.bgLayer2);this.log('layer2 fade');}else{Effect.Appear(this.bgLayer2);this.log('layer2 appear');}
this.hideIndicator();},slideStart:function(){this.log('slideStart');this.slideUpdate();this.executer=new PeriodicalExecuter(this.slideUpdate.bind(this),this.updateInterval);},log:function(s){if(this.debug&&window.console)console.log('BgSlide: '+s);}});document.on('dom:loaded',function(){var myBgSlide=new BgSlide(BgSlideConfig);});
