/**
 * 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;
		
		if(typeof this.options != 'undefined') {
			for(prop in this.options) {
				this[prop] = this.options[prop];
			}
		}
		
		$('#' + this.carouselId).jcarousel({
			scroll: 1,
			animation: 'slow',
			auto: 7,
			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.controlsId) {
			$('#' + this.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 = $('#' + self.controlsId + ' a').index(this);
				self.carousel.scroll(index + 1);
				self.carousel.startAuto();
			});
		}
	},
	carouselItemVisibleInCallback: function(carousel, slide, index, state) {
		var self = this;
		
		if(this.controlsId) {
			$('#' + this.controlsId + ' li:nth-child(' + index + ')').addClass('selected');
		}
		
		var player = $(slide).find('.player-wrapper object')[0];
		
		this.playerPoller = setInterval(function() {
			if((player && self.players[player.id].state != -1)) {
				player.playVideo();
				clearInterval(self.playerPoller);
			}
			else {
				player = $(slide).find('.player-wrapper object')[0];
			}
		}, 10);
	},
	carouselItemVisibleOutCallback: function(carousel, slide, index, state) {
		if(this.controlsId) {
			$('#' + this.controlsId + ' li:nth-child(' + index + ')').removeClass('selected');
		}
	},
	addPlayer: function(playerId, videoId) {
		var self = this;
		
		this.players[playerId] = new YouTubePlayer(playerId, {
			videoId: videoId,
			width: 720,
			height: 404,
			onBuffering: function(p) {
				self.carousel.stopAuto();
			},
			onPlay: function(p) {
				self.carousel.stopAuto();
				if(!self.playerIntervals[playerId]) {
					self.playerIntervals[playerId] = setInterval(function() {
						$('#' + playerId + '-elapsed').css('width', Math.round((100 * p.getCurrentTime()) / p.getDuration()) + '%');
					}, 1000);
				}
				$('#' + playerId + '-wrapper').addClass('playing');
			},
			onPause: function(p) {
				$('#' + playerId + '-wrapper').removeClass('playing');
				self.clearPlayerInterval(playerId);
			},
			onEnd: function(p) {
				$('#' + playerId + '-wrapper').removeClass('playing');
				$('#' + playerId + '-elapsed').css('width', '100%');
				self.clearPlayerInterval(playerId);
				self.carousel.startAuto();
			}
		});
		
		$('#' + playerId + '-play').click(function(e) {
			e.preventDefault();
			self.players[playerId].togglePlay();
		});
		
		$('#' + playerId + '-mute').click(function(e) {
			e.preventDefault();
			self.players[playerId].toggleMute();
			$('#' + playerId + '-wrapper').toggleClass('muted');
		});
	},
	clearPlayerInterval: function(playerId) {
		clearInterval(this.playerIntervals[playerId]);
		this.playerIntervals[playerId] = null;
	},
	isPlayerPlaying: function() {
		return $('#' + this.carouselId + ' .playing').length > 0;
	}
};

