/**
 * Defines step forward/back controls for zPlayer.
 * 
 * @author Joey Line <joey.line@skiclubz.com>
 */
(function($) {
	zPlayer.prototype.stepControls = function() {
		var player = this,
			clickHandler = function(e) {
				e.preventDefault();
				
				// if the video has ended, start it playing so that it will actually accept the step
				if(player.domelement.ended) {
					player.domelement.play();
				}
				
				// pause the video if it's already playing
				if(!player.domelement.paused) {
					player.domelement.pause();
				}
				
				if($(e.target).attr('name')=='stepForward') {
					// try not to go past the end of the video!
					// NOTE: according to the HTML5 Video spec, setting the current time 
					// greater than the duration just sets the current time to the duration.
					// However, every browser I've tried in either has a problem with this or
					// the similar check in the else below.
					if(player.domelement.currentTime + player.config.stepSize <= player.domelement.duration
						&& player.domelement.currentTime + player.config.stepSize <= player.endBuffer) {
						zPlayer.utils.log('stepping forward to ' + player.domelement.currentTime + player.config.stepSize + '!');
						// step forward in the video
						player.domelement.currentTime = player.domelement.currentTime + player.config.stepSize;
					}
				} else {
					// try not to go past the start of the video!
					// FIXME: This should probably use video.startTime, but it doesn't work with
					// Firefox 3.6
					if(player.domelement.currentTime - player.config.stepSize >= 0) {
						zPlayer.utils.log('stepping back to ' + player.domelement.currentTime + player.config.stepSize + '!');
						// step back in the video
						player.domelement.currentTime = player.domelement.currentTime - player.config.stepSize;
					}
				}
				
				if(player.endBuffer < player.domelement.duration)
					(player.controlbar.bufferHandler(player))({});
			};
		
		var stepBackButton = this.controlbar.buildElement(this, 'stepBack', 'left', true)
			.attr('name', 'stepBack')
			.click(clickHandler);
		var stepForwardButton = this.controlbar.buildElement(this, 'stepForward', 'left', true)
			.attr('name', 'stepForward')
			.click(clickHandler);
	};
})(jQuery);
