/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function setActive(el){
	jQuery('.vid-thumbs a').attr('id','');
	jQuery(el).attr('id','activevid');
}


function embedPlayer(vid) {
    var params = { allowScriptAccess: 'always',wmode:'transparent'};
    var atts = { id: 'myytplayer' };
    swfobject.embedSWF('http://www.youtube.com/apiplayer?enablejsapi=1&version=3', 
            'ytapiplayer', '425', '356', '8', null, null, params, atts);
			ytvideo = vid;
}
 
function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById('myytplayer');
    setInterval(updateytplayerInfo, 250);
    updateytplayerInfo();
    ytplayer.addEventListener('onStateChange', 'onytplayerStateChange');
    document.getElementById('controls').style.display = 'block';
	ytplayer.loadVideoById(ytvideo, 0);
	vidMute();
}
 
function onytplayerStateChange(newState) {
	var curVideo = jQuery('#activevid').attr('class').split('vid-');
	if(newState == 0){
		jQuery('.vid-thumbs a').attr('id','');
		var nxtVideo = jQuery('.vid-'+curVideo[1]).parent().next().find('a');
		jQuery(nxtVideo).attr('id','activevid');
		nxtVideo = jQuery(nxtVideo).attr('class').split('vid-');
		ytplayer.loadVideoById(nxtVideo[1], 0);
	}
     setytplayerState(newState);
}
 
function showVolume() {
    alert( ytplayer.getVolume() );
}
 
function updateytplayerInfo() {
    if (ytplayer) {
        updateTimebar();
    }
}
 
function updateTimebar() {
    var all = ytplayer.getDuration();
    var part = ytplayer.getCurrentTime();
    var percent = getPercent(all, part);
    var timebarWidth = 410; 
    document.getElementById('timebarIndicator').style.left = percent * (timebarWidth / 100) + "px";
}
 
function getPercent(all, part) {
   return (all > 0) ? (100 / all) * part : 0;
}
