;(function($) {
	$.fn.OoyalaPlayer = function(o) {
		o = $.extend({
			playerId: '',
			autoplay: false,
			detailsTitleSelector: '.detailsTitle',
			detailsDescSelector: '.detailsDesc',
			detailsBtnPrevSelector: '.detailsBtnPrev',
			detailsBtnNextSelector: '.detailsBtnNext',
			playlistContinousSelector: '.continous',
			playlistContainerSelector: '.playlistCont',
			playlistPrevBtnSelector: '.playlistPrevBtn',
			playlistNextBtnSelector: '.playlistNextBtn',
			playlistCurrentPage: '.playlistCurrentPage',
			playlistTotalPages: '.playlistTotalPages'
		}, o || {});
		
		/*return this.each( function() {*/
			return new $.OoyalaPlayer(o, this.get(0));
		/*});*/
	}
	
	$.OoyalaPlayer = function(o, container) {
		this.runonce = false;
		
		this.options = o;
		this.container = container;
		
		/* player specific stuff */
		this.oPlayer = null;
		this.oPlaylist = null;
		this.oCurrentItem = null;
		this.oCurrentIndex = 0;
		this.continuousPlay = true;
		
		/* navigation specific stuff */
		this.detailsElements = { title: null, description: null, prevBtn: null, nextBtn: null }
		this.playlistElements = { continousPlay: null, playlistContainer: null, playlistInstance: null }
		
		this.init = function(domPlayerId) {
			if ( !this.runonce ) {
				this.runonce = true;
				this.options.playerId = domPlayerId;
				
				this.oPlayer = $('#' + this.options.playerId)[0];
				this.oPlayer.hpptOoyalaPlayerInstance = this;
				this.oPlaylist = this.oPlayer.getLineup();
				this.oCurrentItem = this.oPlayer.getCurrentItem();

						
				this.detailsElements.title = $(this.options.detailsTitleSelector, this.container);
				this.detailsElements.description = $(this.options.detailsDescSelector, this.container);
				this.detailsElements.prevBtn = $(this.options.detailsBtnPrevSelector, this.container);
				this.detailsElements.nextBtn = $(this.options.detailsBtnNextSelector, this.container);
				this.playlistElements.continousPlay = $(this.options.playlistContinousSelector, this.container);
				this.playlistElements.playlistContainer = $(this.options.playlistContainerSelector, this.container);
				
				
				if ( this.oPlaylist && this.oCurrentItem ) {
					this.changeDetails();
					this.buildPlaylist();
				}
				
				var self =  this;
				this.detailsElements.prevBtn.click( function() { self.changeMovie(self.oCurrentIndex-1, true); });
				this.detailsElements.nextBtn.click( function() { self.changeMovie(self.oCurrentIndex+1, true); });
				
				$(this.options.playlistContinousSelector).toggle(
					function() {
						self.continuousPlay = false;
						$(this).toggleClass('checked');
					},
					function() {
						self.continuousPlay = true;
						$(this).toggleClass('checked');
					}
				);
				
				if ( this.options.autoplay ) this.oPlayer.playMovie();
			}
		}
		
		this.buildPlaylist = function() {
			var self = this;
			var list = $('<ul>'), liItem = null, link = null, img = null;
			$.each(this.oPlaylist, function(index, item) {
				liitem = $('<li>');
				link = $('<a>').appendTo( liitem );
				img = $('<img>').css({width: 143, height: 80});
				link.append( img ).append( $('<strong></strong>').text( item.title ) ).append( $('<p></p>').text( item.description ) );
				img.attr('src', self.oPlayer.getPromoFor(item.embedCode, 143, 80));
				link.click( function() {
					self.changeMovie(index, true);
				});
				list.append( liitem );
			});
			self.playlistElements.playlistContainer.append( list );
			self.playlistElements.playlistContainer.jCarouselLite( {
				 btnPrev: this.options.playlistPrevBtnSelector,
				 btnNext: this.options.playlistNextBtnSelector,
				 metaCurrentPage: this.options.playlistCurrentPage,
				 metaTotalPages: this.options.playlistTotalPages,
				 circular: false,
				 visible: 5,
				 scroll: 5,
				 mouseWheel: true,
				 easing: 'easeInCubic',
				 speed: 600,
				 initCallBack: function( instance ) { self.playlistElements.playlistInstance = instance; }
			 });
			this.updateNavigation();
		}
		
		this.changeMovie = function(itemIndex, changeMovie) {
			this.oCurrentIndex = itemIndex;
			this.oCurrentItem = this.oPlaylist[itemIndex];
			if ( changeMovie ) {
				this.oPlayer.changeCurrentItem( this.oCurrentItem.embedCode );
				this.oPlayer.playMovie();
			}
			this.changeDetails();
			this.updateNavigation();
		}
		
		this.changeDetails = function() {
			this.detailsElements.title.text(this.oCurrentItem.title);
			this.detailsElements.description.text(this.oCurrentItem.description);
		}
		
		this.updateNavigation = function() {
			this.detailsElements.prevBtn.css("display", (this.oCurrentIndex>0)?"block":"none");
			this.detailsElements.nextBtn.css("display", (this.oCurrentIndex+1 != this.oPlaylist.length)?"block":"none");
			
			this.playlistElements.playlistContainer.find("a").removeClass("active").eq(this.oCurrentIndex).addClass('active');
			// this.playlistElements.playlistInstance.navMethod( this.oCurrentIndex );
		}
		
		this.continousPlay = function() {
			 if (this.continuousPlay == false) {
				this.changeMovie( this.oCurrentIndex+1, false );
				setTimeout( this.oPlayer.pauseMovie(), 1000);
			} else {
				this.changeMovie( this.oCurrentIndex+1, true );
			}
		}
		
		this.___pause = function(ms) {
			var date = new Date();
			curDate = null;
			do { var curDate = new Date(); }
			while (curDate - date < ms);
		};		
	}
})(jQuery);

var playerInstance = $('#videoSection').OoyalaPlayer({
	autoplay: true,
	detailsTitleSelector: '#mediaDetails h1',
	detailsDescSelector: '#mediaDetails p',
	detailsBtnPrevSelector: '.videoDescNav .prev a',
	detailsBtnNextSelector: '.videoDescNav .next a',
	playlistContinousSelector: '#movieThumbList .continuous',
	playlistContainerSelector: '#movieThumbList .viewport',
	playlistPrevBtnSelector: '#movieThumbList .oo_nav .btnPrev',
	playlistNextBtnSelector: '#movieThumbList .oo_nav .btnNext',
	playlistCurrentPage: '#movieThumbList .count .currentPage',
	playlistTotalPages: '#movieThumbList .count .numPages'
});

var OoyalaEventHander = {
	handleEvent: function( oPlayerId, event, params ) {
		switch (event) {
			case "loadComplete":
				return;
			case "playheadTimeChanged":
				playerInstance.init(oPlayerId);
				return;
			case "totalTimeChanged":
				playerInstance.init(oPlayerId);
				return;				
			case "playComplete":
				var hpptPlayerInstance = $('#'+oPlayerId)[0].hpptOoyalaPlayerInstance;
				if ( hpptPlayerInstance ) hpptPlayerInstance.continousPlay();
				return;
			default:
				return;
		}
	}
}
