$.fn.tl_video = function(options) {

	return this.each(function() {
			
		var video 					= $(this),
				control_markup 	= '<div class="tl-video-control clearfix"> \
														<button class="play ir">play</button> \
														<div class="gutter"></div> \
														<span class="timer">00:00</span> \
														<button class="mute ir">mute</button> \
													 </div>';		
				
		video
			.wrap($(document.createElement('div')).addClass('tl-video-player'))
			.after(control_markup);
		
		var container 		= video.parent('.tl-video-player'),
				controls			= container.find('div.tl-video-control'),
				play_btn			= controls.find('button.play'),
				gutter				= controls.find('div.gutter'),
				timer					= controls.find('span.timer'),
				mute_btn			= controls.find('button.mute'),
				fullscreen		= controls.find('a.fullscreen');

		var seeking;
		
		var createSeek = function() {
			if (video.attr('readyState')) {
				var duration = video.attr('duration'); 
								
				gutter.slider({
					value: 0,
					step: 0.01,
					range: 'min',
					max: duration,
					animate: true,
					slide: function(event, ui) {
						seeking = true;						
					},
					stop: function(event, ui) {
						seeking = false;
						video.attr('currentTime', ui.value);
					}
				});
			}
			else {
				setTimeout(createSeek, 150);
			}
		};
		
		var updateSeek = function() {
			var currentTime = video.attr('currentTime');
			(!seeking) && (gutter.slider('value', currentTime));
			timer.text(formatTime(currentTime));
		};

		var playVideo = function() {
			video.attr('paused') ? video[0].play() : video[0].pause();		
		};
		
		var mute = function() {
			video.attr('muted', !video.attr('muted'));
		}
		
		var formatTime = function(secs) {
			var minutes = Math.floor(secs/60) < 10 ? "0" + Math.floor(secs/60) : Math.floor(secs/60),
					seconds = Math.floor(secs-(minutes*60))<10?"0"+Math.floor(secs-(minutes*60)):Math.floor(secs-(minutes*60));

			return  minutes + ":" + seconds;
		};
		
		var bindVideoEvents = function() {
			video
				.bind('play', function() {
					//console.log('playing');
					play_btn.addClass('paused');
				})
				.bind('pause', function() {
					//console.log('paused');
					play_btn.removeClass('paused');
				})
				.bind('ended', function() {
				 	video[0].pause();
				})
				.bind('volumechange', function() {
					video.attr('muted') ? mute_btn.addClass('muted') : mute_btn.removeClass('muted');
				})
				.bind('timeupdate', updateSeek);
		};
		
		var fadeInOptions = function() {
			$('.tl-video-player')
				.hover(
						function() {
							setTimeout(function() { 
								controls.stop(true, true).fadeIn('slow')
							}, 300);
						},
						function() {
							setTimeout(function() { 
								controls.stop(true, true).fadeOut('slow')
							}, 300);
						}
					);
		};
		
		$('#flash-video-container button')
			.live('click', function() {
				($(this).hasClass('play')) && (playVideo());
				($(this).hasClass('mute')) && (mute());
				return false;
			});

		bindVideoEvents();	
		createSeek();
		video.removeAttr('controls');
		fadeInOptions();
	});
}
