(function() {
	function createVideo(container, url) {
	  $('#overlay-video').bind('state.fv', function(e, state) {
	    if (state == "initial" && $.inArray(navigator.platform, ['iPad', 'iPhone', 'iPod']) > -1 ) {
	      $(this).children('.flareVideo').css('padding-bottom', '0');
	    }
	  })
		var fv = $("#overlay-video").flareVideo({
			'autoplay': true,
			'flashSrc': '/_swf/flarevideo.swf',
			'srcs': [
  			{
  			  src:  url,
  			  type: 'video/mp4'
  			}
  		]
		});
		//fv.load();
	}
	
	$(document).ready(function() {
		$('a[rel=fancybox-video]').each(function() {
			$(this).fancybox({
				'content': '<div id="overlay-video"></div>', 
				'width': 640,
				'height': $.inArray(navigator.platform, ['iPad', 'iPhone', 'iPod']) > -1 ? 360 : 400,
				'autoDimensions': false,
				'autoScale': true,
				'overlayOpacity': 0.8,
				'overlayColor': '#eee',
				'orig': $(this),
				'transitionIn': 'elastic',
				'transitionOut': 'none',
				'scrolling': 'no',
				'onStart' : function() {
			  		$('#fancybox-outer').removeClass('whiteoverlay');
				},
				'onComplete': function(currentArray, currentIndex, currentOpts) {
					$('#fancybox-outer').addClass('shadowed');
					createVideo($('#fancybox-inner'),currentOpts.href)
				},
				'onCleanup': function() {
					$('#fancybox-outer').removeClass('shadowed');
					$('#overlay-video').remove();
				}
			})
		})
	});
})();
