/**
 * YouTubeCarousel.js
 *
 * @version 1.0
 * @author Maurice Snip <mauricesnip at hotmail dot com>
 * @uses swfobject.js <http://code.google.com/p/swfobject/>
 */
function YouTubeCarousel(el, options) {
	this.el = $(el);
	this.options = options;
	this.carousel = null;
	this.players = [];
	this.init();
}

YouTubeCarousel.prototype = {

	/**
	 * Initialize
	 */
	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,
                easing: 'swing',
				controlSelectedClass: 'selected',
				controlSelector: '#spotlight-controls li',
                initCallback: null,
                reloadCallback: null,
                itemVisibleInCallback: null
			}, this.options || {});

			// Create YouTube players
			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();
					}
				}));
			});

			// Create carousel
			this.el.jcarousel({
                scroll: 1,
				animation: self.options.animation,
				auto: self.options.auto,
				easing: self.options.easing,
				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();
						}
					});
                    if(typeof(self.options.initCallback) === 'function') {
                        self.options.initCallback(carousel);
                    }
				},
                reloadCallback: function(carousel) {
                    if(typeof(self.options.initCallback) === 'function') {
                        self.options.initCallback(carousel);
                    }
                },
				itemVisibleInCallback: {
					onBeforeAnimation: function(carousel, slide, index, state) {
						$(self.options.controlSelector + ':nth-child(' + index + ')').addClass(self.options.controlSelectedClass);
                        if(self.options.itemVisibleInCallback !== null && typeof(self.options.itemVisibleInCallback.onBeforeAnimation) === 'function') {
                            self.options.itemVisibleInCallback.onBeforeAnimation(carousel, slide, index, state, self.isPlayerSlide(slide));
                        }
					},
					onAfterAnimation: function(carousel, slide, index, state) {
						if(self.isPlayerSlide(slide)) {
							var player = self.getPlayerFromSlide(slide);

							if(player.ready) {
								player.playVideo();
							}
							else {
								var poller = setInterval(function() {
									if(player.ready) {
										player.playVideo();
										clearInterval(poller);
										delete poller;
									}
								}, 10);
							}
						}
					}
				},
				itemVisibleOutCallback: {
					onBeforeAnimation: function(carousel, slide, index, state) {
						$(self.options.controlSelector+ ':nth-child(' + index + ')').removeClass(self.options.controlSelectedClass);

						if(self.isPlayerSlide(slide)) {
							self.getPlayerFromSlide(slide).pauseVideo();
						}
					}
				}
			});
		}
	},

	/**
	 * Returns true if the slide has a YouTube player, false otherwise
	 *
	 * @param {Object} slide The slide to check
	 * @return {Boolean} True if the slide has a YouTube player, false otherwise
	 */
	isPlayerSlide: function(slide) {
		return $(slide).find('.player').length;
	},

	/**
	 * Returns true if a YouTube player is playing, false otherwise
	 *
	 * @return {Boolean} True if a YouTube player is playing, false otherwise
	 */
	isPlayerPlaying: function() {
		return this.el.find('.playing').length;
	},

	/**
	 * Get a YouTubePlayer object by slide
	 *
	 * @param {Object} slide The slide to check
	 * @return {YouTubePlayer} The YouTubePlayer object
	 */
	getPlayerFromSlide: function(slide) {
		var playerEl = $(slide).find('.player')[0];

		for(var i = 0; i < this.players.length; i++) {
			if(this.players[i].player[0] === playerEl) {
				return this.players[i];
			}
		}
	}
};
