var popupLoaded;var closeable;var pluginURL;var hasVideo = false;var hasAudio = false;jQuery(document).ready(function($) { 	// Called by entry and exit popups	function PopupInit() {		// Get plugin URL for AJAX		if ($('input[name=pluginURL]').length > 0) pluginURL = $('input[name=pluginURL]').val();				// Display Popup	    if ($('#popup').length > 0) {	        $('#overlay').css('height', $(document).height()+'px');	        $('#overlay').show();		        // Only need to resize in JS if class=manual.  Otherwise is fullscreen or % which is dealt with purely in CSS	        $('.manual').css('top', (($(window).height()/2)-($('#popup').height()/2))+'px');	        $('.manual').css('left', (($(document).width()/2)-($('#popup').width()/2))+'px');				// Hide timer button?			if ($('input[name=timerHideCloseButton]').length > 0) {				if ($('input[name=timerHideCloseButton]').val() == '1') {					DisableClose();				} else {					EnableClose();				}			} else {				EnableClose();			}						$('#popup').show();						// Resize video + content panes?			if ($('#popup div.has-media').length > 0) {				var popupBodyWidth = $('#popup').width() - 40; // 40px is border + body padding				var videoWidth = $('#video').width();				var videoWrapperWidth = $('#video_wrapper').width();				$('#popup div.has-media').width(((videoWidth > videoWrapperWidth) ? videoWrapperWidth : videoWidth));				$('#popup div.has-content').width((popupBodyWidth - videoWidth - 10));				}		        popupLoaded = true;	    }		    	    // Close popup if close button clicked	    $('a.close').live('click', function(e) {	        e.preventDefault();	        ClosePopup();	    });	    	    // Close popup if overlay clicked	    $('#overlay').live('click', function(e) { ClosePopup(); });		    // Close popup if ESC key pressed	    $(document).keyup(function(e){			if (e.keyCode == 27 && popupLoaded) ClosePopup();	    });	    	    // Timer		if ($('input[name=timerDuration]').length > 0) {			var duration = $('input[name=timerDuration]').val();			var action = $('input[name=timerAction]').val();			var url = $('input[name=timerURL]').val();			if (duration > 0) {				// Check if a normal countdown or displayed				if ($('#popup span.countdown').length > 0) {					// Display					$('span.countdown').countdown({						until: +duration,						format: 'S',						layout: '{sn}',						onExpiry: function() {							DoAction(action, url)						}					});				} else {					// Standard								var t = setTimeout(function() {						DoAction(action, url);					}, (duration * 1000));				}			}		}			// Video		if ($('#popup #video').length > 0) {			hasVideo = true;			jwplayer('video').setup({				playlist: [					{ file: $('input[name=mediaURL]').val() }				],				wmode: 'transparent',				autostart: $('input[name=mediaAutoplay]').val(),				controlbar: $('input[name=mediaControlBar]').val(),				repeat: $('input[name=mediaRepeat]').val(),				modes: [					{ type: 'flash', src: $('input[name=playerURL]').val() },					{ type: 'html5' }				],				width: $('input[name=mediaWidth]').val(),				height: $('input[name=mediaHeight]').val()			}).onComplete(function() {	  			DoAction($('input[name=mediaAction]').val(), $('input[name=mediaDestinationURL]').val());	  		});		}				// Audio		if ($('#popup #audio').length > 0) {			hasAudio = true;			jwplayer('audio').setup({				playlist: [					{ file: $('input[name=mediaURL]').val() }				],				wmode: 'transparent',				autostart: $('input[name=mediaAutoplay]').val(),				controlbar: $('input[name=mediaControlBar]').val(),				modes: [					{ type: 'flash', src: $('input[name=playerURL]').val() },					{ type: 'html5' }				],				width: $('input[name=mediaWidth]').val(),				height: $('input[name=mediaHeight]').val()			}).onComplete(function() {	  			DoAction($('input[name=mediaAction]').val(), $('input[name=mediaDestinationURL]').val());	  		});	  			  		// If no controlbar required, hide the audio element completely, so no logo or other details still display	  		if ($('input[name=mediaControlBar]').val() == 'none') {	  			$('#popup #audio').css('width', '1px');	  			$('#popup #audio').css('height', '1px');	  			$('#popup #audio').css('overflow', 'hidden');	  		}		}				// Track clickthroughs from popup		$('#popupBody a').live('click', function(e) {			e.preventDefault();						var url = $(this).attr('href');						$.ajax({				type: 'GET',	  			url: pluginURL+'/ajax.php',	  			data: 'popupID='+$('input[name=popupID]').val(),	  			success: function(data) {	    			window.location.href = url;	  			}			});		});	}	// Exit popup	if ($('input[name=exitPopupID]').length > 0) {	 	$(window).bind('beforeunload', function() {	 		$.ajax({	 			url: pluginURL+'/popup.php',	 			data: 'popupID='+$('input[name=exitPopupID]').val(),	 			success: function(data) {	 				$('#wp-popit').html(data);	 				PopupInit();	 				$(window).unbind('beforeunload'); // So we dont repeat the exit popup	 			}	 		});	 			    	return $('input[name=exitPopupBrowserMessage]').val();	    }); 		// Unbind beforeunload if link is excluded	 	$('a').live('click', function(e) {	 		// Get external links and unbind prompt if we are visiting one of these domains	 		if ($('input[name=ignoreExternalLinks]').length > 0) {		 		var ignoreExternalLinks = $('input[name=ignoreExternalLinks]').val().split(',');		 		for (i in ignoreExternalLinks) {		 			if ($(this).attr('href').search(ignoreExternalLinks[i]) > 0) {		 				// Link is excluded, do not bind exit popup		 				window.location.href = $(this).attr('href');		 				$(window).unbind('beforeunload');		 			}		 		}	 		}		});	}	// Initialise popup    PopupInit();});function DoAction(action, url) {	// Enable close button, in case it has been hidden before	EnableClose();				// Perform action (either close or go to a URL)	switch (action) {		case 'close':			ClosePopup();			break;		case 'url':			window.location.href = url;			break;	}	}function EnableClose() {	jQuery('#popupMain').addClass('hasCloseButtonVisible');	jQuery('#popup a.close').show();	closeable = true;}function DisableClose() {	jQuery('#popupMain').removeClass('hasCloseButtonVisible');	jQuery('#popup a.close').hide();	closeable = false;	}function ClosePopup() {    if (!popupLoaded) return false;    if (!closeable) return false;	if (hasVideo) jwplayer('video').stop();	if (hasAudio) jwplayer('audio').stop();    jQuery('#overlay').remove();    jQuery('#popup').remove();    popupLoaded = false;     }
