/**
 * YouTubeCarousel.js - YouTubePlayer and jCarousel combined
 * 
 * @author  Webstores <info at webstores dot nl>
 *          Copyright (c) Webstores internet totaalbureau <http://www.webstores.nl/>
 */
function YouTubeCarousel(carouselId,options){this.carouselId=carouselId;this.carousel=null;this.players={};this.options=options;this.playerIntervals={};this.playerPoller=null;this.init();};YouTubeCarousel.prototype={init:function(){var self=this;this.options=jQuery.extend({controlsId:'carousel-controls',playerWidth:950,playerHeight:540,autoPlayOnSlide:true},this.options||{});jQuery(this.carouselId).jcarousel({scroll:1,animation: 1000,auto:6,wrap:'both',initCallback:function(carousel){self.carouselInitCallback(carousel);},itemVisibleInCallback:function(carousel,slide,index,state){self.carouselItemVisibleInCallback(carousel,slide,index,state);},itemVisibleOutCallback:function(carousel,slide,index,state){self.carouselItemVisibleOutCallback(carousel,slide,index,state);}});},carouselInitCallback:function(carousel){var self=this;this.carousel=carousel;this.carousel.clip.hover(function(){self.carousel.stopAuto();},function(){if(!self.isPlayerPlaying()){self.carousel.startAuto();}});if(this.options.controlsId){jQuery('#'+this.options.controlsId+' a').click(function(e){e.preventDefault();for(playerId in self.players){if(self.players[playerId].player&&self.players[playerId].getPlayerState()===1){self.players[playerId].pauseVideo();}}self.carousel.stopAuto();var index=jQuery('#'+self.options.controlsId+' a').index(this);self.carousel.scroll(index+1);self.carousel.startAuto();});}},carouselItemVisibleInCallback:function(carousel,slide,index,state){var self=this;if(this.options.controlsId){jQuery('#'+this.options.controlsId+' li:nth-child('+index+')').addClass('selected');}var player=jQuery(slide).find('.player-wrapper object')[0];this.playerPoller=setInterval(function(){if((player&&self.players[player.id].state!=-1)&&self.options.autoPlayOnSlide){player.playVideo();clearInterval(self.playerPoller);}else{player=jQuery(slide).find('.player-wrapper object')[0];}},10);},carouselItemVisibleOutCallback:function(carousel,slide,index,state){if(this.options.controlsId){jQuery('#'+this.options.controlsId+' li:nth-child('+index+')').removeClass('selected');}},addPlayer:function(playerId,videoId){var self=this;this.players[playerId]=new YouTubePlayer(playerId,{videoId:videoId,width:self.options.playerWidth,height:self.options.playerHeight,onBuffering:function(p){self.carousel.stopAuto();},onPlay:function(p){self.carousel.stopAuto();if(!self.playerIntervals[playerId]){self.playerIntervals[playerId]=setInterval(function(){jQuery('#'+playerId+'-elapsed').css('width',Math.round((100*p.getCurrentTime())/p.getDuration())+'%');},1000);}jQuery('#'+playerId+'-wrapper').addClass('playing');},onPause:function(p){jQuery('#'+playerId+'-wrapper').removeClass('playing');self.clearPlayerInterval(playerId);},onEnd:function(p){jQuery('#'+playerId+'-wrapper').removeClass('playing');jQuery('#'+playerId+'-elapsed').css('width','100%');self.clearPlayerInterval(playerId);self.carousel.startAuto();}});jQuery('#'+playerId+'-play').click(function(e){e.preventDefault();self.players[playerId].togglePlay();});jQuery('#'+playerId+'-mute').click(function(e){e.preventDefault();self.players[playerId].toggleMute();jQuery('#'+playerId+'-wrapper').toggleClass('muted');});},clearPlayerInterval:function(playerId){clearInterval(this.playerIntervals[playerId]);this.playerIntervals[playerId]=null;},isPlayerPlaying:function(){return jQuery(this.carouselId+' .playing').length>0;}};
