function supportsVideo() {
	return document.createElement('video').canPlayType;
}

function supportsH264() {
	var v;
	if (!supportsVideo()) { 
		return false;
	}
	v = document.createElement("video");
	return v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
}

function changeFileExtension(fileName, extension) {
	var fileExtension = extension || "mp4";
	return fileName.slice(0, -3) + fileExtension;
}

function secToMin(seconds){
	var minutes = Math.floor(seconds / 60);
	seconds = ("0" + Math.floor(seconds % 60)).slice(-2);
	return minutes + ":" + seconds;
}

if (typeof(ecarlist) === 'undefined') ecarlist = {};

ecarlist.htmlVideo = function (playerOptions) {
	var options, video, playlist, controls;
	
	options = $ec.extend({
		container : "#ec_flowplayer",
		controls : {
			id : "#ec_flowplayer_controlbar",
			components : "play track time" /* Components to add to the control bar. Options are play, track, time, next, previous. Space seperated. */
		}
	}, playerOptions);
	
	function init () {		
		$ec(".ecl_video_unmute").remove(); /* ipad and iphone do not support mute() */
		$ec(options.container).empty()
		.append(video.playerElement)
		.click(function (event) {
			return false;
		});		
		playlist.loadFirstVideo();		
		controls.init(options.controls);		
	}
	
	
	video = {
		playerElement: document.createElement("video"),
		setSource: function (videoSource) {
			$ec(this.playerElement).attr('src', changeFileExtension(videoSource));
			var interval = window.setInterval(function(){
				if (video.playerElement.readyState > 0){
					$ec(controls).trigger('videoLoaded');
					window.clearInterval(interval);
				}
			}, 500);
			
		},
		play: function () {
			$ec('#playAgain').remove();
			window.clearInterval(this.playTimer);			
			this.playerElement.play();
			this.playing = true;
			$ec(controls).trigger('videoPlaying');			
			this.playTimer = window.setInterval(function(){
				$ec(controls).trigger('videoTimeUpdated');
			}, 250);
		},
		pause: function () {
			window.clearInterval(this.playTimer);
			this.playerElement.pause();		
			this.playing = false;	
			$ec(controls).trigger('videoPaused');
		},
		playTimer: null,
		playing: false
	};
	
	$ec(video).bind('videoSourceChanged', function () {
		this.playing = true;
		this.play();
	});	
	
	$ec(video.playerElement).bind('ended', function () {
		video.pause();
		video.playing = false;
		playlist.loadNextVideo();		
	});
	
	$ec(video).bind("lastVideoEnded", function () {
		$ec("#playAgain").remove();
		$ec('<a id="playAgain">play again</a>')
			.click(function(){
				playlist.loadFirstVideo();
			})
			.appendTo(options.container);			
	});
	
	
	playlist = {
		videos: $ec('#ec_playlist a')
			.each(function () {
				$ec(this).attr('count', $ec(this).attr('id').replace('flowplayer_video', ''));				
			})
			.click(function (event) {
				event.preventDefault();
				playlist.loadVideo(this);				
			}),
		currentVideoNum: "1",
		numVideos: $ec('#ec_playlist a').size(),
		loadVideo: function (thisVid) {
			video.setSource(thisVid.href);
			this.setCurrentVideo($(thisVid).attr('count'));
			$ec(video).trigger('videoSourceChanged');
		},
		loadFirstVideo: function () { 
			video.setSource(playlist.videos[0].href);
			this.setCurrentVideo($ec(this.videos[0]).attr('count'));
		},
		loadNextVideo: function () {
			if (this.currentVideoNum < this.numVideos) {				
				video.setSource(this.videos[this.currentVideoNum].href);
				this.setCurrentVideo(parseInt(this.currentVideoNum, 10) + 1);
				$ec(video).trigger('videoSourceChanged');				
			} else if (video.playing === false) {
				$ec(video).trigger('lastVideoEnded');
			}					
		},
		loadPrevVideo: function () {
			if (this.currentVideoNum > 1) {				
				this.setCurrentVideo(parseInt(this.currentVideoNum, 10) - 1);
				video.setSource(this.videos[this.currentVideoNum - 1].href); /* video list is 0 indexed, currentVideoNum is not, so subtract one more. __confusing__ */
				$ec(video).trigger('videoSourceChanged');
			}					
		},
		setCurrentVideo: function (videoNum) {						
			this.currentVideoNum = videoNum;
			this.videos.removeClass('playing');
			$ec(this.videos.get(videoNum - 1)).addClass('playing');
		}
	};	
	
	
	controls = {
		controlBar: $(options.controls.id),
		play: $ec('<a class="play">play</a>'),
		track: $ec('<div class="track"><div class="buffer"></div><div class="progress"></div></div>'),
		time: $ec('<div class="time"><span>0:00</span> / <strong></strong></div>'),
		next: $ec('<div class="next">next</div>'),
		previous: $ec('<div class="previous">previous</div>'),
		init: function(controlOptions){
			if (controlOptions.components.indexOf("play") !== -1) {
				this.controlBar.append(this.play);
				
				$ec(this).bind('videoPlaying', function () {
					$ec(this.play).removeClass('play').addClass('pause');
				});
				
				$ec(this).bind('videoPaused', function () {
					$ec(this.play).removeClass('pause').addClass('play');
				});	
		
				$ec(this.play).click(function () {
					if ($ec(this).hasClass('play')) {
						video.play();
					} else {
						video.pause();
					}
				});
			}
			
			if (controlOptions.components.indexOf("track") !== -1) {
				this.controlBar.append(this.track);
				$ec(this).bind('videoLoaded', function () {
					var ratio = this.track.width() / Math.round(video.playerElement.duration);
					$ec(".buffer", this.track).width(Math.round(video.playerElement.duration) * ratio);
				});	
				$ec(this).bind('videoTimeUpdated', function () {
					var ratio = this.track.width() / Math.round(video.playerElement.duration);
					$ec(".buffer", this.track).width(Math.round(video.playerElement.duration) * ratio);					
					$ec(".progress", this.track).width(Math.round(video.playerElement.currentTime) * ratio);
				});
				
				$ec(".buffer, .progress", this.track).click(function(e){
					video.playerElement.currentTime = Math.round((e.pageX - $(this).offset().left) / (controls.track.width() / video.playerElement.duration));
					$ec(controls).trigger('videoTimeUpdated');
				});
			}
			
			if (controlOptions.components.indexOf("time") !== -1) {
				this.controlBar.append(this.time);
				
				$ec(this).bind('videoLoaded', function () {
					$ec("strong", this.time).html(secToMin(Math.round(video.playerElement.duration)));
				});
				$ec(this).bind('videoTimeUpdated', function () {
					$ec("span", this.time).html(secToMin(Math.round(video.playerElement.currentTime)));
				});
			}	
			
			if (controlOptions.components.indexOf("next") !== -1) {
				this.controlBar.append(this.next);
				
				$ec(this.next).click(function () {
					playlist.loadNextVideo();
				});
			}	
			
			if (controlOptions.components.indexOf("previous") !== -1) {
				this.controlBar.append(this.previous);
				
				$ec(this.previous).click(function () {
					playlist.loadPrevVideo();
				});
			}			
		}
	};				
	
	init();
};



