/**
 * Adds a play button to the main display when the video is stopped and fixes the issue with Safari
 * dropping the poster image as soon as it starts preloading the video.
 * 
 * @author Joey Line <joey.line@skiclubz.com>
 */
(function($) {
	zPlayer.prototype.playButton = function() {
		if(!zPlayer.utils.isiPhone() && !zPlayer.utils.isiPad()) {
			var player = this;
			var poster = $('<a href="#"></a>')
				.css('position', 'absolute')
				.css('top', 0)
				.css('left', 0)
				.appendTo(this.container)
				.click(function() {
					player.sendEvent('PLAY', true);
					return false;
				});
			
			$('<img src="' + $(player.domelement).attr('poster') + '" />')
				.css('width', '100%')
				.css('height', 'auto')
				.appendTo(poster);
			$(document).bind('fullscreen.zplayer', function(eObj, fullscreen) {
				if(fullscreen)
					player.sendEvent('PLAY', true);
			});
				
			var button = $('<a href="#" alt="Play">Play</a>')
				.html('<img src="' + player.skin.display.elements['playIcon'].src + '" />')
				.css('position', 'absolute')
				.css('top', '50%')
				.css('left', '50%')
				.css('margin', '-' + (player.skin.display.elements['playIcon'].height / 2 + player.display.controlbar.height / 2)  + 'px 0 0 -' + (player.skin.display.elements['playIcon'].width / 2) + 'px')
				.appendTo(this.container)
				.click(function() {
					player.sendEvent('PLAY', true);
					return false;
				});
			
			var hideEvents = ['play', 'playing'];
			$.each(hideEvents, function(index, type) {
				player.domelement.addEventListener(type, function() {
					button.hide();
					poster.hide();
				}, false);
			});
			
			// seeking is special - if you seek to the start of a video, show the button.
			player.domelement.addEventListener('seeking', function() {
				if(player.domelement.currentTime == 0) {
					button.show();
				} else {
					button.hide();
				}
				
				poster.hide();
			}, false);
			
			var showEvents = ['ended', 'pause'];
			$.each(showEvents, function(index, type) {
				player.domelement.addEventListener(type, function() {
					button.show();
				}, false);
			});
		}
	};
})(jQuery);

