/*
 * Chromeless player has no controls.
 */

if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}

var myPlayer = {
	current_time: 0,
	duration: 0,
	current_state: -2,
	has_watched: 0,
	has_started: 0,
	video_id: null,
	
	hasWatched: function() {
  	return this.getCurrentState() == 'ended' && this.current_time > 0 && this.current_time >= this.duration;
	},

	// Update a particular HTML element with a new value
	updateHTML: function(elmId, value) {
  	if (elt = document.getElementById(elmId)) {
    	elt.innerHTML = value;
  	}
	},

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

	// This function is called when the player changes state
	onPlayerStateChange: function(newState) {
  	this.current_state = newState;
	},

	getCurrentState: function() {
  	return this.stateName(this.current_state);
	},

	stateName: function(stateNum) {
  	switch(stateNum) {
    	case -1:
      	return 'unstarted';
    	case 0:
      	return 'ended';
    	case 1:
      	return 'playing';
    	case 2:
      	return 'paused';
    	case 3:
      	return 'buffering';
    	case 5:
      	return 'cued';
    	default:
      	return 'unknown';
  	}
	},


	// Allow the user to set the volume from 0-100
	setVideoVolume: function() {
  	var volume = parseInt(document.getElementById("volumeSetting").value);
  	if(isNaN(volume) || volume < 0 || volume > 100) {
    	alert("Please enter a valid volume between 0 and 100.");
 	 	}
  	else if(this.ytplayer) {
    	this.ytplayer.setVolume(volume);
  	}
	},

	playVideo: function() {
  	if (this.ytplayer) {
    	this.ytplayer.playVideo();
  	}
	},

	pauseVideo: function() {
  	if (this.ytplayer) {
    	this.ytplayer.pauseVideo();
  	}
	},

	muteVideo: function() {
  	if(this.ytplayer) {
    	this.ytplayer.mute();
 	 	}
	},

	unMuteVideo: function() {
  	if(this.ytplayer) {
    	this.ytplayer.unMute();
 		}
	}
}

// Display information about the current state of the player
function updatePlayerInfo(thisObject) {
	// Also check that at least one function exists since when IE unloads the
	// page, it will destroy the SWF before clearing the interval.
	if(thisObject.ytplayer && thisObject.ytplayer.getDuration) {
		if (thisObject.ytplayer.getCurrentTime() > 0) {
			thisObject.current_time = thisObject.ytplayer.getCurrentTime();
			if(thisObject.has_started == 0 && thisObject.has_watched == 0) {
				thisObject.has_started = 1;
				thisObject.onStart(thisObject.ytplayer);
			}
		}

		thisObject.duration = thisObject.ytplayer.getDuration();
		thisObject.updateHTML("videoDuration", thisObject.ytplayer.getDuration());
		thisObject.updateHTML("videoCurrentTime", thisObject.ytplayer.getCurrentTime());
		thisObject.updateHTML("bytesTotal", thisObject.ytplayer.getVideoBytesTotal());
		thisObject.updateHTML("startBytes", thisObject.ytplayer.getVideoStartBytes());
		thisObject.updateHTML("bytesLoaded", thisObject.ytplayer.getVideoBytesLoaded());
		thisObject.updateHTML("volume", thisObject.ytplayer.getVolume());
	}

	if(thisObject.has_watched == 0 && thisObject.hasWatched()) {
		thisObject.has_watched = 1;
		thisObject.onDone(thisObject.ytplayer);
	}
}

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
	var thisPlayer = myVideos[playerId];
  thisPlayer.ytplayer = document.getElementById(playerId);
  // This causes the updatePlayerInfo function to be called every 250ms to
  // get fresh data from the player
  setInterval(function() {
		updatePlayerInfo(thisPlayer)
	}, 250);
  updatePlayerInfo(thisPlayer);
  thisPlayer.ytplayer.addEventListener("onStateChange", "myVideos['" + playerId + "'].onPlayerStateChange");
  thisPlayer.ytplayer.addEventListener("onError", "myVideos['" + playerId + "'].onPlayerError");
  //Load an initial video into the player
  thisPlayer.ytplayer.cueVideoById(thisPlayer.video_id);

  if (thisPlayer.onInit) {
    thisPlayer.onInit();
  }
}

var myVideos = new Array();
// The "main method" of this sample. Called when someone clicks "Run".
function loadPlayer(videoId, onStart, onDone, embedId, playerId, onInit, width, height) {
  // Lets Flash from another domain call JavaScript
  
	var thisPlayer = Object.create(myPlayer)
	//var thisPlayer = myPlayer;
	thisPlayer.video_id = videoId;
	thisPlayer.onStart = onStart;
	thisPlayer.onDone = onDone;
	thisPlayer.onInit = onInit;
	myVideos["player"+playerId] = thisPlayer;
	
	/*
	window._ytplayer = {};
  window._ytplayer.video_id = video_id;
  window._ytplayer.onStart = onStart;
  window._ytplayer.onDone = onDone;
  window._ytplayer.onInit = onInit;
	*/

  var videoWidth = width ? width : "435";
  var videoHeight = height ? height : "295";

  var params = { allowScriptAccess: "always", wmode: "transparent" };
  // The element id of the Flash embed
  var atts = { id: "player" + playerId };
  // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
  swfobject.embedSWF("http://www.youtube.com/apiplayer?" +
      "&enablejsapi=1&playerapiid=player" + playerId,
      embedId, videoWidth, videoHeight, "8", null, null, params, atts);
}

