<?php
/* wppa-scripts.php
* Package: wp-photo-album-plus
*
* This file contains all functions for activating javascript
*
* Version 8.0.08.002
*/

// Place all wppa related js declarations in the header, both admin and frontend
function wppa_initialize_javascript() {
global $wppa_api_version;
global $wppa_lang;
global $wppa_session;
global $wpdb;

	/* wppa-utils.js */
	$result = '
	wppaDebugCounter = 0,';

	/* wppa.js */
	$result .= '
	wppaIsChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime),
	wppaOvlActivePanorama = 0,';

	/* Global and slideshow vars */
	$result .= '
	wppaFullValign = [],
	wppaFullHalign = [],
	wppaFullFrameDelta = [],
	wppaAutoColumnWidth = [],
	wppaAutoColumnFrac = [],
	wppaFadeInAfterFadeOut = false,
	wppaPreambule = [],
	wppaThumbnailPitch = [],
	wppaFilmStripLength = [],
	wppaFilmStripMargin = [],
	wppaFilmStripAreaDelta = [],
	wppaIsMini = [],
	wppaPortraitOnly = [],
	wppaLightBox = [],
	wppaSlideInitRunning = [],
	wppaSlidePause = [],
	wppaSlideBlank = [],
	wppaFilmPageSize = [],
	wppaAspectRatio = [],
	wppaFullSize = [],
	wppaLightboxSingle = [],
	wppaSiteUrl = "' . site_url() . '",
	wppaMasonryCols = [],
	wppaVideoPlaying = [],
	wppaAudioPlaying = [],
	wppaHis = 0,
	wppaStartHtml = [],
	wppaCanAjaxRender = false,
	wppaCanPushState = false,
	wppaMaxOccur = 0,
	wppaFirstOccur = 0,
	wppaUsePhotoNamesInUrls = false,
	wppaShareHideWhenRunning = false,
	wppaCommentRequiredAfterVote = true,
	wppaTopMoc = 0,
	wppaColWidth = [],
	wppaMCRWidth = [],
	wppaFotomotoHideWhenRunning = false,
	wppaFotomotoMinWidth = 400,
	wppaPhotoView = [],
	wppaBackgroundColorImage = "",
	wppaPopupLinkType = "",
	wppaPopupOnclick = [],
	wppaThumbTargetBlank = false,
	wppaRel = "rel",
	wppaEditPhotoWidth = "960",
	wppaThemeStyles = "",
	wppaStickyHeaderHeight = 0,
	wppaModalBgColor = "#ffffff",
	wppaBoxRadius = 0,
	wppaUploadEdit = "none",
	wppaPageArg = "",
	wppaCoverImageResponsive = [],
	wppaSearchBoxSelItems = [],
	wppaSlideWrap = [],
	wppaHideRightClick = false,
	wppaGeoZoom = 10,
	wppaAreaMaxFrac = 1.0,
	wppaNiceScroll = false,
	wppaIconSizeNormal = "default",
	wppaIconSizeStars = 24,
	wppaFilmInit = [],
	wppaResizeEndDelay = 200,
	wppaScrollEndDelay = 200,
	wppaArtmonkeyFileNotSource = false,
	wppaRequestInfoDialogText = "Please specify your question",
	wppaGlobalFsIconSize = 32,
	wppaFsFillcolor = "#999999",
	wppaFsBgcolor = "transparent",
	_wppaId = [],
	_wppaRealId = [],
	_wppaAvg = [],
	_wppaDisc = [],
	_wppaMyr = [],
	_wppaVRU = [],
	_wppaLinkUrl = [],
	_wppaLinkTitle = [],
	_wppaLinkTarget = [],
	_wppaCommentHtml = [],
	_wppaIptcHtml = [],
	_wppaExifHtml = [],
	_wppaToTheSame = false,
	_wppaSlides = [],
	_wppaNames = [],
	_wppaFilmThumbTitles = [],
	_wppaFullNames = [],
	_wppaDsc = [],
	_wppaOgDsc = [],
	_wppaCurIdx = [],
	_wppaNxtIdx = [],
	_wppaTimeOut = [],
	_wppaSSRuns = [],
	_wppaFg = [],
	_wppaTP = [],
	_wppaIsBusy = [],
	_wppaFirst = [],
	_wppaVoteInProgress = false,
	_wppaTextDelay = ' . wppa_opt( 'animation_speed' ) . ',
	_wppaUrl = [],
	_wppaSkipRated = [],
	_wppaLbTitle = [],
	_wppaStateCount = 0,
	_wppaDidGoto = [],
	_wppaShareUrl = [],
	_wppaShareHtml = [],
	_wppaFilmNoMove = [],
	_wppaHiresUrl = [],
	_wppaIsVideo = [],
	_wppaVideoHtml = [],
	_wppaAudioHtml = [],
	_wppaVideoNatWidth = [],
	_wppaVideoNatHeight = [],
	_wppaWaitTexts = [],
	_wppaImageAlt = [],
	_wppaLastIdx = [],
	_wppaStopping = [],
	_wppaFilename = [],
	__wppaOverruleRun = false,
	wppaOvlIdx = 0,
	wppaOvlFirst = true,
	wppaOvlKbHandler = "",
	wppaOvlSizeHandler = "",
	wppaOvlPadTop = 5,
	wppaOvlRunning = false,
	wppaOvlVideoPlaying = false,
	wppaOvlAudioPlaying = false,
	wppaOvlShowLegenda = true,
	wppaOvlShowStartStop = true,
	wppaOvlRadius = 0,
	wppaOvlBorderWidth = 16,
	wppaOvlOpen = false,
	wppaOvlClosing = false,
	wppaThumbSize = 100,
	wppaTfMargin = 4,
	wppaZoomData = [],
	wppaSphericData = [],
	wppaOvlTxtHeight = 36,
	wppaOvlOpacity = 0.8,
	wppaOvlOnclickType = "none",
	wppaOvlTheme = "black",
	wppaOvlAnimSpeed = 300,
	wppaOvlSlideSpeed = 3000,
	wppaOvlFontFamily = "Helvetica",
	wppaOvlFontSize = "10",
	wppaOvlFontColor = "",
	wppaOvlFontWeight = "bold",
	wppaOvlLineHeight = "12",
	wppaOvlShowCounter = true,
	wppaOvlIsVideo = false,
	wppaShowLegenda = "",
	wppaOvlFsPhotoId = 0,
	wppaPhotoId = 0,
	wppaOvlVideoStart = false,
	wppaOvlAudioStart = false,
	wppaLastIptc = "",
	wppaLastExif = "",
	wppaIsIpad = false,
	wppaSvgFillcolor = "gray",
	wppaSvgBgcolor = "transparent",
	wppaSvgCornerStyle = "light",
	wppaCoverSpacing = 8,
	wppaEasingSlide = "' . wppa_opt( 'easing_slide' ) . '",
	wppaEasingLightbox = "' . wppa_opt( 'easing_lightbox' ) . '",
	wppaEasingPopup = "' . wppa_opt( 'easing_popup' ) . '",
	wppaEasingDefault = "swing",';

	/* wppa-ajax-front.js */
	$result .= '
	wppaRenderAdd = false,
	wppaWaitForCounter = 0,';

	/* wppa-lightbox.js */
	$result .= '
	wppaIsVideo = false,
	wppaHasAudio = false,
	wppaOvlIsPdf = false,
	wppaOvlImgs = [],
	wppaKbHandlerInstalled = false,
	wppaOvlCurIdx = 0,
	wppaOvlSvgInverse = false,
	wppaOvlHasPanoramas = false,
	wppaGlobalOvlPanoramaId = 0,
	wppaOvlBrowseOnClick = false,
	wppaOvlGlobal = false,
	wppaWppaOverlayActivated = false,
	wppaOvlTimer = 0,
	wppaSavedContainerWidth = 0,
	wppaSavedContainerHeight = 0,';

	/* wppa-touch.js */
	$result .= '
	wppaTriggerElementID = null,
	wppaFingerCount = 0,
	wppaStartX = 0,
	wppaStartY = 0,
	wppaCurX = 0,
	wppaCurY = 0,
	wppaDeltaX = 0,
	wppaDeltaY = 0,
	wppaHorzDiff = 0,
	wppaVertDiff = 0,
	wppaMinLength = 72,
	wppaSwipeLength = 0,
	wppaSwipeAngle = null,
	wppaSwipeDirection = null,
	wppaSwipeOnLightbox = false,
	wppaSwipeMocc = 0,
	wppaMobileTimeStart = 0,';

	/* admin-scripts.js */
	$result .= '
	wppa_moveup_url = "#",
	wppa_import = "Import",
	wppa_update = "Update",
	wppaUploadToThisAlbum = "Upload to this album",
	wppaCloseText = "' . __( 'Close!', 'wp-photo-album-plus' ) . '",
	wppaCropper = [],';

	// Find ajax url
	$can_ajax = 'true';
	switch ( wppa_opt( 'ajax_method' ) ) {
		case 'normal':
			if ( is_admin() ) $ajax_url = site_url() . '/wp-admin/admin-ajax.php';
			else $ajax_url = site_url() . '/wppaajax';
			break;
		case 'wppaajax':
			$ajax_url = site_url() . '/wppaajax';
			break;
		case 'admin':
			$ajax_url = site_url() . '/wp-admin/admin-ajax.php';
			break;
		case 'extern':
			if ( is_admin() ) $ajax_url = site_url() . '/wp-admin/admin-ajax.php';
			else $ajax_url = WPPA_URL . '/wppa-ajax-front.php';
			break;
		case 'none':
			$ajax_url = site_url() . '/wp-admin/admin-ajax.php';
			$can_ajax = 'false'; 	// works on frontend only
			break;
	}

	/* Language and config specific inits */
	$result .= '
	wppaImageDirectory = "' . wppa_get_imgdir() . '",
	wppaWppaUrl = "' . wppa_get_wppa_url() . '",
	wppaIncludeUrl = "' . trim( includes_url(), '/' ) . '",
	wppaAjaxUrl = "' . $ajax_url . '",
	wppaUploadUrl = "' . WPPA_UPLOAD_URL . '",
	wppaIsIe = ' . ( wppa_is_ie() ? 'true' : 'false' ) . ',
	wppaIsSafari = ' . ( wppa_is_safari() ? 'true' : 'false' ) . ',
	wppaSlideshowNavigationType = "' . wppa_get_navigation_type() . '",
	wppaAudioHeight = ' . wppa_get_audio_control_height() . ',
	wppaFilmThumbTitle = "' . ( wppa_opt( 'film_linktype' ) == 'lightbox' ? wppa_zoom_in( false ) : __( 'Double click to start/stop slideshow running', 'wp-photo-album-plus' ) ) . '",
	wppaClickToView = "' . ( wppa_opt( 'film_linktype' ) == 'lightbox' ? wppa_zoom_in( false ) : __( 'Click to view', 'wp-photo-album-plus' ) ) . '",
	wppaLang = "' . $wppa_lang . '",
	wppaVoteForMe = "' . __( wppa_opt( 'vote_button_text' ), 'wp-photo-album-plus' ) . '",
	wppaVotedForMe = "' . __( wppa_opt( 'voted_button_text' ), 'wp-photo-album-plus' ) . '",
	wppaDownLoad = "' . __( 'Download', 'wp-photo-album-plus' ) . '",
	wppaSlideShow = "' . __( 'Slideshow', 'wp-photo-album-plus' ) . '",
	wppaPhoto = "' . __( 'Photo', 'wp-photo-album-plus' ) . '",
	wppaOf = "' . __( 'of', 'wp-photo-album-plus' ) . '",
	wppaNextPhoto = "' . __( 'Next photo', 'wp-photo-album-plus' ) . '",
	wppaPreviousPhoto = "' . __( 'Previous photo', 'wp-photo-album-plus' ) . '",
	wppaNextP = "' . __( 'Next', 'wp-photo-album-plus' ) . '",
	wppaPrevP = "' . __( 'Prev.', 'wp-photo-album-plus' ) . '",
	wppaAvgRating = "' . __( 'Average&nbsp;rating', 'wp-photo-album-plus' ) . '",
	wppaMyRating = "' . __( 'My&nbsp;rating', 'wp-photo-album-plus' ) . '",
	wppaAvgRat = "' . __( 'Avg.', 'wp-photo-album-plus' ) . '",
	wppaMyRat = "' . __( 'Mine', 'wp-photo-album-plus' ) . '",
	wppaDislikeMsg = "' . __( 'You marked this image as inappropriate.', 'wp-photo-album-plus' ) . '",
	wppaStart = "' . __( 'Start', 'wp-photo-album-plus' ) . '",
	wppaStop = "' . __( 'Stop', 'wp-photo-album-plus' ) . '",
	wppaPleaseName = "' . __( 'Please enter your name', 'wp-photo-album-plus' ) . '",
	wppaPleaseEmail = "' . __( 'Please enter a valid email address', 'wp-photo-album-plus' ) . '",
	wppaPleaseComment = "' . __( 'Please enter a comment', 'wp-photo-album-plus' ) . '",
	wppaProcessing = "' . __( 'Processing...', 'wp-photo-album-plus' ) . '",
	wppaDone = "' . __( 'Done!', 'wp-photo-album-plus' ) . '",
	wppaUploadFailed = "' . __( 'Upload failed', 'wp-photo-album-plus' ) . '",
	wppaServerError = "' . __( 'Server error.', 'wp-photo-album-plus' ) . '",
	wppaGlobalFsIconSize = "'.wppa_opt( 'nav_icon_size_global_fs' ) . '",
	wppaFsFillcolor = "'.wppa_opt( 'fs_svg_color' ) . '",
	wppaFsBgcolor = "'.wppa_opt( 'fs_svg_bg_color' ) . '",
	wppaFsPolicy = "' .  ( is_admin() ? 'none' : wppa_opt( 'fs_policy' ) ) . '",
	wppaNiceScroll = ' . ( wppa_switch( 'nicescroll' ) && ! wppa_is_mobile() ? 'true' : 'false' ) . ',
	wppaVersion = "'.$wppa_api_version.'",
	wppaDebug = '.( wppa_switch( 'allow_debug' ) ? 'true' : 'false' ) . ',
	wppaBackgroundColorImage = "'.wppa_opt( 'bgcolor_img' ) . '",
	wppaPopupLinkType = "'.wppa_opt( 'thumb_linktype' ) . '",
	wppaAnimationType = "'.wppa_opt( 'animation_type' ) . '",
	wppaAnimationSpeed = '.wppa_opt( 'animation_speed' ) . ',
	wppaThumbnailAreaDelta = '.wppa_get_thumbnail_area_delta() . ',
	wppaTextFrameDelta = '.wppa_get_textframe_delta() . ',
	wppaBoxDelta = '.wppa_get_box_delta() . ',
	wppaSlideShowTimeOut = '.wppa_opt( 'slideshow_timeout' ) . ',
	wppaFilmShowGlue = '.( wppa_switch( 'film_show_glue' ) ? 'true' : 'false' ) . ',
	wppaMiniTreshold = '.( wppa_opt( 'mini_treshold' ) ? wppa_opt( 'mini_treshold' ) : '0' ) . ',
	wppaRatingOnce = '.( wppa_switch( 'rating_change' ) || wppa_switch( 'rating_multi' ) ? 'false' : 'true' ) . ',
	wppaHideWhenEmpty = '.( wppa_switch( 'hide_when_empty' ) ? 'true' : 'false' ) . ',
	wppaBGcolorNumbar = "'.wppa_opt( 'bgcolor_numbar' ) . '",
	wppaBcolorNumbar = "'.wppa_opt( 'bcolor_numbar' ) . '",
	wppaBGcolorNumbarActive = "'.wppa_opt( 'bgcolor_numbar_active' ) . '",
	wppaBcolorNumbarActive = "'.wppa_opt( 'bcolor_numbar_active' ) . '",
	wppaFontFamilyNumbar = "'.wppa_opt( 'fontfamily_numbar' ) . '",
	wppaFontSizeNumbar = "'.wppa_opt( 'fontsize_numbar' ) . 'px",
	wppaFontColorNumbar = "'.wppa_opt( 'fontcolor_numbar' ) . '",
	wppaFontWeightNumbar = "'.wppa_opt( 'fontweight_numbar' ) . '",
	wppaFontFamilyNumbarActive = "'.wppa_opt( 'fontfamily_numbar_active' ) . '",
	wppaFontSizeNumbarActive = "'.wppa_opt( 'fontsize_numbar_active' ) . 'px",
	wppaFontColorNumbarActive = "'.wppa_opt( 'fontcolor_numbar_active' ) . '",
	wppaFontWeightNumbarActive = "'.wppa_opt( 'fontweight_numbar_active' ) . '",
	wppaNumbarMax = "'.wppa_opt( 'numbar_max' ) . '",
	wppaNextOnCallback = '.( wppa_switch( 'next_on_callback' ) ? 'true' : 'false' ) . ',
	wppaStarOpacity = '.str_replace(',', '.',( wppa_opt( 'star_opacity' )/'100' )) . ',
	wppaEmailRequired = "'.wppa_opt( 'comment_email_required' ) . '",
	wppaSlideBorderWidth = '.wppa_fbw().',
	wppaAllowAjax = '.$can_ajax.',
	wppaUsePhotoNamesInUrls = '.( wppa_switch( 'use_photo_names_in_urls' ) ? 'true' : 'false' ) . ',
	wppaThumbTargetBlank = '.( wppa_switch( 'thumb_blank' ) ? 'true' : 'false' ) . ',
	wppaRatingMax = '.wppa_opt( 'rating_max' ) . ',
	wppaRatingDisplayType = "'.wppa_opt( 'rating_display_type' ) . '",
	wppaRatingPrec = '.wppa_opt( 'rating_prec' ) . ',
	wppaStretch = '.( wppa_switch( 'enlarge' ) ? 'true' : 'false' ) . ',
	wppaMinThumbSpace = '.wppa_opt( 'tn_margin' ) . ',
	wppaThumbSpaceAuto = '.( wppa_switch( 'thumb_auto' ) ? 'true' : 'false' ) . ',
	wppaMagnifierCursor = "'.wppa_opt( 'magnifier' ) . '",
	wppaArtMonkyLink = "'.wppa_opt( 'art_monkey_link' ) . '",
	wppaAutoOpenComments = '.( wppa_switch( 'auto_open_comments' ) ? 'true' : 'false' ) . ',
	wppaUpdateAddressLine = '.( wppa_switch( 'update_addressline' ) ? 'true' : 'false' ) . ',
	wppaSlideSwipe = '.( wppa_switch( 'slide_swipe' ) ? 'true' : 'false' ) . ',
	wppaMaxCoverWidth = '.wppa_opt( 'max_cover_width' ) . ',
	wppaSlideToFullpopup = '.( wppa_opt( 'slideshow_linktype' ) == 'fullpopup' ? 'true' : 'false' ) . ',
	wppaComAltSize = '.wppa_opt( 'comten_alt_thumbsize' ) . ',
	wppaBumpViewCount = '.( wppa_switch( 'track_viewcounts' ) ? 'true' : 'false' ) . ',
	wppaBumpClickCount = '.( wppa_switch( 'track_clickcounts' ) ? 'true' : 'false' ) . ',
	wppaShareHideWhenRunning = '.( wppa_switch( 'share_hide_when_running' ) ? 'true' : 'false' ) . ',
	wppaFotomoto = '.( wppa_switch( 'fotomoto_on' ) ? 'true' : 'false' ) . ',
	wppaArtMonkeyButton = '.( wppa_opt( 'art_monkey_display' ) == 'button' ? 'true' : 'false' ) . ',
	wppaFotomotoHideWhenRunning = '.( wppa_switch( 'fotomoto_hide_when_running' ) ? 'true' : 'false' ) . ',
	wppaCommentRequiredAfterVote = '.( wppa_switch( 'vote_needs_comment' ) ? 'true' : 'false' ) . ',
	wppaFotomotoMinWidth = '.wppa_opt( 'fotomoto_min_width' ) . ',
	wppaShortQargs = '.( wppa_switch( 'use_short_qargs' ) ? 'true' : 'false' ) . ',
	wppaOvlHires = '.( wppa_switch( 'lb_hres' ) ? 'true' : 'false' ) . ',
	wppaSlideVideoStart = '.( wppa_switch( 'start_slide_video' ) ? 'true' : 'false' ) . ',
	wppaSlideAudioStart = '.( wppa_switch( 'start_slide_audio' ) ? 'true' : 'false' ) . ',
	wppaRel = "data-rel",
	wppaOvlRadius = '.wppa_opt( 'ovl_border_radius' ) . ',
	wppaOvlBorderWidth = '.wppa_opt( 'ovl_border_width' ) . ',
	wppaEditPhotoWidth = "'.(wppa_opt( 'upload_edit' ) == 'new' ? 500 : 960) . '",
	wppaThemeStyles = "'.(wppa_switch( 'upload_edit_theme_css' ) ? get_stylesheet_uri() : '' ) . '",
	wppaStickyHeaderHeight = '.wppa_opt( 'sticky_header_size' ) . ',
	wppaRenderModal = ' . ( wppa_switch( 'ajax_render_modal' ) ? 'true' : 'false' ) . ',
	wppaModalQuitImg = "url(' . wppa_get_imgdir( 'smallcross-' . wppa_opt( 'ovl_theme' ) . '.gif' ) . ' )",
	wppaBoxRadius = "' . wppa_opt( 'bradius' ) . '",
	wppaModalBgColor = "' . wppa_opt( 'bgcolor_modal' ) . '",
	wppaUploadEdit = "' . wppa_opt( 'upload_edit' ) . '",
	wppaSvgFillcolor = "' . wppa_opt( 'svg_color' ) . '",
	wppaSvgBgcolor = "' . wppa_opt( 'svg_bg_color' ) . '",
	wppaOvlSvgFillcolor = "' . wppa_opt( 'ovl_svg_color' ) . '",
	wppaOvlSvgBgcolor = "' . wppa_opt( 'ovl_svg_bg_color' ) . '",
	wppaSvgCornerStyle = "' . wppa_opt( 'icon_corner_style' ) . '",
	wppaHideRightClick = ' . ( wppa_switch( 'no_rightclick' ) ? 'true' : 'false' ) . ',
	wppaGeoZoom = ' . wppa_opt( 'geo_zoom' ) . ',
	wppaLazyLoad = ' . ( wppa_lazy() ? 'true' : 'false' ) . ',
	wppaAreaMaxFrac = ' . ( wppa_opt( 'area_size' ) < 1 ? wppa_opt( 'area_size' ) : 1.0 ) . ',
	wppaIconSizeNormal = "' . wppa_opt( 'nav_icon_size' ) . '",
	wppaIconSizeSlide = "' . wppa_opt( 'nav_icon_size_slide' ) . '",
	wppaResponseSpeed = ' . wppa_opt( 'response_speed' ) . ',
	wppaExtendedResizeCount = ' . wppa_opt( 'extended_resize_count' ) . ',
	wppaExtendedResizeDelay = ' . wppa_opt( 'extended_resize_delay' ) . ',
	wppaCoverSpacing = ' . wppa_opt( 'cover_spacing' ) . ',
	wppaFilmonlyContinuous = ' . ( wppa_switch( 'filmonly_continuous' ) ? 'true' : 'false' ) . ',
	wppaNoAnimateOnMobile = ' . ( wppa_switch( 'no_animate_on_mobile' ) ? 'true' : 'false' ) . ',
	wppaAjaxScroll = ' . ( wppa_switch( 'ajax_scroll' ) && ! is_admin() ? 'true' : 'false' ) . ',
	wppaThumbSize = ' . wppa_opt( 'thumbsize' ) . ',
	wppaTfMargin = ' . wppa_opt( 'tn_margin' ) . ',
	wppaArtmonkeyFileNotSource = ' . ( 	wppa_opt( 'art_monkey_link' ) == 'file' &&
										wppa_opt( 'art_monkey_display' ) == 'text' &&
										! wppa_switch( 'artmonkey_use_source' ) ? 'true' : 'false' ) . ',
	wppaRequestInfoDialogText = "' . wppa_opt( 'request_info_text' ) . '",
	wppaThumbAspect = ' . wppa_thumb_asp() . ',';

	/* Lightbox vars */
	$fontsize_lightbox = wppa_opt( 'fontsize_lightbox' ) ? wppa_opt( 'fontsize_lightbox' ) : '10';
	$d = wppa_switch( 'ovl_show_counter') ? 1 : 0;
	$ovlh = wppa_opt( 'ovl_txt_lines' ) == 'auto' ? 'auto' : ((wppa_opt( 'ovl_txt_lines' ) + $d) * ($fontsize_lightbox + 2));
	$lb_global = '';
	if ( wppa_switch( 'lightbox_global' ) && ! is_admin() ) {
		if ( wppa_switch( 'lightbox_global_set' ) ) {
			$lb_global = 'wppa[single]';
		}
		else {
			$lb_global = 'wppa';
		}
	}

	$result .= '
	wppaOvlTxtHeight = "'.$ovlh.'",
	wppaOvlOpacity = '.(wppa_opt( 'ovl_opacity' )/100).',
	wppaOvlOnclickType = "'.wppa_opt( 'ovl_onclick' ).'",
	wppaOvlTheme = "'.wppa_opt( 'ovl_theme' ).'",
	wppaOvlAnimSpeed = '.wppa_opt( 'ovl_anim' ).',
	wppaOvlSlideSpeed = '.wppa_opt( 'ovl_slide' ).',
	wppaVer4WindowWidth = 800,
	wppaVer4WindowHeight = 600,
	wppaOvlShowCounter = '.( wppa_switch( 'ovl_show_counter') ? 'true' : 'false' ).',
	'.( wppa_opt( 'fontfamily_lightbox' ) ? 'wppaOvlFontFamily = "'.wppa_opt( 'fontfamily_lightbox' ).'",' : '').'
	wppaOvlFontSize = "'.$fontsize_lightbox.'",
	'.( wppa_opt( 'fontcolor_lightbox' ) ? 'wppaOvlFontColor = "'.wppa_opt( 'fontcolor_lightbox' ).'",' : '').'
	'.( wppa_opt( 'fontweight_lightbox' ) ? 'wppaOvlFontWeight = "'.wppa_opt( 'fontweight_lightbox' ).'",' : '').'
	'.( wppa_opt( 'fontsize_lightbox' ) ? 'wppaOvlLineHeight = "'.(wppa_opt( 'fontsize_lightbox' ) + '2').'",' : '').'
	wppaOvlVideoStart = '.( wppa_switch( 'ovl_video_start' ) ? 'true' : 'false' ).',
	wppaOvlAudioStart = '.( wppa_switch( 'ovl_audio_start' ) ? 'true' : 'false' ).',
	wppaOvlShowStartStop = '.( wppa_switch( 'ovl_show_startstop' ) ? 'true' : 'false' ).',
	wppaIsMobile = '.( wppa_is_mobile() ? 'true' : 'false' ).',
	wppaIsIpad = '.( wppa_is_ipad() ? 'true' : 'false' ).',
	wppaOvlIconSize = "'.wppa_opt( 'nav_icon_size_lightbox' ).'px",
	wppaOvlBrowseOnClick = '.( wppa_switch( 'ovl_browse_on_click' ) ? 'true' : 'false' ).',
	wppaOvlGlobal = ' . ( $lb_global ? '"' . $lb_global . '"' : 'false' ) . ',
	wppaPhotoDirectory = "'.WPPA_UPLOAD_URL.'/",
	wppaThumbDirectory = "'.WPPA_UPLOAD_URL.'/thumbs/",
	wppaTempDirectory = "'.WPPA_UPLOAD_URL.'/temp/",
	wppaFontDirectory = "'.WPPA_UPLOAD_URL.'/fonts/",
	wppaNoPreview = "'.__('No Preview available', 'wp-photo-album-plus').'",
	wppaUIERR = "'.__('Unimplemented virtual album', 'wp-photo-album-plus').'",
	wppaTxtProcessing = "'.__('Processing...', 'wp-photo-album-plus').'",
	wppaTxtDone = "'.__('Done!', 'wp-photo-album-plus').'",
	wppaTxtErrUnable = "'.__( 'ERROR: unable to upload files.', 'wp-photo-album-plus' ).'",
	wppaOutputType = "' . wppa_opt( 'photo_shortcode_fe_type' ) . '";';

	// Tinymce photo
	if ( wppa_switch( 'photo_shortcode_enabled' ) ) {
		$id = $wpdb->get_var( "SELECT id FROM $wpdb->wppa_photos
							   WHERE ext <> 'xxx'
							   AND panorama = 0
							   ORDER BY timestamp DESC
							   LIMIT 1" );

		// Fake we are in a widget, to prevent wppa_get_picture_html() from bumping viewcount
		wppa( 'in_widget', true );

		$result .= '
		wppaShortcodeTemplate = "' . esc_js( wppa_get_picture_html( array( 'id' => $id, 'type' => 'sphoto' ) ) ) . '";
		wppaShortcodeTemplateId = "' . $id . '.' . wppa_get_photo_item( $id, 'ext' ) . '";';

		// Reset faked widget
		wppa( 'in_widget', false );
	}

	// The photo views cache
	if ( isset( $wppa_session['photo'] ) ) {
		foreach ( array_keys( $wppa_session['photo'] ) as $p ) {
			$result .= '
			wppaPhotoView[' . $p . '] = true;';
		}
	}

	// Format
	$result = wppa_compress_js( $result );
	$result = '
<!-- wppa js inits -->
<script>
' . $result . '
</script>
<!-- End wppa js inits -->
';
	echo $result;
}
add_action( 'wp_head', 'wppa_initialize_javascript', 2 );
add_action( 'admin_head', 'wppa_initialize_javascript', 2 );

function wppa_thumb_asp() {

	$aspect = 1;
	if ( wppa_opt( 'thumb_aspect' ) != '0:0:none' ) {
		$t = explode( ':', wppa_opt( 'thumb_aspect' ) );
		$aspect = $t[0] / $t[1];
	}
	elseif ( wppa_opt( 'resize_to' ) != '-1' && wppa_opt( 'resize_to' ) != '0' ) {
		$t = explode( 'x', wppa_opt( 'resize_to' ) );
		$aspect = $t[1] / $t[0];
	}
	else {
		$aspect = wppa_opt( 'maxheight' ) / wppa_opt( 'fullsize' );
	}
	return $aspect;
}

function wppa_fbw() {

	if ( is_numeric( wppa_opt( 'fullimage_border_width' ) ) ) {
		$fbw = wppa_opt( 'fullimage_border_width' ) + '1';
	}
	else {
		$fbw = '0';
	}
	return $fbw;
}

/* LOAD JAVASCRIPT */
add_action( 'init', 'wppa_add_javascripts' );

// This function does the actual js enqueueing
function wppa_add_javascripts() {
global $wppa_api_version;
global $wppa_lang;
global $wppa_opt;

	$footer = wppa_is_defer();

	// WPPA+ Javascript files.
	// All wppa+ js files come in 2 flavours: the normal version and a minified version.
	// If the minified version is available, it will be loaded, else the normal version.
	// If you want to debug js, just delete the minified version; this will cause the normal
	// - readable - version to be loaded.

	// The js dependancies
	$js_depts = array(	'jquery',
						'jquery-form',
						'jquery-masonry',
						'jquery-ui-dialog',
					);

	// First see if an 'all' file is present. This is to save http requests
	$all_file = dirname( __FILE__ ) . '/js/wppa-all.js';
	if ( wppa_is_file( $all_file ) ) {
		$js_ver = date( "ymd-Gis", filemtime( $all_file ) );
		wp_enqueue_script( 'wppa', WPPA_URL . '/js/wppa-all.js', $js_depts, $js_ver, false );
	}

	// No all file, do them one by one
	else {
		$js_files = array(
							'wppa-utils',
							'wppa',
							'wppa-slideshow',
							'wppa-ajax-front',
							'wppa-lightbox',
							'wppa-popup',
							'wppa-touch',
							'wppa-zoom',
							'wppa-spheric',
						);

		foreach ( array_keys( $js_files ) as $idx ) {
			if ( is_file( dirname( __FILE__ ) . '/js/' . $js_files[$idx] . '.min.js' ) ) {
				$js_ver = date( "ymd-Gis", filemtime( plugin_dir_path( __FILE__ ) . 'js/' . $js_files[$idx] . '.min.js' ) );
				wp_enqueue_script( $js_files[$idx], WPPA_URL . '/js/' . $js_files[$idx] . '.min.js', $js_depts, $js_ver, $footer );
			}
			else {
				$js_ver = date( "ymd-Gis", filemtime( plugin_dir_path( __FILE__ ) . 'js/' . $js_files[$idx] . '.js' ) );
				wp_enqueue_script( $js_files[$idx], WPPA_URL . '/js/' . $js_files[$idx] . '.js', $js_depts, $js_ver, $footer );
			}
		}
	}

	// Start other (vendor) js files
	$footer = false;

	// google maps
	if ( wppa_switch( 'save_gpx' ) && strpos( wppa_opt( 'custom_content' ), 'w#location' ) !== false ) {
		$key = wppa_opt( 'map_apikey' );
		wp_enqueue_script( 	'wppa-geo',
							'https://maps.googleapis.com/maps/api/js?' . ( $key ? 'key=' . $key : 'v=3.exp' ),
							'',
							$wppa_api_version,
							$footer );
	}

	// Nicescroller
	if ( ! wppa_is_mobile() && ( wppa_switch( 'nicescroll' ) || wppa_switch( 'nicescroll_window' ) || wppa_switch( 'load_nicescroller' ) ) ) {
		$nice_url = WPPA_URL . '/vendor/nicescroll/jquery.nicescroll.min.js';
		wp_enqueue_script( 'nicescrollr-inc-nicescroll-min-js', $nice_url, array( 'jquery', 'nicescrollr-easing-min-js' ), 'all' );
		$easing_url = WPPA_URL . '/vendor/jquery-easing/jquery.easing.min.js';
		wp_enqueue_script( 'nicescrollr-easing-min-js', $easing_url, array( 'jquery' ), 'all' );
	}

	// Easing we need, borrow it from nicescroller if not already loaded
	else {
		$easing_url = WPPA_URL . '/vendor/jquery-easing/jquery.easing.min.js';
		wp_enqueue_script( 'nicescrollr-easing-min-js', $easing_url, array( 'jquery' ), 'all' );
	}

	// Panorama
	if ( wppa_switch( 'enable_panorama' ) ) {
		$three_url = WPPA_URL . '/vendor/three/three.min.js';
		$ver = '122';
		wp_enqueue_script( 'wppa-three-min-js', $three_url, array(), $ver );
	}

	// End other (vendor) js files

}

// Add script to the page specific data. Input text should have no <script> tag
// If admin or no defer js or cached: output with tags by wppa_out()
function wppa_js( $txt ) {
global $wppa_js_page_data;
global $wppa_script_open;

	// Validate input
	if ( wppa_switch( 'allow_debug' ) ) {
		$i = 0;
		$arr = array( '[' => 0, ']' => 0, '(' => 0, ')' => 0, '{' => 0, '}' => 0 );
		$t = array_keys( $arr );
		while ( $i < strlen( $txt ) ) {
			$c = substr( $txt, $i, 1 );
			if ( in_array( $c, $t ) ) $arr[$c]++;
			$i++;
		}
		for ( $i = 0; $i < 6; $i += 2 ) {
			if ( $arr[$t[$i]] != $arr[$t[$i + 1]] ) {
				wppa_log( 'err', 'Unmatched ' . $t[$i] . ' - ' . $t[$i + 1] . ' in ' . $txt . ' (wppa_js)', true );
			}
		}
		if ( substr( $txt, -1 ) != ';' ) {
			wppa_log( 'err', 'Missing ; in ' . $txt . ' (wppa_js)', true );
			$txt .= ';';
		}
	}

	// Do it
	if ( ! wppa_is_defer() || wppa_is_caching() ) {
		wppa_out( '
<script>' . $txt . '</script>' );
		return;
	}

	if ( ! $wppa_script_open ) {
		$wppa_js_page_data = $txt;
		$wppa_script_open = true;
	}
	else {
		$wppa_js_page_data .= '
' . $txt;
	}
}

// Output page specific script in the footer.
// This has only content when defer js is on and not admin and anything not cached
add_action( 'wp_footer', 'wppa_print_psjs', 99 );

function wppa_print_psjs() {
global $wppa_js_page_data;
global $wppa_script_open;

	if ( $wppa_js_page_data ) {
		echo '
<!-- WPPA Page specific js -->
<script>' . wppa_compress_js( $wppa_js_page_data ) . '
</script>
<!-- End WPPA Page specific js -->
';

		$wppa_js_page_data = '';
		$wppa_script_open = false;
	}
}

// Compress javascript
function wppa_compress_js( $txt ) {

	$result = $txt;
	$result = str_replace( "\t", "", $result );
	$result = str_replace( "\r\n", "", $result );
	$result = str_replace( " = ", "=", $result );
	$result = str_replace( "; ", ";", $result );
	$result = str_replace( "/*", "\r\n/*", $result );

	return $result;
}

// Decide if we do defer js
function wppa_is_defer() {

	if ( ! wppa_switch( 'defer_javascript' ) ) return false;
	if ( wppa( 'ajax' ) ) return false;
	if ( wppa( 'cron' ) ) return false;
	if ( is_admin() ) return false;
	if ( wppa( 'is_slide' ) || wppa( 'is_slideonly' ) || wppa( 'is_filmonly' ) ) return false;
	return true;
}
