function supports_video() {
  return !!document.createElement('video').canPlayType;
}
function supports_h264_baseline_video() {
  if (!supports_video()) { return false; }
  var v = document.createElement("video");
  return v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
}
var volInit = 100;
var loadingInterval;

	function checkVideoPlaying(){
		if($("#html5video")[0].currentTime>1){
			$("#videoloading").fadeOut();
			clearInterval(loadingInterval);
		}
	}

$(document).ready(function(){

	if(supports_h264_baseline_video()){
		$("#player").html('<div id="videoloading">Loading...</div><video id="html5video" width="976" height="200" controls="controls" autoplay="autoplay"><source src="https://s3.amazonaws.com/naturespace/videos/naturespacehomepage.mp4" type="video/mp4" /></video>');

		$("#toggle_play").toggle(function(){
			$(this).text('Play');
			$("#html5video")[0].pause();
			return false;
		},function(){
			$(this).text('Pause');
			$("#html5video")[0].play();	
			return false;
		});
		$("#volume_min").click(
			function() {
			
				if (volInit > 0) { volInit -= 10; }
				document.title=volInit/10;					
				$("#html5video")[0].volume=volInit/100;
		
				return false;
			});
		$("#volume_max").click(
			function() {

				if (volInit < 100) { volInit += 10; }
				document.title=volInit/10;
				$("#html5video")[0].volume=volInit/100;
				return false;
			});	
		loadingInterval=setInterval("checkVideoPlaying()",100);
	}else{
		var so = new SWFObject('/images/video/player.swf','mpl','976','200','9');
		so.addParam('allowscriptaccess','always');
		so.addParam('allowfullscreen','false');
		so.addParam('icons', 'true');
		so.addParam('flashvars','&file=https://s3.amazonaws.com/naturespace/videos/naturespacehomepage.flv&controlbar=none&autostart=true&repeat=always&volume=100&icons=true&bufferlength=20&skin=/images/video/thin_playless.swf');
		so.write('player');	
		

		jQuery("#toggle_play").toggle(
			function() {
				jQuery(this).text('Play');
				window.document['mpl'].sendEvent('PLAY', false);
				return false;
			},
			function() {
				jQuery(this).text('Pause');
				window.document['mpl'].sendEvent('PLAY', true);
				return false;
			});
		jQuery("#volume_min").bind("click",
			function() {
				if (volInit > 0) { volInit -= 10; }
				window.document['mpl'].sendEvent('VOLUME', volInit);
				return false;
			});
		jQuery("#volume_max").bind("click",
			function() {
				if (volInit < 100) { volInit += 10; }
				window.document['mpl'].sendEvent('VOLUME', volInit);
				return false;
			});		
		
	}
});
