/*
HD Switch for VideoJS
Version 0.2
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/>.

see http://martin-gamnitzer.de/2010/11/hd-button-fur-den-html5-videoplayer-videojs/
and http://help.videojs.com/discussions/suggestions/14-implemented-format-and-quality-selector

Original author: Martin Gamnitzer
Changed by: Martin Schnabel

*/
window.has_videojs_hdswitch = true;
VideoJS.player.newBehavior("hd_button", function(player){
		this.onPlay(this.volumeHack); // Hack for volume on Opera
	},{
	checkHD: function() {
		src = this.video.currentSrc;
		format = src.slice(src.lastIndexOf("."));
		dataDuration = this.video.attributes["data-duration"]
		if (dataDuration != undefined)
			this.options.customDuration = dataDuration.value;
		var children = this.video.children;
	    for (var i=0,j=children.length; i<j; i++) {
	      if (children[i].tagName.toUpperCase() == "SOURCE") {
	    	quality = children[i].attributes["data-quality"];
	    	source = children[i].src;
	        if (quality != undefined && quality.value == 'hd' && source.substr(source.length-format.length) == format) {
	        	return source;
	        }
	      }
	    }
	    return false;
	},
	createHDSwitch: function() {
		if (!this.controls) return;
		var hasHD = this.checkHD();
		var classNames = "vjs-hd-disabled vjs-sd";
		if (hasHD != false) {
			classNames = "vjs-hd-control vjs-sd";
			this.sdSrc = this.video.currentSrc;
			this.hdSrc = hasHD;
		}
		this.hdControl = _V_.createElement("div", { className: classNames, innerHTML: "<span>HD</span>" });
	    this.controls.appendChild(this.hdControl);
	    if (hasHD != false)
	    	this.hdControl.addEventListener("click", this.onHdControlClick.context(this), false);
		if (typeof this.options.hdSuffix == "undefined")
			this.options.hdSuffix ="_hd";
	},
	onHdControlClick: function(event) {
	    if (!this.videoIsHd) {
	        this.videoIsHd = true;
	        this.hdControl.className = "vjs-hd-control vjs-hd";
	        this.changeSource(this.hdSrc, true)
	    } else {
	        this.videoIsHd = false;
	        this.hdControl.className = "vjs-hd-control vjs-sd";
	        this.changeSource(this.sdSrc, true);
	    }
	},
	changeSource: function(newSrc, resume) {
		this.videoWasPlaying = !this.video.paused;
		this.pause();
		this.resumeTime = this.video.currentTime-0.5;
		if (this.resumeTime < 0) this.resumeTime = 0;
		this.showSpinners();
		this.video.src = newSrc;
		if (!resume) return;
		this.video.load();
		this.percentLoaded = 0;
		this.trackBuffered();
		this.checkResume();
	},
	checkResume: function() {
		this.checkCount++;
		if (this.checkCount > 200){ this.checkCount=0; this.video.load(); this.play(); return; }
		if (this.video.readyState < this.video.HAVE_METADATA) setTimeout(this.checkResume.context(this), 40);
		else {
			this.checkCount=0; 
			this.currentTime(this.resumeTime);
			if (this.videoWasPlaying) this.play();
		}
	},
	/* A bug in opera 11.50 forces us to actually change the volume to make it have any effect */
	volumeHack: function(event) {
		if (window.opera) {
			volume = this.values.volume
			if (volume > 0.5)
				volume -= 0.001;
			else
				volume += 0.001;
			this.volume(volume);
		}
	}
});
