
(function($) {
	// plugin definition
	$.fn.gVideo = function(idVideo,source,options) {
			
		// build main options before element iteration		
		var defaults = {
			theme: 'simpledark',
			childtheme: '',
			streamVideo: false,
			width:400,
			height:225
		};


		var options = $.extend(defaults, options);
		
		// In case we want to include a vimeo Video or you tube..
		if(options.streamVideo==true)
		{
			var iframe = '<iframe title="YouTube video player" type="text/html" class="youtube-player" src="'+source[0].source+'" width="'+options.width+'" height="'+options.height+'" frameborder="0"></iframe>'; 
			$(this).append(iframe);
			exit();
		}
		
		// iterate and reformat each matched element
		
		return this.each(function() {
			var $gVideo = $(this);

			//create html structure
			
			//main wrapper
			var $video_wrap = $('<div></div>').addClass('video-player').addClass(options.theme).addClass(options.childtheme);
			
			var sourceVideo ='';
			
			for(var i=0;i<source.length;i++) 
			{
				var tabSource = source[i]
				sourceVideo +='<source src="'+tabSource.source+'">';
			}
			
			var $videoSource = '<video controls="controls" width="'+options.width+'" height="'+options.height+'" id="'+idVideo+'">'+sourceVideo+'</video>';
			$gVideo.append($video_wrap);
			$video_wrap.append($videoSource);
			//controls wraper
			var $video_controls = $('<div class="video-controls"><a class="video-play" title="Play/Pause"></a><div class="video-seek"></div><div class="video-timer">00:00</div><div class="volume-box"><div class="volume-slider"></div><a class="volume-button" title="Mute/Unmute"></a></div></div>');						
			$video_wrap.append($video_controls);
			
			//Get Video Object
			var videoObj = document.getElementById(idVideo);

			//get new elements
			var $video_container = $(this).find('.video-player');
			var $video_controls = $('.video-controls', $video_container);
			var $play_btn = $('.video-play', $video_container);
			var $video_seek = $('.video-seek', $video_container);
			var $video_timer = $('.video-timer', $video_container);
			var $volume = $('.volume-slider', $video_container);
			var $volume_btn = $('.volume-button', $video_container);
			
		//$video_controls.hide(); // keep the controls hidden

			var gPlay = function() {
				if($(videoObj).attr('paused') == false) {
					videoObj.pause();					
				} else {					
				$(videoObj)[0].play();				
				}
			};

			$play_btn.click(gPlay);
			$(videoObj).click(gPlay);

			$(videoObj).bind('play', function() {
				$play_btn.addClass('paused-button');
			});

			$(videoObj).bind('pause', function() {
				$play_btn.removeClass('paused-button');
			});

			$(videoObj).bind('ended', function() {
				$play_btn.removeClass('paused-button');
			});

			var seeksliding;			
			var createSeek = function() {
				if($(videoObj).attr('readyState')) {
					var video_duration = $(videoObj).attr('duration');
					$video_seek.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;						
							$(videoObj).attr("currentTime",ui.value);
						}
					});
					//$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 =$(videoObj).attr('currentTime');
				if(!seeksliding) $video_seek.slider('value', currenttime);
				$video_timer.text(gTimeFormat(currenttime));							
			};

			$(videoObj).bind('timeupdate', seekUpdate);	

			var video_volume = 1;
			$volume.slider({
				value: 1,
				orientation: "vertical",
				range: "min",
				max: 1,
				step: 0.05,
				animate: true,
				slide:function(e,ui){
						$(videoObj).attr('muted',false);
						video_volume = ui.value;
						$(videoObj).attr('volume',ui.value);
					}
			});
			var muteVolume = function() {
				if($(videoObj).attr('muted')==true) {
					$(videoObj).attr('muted', false);
					$volume.slider('value', video_volume);
					$volume_btn.removeClass('volume-mute');					
				} else {
					$(videoObj).attr('muted', true);
					$volume.slider('value', '0');
					$volume_btn.addClass('volume-mute');
				};
			};
			$volume_btn.click(muteVolume);

	$(videoObj).removeAttr('controls');

		});
	};



})(jQuery);
