var vidpause = 0;
var vidmuted = 0;
var ctrlsinit = 0;
jQuery(document).ready(function($) {
	$('div#videoframe').mouseenter(function(e) {
		if (ctrlsinit == 1) return;
		var controls = $('<div style="position:absolute;left:2px;top:170px"><a href="#" class="vidpause" title="Video pauzeren/hervatten">pause</a> <a href="#" title="Geluid uit/aan" class="vidmute">geluid uit</a></div>');
		$(this).append(controls);
		controls.find('a.vidpause').click(function(e) {
			e.preventDefault();
			if (vidpause == 0) {
				vidpause = 1;
				pauseVideo();
				$(this).html('start');
			} else {
				vidpause = 0;
				playVideo();
				$(this).html('pause');
			}
		});
		controls.find('a.vidmute').click(function(e) {
			e.preventDefault();
			if (vidmuted == 0) {
				vidmuted = 1;
				muteVideo();
				$(this).html('geluid aan');
			} else {
				vidmuted = 0;
				unMuteVideo();
				$(this).html('geluid uit');
			}
		});
		ctrlsinit = 1;
	});
	loadPlayer();
});

  google.load("swfobject", "2.1");
  // Update a particular HTML element with a new value
  function updateHTML(elmId, value) {
    document.getElementById(elmId).innerHTML = value;
  }
  
  // This function is called when an error is thrown by the player
  function onPlayerError(errorCode) {
    //alert("An error occured of type:" + errorCode);
    $('div#videoframe').hide().remove();
  }
  
  // This function is called when the player changes state
  function onPlayerStateChange(newState) {
    if (newState == 0) {
    	// video stopped
    	$('div#videoframe').hide().remove();
    }
  }
  
  // Display information about the current state of the player
  function updatePlayerInfo() {
    return;
  }

  function playVideo() {
    if (ytplayer) {
      ytplayer.playVideo();
    }
  }
  
  function pauseVideo() {
    if (ytplayer) {
      ytplayer.pauseVideo();
    }
  }
  
  function stopVideo() {
    if (ytplayer) {
      ytplayer.stopVideo();
    }
  }
  
  function muteVideo() {
    if(ytplayer) {
      ytplayer.mute();
    }
  }
  
  function unMuteVideo() {
    if(ytplayer) {
      ytplayer.unMute();
    }
  }
  
  // This function is automatically called by the player once it loads
  function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("videoplayer");
    // This causes the updatePlayerInfo function to be called every 250ms to
    // get fresh data from the player
    ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
    ytplayer.addEventListener("onError", "onPlayerError");
    //Load an initial video into the player
    ytplayer.cueVideoById("ffEGUvkZXvs");
    ytplayer.playVideo();
  }
  
  // The "main method" of this sample. Called when someone clicks "Run".
  function loadPlayer() {
    // Lets Flash from another domain call JavaScript
    var params = { allowScriptAccess: "always", wmode: "opaque" };
    // The element id of the Flash embed
    var atts = { id: "videoplayer" };
    // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
    swfobject.embedSWF("http://www.youtube.com/apiplayer?" +
                       "&enablejsapi=1&playerapiid=player1", 
                       "videoplayer", "284", "188", "8", null, null, params, atts);
  }
