/**
 * YouTubeCarousel.js
 * 
 * @version 1.0
 * @author Maurice Snip <mauricesnip at hotmail dot com>
 */
function YouTubeCarousel(el,options){this.el=$(el);this.options=options;this.carousel=null;this.players=[];this.init();}YouTubeCarousel.prototype={init:function(){if(this.el.length){var self=this;this.options=$.extend({playerSelector:'.youtube',startMuted:true,autoSize:true,aspectRatio:16/9,width:470,height:264,animation:'slow',auto:7,controlSelectedClass:'selected',controlSelector:'#spotlight-controls li'},this.options||{});this.el.find(this.options.playerSelector).each(function(){self.players.push(new YouTubePlayer(this,{startMuted:self.options.startMuted,width:self.options.width,height:self.options.height,autoSize:self.options.autoSize,aspectRatio:self.options.aspectRatio,onEnd:function(){self.carousel.startAuto();},onPlay:function(){self.carousel.stopAuto();},onPause:function(){self.carousel.startAuto();},onBuffering:function(){self.carousel.stopAuto();}}));});this.el.jcarousel({scroll:1,animation:self.options.animation,auto:self.options.auto,wrap:'both',initCallback:function(carousel){self.carousel=carousel;$(self.options.controlSelector).each(function(i){$(this).bind('click',function(e){e.preventDefault();carousel.stopAuto();carousel.scroll(i+1);carousel.startAuto();});});carousel.clip.hover(function(){carousel.stopAuto();},function(){if(!self.isPlayerPlaying()){carousel.startAuto();}});},itemVisibleInCallback:{onBeforeAnimation:function(carousel,slide,index,state){$(self.options.controlSelector+':nth-child('+index+')').addClass(self.options.controlSelectedClass);},onAfterAnimation:function(carousel,slide,index,state){if(self.isPlayerSlide(slide)){self.getPlayerById($(slide).find('object, embed')[0].id).playVideo();}}},itemVisibleOutCallback:{onBeforeAnimation:function(carousel,slide,index,state){$(self.options.controlSelector+':nth-child('+index+')').removeClass(self.options.controlSelectedClass);if(self.isPlayerSlide(slide)){self.getPlayerById($(slide).find('object, embed')[0].id).pauseVideo();}}}});}},isPlayerSlide:function(slide){return $(slide).find('.player').length;},isPlayerPlaying:function(){return this.el.find('.playing').length;},getPlayerById:function(id){for(var i=0;i<this.players.length;i++){if(this.players[i].playerId===id){return this.players[i];}}}};
