
var ytcontrolstate=0; // State of the Player control: 0: invisible; 1:Play; 2:Pause

var params = { allowScriptAccess: 'always', wmode:'transparent'};
var atts = { id: 'myytplayer' };
swfobject.embedSWF('http://www.youtube.com/apiplayerbeta?enablejsapi=1&amp;playerapiid=ytplayer&amp;version=3', 
        'ytapiplayer', '425', '319', '8', null, null, params, atts);
//swfobject.embedSWF('http://www.youtube.com/apiplayer?enablejsapi=1&amp;playerapiid=ytplayer', 
//'ytapiplayer', '425', '319', '8', null, null, params, atts);
///


// Loads the selected video into the player.
function loadVideo(videoID) {  
  if(ytplayer) {
	  // load video
	  ytplayer.loadVideoById(videoID);
  }
}

// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
  alert("An error occured of type:" + errorCode);
}

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
	ytplayer = document.getElementById("myytplayer");
	ytplayer.addEventListener("onError", "onPlayerError");
	ytplayer.addEventListener( "onStateChange", "onYtStateChanged" );
}

/**
* Reagiert auf das Event, dass sich der Status ändert.
* Ändert die Anzeige des Play/Pause-Buttons.
* Possible values are unstarted (-1), ended (0), playing (1), paused (2), buffering (3), video cued (5)
*/
function onYtStateChanged( newState ) {
	//alert(newState);    
	switch( newState ) {
		case -1:
			break;
		case 1: // Video Playing
			$("#ytcontrol").css("background-image","url(./themes/mysite/images/negative_small/pausedown.png)");
			$("#ytcontrol").css("display","block");
			ytcontrolstate=0;
			break;
		case 0: // Video ended
			$("#ytcontrol").css("background-image","url(./themes/mysite/images/negative_small/playdown.png)");
			$("#ytcontrol").css("display","block");
			ytcontrolstate=1;
			break;
		case 2: // Video paused
        	$("#ytcontrol").css("background-image","url(./themes/mysite/images/negative_small/playdown.png)");
			$("#ytcontrol").css("display","block");
			//$("#ytcontrol").click( function( e ) {
//				ytplayer.playVideo();
//			} );
	       break;
		case 3: // Video buffering
			$("#ytcontrol").css("display","none");
			ytcontrolstate=0;
	       break;
	    default:
	       //$( "#ytplay" ).attr( "value", "Play" );
	}
}

function controlPlayer(){
	//alert("hier");
	if (ytcontrolstate==0){
		ytcontrolstate=1;
		ytplayer.pauseVideo();
	}else{
		ytcontrolstate=0;
		ytplayer.playVideo();
	}
}

// The "main method" of this sample. Called when someone clicks "Run".
//function loadPlayer() {
//  // The video to load
//  var videoID = "ylLzyHk54Z0"
//  // Lets Flash from another domain call JavaScript
//  var params = { allowScriptAccess: "always" };
//  // The element id of the Flash embed
//  var atts = { id: "ytPlayer" };
//  // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
//  swfobject.embedSWF("http://www.youtube.com/v/" + videoID +
//                     "&enablejsapi=1&playerapiid=player1",
//                     "videoDiv", "480", "295", "8", null, null, params, atts);
//}



