// YouTube video controls.

/*
function onYouTubePlayerReady(thePlayer) {
	ytplayer = document.getElementById(thePlayer.id);
	
	//setInterval(updateytplayerInfo, 250);
	
	//ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
	//ytplayer.addEventListener("onError", "onPlayerError");
	addListeners();
	
}
*/

function addListeners() {
	if (ytplayer) { 
		ytplayer.addModelListener("STATE", "stateListener");
	} else {
		setTimeout("addListeners()",100);
	}
}

var ytplayerState;

function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate; 
	previousState = obj.oldstate; 

	ytplayerState = (currentState == 'IDLE')?-1:(currentState == 'BUFFERING')?3:(currentState == 'PLAYING')?1:(currentState == 'PAUSED')?2:0;
}


function onPlayerError(errorCode) {
	alert("An error occured: " + errorCode);
}

// functions for the api calls
function loadNewVideo(id, startSeconds) {
	if (ytplayer) {
		ytplayer.sendEvent('LOAD', 'http://www.youtube.com/watch?v='+id);
		//ytplayer.loadVideoById(id, parseInt(startSeconds))
	}
}

function play() {
	if (ytplayer) {
		ytplayer.sendEvent('PLAY', true);
//		ytplayer.playVideo();
	}
}

function pause() {
	if (ytplayer) {
		ytplayer.sendEvent('PLAY');
//		ytplayer.pauseVideo();
	}
}

function stop() {
	if (ytplayer) {
		ytplayer.sendEvent('STOP');
	}
}

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

function seekTo(seconds) {
	if (ytplayer) {
		player.sendEvent('SEEK', seconds);
//		ytplayer.seekTo(seconds, true);
	}
}


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

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


function setVolume(newVolume) {
	if (ytplayer) {
		player.sendEvent('VOLUME', seconds)
//		ytplayer.setVolume(newVolume);
	}
}

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

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