/*
 * Chromeless player has no controls.
 */

// 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);
}

// This function is called when the player changes state
function onPlayerStateChange(newState) {
//  updateHTML("playerState", newState);
}

function playVideo() {
  if (ytplayer) {
    ytplayer.playVideo();
  }
}

function startOver() {
  if (ytplayer) {
    ytplayer.seekTo(0);
  }
}

function pauseVideo() {
  if (ytplayer) {
    ytplayer.pauseVideo();
  }
}

function muteVideo() {
  if(ytplayer) {
    ytplayer.mute();
  }
}

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

// Set the loaded player to a specific height and width.
function resizePlayer(width, height) {
  var playerObj = document.getElementById("ytPlayer");
  playerObj.height = height;
  playerObj.width = width;

  var controls_left = (width/2)-30;
  var controls_top = (height/2)-17.5;

  $("#overlay").css('width', width);
  $("#overlay").css('height', height);
  $("#controls").css('top', controls_top);
  $("#controls").css('left', controls_left);
}

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("ytPlayer");
  // This causes the updatePlayerInfo function to be called every 250ms to
  // get fresh data from the player
//  setInterval(updatePlayerInfo, 250);
//  updatePlayerInfo();
  // ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
  // ytplayer.addEventListener("onError", "onPlayerError");
  //Load an initial video into the player
  ytplayer.loadVideoById("sVzvRsl4rEM");
  ytplayer.mute();
}

// The "main method" of this sample. Called when someone clicks "Run".
function loadPlayer() {
  // 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/apiplayer?" +
                     "&enablejsapi=1&playerapiid=player1", 
                     "videoDiv", "480", "295", "8", null, null, params, atts);
}
function _run() {
  loadPlayer();
}

function makeVideoBig() {
	startOver();
	resizePlayer(640, 385);
	unMuteVideo();
	$("#toggleShowVideo").attr('src', '/plusoneredesign/wordpress/wp-content/themes/plusone/images/closevideo.png');
	$("#togglePlayback").fadeIn();
}

function makeVideoSmall() {
	startOver();
	resizePlayer(480, 295);
	muteVideo();
	$("#toggleShowVideo").attr('src', '/plusoneredesign/wordpress/wp-content/themes/plusone/images/showvideo.png');
	$("#togglePlayback").fadeOut();
}
