/*
	This file contains JavaScript related to the video elements.
	
	Remember to comment!
*/

$(document).ready(function() {
	
	// Detect if user is using iPod/iPhone to view the site
	// Flash doesn't work on these devices, so lightbox won't show anything
	// Detecting iPod/iPhone allows a user to click on a video and be redirected
	// to youtube, where ipods/iphones are supported
	if((!navigator.userAgent.match(/iPhone/i)) && (!navigator.userAgent.match(/iPod/i))) {
		// Add lightbox effect to div's with class "video" and it's children anchors with relation starting with "prettyphoto"
		// Example: <div class="video"><a href="link/to/video" rel="prettyphoto"><img src="high" alt="Alt text" title="Title" /></a></div>
		$('div.video a[rel^="prettyphoto"]').prettyPhoto();
	}
	
	// Run jquery equal height's plugin
	// Make all the video boxes the same height
	// Very useful!
	$('div.video-row').each(function() {
		$(this).children('div.video').equalHeights();
	});
});	

$(window).load(function() {
	// Detect if a video should be played based on the URL
	// Get URL and split at # mark
	var vid = window.location.href.split('#');
	// Set the desination video to the last element in the array
	var vid = vid.pop();
	// If jquery finds an achor tag with the same class as the above video
	if ($('a[name="'+ vid + '"]').length > 0) {
		// Set variables prettyphoto needs to play
		var href = $('a[name="'+ vid + '"]').attr('href');
		var title =  $('a[name="'+ vid + '"]').children('img').first().attr('title');
		var description = $('a[name="'+ vid + '"]').children('img').first().attr('alt');
		// Open prettyphoto lightbox
		$.prettyPhoto.open(href, title, description);
	}
});
