var player, player_f;
PlayerManager.addCallback('video_player', function(manager){
	player = manager;
	if(player_f){
		initFooty(player, player_f);
	}
});
PlayerManager.addCallback('video_player_footy', function(manager){
	player_f = manager;
	if(player){
		initFooty(player, player_f);
	}
});
function initFooty(main_player, footy_player){
	// when the footy player stops, we fake the endscreen, remove the overlay and unpause the page player
	$(footy_player).bind(PlayerManager.EVENT_COMPLETE, function(){
		$('#videoPlayerFootyOver').show();
		$('#overlay').remove();
		main_player.unpause();
	});
	// when the footy player plays, we add the overlay and pause the page player
	$(footy_player).bind(PlayerManager.EVENT_PLAYING, function(){
		$('#add_overlay').click();
		main_player.pause();
	});
	// when the footy player pauses, we remove the overlay and unpause the page player
	$(footy_player).bind(PlayerManager.EVENT_PAUSED, function(){
		$('#overlay').remove();
		main_player.unpause();
	});
}
$(document).ready(function(){

	var opaUnit = document.getElementById("adExpand");
	if (opaUnit == null)
	{
	// if there's no opa, we're going to auto expand and set a cookie to do so only once a day
		// onload for cookie check
		if(!getCookie('footy_expand')) {
		// if cookie not found expand the footy and create it
		$('#footy_ad').hide().slideDown('slow');
			$('div.footy').addClass('expanded');
			setTimeout(function(){
				$('#footy_ad').slideUp('slow');
			}, 8000);
			setTimeout(function(){
			   $('div.footy').removeClass('expanded');
			}, 9000);
			setCookie('footy_expand', 'footy_expand', 1); // 24 hours
		}
	}

	// expand and contract footy
	$("#footy_bar").click(function () {
		if ($("#footy_ad").is(":hidden")) {
				$('div.footy').addClass('expanded');
	   			//$('#footy_ad').slideDown('slow');
	   			$('#footy_ad').addClass('footy_ad_open');
	   			$('#overlay').remove();
		} else {
	   			//$('#footy_ad').slideUp('slow');
	   			$('#footy_ad').removeClass('footy_ad_open');
	   			$('#overlay').remove();
				setTimeout(function(){
				   $('div.footy').removeClass('expanded');
				}, 1000);
		}
	});
	// clicking on right side removes the overlay
	$('#footy_right').click(function(){
		$('#overlay').remove();
		if (player_f){
			player_f.pause();
		}
		if (player){
			player.unpause();
		}
	});
	//overlay click function
	$('#add_overlay').click(function(){
   		var docHeight = $(document).height();
   		$("body").append("<div id='overlay'></div>");
   		$("#overlay")
      		.height(docHeight)
      		.css({
			'opacity' : 0.6,
			'position': 'fixed',
			'top': 0,
			'left': 0,
			'background-color': 'black',
			'width': '100%',
			'z-index': 5000
		});

	});

});

