function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function primeVideoPlayer() {
	//first send the total video count to the Flash player
	var movieCount= $('.video-path a').length;
	getFlashMovie("KPG-Video").sendMovieCountToFlash(movieCount);
		
	//next send the array of the path to every video in the playlist to the Flash player
	var movies = new Array();
	$('.video-path a').each(function (i) {
		movies[i] = $(this).attr("href");
		getFlashMovie("KPG-Video").sendMoviePathToFlash(movies[i]);
	});

	//next send the array of the title of every video in the playlist to the Flash player
	var movieTitles = new Array();
	$('.movie-image h5').each(function (i) {
		movieTitles[i] = $(this).text();
		getFlashMovie("KPG-Video").sendMovieTitleToFlash(movieTitles[i]);
	});
	//Finally tell Flash to play the first movie and set the scrubber to a specific position
	getFlashMovie("KPG-Video").startFirstMovie(10);
}	

$(document).ready(function() {
		
	//Embed the video player .swf	
	var flashvars = {};
	var params = {
		allowScriptAccess:'sameDomain',
		bgcolor: "#ffffff",
		quality: "high"
	};
	var attributes = {
		id: "KPG-Video",
		align: "middle",
		name: "SlideShow"
	};
	swfobject.embedSWF("/flash/video-player/KPG-Video.swf", "replacedContent", "440", "365", "9.0.0", "/flash/video-player/expressInstall.swf", flashvars, params, attributes);
	
	$("#controller").jCarouselLite({
        btnNext: "#scrollerNext",
        btnPrev: "#scrollerPrev",
		visible: "5",
		circular: false
    });

	$(".video-copy").fadeOut();
	$('#screen .video-copy').eq(0).attr('id', 'currentCopy').fadeIn();

	//The following variables and two functions add/remove the disabled class to the previous and next buttons as appropiate
	//Note that currentNews is initialized to the total number of movies displayed in the scroller
	var currentNews=5;
	var totalNews=$('#controller li').length;
	$('#scrollerPrev').addClass("disabled");
	$('#scrollerNext').click(function() {
		currentNews++;		
		if (currentNews==totalNews)
		{
			$('#scrollerNext').addClass("disabled");
		}
		else if (currentNews==6)
		{
			$('#scrollerPrev').removeClass("disabled")			
		}
		return false;
	});
	$('#scrollerPrev').click(function() {
		currentNews--;		
		if (currentNews==5)
		{
			$('#scrollerPrev').addClass("disabled");
		}
		else if (currentNews==(totalNews-1))
		{
			$('#scrollerNext').removeClass("disabled")			
		}
		return false;
	});

	$('#controller div.movie-image').click(function(event) {
		/*When a play list item is clicked the flv path is captured and used in a swfObject call that replaces the current movie,
			the previous current playlist item is unstyled and the new current one styled and made full opacity,
			and the player title and teaser updated  */
		$('#screen').removeClass('initial');
		$('#controller li#current').attr('id', '');
		$(this).parent().attr('id', 'current');
		$(this).parent().find('img').css("opacity", 1.0);
		var titleCopy = $(this).find('h5').html();
		$('#screen h1').html(titleCopy); // update a title with the current movie title
		var text = $("#controller div.movie-image").index(this);
		getFlashMovie("KPG-Video").sendNewMovieToFlash(text);
		var str = $('#controller div.movie-image').index(this);
		$('#screen #currentCopy').attr('id', '').fadeOut();
		$('#screen .video-copy').eq(str).attr('id', 'currentCopy').fadeIn();
	});	
	
	$('#controller li:first').attr('id', 'current'); //style first playlist item as current
	var titleCopy = $('#controller li:first h5').html(); //capture title first playlist item
	$('#screen h1').html(titleCopy);  // update a title with the current movie title

	$('#controller li').hover(
      function () {
				$(this).addClass('active');	
				$(this).find('img').css("opacity", 0.75);
      }, 
      function () {
				$(this).removeClass('active');	
				$(this).find('img').css("opacity", 1.0);
      }
    );	
}); // document ready

function getImageFromFlash(str) {
	var nextImage = $('.movie-image').find('img').eq(str).attr("src");
	return nextImage;
}

function updatePlaylist(str) {	
	$('#screen').removeClass('initial');
	$('#controller li#current').attr('id', '');
	$('#controller li').eq(str).attr('id', 'current');
	$('#screen #currentCopy').attr('id', '').fadeOut();
	$('#screen .video-copy').eq(str).attr('id', 'currentCopy').fadeIn();
	var titleCopy = $('#controller li').eq(str).find('h5').html();
	 $('#screen h1').html(titleCopy);
}
