var youtubes_on_page = 0;
var video_id_to_load;

//setup the quality by uncommenting one:
//var quality_type = ""; // Shitty
//var quality_type = "&ap=%2526fmt%3D22"; // HD
var quality_type = "&ap=%2526fmt%3D18"; // High quality but not HD

function add_youtube(video_id){
	video_id_to_load = video_id;
    var params = { allowScriptAccess: "always", bgcolor: "#cccccc" };
    var atts = { id: "myytplayer" };
    swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer" + quality_type, "ytapiplayer", "681", "383", "8", null, null, params, atts);
}


var ytplayer;


function updateHTML(elmId, value) {
  document.getElementById(elmId).innerHTML = value;
}

function hide_and_begin() {
	var duration = 0.6;
	new Effect.Appear("swf", {duration:duration});
	new Effect.Fade("preview_image", {duration:duration, afterFinish:function(){
		play();
	}});
}

function setytplayerState(newState) {
	if (newState == 1) {
		$("playpause").addClassName("playing");
	}
	else {
		$("playpause").removeClassName("playing");
	}
}

function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("myytplayer");
  setInterval(updateytplayerInfo, 250);
  updateytplayerInfo();
  ytplayer.loadVideoById(video_id_to_load, 0);
  ytplayer.pauseVideo();
  ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}

function onytplayerStateChange(newState) {
  setytplayerState(newState);
}

function updateytplayerInfo() {
	if (ytplayer) {
		if (getBytesLoaded() > 0) {
			var loaded_percent = getBytesLoaded() / getBytesTotal() * 100.0;
			$("loaded_bar").style.width = loaded_percent + "%";
		}
		if (getCurrentTime() > 0) {
			var duration_percent = getCurrentTime() / getDuration() * 100.0;
			$("normal_bar").style.width = duration_percent + "%";
		}

		// updateHTML("bytesloaded", getBytesLoaded());
		// updateHTML("bytestotal", getBytesTotal());
		// updateHTML("videoduration", getDuration());
		// updateHTML("videotime", getCurrentTime());
		// updateHTML("startbytes", getStartBytes());
		// updateHTML("volume", getVolume());
	}
}

// functions for the api calls
function loadNewVideo(id, startSeconds) {
  if (ytplayer) {
    ytplayer.loadVideoById(id, parseInt(startSeconds));
  }
}

function cueNewVideo(id, startSeconds) {
  if (ytplayer) {
    ytplayer.cueVideoById(id, startSeconds);
  }
}

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

function play_pause() {
	if (ytplayer) {
		if (ytplayer.getPlayerState() == 2) {
			play();
		}
		else {
			pause();
		}
	}
	else {
		hide_and_begin();
	}
}


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

function stop() {
  if (ytplayer) {
    ytplayer.stopVideo();
  }
}

function getPlayerState() {
  if (ytplayer) {
    return ytplayer.getPlayerState();
  }
}

function seekTo(seconds) {
  if (ytplayer) {
    ytplayer.seekTo(seconds, true);
  }
}

function getBytesLoaded() {
  if (ytplayer) {
    return ytplayer.getVideoBytesLoaded();
  }
}

function getBytesTotal() {
  if (ytplayer) {
    return ytplayer.getVideoBytesTotal();
  }
}

function getCurrentTime() {
  if (ytplayer) {
    return ytplayer.getCurrentTime();
  }
}

function getDuration() {
  if (ytplayer) {
    return ytplayer.getDuration();
  }
}

function getStartBytes() {
  if (ytplayer) {
    return ytplayer.getVideoStartBytes();
  }
}

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

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

function getEmbedCode() {
  alert(ytplayer.getVideoEmbedCode());
}

function getVideoUrl() {
  alert(ytplayer.getVideoUrl());
}

function setVolume(newVolume) {
  if (ytplayer) {
    ytplayer.setVolume(newVolume);
  }
}

function getVolume() {
  if (ytplayer) {
    return ytplayer.getVolume();
  }
}

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

