/***************************************************************
 * author: Leonhardt Multimedia GmbH - Stefan Fischer
 * version: 0.1
 * date: 2010-12-01
 *
 ***************************************************************/

function boschVideoPlayer(){
	this.videoBoxId;
	this.videoPath;
	this.imagePath;
	this.vWidth;
	this.vHeight;
	this.shared;
	this.embed;
	this.flash;
	this.flashversion;
	//this.embedPath;
	
  this.testHtmlVideoTag = function(){
		var videoElement = document.createElement('video');
		return !!(videoElement.canPlayType && videoElement.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"').replace(/no/, ''))
	}
  
  this.fallbackHtmlVideoTag = function(){
		if (this.testHtmlVideoTag()){
			$('#'+this.videoBoxId).html('<div class="videoPlayer" id="'+this.videoBoxId+'VideoHtml"><video id="'+this.videoBoxId+'Video" src="'+this.videoPath+'" poster="'+this.imagePath+'" width="'+this.vWidth+'" height="'+this.vHeight+'" controls="controls"></video></div>');
			$('#'+this.videoBoxId+'Video').vPlayerControls({btnShareShow:this.shared, btnEmbedShow:this.embed});
		}
	}
  
  this.show=function(){
		var flashvars = {
			path:this.videoPath,
			preview:this.imagePath,
			fs_open:boschVideoPlayerLabels.btnFsOpen,
			fs_close:boschVideoPlayerLabels.btnFsClose,
			share_label:boschVideoPlayerLabels.btnShared,
			share_head:boschVideoPlayerLabels.headlineShared,
			share_text:boschVideoPlayerLabels.textShared,
			share_url:document.URL,
			allowshare:this.shared,
			embed_label:boschVideoPlayerLabels.btnEmbed,
			embed_head:boschVideoPlayerLabels.headlineEmbed,
			embed_text:boschVideoPlayerLabels.textTextEmbed,
			embed_code:'JavaScript-Code',
			//embed_code:this.embedPath,
			allowembed:this.embed
		};
		var flashparams = {
			bgcolor:'#ffffff',
			menu:'false',
			quality:'high',
			wmode:'transparent',
			allowFullScreen:'true'
		};
		var flashattributes = {
			id:this.videoBoxId+'Flash'
		};
		swfobject.embedSWF(this.flash, this.videoBoxId, this.vWidth, this.vHeight+38, this.flashversion , "", flashvars, flashparams, flashattributes, this.fallbackHtmlVideoTag());
  }
}



(function($){
	$.fn.vPlayerControls = function(options){
		var defaults ={
			btnShareShow: true,
			btnEmbedShow: true
		};
		var options = $.extend(defaults, options);

		return this.each(function(){
			var $video = $(this);
			var videoWidth = $video.attr('width');
			var videoHeight = $video.attr('height');
			var boxId = '#' + $video.parent().attr('id');

			//overlay
			$video.before('<div class="btnPlayOverlay" style="width:' + videoWidth + 'px;height:' + videoHeight + 'px;"><!--item--></div>');


			//co	ntrols
			$video.after($('<div class="videoControls"><div class="videoControlBarLeft"><!--item--></div><div class="button btnPlayPause play"><!--item--></div><div class="videoSeek"><!--item--></div><div class="videoTimer">00:00</div><div class="videoDuration"><!--item--></div><div class="videoControlBarRight"><!--item--></div><div class="buttons hover"><div class="volumeBox"><div class="volumeSlider"><!--item--></div><div class="button btnVolume"><!--item--></div></div></div></div>'));
			$(boxId + ' .videoControls').css({'width': videoWidth + 'px'});


			//fullscreen
			$(boxId + ' .volumeBox').after('<div class="button btnFullscreen show"><div class="buttonHover">'+boschVideoPlayerLabels.btnFsOpen+'</div><div class="arrowDown"><!--item--></div></div>');
			$(boxId + ' .btnFullscreen.show').live('click', function(){
					$(boxId + ' .btnFullscreen.show').removeClass('show').addClass('hide');
					$(boxId + ' .btnFullscreen .buttonHover').text(boschVideoPlayerLabels.btnFsClose);
					$(boxId).addClass('fullscreen');
					$video.attr({'width':window.innerWidth,'height':window.innerHeight-38});
					$(boxId + ' .btnPlayOverlay').css({'width':window.innerWidth + 'px','height':window.innerHeight-38 + 'px'});
					$(boxId + ' .videoControls').css({'width':window.innerWidth + 'px'});
					$('body').css('overflow', 'hidden');
					x = $(window).scrollLeft();
					y = $(window).scrollTop();
					window.scroll(0,0);
			});
			$(boxId + ' .btnFullscreen.hide').live('click', function(){
					$(boxId + ' .btnFullscreen.hide').removeClass('hide').addClass('show');
					$(boxId + ' .btnFullscreen .buttonHover').text(boschVideoPlayerLabels.btnFsOpen);
					$(boxId).removeClass('fullscreen');
					$video.attr({'width':videoWidth,'height':videoHeight});
					$(boxId + ' .btnPlayOverlay').css({'width':videoWidth + 'px','height':videoHeight + 'px'});
					$(boxId + ' .videoControls').css({'width': videoWidth + 'px'});
					$('body').css('overflow', 'auto');
					window.scroll(x,y);
			});


$(boxId + '.videoPlayer .volumeBox').css({'right':'97px'})

			//share
			if (options.btnShareShow){
				$(boxId + ' .volumeBox').after('<div class="shareBox"><strong>'+boschVideoPlayerLabels.headlineShared+'</strong><br/>'+boschVideoPlayerLabels.textShared+'<br/><input type="text" value="'+window.location.href+'"/></div><div class="button btnShare show"><div class="buttonHover">'+boschVideoPlayerLabels.btnShared+'</div><div class="arrowDown"><!--item--></div><!--item--></div>');
			}else{
				$(boxId + '.videoPlayer .volumeBox').css({'right':parseInt($(boxId + ' .volumeBox').css('right'))-32+'px'});
			}
			var btnShareBoxShow = function(){
				$(boxId + ' .btnShare.show').removeClass('show').addClass('hide');
				$(boxId + ' .buttons').removeClass('hover').addClass('noHover');
				$(boxId + ' .shareBox').show();
				$(boxId + ' .btnShare.hide .arrowDown').attr({'style':'display:block'});
			};
			var btnShareBoxHide = function(){
				$(boxId + ' .btnShare.hide').removeClass('hide').addClass('show');
				$(boxId + ' .buttons').removeClass('noHover').addClass('hover');
				$(boxId + ' .shareBox').hide();
				$(boxId + ' .btnShare.show .arrowDown').removeAttr('style');
			};
			var btnShareClick = function(){
				if($(this).attr('class')=='button btnShare show'){
					btnEmbedBoxHide();
					btnShareBoxShow();
				}else{
					btnShareBoxHide();
				}
			};
			$(boxId + ' .btnShare').live('click',btnShareClick);


			//embed
			if (options.btnEmbedShow){
				$(boxId + ' .volumeBox').after('<div class="embedBox"><strong>'+boschVideoPlayerLabels.headlineEmbed+'</strong><br/>'+boschVideoPlayerLabels.textTextEmbed+'<br/><input type="text" value="<CODE>"/></div><div class="button btnEmbed show"><div class="buttonHover">'+boschVideoPlayerLabels.btnEmbed+'</div><div class="arrowDown"><!--item--></div><!--item--></div>');
			}else{
				$(boxId + ' .volumeBox').css({'right':parseInt($(boxId + ' .volumeBox').css('right'))-32+'px'});
			}
			var btnEmbedBoxShow = function(){
				$(boxId + ' .btnEmbed.show').removeClass('show').addClass('hide');
				$(boxId + ' .buttons').removeClass('hover').addClass('noHover');
				$(boxId + ' .embedBox').show();
				$(boxId + ' .btnEmbed.hide .arrowDown').attr({'style':'display:block'});
			};
			var btnEmbedBoxHide = function(){
					$(boxId + ' .btnEmbed.hide').removeClass('hide').addClass('show');
					$(boxId + ' .buttons').removeClass('noHover').addClass('hover');
					$(boxId + ' .embedBox').hide();
					$(boxId + ' .btnEmbed.show .arrowDown').removeAttr('style');
			};
			var btnEmbedClick = function(){
				if($(this).attr('class')=='button btnEmbed show'){
					btnShareBoxHide();
					btnEmbedBoxShow();
				}else{
					btnEmbedBoxHide();
				}
			};
			$(boxId + ' .btnEmbed').live('click',btnEmbedClick);
//$(boxId + ' .buttons').outerWidth()
//$(boxId + ' .volumeBox').outerWidth()
//alert(videoWidth - $(boxId + ' .buttons').outerWidth() - $(boxId + ' .volumeBox').outerWidth() - $(boxId + ' .videoTimer').outerWidth() - 46);
$(boxId + ' .videoSeek').css({'width':videoWidth - $(boxId + ' .buttons').outerWidth() - $(boxId + ' .volumeBox').outerWidth() - $(boxId + ' .videoTimer').outerWidth() -74 - 46 + 'px'})

			//$(boxId + ' .videoSeek').append('<img src="img_shared/scrollbar_handle.png" width="100%" height="10" />');
			
			
			var $videoContainer = $video.parent('.videoPlayer');
			var $video_controls = $('.videoControls', $videoContainer);
			var $controlPlayButton = $('.btnPlayPause', $videoContainer);
			var $controlVideoSeek = $('.videoSeek', $videoContainer);
			var $controlVideoTimer = $('.videoTimer', $videoContainer);
			var $videoDuration = $('.videoDuration', $videoContainer);
			var $controlVideoVolume = $('.volumeSlider', $videoContainer);
			var $controlVideoVolumeButton = $('.btnVolume', $videoContainer);
			
			$video_controls.hide();
						
			var gPlay = function(){
				if($video.attr('paused') == false){
					$video[0].pause();
					$(boxId + ' .btnPlayOverlay').show();
				} else{					
					$video[0].play();
					$(boxId + ' .btnPlayOverlay').hide();
				}
			};
			

			$controlPlayButton.click(gPlay);
			$video.click(gPlay);
			$(boxId + ' .btnPlayOverlay').click(gPlay);
			
			$video.bind('play', function(){
				$controlPlayButton.removeClass('play').addClass('pause');
			});
			
			$video.bind('pause', function(){
				$controlPlayButton.removeClass('pause').addClass('play');
			});
			
			$video.bind('ended', function(){
				$video[0].pause();
				$('.btnPlayOverlay').show();
				$controlPlayButton.removeClass('pause').addClass('play');
			});
			
			var seeksliding;			
			var createSeek = function(){
				if($video.attr('readyState')){
					var video_duration = $video.attr('duration');
					$controlVideoSeek.slider({
						value: 0,
						step: 0.01,
						orientation: "horizontal",
						range: "min",
						max: video_duration,
						animate: true,
						slide: function(){
							seeksliding = true;
						},
						stop:function(e,ui){
							seeksliding = false;
							$video.attr("currentTime",ui.value);
						}
					});
					$video_controls.show();
				} else{
					setTimeout(createSeek, 150);
				}
			};

			createSeek();
		
			var gTimeFormat=function(seconds){
				var m=Math.floor(seconds/60)<10?"0"+Math.floor(seconds/60):Math.floor(seconds/60);
				var s=Math.floor(seconds-(m*60))<10?"0"+Math.floor(seconds-(m*60)):Math.floor(seconds-(m*60));
				return m+":"+s;
			};
			
			var seekUpdate = function(){
				var currenttime = $video.attr('currentTime');
				if(!seeksliding) $controlVideoSeek.slider('value', currenttime);
				$controlVideoTimer.text(gTimeFormat(currenttime) + " / " + gTimeFormat($video.attr('duration')));
			};
			
			$video.bind('timeupdate', seekUpdate);
			
			
			//if ($video.attr('readyState') > 0){
				//$videoDuration.text(Math.round($video.attr('duration')));
			//}
			
			var video_volume = 1;
			$controlVideoVolume.slider({
				value: 1,
				orientation: "vertical",
				range: "min",
				max: 1,
				step: 0.05,
				animate: true,
				slide:function(e,ui){
						$video.attr('muted',false);
						video_volume = ui.value;
						$video.attr('volume',ui.value);
					}
			});
			
			var muteVolume = function(){
				if($video.attr('muted')==true){
					$video.attr('muted', false);
					$controlVideoVolume.slider('value', video_volume);
					
					$controlVideoVolumeButton.removeClass('volumeMute');
				} else{
					$video.attr('muted', true);
					$controlVideoVolume.slider('value', '0');
					
					$controlVideoVolumeButton.addClass('volumeMute');
				};
			};
			
			$controlVideoVolumeButton.click(muteVolume);
			
			$video.removeAttr('controls');
			
		});
	};

	//
	// plugin defaults
	//
	$.fn.vPlayerControls.defaults ={
	};

})(jQuery);

