var player;//a reference to the experience module
var videoPlayer;  //a reference to the videoPlayer module
//body#gbl_iframe_email_bgrnd {background: white url(/ext/inc/gd/images/bbcard_optin_bgrnd.jpg) top center no-repeat;font-family:arial, san-serif;}

var emailgate;
var emailOptInUrl;
var emailGateCloseable;
$(function() {
//var pageURLSegment = jQuery.url.segment(0); // get the first segment from the url path (ie. /girls/)
emailOptInUrl = '/ext/email/optin.html';
$('<div id="girlroadblock"><\/div>').appendTo("body");
if (emailGateCloseable == "" || emailGateCloseable == null || emailGateCloseable == false){
	emailGateCloseable = false;
};

if (emailGateCloseable == true){
	createCloseButton();
	//alert("emailgatecloseable is set to true")
};

emailgate = $("#girlroadblock").overlay({
		top: 100,
	 	left: 'center',
		expose: {
	 		color: '#000',
			loadSpeed: 0,
			opacity: 0.9
		},
		close: 'noclose',
		absolute: false,
		oneInstance: false,
		onLoad: function(){
		//	$('#girlroadblock').css({'position':'fixed'});
		},
		onBeforeLoad: function() {
			if(videoPlayer != null){			
					if(videoPlayer.isPlaying()){
			            videoPlayer.pause(true);
			        } 
				}
		},
		closeOnClick: emailGateCloseable,  
		closeOnEsc: emailGateCloseable,
		api: true
	});
			
	// add click event to overlayCloseBtn that gets created in the success function of the email form 	
	$('#overlayCloseBtn').live("click", function(){
			closeEmailgateOverlay()
	});
	
}); // end ready function	
	

function loadEmailGate(emailGateType, emailPla, emailSkin, emailFormName, emailTime, emailGateCloseable){
	
	
/*
	$.isUserRegistered("article");
	$.isUserRegistered("girl");
	$.isBlocked("girl");
*/
	
	emailTime = parseInt(emailTime); 
	

		
		
		if (emailGateType == "hardblock"){
			$.createCockBlock(  "girl", 
			emailTime,  
			31536000,  
			1,  
			isBlockedCallback,  
			false 
			);
		}
		
		if (!($.isUserRegistered("girl"))){
			if (emailGateType == "softblock"){
				if(!($.isUserRegistered("article"))){
					
					$.removeCockBlock("article");
					$.createCockBlock(  "article", 
					emailTime,  
					31536000,  
					1,  
					isPartialCallback,  
					false 
					);
				
				}
			}
		}

}
	
	
		
 
 	// the girl cookie will be our prevailing cookie.  The article session cookie doesn't prevent girl roadblocks.  Girl Roadblocks will prevent article session cookies. In the success function of the email form, this gets set and a close button appears 
	
function createRegisteredUser(){
	createCloseButton();
	cookieOptions = {domain: ".playboy.com", path: "/", expires: 365};
	$.setIsUserRegistered( "girl", true, cookieOptions );
}
	
	
function openEmailgateOverlay(){
		emailgate.load();
}

function createCloseButton(){
$('#girlroadblock').append('<div id="overlayCloseBtn"></div>');
}
	
function closeEmailgateOverlay() {
	emailgate.close();	
	$("#girlroadblock").remove();
}
	
	
	
// standard Girls CockBlock		
function isBlockedCallback(cockBlockName, cockBlockUnregisteredDelaySeconds, cockBlockRegisteredDelaySeconds, cockBlockUnregisteredDate, cockBlockRegisteredDate, isRegistered, isBlocked){

	var a = $("#exposeMask").css("display"); //CHECK TO SEE IF AN OVERLAY IS ALREADY OPEN
	var b = $.cookie("pbModal"); //CHECK TO SEE IF EMAILGATE HAS FIRED ALREADY
	var c = $.cookie("girlIsUserRegistered"); //CHECK TO SEE IF USER IS LOGGED IN
	
	//alert(b);
	if (b){
		if (a == 'block' || b.indexOf("stopcockblock") > -1 || c == 'true') {
		return (false);
	} else { 
		var currentDate = Date.parse(new Date());
	
		openEmailgateOverlay();
		
		emailgate.onLoad(function(){					
			$('<iframe src="'+ emailOptInUrl +'?campaign='+ cockBlockName+'&pla='+emailPla+'&skin='+emailSkin+'&formName='+emailFormName+'" width="970" height="525" bgcolor="#fff" frameborder="0" id="roadblock_overlay"><\/iframe>').appendTo("#girlroadblock");
		});
	}
	}
	
	
	
}	


function isPartialCallback(cockBlockName, cockBlockUnregisteredDelaySeconds, cockBlockRegisteredDelaySeconds, cockBlockUnregisteredDate, cockBlockRegisteredDate, isRegistered, isBlocked){
	
	var currentDate = Date.parse(new Date());
	
	openEmailgateOverlay();
	
	emailgate.onLoad(function(){	
		$('<iframe src="'+ emailOptInUrl +'?campaign='+ cockBlockName+'&pla='+emailPla+'&skin='+emailSkin+'&formName='+emailFormName+'" width="970" height="525" bgcolor="#fff" frameborder="0" id="roadblock_overlay"><\/iframe>').appendTo("#girlroadblock");	
	});

	
	emailgate.onClose(function(){
		var cookieOptions = {domain: ".playboy.com", path: "/"};
		$.setIsUserRegistered( cockBlockName, true, cookieOptions );
	});
	
}	
	

function onTemplateLoaded(experienceId) {
		player = brightcove.getExperience(experienceId);
		videoPlayer = player.getModule(APIModules.VIDEO_PLAYER);
		pauseState = false;
	}
	
function videostop(){
	
		if(videoPlayer.isPlaying()){
		videoPlayer.pause(true);
		}else{
		
		}
	}
function videoplay(){
		
		if(videoPlayer.isPlaying()){
		}else{
		videoPlayer.pause(false);
		}
	}


	
