/*
HD Switch for VideoJS
Version 0.1
This file is part of the extension "HD Switch for VideoJS".

"HD Switch for VideoJS" is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

"HD Switch for VideoJS" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with VideoJS.  If not, see <http://www.gnu.org/licenses/>.
*/

//extend "buildControlBar" by the HD button 
VideoJS.prototype.oldControlBar = VideoJS.prototype.buildAndActivateControlBar;
VideoJS.prototype.buildAndActivateControlBar = function() {
    this.oldControlBar();
    this.hdControl = _V_.createElement("div", { className: "vjs-hd-control vjs-sd", innerHTML: '<span title="in HD anschauen">HD</span>' });
    this.controls.appendChild(this.hdControl);
    this.hdControl.addEventListener("click", this.onHdControlClick.context(this), false);
	if(typeof this.options.hdSuffix == "undefined")
		this.options.hdSuffix ="_hd";	
	// additional FB button script by ED
	this.fbShare = _V_.createElement("div", { className: "vjs-fb-share", innerHTML: '<img class="fb-link" title="Dieses Video auf Facebook posten" src="bilder/facebook_f.gif" width="10" height="19"/>'});
	this.controls.appendChild(this.fbShare);
	this.fbShare.addEventListener("click", this.onFbShareControlClick.context(this), false); 
}


// listen to clicks on HD button
VideoJS.prototype.onHdControlClick = function(event){
	
	var bufferTime = this.currentTime();
	var isPause = this.video.paused;
	
	if (!this.videoIsHd) {
        this.videoIsHd = true;
        this.hdControl.className = "vjs-hd-control vjs-hd";
        if(typeof this.sdSrc == "undefined")
        {
            this.sdSrc = this.video.currentSrc;
            // this.hdSrc = this.sdSrc.slice(0,this.sdSrc.length-4) + this.options.hdSuffix + this.sdSrc.slice(this.sdSrc.length-4);
             this.hdSrc = this.sdSrc.slice(0,this.sdSrc.lastIndexOf('.')) + this.options.hdSuffix + this.sdSrc.slice(this.sdSrc.lastIndexOf('.'));
        }
        this.video.src = this.hdSrc;
    } else {
        this.videoIsHd = false;
        this.hdControl.className = "vjs-hd-control vjs-sd";
        this.video.src = this.sdSrc;
    }
    //copy status (play,pause)
    this.video.load();
   	// added by ed: wait for matedata to load before jumping to currentTime ( http://blog.gingertech.net/2009/08/19/jumping-to-time-offsets-in-videos/ )
	this.video.addEventListener("loadedmetadata", function () {
   		this.currentTime = bufferTime-0.2;
		isPause ? this.pause() : this.play();
   	}, false);
}

// FB added by ED
VideoJS.prototype.onFbShareControlClick = function(event){
	this.video.pause();
	// this.fbShare.firstChild.href += "share.php?u=" + this.video.src;
}


// overwrites original behaviour
VideoJS.player.newBehavior("fullscreenToggle", function(element){
    _V_.addListener(element, "click", this.onFullscreenToggleClick.context(this));
  },{
    // When the user clicks on the fullscreen button, update fullscreen setting
    onFullscreenToggleClick: function(event){
      if (!this.videoIsFullScreen) {
        // new code from ED
        leftMarginBuffer = document.getElementById('filmcontainer').style.marginLeft;
        document.getElementById('filmcontainer').style.marginLeft = "0";
        // if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
			document.getElementById('menue_links').style.zIndex = 3;
        //}
        this.enterFullScreen();
      } else {
        this.exitFullScreen();
        document.getElementById('filmcontainer').style.marginLeft = leftMarginBuffer;
        //document.getElementById('menue_links').style.display = "block";
        document.getElementById('menue_links').style.zIndex = 5;
      }
    },

    fullscreenOnWindowResize: function(event){ // Removeable
      this.positionControlBars();
    },
    // Create listener for esc key while in full screen mode
    fullscreenOnEscKey: function(event){ // Removeable
      if (event.keyCode == 27) {
        this.exitFullScreen();
        document.getElementById('filmcontainer').style.marginLeft = leftMarginBuffer;
        // document.getElementById('menue_links').style.display = "block";
        document.getElementById('menue_links').style.zIndex = 5;
      }
    }
  }
);
