function receiveOoyalaEvent(playerId, eventName, p) {
	switch(eventName) { 
    case 'playheadTimeChanged': 
    	mySetupPlayer.playerReady();
      return; 
    case 'stateChanged': 
      return; 
    case 'playComplete': 
    	mySetupPlayer.continuous();
		return;
    case 'currentItemEmbedCodeChanged':
    	mySetupPlayer.updateDetails(p);
      return; 
    case 'totalTimeChanged': 
    	mySetupPlayer.playerReady();
      return; 
    case 'embedCodeChanged': 
      return; 
    case 'volumeChanged': 
      return; 
    case 'loadComplete':  
    	mySetupPlayer.playerLoaded(playerId);
      return;   
  } 
} 

var setupPlayer = new Class({
	initialize: function() {
		this.myPlayerLoaded 	= false;
		this.myPlayerReady 		= false;
		this.myDomReady 		= false;
		this.runOnce 			= true;
	},
	playerLoaded: function(playerId) {
		this.myPlayerLoaded 	= true;
		this.myPlayerId 		= playerId;
		
	},
	playerReady: function() {
		if (this.runOnce) {
			this.runOnce 		= false;
			//setup Player elements
			this.myPlayer 			= $(this.myPlayerId);
			this.myPlaylist 		= this.myPlayer.getLineup();
			this.currentItem		= this.myPlayer.getCurrentItem();
	
			this.myPlayerReady = true;
			if (this.myDomReadyReady) {
				this.myPlayerReady = false;
				this.buildCarousel();
			}
		}
	},
	domReady: function(options) {

		//setup DOM elements
		this.myOptions 			= options;
		this.myDetails			= $(this.myOptions.detailsPane);
		this.defaultVideo		= this.myOptions.defaultVideo;
		this.myContinuousBtn 	= $(this.myOptions.continuousBtn);
		this.filmStripOptions	= this.myOptions.filmStripOptions;
		this.myContinuousPlay	= true;
		
		this.myDetailsNav		= this.myDetails.getElements('ul.nav li a');
		this.myCarousel 		= $(this.myOptions.carouselPane); 
	
		this.myDetailsNav.each(function(item, index){
			item.addEvent('click', function(e) {
				this.changeMedia(this.myPlaylist[item.getProperty('rel')],true);
				e.stop();
			}.bind(this));
		}.bind(this));
	
		//toggle looping
		this.myContinuousBtn.addEvent('click', function(e) {
			this.myContinuousBtn.toggleClass('on');
			if (this.myContinuousBtn.hasClass('on')) {
				this.myContinuousPlay = true
			} else {
				this.myContinuousPlay = false;
			}
			e.stop();
		}.bind(this));
	
		this.myDomReadyReady = true;
		if (this.myPlayerReady) {
			this.myDomReadyReady = false;
			this.buildCarousel();
		}

	},
	buildCarousel: function() {
		var carouselContainer 	= this.myCarousel.getElement('.viewport');
		var carouselList 		= new Element('ul', {'class':'filmstrip'}).inject(carouselContainer);
		this.myPlaylist.each(function(item, index){
			var new_item = new Element('li', {'html':
				'<a class="' +  + '" href="?mediaId=' + item.embedCode + '">'+
					'<img width="143" height="80" class="pic" src="'+this.myPlayer.getPromoFor(item.embedCode, 143,
80)+'" />'+
					'<h2>'+item.title+'</h2>'+
					'<p>'+item.description+'</p>'+
				'</a>'
			}).inject(carouselList);
			new_item.addEvent('click', function(e) {
				this.changeMedia(item,true);
				e.stop();
			}.bind(this))
		}.bind(this));
		
		//create filmstrip
		this.myFilmStrip = new FilmStrip(this.filmStripOptions);
		
		//deep link video
		if (this.defaultVideo != "") {
			this.myPlaylist.each(function(item, index){
				if (item.embedCode == this.defaultVideo) this.currentItem = item;
			}.bind(this));
			this.changeMedia(this.currentItem,true);
		} else {
			this.changeMedia(this.currentItem);
		}
	},	
	changeMedia: function(item,autoplay) {
		var myItem = item;
		this.myPlayer.changeCurrentItem(myItem.embedCode);

		if (autoplay) this.myPlayer.playMovie();
	
		this.updateDetails(item);
	},
	updateDetails: function(item) {	
		var myItem = item;
		this.myDetails.getElement('h1').innerHTML 	= myItem.title;
		this.myDetails.getElement('p').innerHTML 	= myItem.description;
		
		var currentIndex = 0;
						
		this.myPlaylist.each(function(item, index){
			if (item.embedCode == myItem.embedCode) currentIndex = index;
		});

		this.myDetailsNav[0].setProperty('rel',currentIndex-1);
		this.myDetailsNav[1].setProperty('rel',currentIndex+1);
		
		//hide next + prev if at end or beginning
		(currentIndex < 1) ? this.myDetailsNav[0].setStyle('visibility','hidden') : this.myDetailsNav[0].setStyle('visibility','visible');
		(currentIndex >= this.myPlaylist.length-1) ? this.myDetailsNav[1].setStyle('visibility','hidden') : this.myDetailsNav[1].setStyle('visibility','visible');
		
		var myCarouselItems = this.myCarousel.getElements('.filmstrip li a');
		myCarouselItems.each(function(item, index){
			item.removeClass('on');
		});

		myCarouselItems[currentIndex].addClass('on');
		this.myFilmStrip.goToItem(currentIndex);

	},
	continuous: function() {
		if (this.myContinuousPlay == false) {
			this.myPlayer.pauseMovie().delay(1000);
		}
	}
});   
 

