var overlayApi;
var sourceCode;
var theLinkHref;
var iframe_src;

$(function() {
				
	
	$.cookie('pbExitPop','showpop', {domain: '.playboy.com', path: '/'});
	
	$('<div id="regoverlay"><\div>').appendTo($('body'));
	$('#regoverlay').html('<div id="overlayCloseBtn"></div>');
	
	// Adding RegForm Overlay to all the links on the page that have tours.playboy.com/join/cc/index.html

	/*
if (($.cookie('gcc') == "US") || ($.cookie('gcc') == "CA") ){

		theLinkHref = $('a').attr("href");
			$('a').each(function(){ 
		
			theLinkHref = $(this).attr("href"); 
			if( theLinkHref != null && theLinkHref.indexOf("http://tours.playboy.com/join/cc/index.html")>=0){
		     		$(this).attr("rel", "getsOverlay");
		     		//$(this).css({"color":"#ff0000"})
		     		
		    	} 
		         
	         
		});

	}
*/


	$("a[rel='getsOverlay']").live('click', function() {
		cmSetProduction();
		cmCreateConversionEventTag ("Playboy_Com_Overlay_Reg_Form","1","Registration","0");
		
		var overlayOpenerHref = this.href;
		sourceCode = $.url.setUrl(overlayOpenerHref).param("source");
		
		openOverlay();
		
		return false;
		
	});


	iframe_src = 'https://secure2.playboy.com/join/cc/reg_v2/form.html?source=';
	
	
	
	$("#regoverlay").overlay({
		top: 100,
	 	left: 'center',
	 	//absolute: true,
		expose: {
	 		color: '#000',
			loadSpeed: 100,
			opacity: 0.6,
			zIndex: 9999
		},
		fixed: false,
		close: ".noclose",
		oneInstance: false,
		onBeforeLoad: function(){
				if(videoPlayer != null){			
						if(videoPlayer.isPlaying()){
				            videoPlayer.pause(true);
				        } 
					}
		
		},
		onLoad: function() {
			$('<iframe src="'+ iframe_src +''+ sourceCode+'" width="920" height="660" bgcolor="#fff"  frameborder="0" id="reg_v2_overlay"><\/iframe>').appendTo($('#regoverlay'));
		},
		onClose: function() {
			$('#reg_v2_overlay').remove();
		},
		closeOnClick: false,  
		closeOnEsc: false, 
		
		api: true
	});
		
	overlayApi = $("#regoverlay").overlay();

	//This handled thru GWO - don't uncomment this out.  ask fregly how to pin to the overlay using GWO.
	//	$('#gbl_head_un_loggedoff_joinnow').live("click", function() {
	//		openOverlay();
	//  	return false;
	//	
	//	});
	
	
		$('#overlayCloseBtn').live("click", function(){
		if ($.cookie('pbExitPop') == 'noshowpop') {
			
			closeOverlay(); 
			setTimeout("removeExitPop()",500); // delay the removal of the exit pop link (better experience)

			
		 } 
	
		if ($.cookie('pbExitPop') == 'showpop') {
		 	
		 	displayExitPop();	
		 	$.cookie('pbExitPop','noshowpop', {domain: '.playboy.com', path: '/'});
		 } 
	
		
		});
	
}); // End Doc Ready
 

function openOverlay(){
	$('#regoverlay').overlay().load();
}

function closeOverlay() {
	$('#regoverlay').overlay().close();	
}

function displayExitPop() {
	$('<a target="_top" id="exitPopframe" href="https://secure2.playboy.com/join/partner.do?partner_name=isvod&p_instrument=1&join_message=0&show_acct=1&offer=CC60R95&source=CC_WEBJP_20100917_GEN_REG_PRSP_EXIT_SVTY_NA_NEWREG" ></a>').appendTo($('#regoverlay'));
	
}

function removeExitPop(){
	$('#exitPopframe').remove();
}


// Brightcove Video functions if video is playing/pause it for overlay
 			
	var player;//a reference to the experience module
	var videoPlayer;  //a reference to the videoPlayer module


    function onTemplateLoaded(experienceId) {
        player = brightcove.getExperience(experienceId);
        videoPlayer = player.getModule(APIModules.VIDEO_PLAYER);
        pauseState = false;
    }
 
    function videostop(){

        if(videoPlayer.isPlaying()){
            videoPlayer.pause(true);
        }else{
            //videoPlayer.pause(false);
        }
    }
    function videoplay(){

        if(videoPlayer.isPlaying()){
            //videoPlayer.pause(true);
        }else{
            videoPlayer.pause(false);
        }
	}
 

