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

/* OLDDDDDDDDDD
function embedPlayer(vid) {
    var params = { allowScriptAccess: 'always'};
    var atts = { id: 'myytplayer' };
    swfobject.embedSWF('http://www.youtube.com/apiplayer?enablejsapi=1&version=3', 
            'ytapiplayer', '543', '299', '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);
	ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}

function onytplayerStateChange(newState) {
//     setytplayerState(newState);
	 switch(newState){
		case -1: // unstarted
			$(".pause").hide();
			$(".play").show();
		break;
		case 0: // stoped
			$(".pause").hide();
			$(".play").show();
		break;	
		case 1: // playing
			$(".play").hide();
			$(".pause").show();
		break;	
		case 2: // paused
			$(".pause").hide();
			$(".play").show();
		break;	
	 }
}
 
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;
}
* OLDDDDDDDDDDDD
*/ 







/* 
 *******************************************************************
  *****************************************************************
 ********************************************************************
* Chromeless player has no controls.
*/

// Update a particular HTML element with a new value
/*
       * Chromeless player has no controls.
       */
      
      // Update a particular HTML element with a new value
      function updateHTML(elmId, value) {
        document.getElementById(elmId).innerHTML = value;
      }
      
      // This function is called when an error is thrown by the player
      function onPlayerError(errorCode) {
        alert("An error occured of type:" + errorCode);
      }
      
      // This function is called when the player changes state
      function onPlayerStateChange(newState) {
//        updateHTML("playerState", newState);
		switch(newState){
			case -1: // unstarted
				$(".pause").hide();
				$(".play").show();
			break;
			case 0: // stoped
				$(".pause").hide();
				$(".play").show();
			break;	
			case 1: // playing
				$(".play").hide();
				$(".pause").show();
			break;	
			case 2: // paused
				$(".pause").hide();
				$(".play").show();
			break;	
	 }
}
      
	  function getMins(time){
		var vidDurAll = Math.round(time)
		
		var vidDurMin = Math.floor(vidDurAll/60);
		
		if (vidDurMin < 10){
			vidDurMin = "0" + vidDurMin;
		}
		
		var vidDurSec = (vidDurAll - (vidDurMin * 60)) 
		
		if (vidDurSec < 10){
			vidDurSec = "0" + vidDurSec;
		}
		return vidDurMin + ":" + vidDurSec; 
	}
	  
      // Display information about the current state of the player
      function updatePlayerInfo() {
        // Also check that at least one function exists since when IE unloads the
        // page, it will destroy the SWF before clearing the interval.
        if(ytplayer && ytplayer.getDuration) {
			
			var duration = getMins(ytplayer.getDuration());
			var current = getMins(ytplayer.getCurrentTime());
			
			updateHTML("videoDuration", duration);
			updateHTML("videoCurrentTime", current);
			var tempState = ytplayer.getPlayerState();
			if (tempState == 1 ){
			  updateTimebar();
			}
//          updateHTML("bytesTotal", ytplayer.getVideoBytesTotal());
//          updateHTML("startBytes", ytplayer.getVideoStartBytes());
//          updateHTML("bytesLoaded", ytplayer.getVideoBytesLoaded());
          if (ytplayer.isMuted()){
			updateHTML("volume", 'mute');
			$(".vol-low").removeClass("active");
			$(".vol-mid").removeClass("active");
			$(".vol-high").removeClass("active");
		  }else{
			var vol = ytplayer.getVolume();
			updateHTML("volume", vol);
			switch(vol){
				case 25: 
					$(".vol-low").addClass("active");
					$(".vol-mid").removeClass("active");
					$(".vol-high").removeClass("active");
				break;
				case 50: 
					$(".vol-low").addClass("active");
					$(".vol-mid").addClass("active");
					$(".vol-high").removeClass("active");
				break;
				case 100: 
					$(".vol-low").addClass("active");
					$(".vol-mid").addClass("active");
					$(".vol-high").addClass("active");
				break;
			}
		  }
		  
        }
      }
      
      // Allow the user to set the volume from 0-100
      function setVideoVolume(volume) {
//        var volume = parseInt(document.getElementById("volumeSetting").value);
        if(isNaN(volume) || volume < 0 || volume > 100) {
          alert("Please enter a valid volume between 0 and 100.");
        }
        else if(ytplayer){		
          unMuteVideo();		  
          ytplayer.setVolume(volume);		  
        }
      }
      
      function playVideo() {
        if (ytplayer) {
          ytplayer.playVideo();
        }
      }
      
      function pauseVideo() {
        if (ytplayer) {
          ytplayer.pauseVideo();
        }
      }
      
      function muteVideo() {
        if(ytplayer) {
			ytplayer.mute();
			$(".mute").hide();
			$(".unmute").show();
        }
      }
      
      function unMuteVideo() {
        if(ytplayer) {
			ytplayer.unMute();
			$(".unmute").hide();
			$(".mute").show();
        }
      }
      
      
      // This function is automatically called by the player once it loads
      function onYouTubePlayerReady(playerId) {
        ytplayer = document.getElementById("ytPlayer");
        // This causes the updatePlayerInfo function to be called every 250ms to
        // get fresh data from the player
        setInterval(updatePlayerInfo, 250);
        updatePlayerInfo();
        ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
        ytplayer.addEventListener("onError", "onPlayerError");
        //Load an initial video into the player
        ytplayer.cueVideoById(ytvideo);		
      }
      
      // The "main method" of this sample. Called when someone clicks "Run".
      function loadPlayer(vid) {
        // Lets Flash from another domain call JavaScript
        var params = { allowScriptAccess: "always" };
        // The element id of the Flash embed
        var atts = { id: "ytPlayer" };
        // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
        swfobject.embedSWF("http://www.youtube.com/apiplayer?" +
                           "&enablejsapi=1&playerapiid=player1", 
                           "videoDiv", "476", "400", "8", null, null, params, atts);
		ytvideo = vid;				   
      }

	function updateTimebar() {
		var all = ytplayer.getDuration();
		var part = ytplayer.getCurrentTime();
		var percent = getPercent(all, part);
		var timebarWidth = 245; 
		var nagTimebarWidth = -245; 
		document.getElementById('timebarIndicator').style.left = (nagTimebarWidth + Math.round((percent * (timebarWidth / 100)))) + "px";
		var right = percent * (timebarWidth / 100);
//		console.log(percent);
//		console.log(right);
	}
	
	function getPercent(all, part) {
	   return (all > 0) ? (100 / all) * part : 0;
	}
