/**
 * This file includes all methods needed to control the flash video player
 */

var showingVideo = false;

$(function(){
	
	$('#whitebox').css({top: getWhiteBoxHeight('negative')});
	
	var h = -($('#overlay').height()+25) + 'px';
	$('#overlay').css('top', h);
	
	//cant do this because people may need to scroll down to close player in smaller screens
//	$(window).scroll(function(){
//		centerVideo();
//	}); 
	
});

/*
 * public methods
 */
function showVideo(title, flv_path)
{
	showOverlay(title);
	onVideoLoading();
	loadVideo(flv_path);
}

//tell video player which video to load
function loadVideo(flv_path)
{
	getFlexApp('VideoPlayer').loadVideo(flv_path);
}

function hideOverlay()
{
	getFlexApp('VideoPlayer').unLoadVideo();
	
	setTimeout(function(){
		$('#whitebox #flip #video_close_en').css('display', 'none');
		$('#whitebox #flip #video_close_fr').css('display', 'none');
		
		$('#whitebox #flip').slideUp(150, function(){
			$('#whitebox').animate({top: getWhiteBoxHeight('negative')}, 250, function(){
				$('#blackbox').fadeOut(250, function(){
					var h = -($('#overlay').height()+25) + 'px';
					$('#overlay').css('top', h);
				});
			});
		});
	}, 500);
	
	showingVideo = false;
}

//this is called from the flash video player after the video has been loaded
function onVideoLoaded()
{
	$('#whitebox #video_container #loader').fadeOut(150, function(){
		$('#whitebox #video_container #display').css({display: 'inline', opacity: '1', filter: 'alpha(opacity:100)'});
	});
}


/*
 * private methods
 */
function showOverlay(title)
{
	$('#overlay').css('top', '0px');
	var top = String(($(window).height() - 480)/2 + $(window).scrollTop()) + 'px';
	$("#whitebox").css('top', getWhiteBoxHeight('negative')).animate({top: top}, 500, function(){
		setTimeout(function(){
			$('#whitebox #flip').slideDown(350, function(){
				$('#whitebox #flip #video_close_en').css('display', 'inline');
				$('#whitebox #flip #video_close_fr').css('display', 'inline');
			});
		}, 250)
	});
	
	$('#overlay').css('display', 'inline');
	$('#blackbox').css('display', 'inline');
	$('#whitebox').css('display', 'inline');
	
	$('#overlay').css('height', $(document).height());
	$('#blackbox').css('height', $(document).height());
	
	$('#blackbox').fadeTo(0, 0, function(){
		$('#blackbox').fadeTo(250, 0.85);
	});
	
	$('#whitebox #caption').text(title);
	
	showingVideo = true;
}

function onVideoLoading()
{
	$('#whitebox #video_container #loader').css('display', 'inline');
	$('#whitebox #video_container #display').css({opacity: '0', filter: 'alpha(opacity=0)'});
}

//Returns the appropriate reference depending on the browser.
function getFlexApp(movieName)
{
	if (window.document[movieName]) 
	{
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet")==-1)
	{
		if (document.embeds && document.embeds[movieName])
			return document.embeds[movieName]; 
	}
	else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
	{
		return document.getElementById(movieName);
	}
}

function getWhiteBoxHeight(direction)
{
	if(direction == 'negative')
		return '-' + $('#whitebox').height() + 'px';
	else
		return $('#whitebox').height() + 'px';
}

//function centerVideo()
//{
//	if(showingVideo){
//		var top = String(($(window).height() - 480)/2 + $(window).scrollTop()) + 'px';
//		$('#whitebox').animate({top: top}, 250);
//	}
//}

