/*  * videoPlayer - jQuery plugin 1.0.0  *  * Copyright (c) 2011 Eric J. Czar  *  * www.illumi.com  * info@illumi.com  *  */
(function($) {
	// plugin definition
	$.fn.gVideo = function(options) {		
		var defaults = { theme: 'simpledark', childtheme: '' };	// build main options before element iteration
		var options = $.extend(defaults, options);
		// iterate and reformat each matched element
		return this.each(function() {
			var $gVideo = $(this);
			
			//create html structure
			var $video_wrap = $('<div></div>').addClass('video-player').addClass(options.theme).addClass(options.childtheme);	//main wrapper
			//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>');						
			$gVideo.wrap($video_wrap);
			$gVideo.after($video_controls);
			
			//get new elements
			var $video_container = $gVideo.parent('.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($gVideo.attr('paused') == false) { $gVideo[0].pause(); } else { $gVideo[0].play(); } };
			
			$play_btn.click(gPlay);
			$gVideo.click(gPlay);
			
			$gVideo.bind('play', function() { $play_btn.addClass('paused-button'); });
			$gVideo.bind('pause', function() { $play_btn.removeClass('paused-button'); });
			$gVideo.bind('ended', function() { $play_btn.removeClass('paused-button'); });
			
			var seeksliding;			
			var createSeek = function() {
				if($gVideo.attr('readyState')) {
					var video_duration = $gVideo.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; $gVideo.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 = $gVideo.attr('currentTime'); if(!seeksliding) $video_seek.slider('value', currenttime); $video_timer.text(gTimeFormat(currenttime)); };			
			$gVideo.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){
						$gVideo.attr('muted',false);
						video_volume = ui.value;
						$gVideo.attr('volume',ui.value);
					}
			});
			
			var muteVolume = function() {
				if($gVideo.attr('muted')==true) {
					$gVideo.attr('muted', false); $volume.slider('value', video_volume); $volume_btn.removeClass('volume-mute');					
				} else {
					$gVideo.attr('muted', true); $volume.slider('value', '0'); $volume_btn.addClass('volume-mute');
				};
			};
			$volume_btn.click(muteVolume);
			$gVideo.removeAttr('controls');
		});
	};

	//
	// plugin defaults
	//
	$.fn.gVideo.defaults = {};
})(jQuery);
