/*
 * Ext Core Library $version&#xD;&#xA;http://extjs.com/&#xD;&#xA;Copyright(c) 2006-2009, $author.&#xD;&#xA;&#xD;&#xA;The MIT License&#xD;&#xA;&#xD;&#xA;Permission is hereby granted, free of charge, to any person obtaining a copy&#xD;&#xA;of this software and associated documentation files (the &quot;Software&quot;), to deal&#xD;&#xA;in the Software without restriction, including without limitation the rights&#xD;&#xA;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell&#xD;&#xA;copies of the Software, and to permit persons to whom the Software is&#xD;&#xA;furnished to do so, subject to the following conditions:&#xD;&#xA;&#xD;&#xA;The above copyright notice and this permission notice shall be included in&#xD;&#xA;all copies or substantial portions of the Software.&#xD;&#xA;&#xD;&#xA;THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR&#xD;&#xA;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,&#xD;&#xA;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE&#xD;&#xA;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER&#xD;&#xA;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,&#xD;&#xA;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN&#xD;&#xA;THE SOFTWARE.&#xD;&#xA;
 */
Ext.ns("Ext.ux");Ext.ux.Carousel=Ext.extend(Ext.util.Observable,{interval:3,transitionDuration:1,transitionType:"carousel",transitionEasing:"easeOut",itemSelector:"img",activeSlide:0,autoPlay:false,showPlayButton:false,pauseOnNavigate:false,wrap:false,freezeOnHover:false,navigationOnHover:false,hideNavigation:false,internalId:null,internalName:"slide-",showSlide:null,width:null,height:null,playTaskStopped:true,constructor:function(b,a){a=a||{};Ext.apply(this,a);this.addEvents("beforeprev","prev","beforenext","next","change","play","pause","freeze","unfreeze");this.el=Ext.get(b);this.slides=this.els=[];if(this.autoPlay||this.showPlayButton){this.wrap=true}if(this.autoPlay&&typeof a.showPlayButton==="undefined"){this.showPlayButton=true}Ext.ux.Carousel.superclass.constructor.call(this,a);var c=this.urlGet(window.location.href,this.internalName+this.internalId);if(c){this.showSlide=c}this.initMarkup();this.initEvents();if(this.carouselSize>0){this.refresh()}},initMarkup:function(){var c=Ext.DomHelper;this.carouselSize=0;var b=this.el.select(this.itemSelector);this.els.container=c.append(this.el,{cls:"ux-carousel-container"},true);this.els.slidesWrap=c.append(this.els.container,{cls:"ux-carousel-slides-wrap"},true);this.els.navigation=c.append(this.els.container,{cls:"ux-carousel-nav"},true).hide();this.els.caption=c.append(this.els.navigation,{tag:"h2",cls:"ux-carousel-caption"},true);this.els.navNext=c.append(this.els.navigation,{tag:"a",href:"#",cls:"ux-carousel-nav-next"},true);if(this.showPlayButton){this.els.navPlay=c.append(this.els.navigation,{tag:"a",href:"#",cls:"ux-carousel-nav-play"},true)}this.els.navPrev=c.append(this.els.navigation,{tag:"a",href:"#",cls:"ux-carousel-nav-prev"},true);this.slideWidth=this.width||this.el.getWidth(true);this.slideHeight=this.height||this.el.getHeight(true);this.els.container.setStyle({width:this.slideWidth+"px",height:this.slideHeight+"px"});this.els.caption.setWidth((this.slideWidth-(this.els.navNext.getWidth()*2)-(this.showPlayButton?this.els.navPlay.getWidth():0)-20)+"px");var a=false;b.appendTo(this.els.slidesWrap).each(function(e){e=e.wrap({cls:"ux-carousel-slide"});this.slides.push(e);e.setWidth(this.slideWidth+"px").setHeight(this.slideHeight+"px");if(this.showSlide&&!a){var d=this.getSlideId(this.slides.length-1);if(d&&d==this.showSlide){this.activeSlide=this.slides.length-1;this.autoPlay=false;a=true}}},this);if(!a){this.showSlide=null}this.carouselSize=this.slides.length;if(this.navigationOnHover){this.els.navigation.setStyle("top",(-1*this.els.navigation.getHeight())+"px")}this.el.clip()},initEvents:function(){this.els.navPrev.on("click",function(a){a.preventDefault();var b=a.getTarget();b.blur();if(Ext.fly(b).hasClass("ux-carousel-nav-disabled")){return}this.prev()},this);this.els.navNext.on("click",function(a){a.preventDefault();var b=a.getTarget();b.blur();if(Ext.fly(b).hasClass("ux-carousel-nav-disabled")){return}this.next()},this);if(this.showPlayButton){this.initPlayPressEvent()}if(this.freezeOnHover){this.els.container.on("mouseenter",function(){if(this.playing){if(!this.playTaskStopped){this.fireEvent("freeze",this.slides[this.activeSlide]);this.playTask=Ext.TaskMgr.stop(this.playTask);this.playTaskStopped=true}}},this);this.els.container.on("mouseleave",function(){if(this.playing){if(this.playTaskStopped){this.fireEvent("unfreeze",this.slides[this.activeSlide]);this.playTask=Ext.TaskMgr.start(this.playTask);this.playTaskStopped=false}}},this,{buffer:(this.interval/2)*1000})}if(this.navigationOnHover){this.els.container.on("mouseenter",function(){if(!this.navigationShown){this.navigationShown=true;this.els.navigation.stopFx(false).shift({y:this.els.container.getY(),duration:this.transitionDuration})}},this);this.els.container.on("mouseleave",function(){if(this.navigationShown){this.navigationShown=false;this.els.navigation.stopFx(false).shift({y:this.els.navigation.getHeight()-this.els.container.getY(),duration:this.transitionDuration})}},this)}if(this.interval&&this.autoPlay){this.play()}},initPlayPressEvent:function(a){var b=a||this.els.navPlay;b.on("click",function(d){d.preventDefault();d.getTarget().blur();if(d.ctrlKey){if(!this.internalId){return}var c=this.getSlideId();if(!c){return}var e=this.urlAppend(window.location.href,this.internalName+this.internalId,c);this.pause();window.location.href=e;return}if(this.playing){this.pause()}else{this.play()}},this)},getSlideId:function(b){if(!b){b=this.activeSlide}var c=this.slides[b].child(this.itemSelector),a;if(!c||!(a=c.getAttribute("slideid"))){return null}return a},prev:function(){if(this.fireEvent("beforeprev")===false){return}if(this.pauseOnNavigate){this.pause()}this.resetTimer();this.setSlide(this.activeSlide-1);this.fireEvent("prev",this.activeSlide);return this},next:function(){if(this.fireEvent("beforenext")===false){return}if(this.pauseOnNavigate){this.pause()}this.resetTimer();this.setSlide(this.activeSlide+1);this.fireEvent("next",this.activeSlide);return this},jumpToSlide:function(a){if(this.pauseOnNavigate){this.pause()}this.resetTimer();this.setSlide(a);return this},resetTimer:function(){if(this.playing){if(!this.playTaskStopped){this.playTask=Ext.TaskMgr.stop(this.playTask);this.playTaskStopped=true}this.playTaskBuffer.delay(this.interval*1000)}},initTaskRunner:function(){this.playTask={run:function(){this.playing=true;this.setSlide(this.activeSlide+1)},interval:this.interval*1000,scope:this}},play:function(){if(!this.playing){this.initTaskRunner();this.playTaskBuffer=this.playTaskBuffer||new Ext.util.DelayedTask(function(){if(this.playTaskStopped){this.playTask=Ext.TaskMgr.start(this.playTask);this.playTaskStopped=false}},this);this.playTaskBuffer.delay(this.interval*1000);this.playing=true;if(this.showPlayButton){this.els.navPlay.addClass("ux-carousel-playing")}this.fireEvent("play")}return this},pause:function(){if(this.playing){if(!this.playTaskStopped){this.playTask=Ext.TaskMgr.stop(this.playTask);this.playTaskStopped=true}this.playTaskBuffer.cancel();this.playing=false;if(this.showPlayButton){this.els.navPlay.removeClass("ux-carousel-playing")}this.fireEvent("pause")}return this},clear:function(){this.els.slidesWrap.update("");this.slides=[];this.carouselSize=0;this.pause();return this},add:function(b,a){var c=Ext.fly(b).appendTo(this.els.slidesWrap).wrap({cls:"ux-carousel-slide"});c.setWidth(this.slideWidth+"px").setHeight(this.slideHeight+"px");this.slides.push(c);if(a){this.refresh()}return this},refresh:function(){this.carouselSize=this.slides.length;this.els.slidesWrap.setWidth((this.slideWidth*this.carouselSize)+"px");if(this.carouselSize>0){if(!this.hideNavigation){this.els.navigation.show()}this.setSlide(this.activeSlide,true)}return this},setSlide:function(b,a){if(!this.wrap&&!this.slides[b]){return}else{if(this.wrap){if(b<0){b=this.carouselSize-1}else{if(b>this.carouselSize-1){b=0}}}}if(!this.slides[b]){return}this.els.caption.update(this.slides[b].child(":first-child",true).title||"");var d=b*this.slideWidth;if(!a){switch(this.transitionType){case"fade":this.slides[b].setOpacity(0);this.slides[this.activeSlide].stopFx(false).fadeOut({duration:this.transitionDuration/2,callback:function(){this.els.slidesWrap.setStyle("left",(-1*d)+"px");this.slides[this.activeSlide].setOpacity(1);this.slides[b].fadeIn({duration:this.transitionDuration/2})},scope:this});break;default:var c=(-1*d)+this.els.container.getX();this.els.slidesWrap.stopFx(false);this.els.slidesWrap.shift({duration:this.transitionDuration,x:c,easing:this.transitionEasing});break}}else{this.els.slidesWrap.setStyle("left",(-1*d)+"px")}this.activeSlide=b;this.updateNav();this.fireEvent("change",this.slides[b],b)},updateNav:function(){this.els.navPrev.removeClass("ux-carousel-nav-disabled");this.els.navNext.removeClass("ux-carousel-nav-disabled");if(!this.wrap){if(this.activeSlide===0){this.els.navPrev.addClass("ux-carousel-nav-disabled")}if(this.activeSlide===this.carouselSize-1){this.els.navNext.addClass("ux-carousel-nav-disabled")}}},urlAppend:function(c,b,e){if(!Ext.isEmpty(b)){if(c.indexOf("#")===-1){return c+"#"+b+"="+e}var a=c.split("#");if(a[1].length==0){return c+"#"+b+"="+e}var d=Ext.urlDecode(a[1]);d[b]=e;return a[0]+"#"+Ext.urlEncode(d)}return c},urlGet:function(c,b){if(!Ext.isEmpty(b)&&(c.indexOf("#")!==-1)){var a=c.split("#");if(a[1].length==0){return false}var d=Ext.urlDecode(a[1]);if(!d[b]){return false}return d[b]}return false}});
