var video;
    var controls;
    var playControl;
    var progressControl;
    var progressHolder;
    var playProgress;
    var playProgressInterval;
    var currentTimeDisplay;
    var durationDisplay;
    var volumeControl;
    var volumeDisplay;
    var fullscreenControl;
 
    var videoWasPlaying;
    var videoIsFullScreen;
    var videoOrigWidth;
    var videoOrigHeight;
 
    // Set up the controls when the DOM is ready.
    var bodyLoaded = function(){

      track_visit = readCookie('track_visit');

      video = document.getElementById("video");
      controls = document.getElementById("controls");
      playControl = document.getElementById("play_control");
      progressControl = document.getElementById("progress_control");
      progressHolder = document.getElementById("progress_bar");
      playProgress = document.getElementById("play_progress");
      currentTimeDisplay = document.getElementById("current_time_display");
      durationDisplay = document.getElementById("duration_display");
      volumeControl = document.getElementById("volume_control");
      volumeDisplay = volumeControl.children[0]
      fullscreenControl = document.getElementById("fullscreen_control");
 
      showController();
      positionController();

	if (track_visit != 2){
		playVideo();
	} else {
		$('#controls, #adepteo_logo').stop().animate({"opacity": "0.85"}, "fast");
	}

      $('.video').hover(
	function() {
	$('#controls, #adepteo_logo').stop().animate({"opacity": "0.85"}, "slow");
	},
	function() {
	$('#controls, #adepteo_logo').stop().animate({"opacity": "0"}, "slow");
      });
      
 
      playControl.addEventListener("click", function(){
        if (video.paused) {
          playVideo();
        } else {
          pauseVideo();
        }
      }, true);
 
      progressHolder.addEventListener("mousedown", function(){
        stopTrackingPlayProgress();
 
        if (video.paused) {
          videoWasPlaying = false;
        } else {
          videoWasPlaying = true;
          video.pause();
        }
 
        blockTextSelection();
        document.onmousemove = function(e) {
          setPlayProgress(e.pageX);
        }
 
        document.onmouseup = function() {
          unblockTextSelection();
          document.onmousemove = null;
          document.onmouseup = null;
          if (videoWasPlaying) {
            video.play();
            trackPlayProgress();
          }
        }
      }, true);
 
      progressHolder.addEventListener("mouseup", function(e){
        setPlayProgress(e.pageX);
      }, true);
 
      volumeControl.addEventListener("mousedown", function(){
        blockTextSelection();
        document.onmousemove = function(e) {
          setVolume(e.pageX);
        }
        document.onmouseup = function() {
          unblockTextSelection();
          document.onmousemove = null;
          document.onmouseup = null;
        }
      }, true);
 
      volumeControl.addEventListener("mouseup", function(e){
        setVolume(e.pageX);
      }, true);
 
      updateVolumeDisplay();
 
      fullscreenControl.addEventListener("click", function(){
        if (!videoIsFullScreen) {
          fullscreenOn();
        } else {
          fullscreenOff();
        }
      }, true);
    }
 
    function playVideo(){
      video.play();
      playControl.className = "pause";
      trackPlayProgress();
    }
 
    function pauseVideo(){
      video.pause();
      playControl.className = "play";
      stopTrackingPlayProgress();
    }
 
    function positionController(){
      //controls.style.top = (video.offsetHeight - controls.offsetHeight) + "px";
      controls.style.left = "0px";
      controls.style.width = video.offsetWidth + "px";
      sizeProgressBar();
    }
 
    function showController(){
      controls.style.display = "block";
    }
 
    function hideController(){
      controls.style.display = "none";
    }
 
    function sizeProgressBar(){
      //progressControl.style.width = (controls.offsetWidth - 125) + "px";
      progressHolder.style.width = (progressControl.offsetWidth - 80) + "px";
      updatePlayProgress();
    }
 
    function trackPlayProgress(){
      playProgressInterval = setInterval(updatePlayProgress, 33);
    }
 
    function stopTrackingPlayProgress(){
      clearInterval(playProgressInterval);
    }
 
    function updatePlayProgress(){
      playProgress.style.width = ((video.currentTime / video.duration) * (progressHolder.offsetWidth - 2)) + "px";
      updateTimeDisplay();
    }
 
    function setPlayProgress(clickX) {
      var newPercent = Math.max(0, Math.min(1, (clickX - findPosX(progressHolder)) / progressHolder.offsetWidth));
      video.currentTime = newPercent * video.duration
      playProgress.style.width = newPercent * (progressHolder.offsetWidth - 2)  + "px";
      updateTimeDisplay();
    }
 
    function updateTimeDisplay(){
      currentTimeDisplay.innerHTML = formatTime(video.currentTime);
      if (video.duration) durationDisplay.innerHTML = formatTime(video.duration);
    }
 
    function setVolume(clickX) {
      var newVol = (clickX - findPosX(volumeControl)) / volumeControl.offsetWidth;
      if (newVol > 1) {
        newVol = 1;
      } else if (newVol < 0) {
        newVol = 0;
      }
      video.volume = newVol;
      updateVolumeDisplay();
    }
 
    // Unique to these controls.
    function updateVolumeDisplay(){
      var volNum = Math.floor(video.volume * 6);
      for(var i=0; i<6; i++) {
        if (i < volNum) {
          volumeDisplay.children[i].style.borderColor = "#000";
        } else {
          volumeDisplay.children[i].style.borderColor = "#2d5785";
        }
      }
    }
 
    function fullscreenOn(){
      videoIsFullScreen = true;
      videoOrigWidth = video.offsetWidth;
      videoOrigHeight = video.offsetHeight;
 
      video.style.width = window.innerWidth + "px";
      video.style.height = window.innerHeight + "px";
      video.style.position = "fixed";
      video.style.left = 0;
      video.style.top = 0;
      controls.style.position = "fixed";
      positionController();
 
      fullscreenControl.className = "active";
    }
 
    function fullscreenOff(){
      videoIsFullScreen = false;
      video.style.width = videoOrigWidth + "px";
      video.style.height = videoOrigHeight + "px";
      video.style.position = "static";
      controls.style.position = "absolute";
      positionController();
      fullscreenControl.className = "";
    }
 
    function blockTextSelection(){
      document.body.focus();
      document.onselectstart = function () { return false; };
    }
 
    function unblockTextSelection(){
      document.onselectstart = function () { return true; };
    }
 
    // Return seconds as MM:SS
    function formatTime(seconds) {
      seconds = Math.round(seconds);
      minutes = Math.floor(seconds / 60);
      minutes = (minutes >= 10) ? minutes : "0" + minutes;
      seconds = Math.floor(seconds % 60);
      seconds = (seconds >= 10) ? seconds : "0" + seconds;
      return minutes + ":" + seconds;
    }
 
    // Get objects position on the page
    function findPosX(obj) {
      var curleft = obj.offsetLeft;
      while(obj = obj.offsetParent) {
        curleft += obj.offsetLeft;
      }
      return curleft;
    }

	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
