/* 
 * Lovehoney Sex Toys TV video interaction code
 * Author: Seb Duggan, 
 * Date: 16/11/2007
 * 
 * Interacts with flvplayer and receives status updates from it 
 */

function setupPlaylist(videoPlayer) {
	$('.video_thumbnails li.remote_control a').click(function(event){
		event.preventDefault();
		var parentLI = $(this).parent();
		var videoToLoad = parentLI.prevAll('').length;

		highlightThumbnail( parseInt(parentLI.attr('id').split('_')[1]) );

		videoPlayer.playlistItem(videoToLoad);
		$('body').scrollTo();
	});

	var videothumbs = $('.video_thumbnails li');
	var videothumbs_height = 0;
	videothumbs.each(function(){
		videothumbs_height = Math.max(videothumbs_height, $(this).height());
	});
	videothumbs.height(videothumbs_height);
}


jQuery.fn.extend({
  scrollTo : function(speed, easing) {
    return this.each(function() {
      var targetOffset = $(this).offset().top;
      $('html,body').animate({scrollTop: targetOffset}, speed, easing);
    });
  }
});

/*
var player = null; 
var currentVideo = null;
function playerReady(thePlayer) { 
	player = window.document[thePlayer.id]; 
	try {
	player.addModelListener("TIME", "timeTracker");
	player.addControllerListener("ITEM", "playlistTracker");
	}
	catch(ex)
	{
	setTimeout(function(){playerReady(thePlayer)}, 500);
	}
}


function playlistTracker(obj){
	currentItem = obj.index;
	setTimeout("changeVideo(currentItem, player)",100);
}
*/

function timeTracker(obj, currentVideo) { 
	// Loop over cue points array
	for (var i=0; i<arr_cues.length; i++) {
		if ( currentVideo == arr_cues[i][0] && obj.position >= arr_cues[i][1] && currentProduct != arr_cues[i][2] ) {
			if (arr_cues.length > i+1 &&
					( ( obj.position < arr_cues[i+1][1] && currentVideo == arr_cues[i+1][0] ) || currentVideo != arr_cues[i+1][0] )
					|| arr_cues.length == i+1) {
				// We've found the cue point - change product and exit loop
				changeProduct(arr_cues[i][2]);
				break;
			}
		}
	}
}

function changeProduct(prodid) {
	// Set the current product global variable
	currentProduct = prodid;

	// Load product info by AJAX and display
	$('#video_productdisplay').load('/tv/product.cfm?p='+currentProduct);	
};

function changeVideo(currentVideo) {

	// Update the video title and blah
	$('#movietitle').html($('#title_'+currentVideo).html());
	$('#video_text').html('<br />'+$('#video_'+currentVideo+'_text').html());

	// Show the presenter + link if defined
	var presenter = $('#video_'+currentVideo+'_presenter');
	if (presenter.length && presenter.text().length) {
		$('#video_presenter').html('Presenter: <a href="/tv/'+presenter.text().toLowerCase().replace(' ','-')+'/">'+presenter.text()+'</a><br /><br />');
	} else {
		$('#video_presenter').html('');
	}
	
	// If there's a link to an external YouTube video, display the code
	var external = $('#video_'+currentVideo+'_external');
	if (external.length && external.html().length) {
		$('#video_external').val(external.html()).parent().show();
	} else {
		$('#video_external').val('').parent().hide();
	}
	
	// Highlight the video now playing
	highlightThumbnail(currentVideo);
};

function highlightThumbnail(vid) {
	$('ul.video_thumbnails li').removeClass('nowplaying');
	$('#thumbnail_'+vid).addClass('nowplaying');
}
