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

function onYouTubePlayerReady(playerId) {
	ytplayer = document.getElementById("myytplayer");
	setInterval(updateytplayerInfo, 250);
  updateytplayerInfo();
  ytplayer.addEventListener('onStateChange', 'onytplayerStateChange');
	ytplayer.cueVideoById($F('yt_first_video'));
	// ytplayer.addEventListener("onError", "onPlayerError");
}

function onytplayerStateChange(newState) {
	// console.log("state: ", newState);

	if (newState == 1) {
		// console.log("is 1");
		ytPlayUi();
	} else if (newState == 2) {
		// console.log("is 2");
		ytPauseUi();
	} else if (newState == 3) {
		// console.log("is 3");
		ytPlayUi();
	}
}

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

// update the timebar
function updateytplayerInfo() {
    if (ytplayer) {
        updateTimebar();
				updateHTML("ytcounter", getCurrentTime());
    }
}

function updateTimebar() {
    var all = ytplayer.getDuration();
    var part = ytplayer.getCurrentTime();
    var percent = getPercent(all, part);
    var timebarWidth = 268;
    // var timebarWidth = $('timebar').getStyle('width');
		// console.log('timebarWidth: ', timebarWidth);
    document.getElementById('yttimebarIndicator').style.left = percent * (timebarWidth / 100) + "px";
}

function getPercent(all, part) {
   return (all > 0) ? (100 / all) * part : 0;
}

// functions for the api calls
function ytPlay() {
	if (ytplayer) {
		ytplayer.playVideo();
	}
}

function ytPlayUi() {
	$('ytpauselink').show();
	$('ytplaylink').hide();
}

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

function ytPauseUi() {
	$('ytpauselink').hide();
	$('ytplaylink').show();
}

function ytCueVideo(unique_id) {
	if (ytplayer) {
		ytplayer.cueVideoById(unique_id);
	}
}

function ytStop() {
	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 zeroPad(num,count) {
	var formattedNum = num + '';
	while(formattedNum.length < count) {
		formattedNum = "0" + formattedNum;
	}
	return formattedNum;
}

function getCurrentTime() {
	if (ytplayer) {
		total_seconds = parseInt(ytplayer.getCurrentTime());
		minutes = parseInt(total_seconds / 60);
		seconds = parseInt(total_seconds % 60);
		return zeroPad(minutes, 2) + ":" + zeroPad(seconds, 2);
	}
}


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

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

function ytMute() {
	if (ytplayer) {
		ytplayer.mute();
		$('ytunmutelink').show();
		$('ytmutelink').hide();
	}
}

function ytUnMute() {
	if (ytplayer) {
		ytplayer.unMute();
		$('ytmutelink').show();
		$('ytunmutelink').hide();
	}
}

function ytPlayVideo(unique_id) {
	if (ytplayer) {
		ytplayer.loadVideoById(unique_id, 0);
	}
}