$(document).ready(function() {
	
	html5 = parseInt(
		(typeof HTMLVideoElement != 'undefined' ? 1 : 0) + "" +
		(typeof HTMLAudioElement != 'undefined' ? 1 : 0) + "" +
		(typeof HTMLCanvasElement != 'undefined' ? 1 : 0)
	) == 111 ? true : false;
	
	/* ROLLOVERS */
	$('img.rollover').hover(function() {
		$(this).data('src', $(this).attr('src'));
		var subSrc = $(this).attr('src').slice(0, -4);
		$(this).attr('src', subSrc + '_hover.png');
	},
	function() {
		$(this).attr('src', $(this).data('src'));
	});
	
	/* LINKS => BUTTONS */
	$('.mainbox_middle a, .fullbox_middle a').not('a.non-itext, a.non, a.button, .engraved a, h1 a, h2 a, h3 a, h4 a, h5 a, h6 a, h a, a.audio').hover(function() {
		$(this).css("opacity", "0");
		$(this).animate({'opacity': '1'}, 300);
	});
	
	
	/* FANCYBOX */
	$.fn.fancybox.defaults.overlayColor = '#000000';
	$.fn.fancybox.defaults.overlayOpacity = .5;
	
	function fancyVideo($a) {
		$a.click(function() {
			$a.attr('title', 'To view at full screen, use the &lsquo;Fullscreen&rsquo; button.');
			$('#fancybox-title-wrap').hide();
			$('#fancybox-outer')
				.css('background-color', 'transparent')
				.hover(
					function() {
						$('#fancybox-close').fadeIn("fast");
						$('.fancybox-title-outside').css('text-align', 'right');
						$('#fancybox-title-wrap').fadeIn("fast");
					},
					function() {
						$('#fancybox-close').fadeOut();
						$('#fancybox-title-wrap').fadeOut();
					}
				);
		});
	}
	function fancyUnvideo($a) {
		$a.removeAttr('title');
		$('#fancybox-outer')
			.css('background-color', '#FFFFFF')
			.unbind('mouseenter mouseleave');
	}
	
	
	/* PRETTY PHOTO */
	$('a[rel^="pretty"]').prettyPhoto({
		theme: 'custom_rounded',
		opacity: .5,
		padding: 30
	});
	
	
	
	/* FLOWPLAYER DEFAULTS */
	var flowPath = '/flash/flowplayer/flowplayer-3.2.5.swf';
	var flowConfig = {
		clip: {
			autoPlay: true,
			scaling: 'fit',
			provider: 'rtmp'
		},
		plugins: {
			rtmp: {
				url: 'flowplayer.rtmp-3.2.1.swf',
				netConnectionUrl: 'rtmp://vod01.netdna.com/play'
			},
			controls: {
				height: 30,
				backgroundColor: 'rgba(125,125,125,0.4)',
				backgroundGradient: [.4,.2,0,0],
				
				buttonColor: 'rgba(255,255,255,0.65)',
				buttonOverColor: '#FFFFFF',
				
				scrubberHeightRatio: 0.3,
				scrubberBarHeightRatio: 0.8,
				volumeSliderHeightRatio: 0.3,
				volumeBarHeightRatio: 0.8,
				
				sliderGradient: [0,0.2,0.4],
				volumeSliderColor: '#000000',
				volumeSliderGradient: [0,0.2,0.4],
				volumeColor: 'rgba(125,125,125,0.45)',
				progressColor: 'rgba(125,125,125,0.45)',
				bufferColor: 'rgba(103,103,103,0.55)',
				timeColor: 'rgba(255,255,255,0.85)',
				durationColor: '#000000',
				
				tooltipColor: 'rgba(0,0,0,0.5)',
				tooltips: {
					buttons: true
				},
				
				autoHide: {
					enabled: true,
					fullscreenOnly: false,
					hideDelay: 2000,
					hideDuration: 1200,
					hideStyle: 'fade'
				}
			},
			TMPlugin: {
				url: 'http://static.inplay.tubemogul.com/bootloader/TubeMogulFlowPlayer.swf',
				playerID: 'P-NGR-V7O',
				trackerIDs: 'TD-7Z7-G1Y',
				publisherID: '1',
				videoID: ''
			}
		},
		onFullscreen: function() {
			this.getControls().css('height', 50);
		},
		onFullscreenExit: function() {
			this.getControls().css('height', 30);
		}
	};
	
	var flowConfigMulti = {
		clip: {
			urlResolvers: 'bwcheck'
		},
		plugins: {
			bwcheck: {
				url: 'flowplayer.bwcheck-3.2.1.swf',
				serverType: 'wowza',
				dynamic: true,
				netConnectionUrl: 'rtmp://vod01.netdna.com/play'
				/* , onStreamSwitch: function(bitrate, streamName) { $('#fancybox-title-main').text('Currently playing: ' + streamName); } */
			}
		}
	};
	
	var flowConfigViral = {
		plugins: {
			viral: {
				url: 'flowplayer.viralvideos-3.2.2-dev.swf',
				embed: { autoPlay: false }
			}
		}
	}
	
	var tempConfig = {
		plugins: {
			controls: {
				height: 30,
				backgroundColor: 'rgba(125,125,125,0.4)',
				backgroundGradient: [.4,.2,0,0],
				
				buttonColor: 'rgba(255,255,255,0.65)',
				buttonOverColor: '#FFFFFF',
				
				scrubberHeightRatio: 0.3,
				scrubberBarHeightRatio: 0.8,
				volumeSliderHeightRatio: 0.3,
				volumeBarHeightRatio: 0.8,
				
				sliderGradient: [0,0.2,0.4],
				volumeSliderColor: '#000000',
				volumeSliderGradient: [0,0.2,0.4],
				volumeColor: 'rgba(125,125,125,0.45)',
				progressColor: 'rgba(125,125,125,0.45)',
				bufferColor: 'rgba(103,103,103,0.55)',
				timeColor: 'rgba(255,255,255,0.85)',
				durationColor: '#000000',
				
				tooltipColor: 'rgba(0,0,0,0.5)',
				tooltips: {
					buttons: true
				},
				
				autoHide: {
					enabled: true,
					fullscreenOnly: false,
					hideDelay: 2000,
					hideDuration: 1200,
					hideStyle: 'fade'
				}
			}
		},
		onFullscreen: function() {
			this.getControls().css('height', 50);
		},
		onFullscreenExit: function() {
			this.getControls().css('height', 30);
		}
	};
	
	var streamingAudioFlowConfig = {
		clip: {
			autoPlay: false,
			provider: 'rtmp'
		},
		plugins: {
			rtmp: {
				url: 'flowplayer.rtmp-3.2.1.swf',
				netConnectionUrl: 'rtmp://vod01.netdna.com/play'
			},
			controls: {
				height: 30,
				backgroundColor: 'rgba(125,125,125,0.4)',
				backgroundGradient: [.4,.2,0,0],
				
				buttonColor: 'rgba(255,255,255,0.65)',
				buttonOverColor: '#FFFFFF',
				
				scrubberHeightRatio: 0.3,
				scrubberBarHeightRatio: 0.8,
				volumeSliderHeightRatio: 0.3,
				volumeBarHeightRatio: 0.8,
				
				sliderGradient: [0,0.2,0.4],
				volumeSliderColor: '#000000',
				volumeSliderGradient: [0,0.2,0.4],
				volumeColor: 'rgba(125,125,125,0.45)',
				progressColor: 'rgba(125,125,125,0.45)',
				bufferColor: 'rgba(103,103,103,0.55)',
				timeColor: 'rgba(255,255,255,0.85)',
				durationColor: '#000000',
				
				tooltipColor: 'rgba(0,0,0,0.5)',
				tooltips: {
					buttons: true
				},
				
				fullscreen: false,
				autoHide: false
			}
		}
	};
	
	/* FLOWPLAYER HELPERS */
	function configureSingle($elem) {
		var rxFilename = /[^/]+$/;
		var filename = $elem.attr('href').match(rxFilename);
		$elem.attr('href', 'mp4:vod/miracle.mschool/' + filename);
		
		var thisConfig = flowConfig;
		
		if ($elem.metadata().viral) {
			thisConfig = $.extend(true, thisConfig, flowConfigViral, {plugins: {viral: {share: {description: $elem.attr('title')}}}});
		}
				
		var flowConfigMeta = $elem.metadata().config;
		if (flowConfigMeta) {
			thisConfig = $.extend(true, thisConfig, flowConfigMeta);
		}
		
		return thisConfig;
	}
	
	function configureMulti($elem) {
		var rxFilename = /[^/]+$/;
		var rxFilePrefix = /[-a-zA-Z0-9\.]+_/;
		
		var filename = $elem.attr('href').match(rxFilename);
		var filePrefix = filename[0].match(rxFilePrefix);
		
		var flowConfigBitrates = {
			clip: {
				bitrates: [
					{ url: 'mp4:vod/miracle.mschool/' + filePrefix + '480p_360k.mp4', width: 854, bitrate: 488, isDefault: true },
					{ url: 'mp4:vod/miracle.mschool/' + filePrefix + '480p_500k.mp4', width: 854, bitrate: 628 },
					{ url: 'mp4:vod/miracle.mschool/' + filePrefix + '480p_640k.mp4', width: 854, bitrate: 768 },
					{ url: 'mp4:vod/miracle.mschool/' + filePrefix + '480p_780k.mp4', width: 854, bitrate: 908 },
					{ url: 'mp4:vod/miracle.mschool/' + filePrefix + '720p_900k.mp4', width: 1280, bitrate: 1028 },
					{ url: 'mp4:vod/miracle.mschool/' + filePrefix + '720p_1200k.mp4', width: 1280, bitrate: 1328 },
					{ url: 'mp4:vod/miracle.mschool/' + filePrefix + '1080p_2800k.mp4', width: 1920, bitrate: 2928 }
				]
			}
		};
		
		var thisConfig = $.extend(true, {}, flowConfig, flowConfigMulti, flowConfigBitrates);
		
		if ($elem.metadata().viral) {
			thisConfig = $.extend(true, thisConfig, flowConfigViral, {plugins: {viral: {share: {description: $(this).attr('title')}}}});
		}
		
		var flowConfigMeta = $elem.metadata().config;
		if (flowConfigMeta) {
			$.extend(true, thisConfig, flowConfigMeta);
		}
		
		return thisConfig;
	}
	
	
	function configureSingleAudio($elem) {
		var rxFilename = /[^/]+$/;		
		var filename = $elem.attr('href').match(rxFilename);
		$elem.attr('href', 'mp3:vod/miracle.mschool/' + filename);
		
		var thisConfig = streamingAudioFlowConfig;
		
		var flowConfigMeta = $elem.metadata().config;
		if (flowConfigMeta) {
			$.extend(true, thisConfig, flowConfigMeta);
		}
		
		return thisConfig;
	}
	
	
	
	/* JQUERY MEDIA PLUGIN */
	$.fn.media.defaults.flvPlayer = '/flash/wasp.swf';
	$.fn.media.defaults.mp3Player = '/flash/wasp.swf';
	$.fn.media.defaults.flvKeyName = 'f';	// wasp's file pointer is just 'f'
	
	// Defaults for all flash players
	$.fn.media.defaults.flashvars = {
		s: 0,			// wasp autoplay: 0 = off (default) | 1 = on
		b: 5,			// wasp buffer seconds
		a: 1,			// wasp aspect ratio: 0 = stretch (default) | 1 = letterbox
		tf: '1 / 2',	// wasp time format string: 1 - current time; 2 - time remaining; 3 - total time; accepts any characters in string
		r: 'NzZMeEN2JTVDJTNETCU4MG1zY20lMkMlODJNbiU1Q3NPWiUyQzFYeiU3RHlmcXAlMkY0MSU4MCUzQnIlN0Jw'	// wasp registration key		
	}
		
	// Lightbox Video Players	
		
	$('a.video[rel="lightbox"]').each(function(i) {
		var $fancylink = $(this);
		var $flowlink = $(this).clone().attr('id', 'lightboxVideo'+i);
		$flowlink.children().remove(); //remove any nested images so flowplayer doesn't make a poster
		$('<div style="display: none;"></div>').append($flowlink).appendTo('body');
		
		if ($flowlink.metadata().multi) {
			var thisConfig = configureMulti($flowlink);
		} else {
			var thisConfig = configureSingle($flowlink);
		}
		
		thisConfig.plugins.TMPlugin.videoID = $flowlink.metadata().vid;
				
		$flowlink.flowplayer(flowPath, thisConfig);
		$fancylink.attr('href', '#lightboxVideo'+i).fancybox({
			autoDimensions: false,
			width: 854,
			height: 480,
			scrolling: 'no',
			padding: 0,
			overlayOpacity: .8,
			overlayColor: '#000',
			showCloseButton: false,
			showNavArrows: false,
			onStart: fancyVideo($fancylink),
			onClosed: fancyUnvideo
		});
	});
	
	
	
	
	// Standalone Video Players	
	
	//$('.mainbox_middle a.video[rel!="lightbox"]').flowplayer('http://builds.flowplayer.netdna-cdn.com/35491/7826/flowplayer-3.2.2-0.swf');
	
	//$('.mainbox_middle a.video[rel!="lightbox"]').flowplayer(flowPath, flowConfig);
	
		
	//flowplayer('player', '/flash/flowplayer/flowplayer-3.2.2.swf');
		
		
	/*	
		clip: {
			url: 'http://vod01.netdna.com/vod/miracle.mschool/whitewater-480p.mp4',
			provider: 'rtmp'
		},
		plugins: {
			rtmp: {
				url: 'vod/miracle.mschool/whitewater-480p.mp4',
				netConnectionUrl: 'rtmp://vod01.netdna.com/play'
			}
		}
	});
	*/
	/*
	var videoSwfUrl = '/flash/wasp.swf';
	var mainboxVideoWidth = '480';
	var mainboxVideoHeight = '270';
	var videoSwfVersion = '7';
	var videoFlashVars = {
		s: 0,			// wasp autoplay: 0 = off (default) | 1 = on
		b: 5,			// wasp buffer seconds
		a: 1,			// wasp aspect ratio: 0 = stretch (default) | 1 = letterbox
		tf: '1 / 2',	// wasp time format string: 1 - current time; 2 - time remaining; 3 - total time; accepts any characters in string
		r: 'NzZMeEN2JTVDJTNETCU4MG1zY20lMkMlODJNbiU1Q3NPWiUyQzFYeiU3RHlmcXAlMkY0MSU4MCUzQnIlN0Jw'	// wasp registration key
	}
	var videoParams = { wmode: 'transparent' }
	var videoAttrs = {}
	var xiUrl = null;
	
	$('.mainbox_middle a.video[rel!="lightbox"]').each(function(i) {
		$this = $(this);
		if ($this.is('[id]')) {
			var replaceId = $this.attr('id');
		} else {
			var replaceId = 'standalone_video_' + i;
			$this.attr('id', replaceId);
		}
		$this.wrap('<div class="video"></div>');
		var videoSrc = $this.attr('href');
		var posterImage = $this.metadata().im;
		var addtlFlashVars = {
			f: videoSrc,
			waspSkin: 'sr_1|1|3|909090|C8C8C8|F8F8F8^st_1||14||000000^sg_1|5|16||505050^sb_1|1|40|C8C8C8|FFFFFF|707070^sp_1||18|FFFFFF|505050^sv_1|4|18|C8C8C8|303030^sf_1|1|18|FFFFFF|303030^sh_1||18|FFFFFF|303030^sa_1|1|7^sz_1|1|7',
			cl: 2,		// wasp toolbar location: 0 = hide | 1 = bottom (default) | 2 = overlay
			im: posterImage
		}
		var flashVars = $.extend(videoFlashVars, addtlFlashVars);
		
		swfobject.embedSWF(videoSwfUrl, replaceId, mainboxVideoWidth, mainboxVideoHeight, videoSwfVersion, xiUrl, flashVars, videoParams, videoAttrs);
		
		
	});
	*/
	/*
	$('.mainbox_middle a.video[rel!="lightbox"]').media({
		width: 480, 
		height: 270, 
		bgColor: 'transparent',
		caption: false,
		flashvars: {
			waspSkin: 'sr_1|1|3|909090|C8C8C8|F8F8F8^st_1||14||000000^sg_1|5|16||505050^sb_1|1|40|C8C8C8|FFFFFF|707070^sp_1||18|FFFFFF|505050^sv_1|4|18|C8C8C8|303030^sf_1|1|18|FFFFFF|303030^sa_1|1|7^sz_1|1|7',
			cl: 2,		// wasp toolbar location: 0 = hide | 1 = bottom (default) | 2 = overlay
			im: '/images/VIDEO%20BACKGROUND.jpg'
		}
	});
	*/
	$('.fullbox_middle a.video[rel!="lightbox"]').media({
		width: 720, 
		height: 405, 
		bgColor: 'transparent',
		caption: false,
		flashvars: {
			waspSkin: 'sr_1|1|3|909090|C8C8C8|F8F8F8^st_1||14||000000^sg_1|5|16||505050^sb_1|1|40|C8C8C8|FFFFFF|707070^sp_1||18|FFFFFF|505050^sv_1|4|18|C8C8C8|303030^sf_1|1|18|FFFFFF|303030^sh_1||18|FFFFFF|303030^sa_1|1|7^sz_1|1|7',
			cl: 2,		// wasp toolbar location: 0 = hide | 1 = bottom (default) | 2 = overlay
			im: '/images/VIDEO%20BACKGROUND.jpg'
		}
	});
	
	// For All Video Players
	$('div.video > object, div.video > embed').addClass('video');
	
		
	// Standalone Audio Player
	
	$('a.audio').each(function() {
		var $audiolink = $(this);
		$(this).addClass("non"); // adding "non" here so that, if javascript is disabled, audio links maintain link appearance
		var $audiodiv = $(this).wrap('<div />').parent();
		$audiodiv.addClass($audiolink.attr('class'));
		$(this).contents().remove(); // remove nested text and images so that Flowplayer doesn't make a poster
		$(this).flowplayer(flowPath, {
			plugins: {
				controls: {
					backgroundColor: '#000077',
					backgroundGradient: [.4,.2,0,0],
					
					buttonColor: 'rgba(255,255,255,0.65)',
					buttonOverColor: '#FFFFFF',
					
					scrubberHeightRatio: 0.3,
					scrubberBarHeightRatio: 0.8,
					volumeSliderHeightRatio: 0.3,
					volumeBarHeightRatio: 0.8,
					
					sliderColor: '#000047',
					sliderGradient: [0,0.2,0.4],
					volumeSliderColor: '#000047',
					volumeSliderGradient: [0,0.2,0.4],
					volumeColor: 'rgba(255,255,255,0.25)',
					progressColor: 'rgba(255,255,255,0.45)',
					bufferColor: 'rgba(255,255,255,0.25)',
					sliderBorder: '1px solid rgba(255,255,255,0.1)',
					volumeBorder: '1px solid rgba(255,255,255,0.1)',
					
					timeColor: 'rgba(255,255,255,0.85)',
					durationColor: 'rgba(255,255,255,0.5)',
					
					tooltipColor: 'rgba(255,255,255,0.4)',
					tooltips: {
						buttons: true
					},
					
					fullscreen: false,
					height: 40,
					autoHide: false
				}
			},
			clip: {
				autoPlay: false
			}
		});
	});
	
	
	
	// Prepare Fancyboxes
	$('a.makeInline').each(function() {
		var href = $(this).attr('href');
		var pound = href.indexOf('#');
		if (pound) {
			var inline = href.substr(pound);
			var url = href.substring(0, pound);
			$('<div></div>').hide().appendTo('body').load(url+' '+inline);
			$(this).attr('href', inline);
		}
	});
	
	// Regular Fancybox
	$('a[rel="lightbox"]:not(.video)').each(function() {
		if ($(this).is('.large')) {
			$(this).fancybox({
				width: 800,
				height: 600,
				showNavArrows: false
			});
		} else {
			$(this).fancybox({
				width: 600,
				height: 410,
				showNavArrows: false
			});
		}
	});
	
	// Legacy Fancyboxes
	// The "each" part prevents Fancybox from killing all Javascript when the selected element does not exist on the page, as it would if Fancybox was called directly with $selector.fancybox()
	$('a[rel^="fancy"][rel!="small"]').each(function() {
		$(this).fancybox({
			width: 800,
			height: 700,
			padding: 12
		});
	});
	
	$('a[rel^="fancy"][rel*="small"]').each(function() {
		$(this).fancybox({
			width: 600,
			height: 410
		});
	});
	
	// Anchor link scrolling
	
	$('a[href^="#"]').not('[rel~="lightbox"]').click(function() {
		var id = $(this).attr('href');
		var name = id.substr(1);
		var $elem = $(id).length ? $(id) : $('a[name="'+name+'"]'); // element to scroll to is either id=href or name=href
		$.scrollTo($elem, 800, {offset: -50});
		return false;
	});
	
});
