
/*
YouTube Controller - Liran Oz 13/11/2008
*/

var enableAutoFwd = false;
function onYouTubePlayerReady(playerId) {
	var ytplayer = document.getElementById("myytplayer");
	ytcp.setytplayerObj(ytplayer);
	ytcp.ytInitPlayer();
	ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}

function onytplayerStateChange(newState) {
	if (newState == 0 && !ytcp.isIgnoreEvents()) {
		if (enableAutoFwd)
			ytcp.ytnext();			//try cue next video
		else {
			ytcp.ytstop();
		}
	}
}


function ytCP(_objName, _videosArr, _ytCallBack)
{
	var playerState = 0;	// 0 -stopped, 1 - playing, 2 - paused
	var curIdx = 0;			//which video is playing (it's index in video)
	var prevObj = null;
	var pauseObj = null;
	var playObj = null;
	var nextObj = null;
	var volObj = null;
	var txtObj = null;
	var volMaskObj = null;
	var monObj = null;
	var cpId = 'ytControlArea';
	var volId = 'volMeter';
	var volCnt = 'volCnt';
	var txtId = 'ytTextArea';
	var volObjWidth = 94;		//should be calculated for general case
	var cpObj = null;
	var objName = _objName;
	var allow = false;
	var totalVideos = _videosArr.length;
	var videosArr = _videosArr;
	var ytplayer = null;
	var ignoreEvents = false;
	var playerVisible = true;
	var playerInit = false;
	var playerMuted = false;
	var callback = _ytCallBack || null;		//pass an prdId to this one

	//build the player
	cpObj = document.getElementById(cpId);
	if (!cpObj)
		return;
	//get obj's
	prevObj = document.getElementById('ytPrev');
	pauseObj = document.getElementById('ytPause');
	playObj = document.getElementById('ytPlay');
	nextObj = document.getElementById('ytNext');
	monObj = document.getElementById('ytMon');

	if (!prevObj || !pauseObj || !playObj || !nextObj || !monObj)		//must have these!
		return;

	//set volume intervals
	volObj = document.getElementById(volCnt);
	volMaskObj = document.getElementById(volId);
	if (!volObj)
		return;

	var vols = volObj.getElementsByTagName('a');
	if (!vols || !vols.length)
		return;

	allow = true;

	this.ytInitPlayer = function()
	{
		var volInterval = 100 / (vols.length-1);
		for (var i=0; i<vols.length; i++) {
			volLevel = Math.ceil(volInterval * i);
			var eStr = objName + ".ytsetvolalt(this);";
			vols[i].setAttribute("onclick", objName + ".ytsetvol(" + volLevel + ");")
			vols[i].onclick = function() {eval(eStr)};
		}
		playerInit = true;

		txtObj = document.getElementById(txtId);
		this.ytsetvol(50);
		this.refreshPlayerStatus();
		this.hidePlayer();
	}

	this.setytplayerObj = function(obj)
	{
		ytplayer = obj;
	}

	this.showPlayer = function()
	{
		if (playerInit && ytplayer && !playerVisible) {
			playerVisible = true;
			ytplayer.style.visibility = "visible";
		}
	}

	this.hidePlayer = function()
	{
		if (playerInit && ytplayer && playerVisible) {
			playerVisible = false;
			ytplayer.style.visibility = "hidden";
		}
	}

	this.isIgnoreEvents = function()
	{
		return ignoreEvents;
	}

	this.refreshPlayerStatus = function()
	{
		if (!allow)
			return;
		//prev button
		if (curIdx == 0) {
			prevObj.className = 'prevButtonDisabled';
			//webkit's
			prevObj.setAttribute('onclick', 'void(0);');
			//IE's
			prevObj.onclick = function() {};
		}
		else {
			prevObj.className = 'prevButton';
			prevObj.setAttribute('onclick', objName + '.ytprev();');
			prevObj.onclick = function() {eval(objName + ".ytprev();")};
		}
		//next button
		if (curIdx >= totalVideos-1) {	//is not supposed to be >
			nextObj.className = 'nextButtonDisabled';
			nextObj.setAttribute('onclick', 'void(0);');
			nextObj.onclick = function() {};
		}
		else {
			nextObj.className = 'nextButton';
			nextObj.setAttribute('onclick', objName + '.ytnext();');
			nextObj.onclick = function() {eval(objName + ".ytnext();")};
		}

		//play & pause button
		if (playerState == 1) {
			playObj.className = 'playButtonDisabled';
			playObj.setAttribute('onclick', objName + '.ytstop();');
			playObj.onclick = function() {eval(objName + ".ytstop();")};

			pauseObj.className = 'pauseButton';
			pauseObj.setAttribute('onclick', objName + '.ytpause();');
			pauseObj.onclick = function() {eval(objName + ".ytpause();")};
		} else {
			playObj.className = 'playButton';
			playObj.setAttribute('onclick', objName + '.ytplay();');
			playObj.onclick = function() {eval(objName + ".ytplay();")};

			pauseObj.className = 'pauseButtonDisabled';
			pauseObj.setAttribute('onclick', 'void(0);');
			pauseObj.onclick = function() {};
		}

		//mute button
		if (playerMuted) {
			monObj.className = 'monitorButtonDisabled';
			monObj.setAttribute('onclick', objName + '.ytunmute();');
			monObj.onclick = function() {eval(objName + ".ytunmute();")};
		}
		else {
			monObj.className = 'monitorButton';
			monObj.setAttribute('onclick', objName + '.ytmute();');
			monObj.onclick = function() {eval(objName + ".ytmute();")};
		}

		ignoreEvents = false;
	}

	this.openVideo = function(idx)
	{
		if (idx < 0 || idx >= totalVideos)
			return;
		this.showPlayer();
		ignoreEvents = true;
		curIdx = idx;
		if (playerState)
			clearVideo();
		playerState = 0;
		this.ytplay();
	}

	this.ytplay = function()
	{
		this.showPlayer();
		if (playerState == 1)
			return;
		if (playerState == 0) {		//was stopped
			loadNewVideo(videosArr[curIdx]['id'], 0);
			playerState = 1;
			//load callback if avail
			if (callback)
				callback(videosArr[curIdx]['prdId']);
			if (videosArr[curIdx]['name'])
				this.setText(videosArr[curIdx]['name']);		//set caption text
		}
		else {						//continue from pausing
			play();
			playerState = 1;
		}
		this.refreshPlayerStatus();
	}

	this.ytstop = function()
	{
		ignoreEvents = true;
		playerState = 0;
		stop();
		this.hidePlayer();
		this.refreshPlayerStatus();
	}


	this.ytpause = function()
	{
		if (playerState == 0 || playerState == 2)
			return;

		playerState = 2;
		pause();
		this.refreshPlayerStatus();
	}

	this.ytprev = function()
	{
		if (curIdx == 0)
			return;
		ignoreEvents = true;
		curIdx --;
		if (playerState)
			clearVideo();
		playerState = 0;
		this.showPlayer();
		this.ytplay();
	}

	this.ytnext = function()
	{
		if (curIdx == totalVideos-1) {
			this.ytstop();		//stop player as well
			this.hidePlayer();
			return;
		}
		ignoreEvents = true;
		curIdx ++;
		if (playerState)
			clearVideo();
		playerState = 0;
		this.showPlayer();
		this.ytplay();
	}

	this.ytsetvol = function(newVol)
	{
		if (newVol < 0 || newVol > 100)
			newVol = 50;
		this.ytunmute();		//unmute if muted
		setVolume(newVol);
		//clip the element
		var elmWidth = Math.ceil(volObjWidth * newVol / 100);
		volMaskObj.style.width = elmWidth + 'px';
	}

	this.ytsetvolalt = function(obj)
	{
		for(i=0; i<vols.length; i++) {
			if (vols[i] == obj)
				break;
		}
		if (i == vols.length)
			return;

		var volInterval = 100 / (vols.length-1);
		this.ytsetvol(i * volInterval);

	}

	this.ytmute = function()
	{
		if (playerMuted)
			return;
		playerMuted = true;
		mute();
		this.refreshPlayerStatus();
	}

	this.ytunmute = function()
	{
		if (!playerMuted)
			return;
		playerMuted = false;
		unMute();
		this.refreshPlayerStatus();
	}

	this.setText = function(str)
	{
		if (!txtObj)
			return;
		txtObj.innerHTML = str;
	}



	/*
	YouTube's chromeless player from youtube API
	*/


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



