
	/**
	 * dTvideo - the jquery plugin
	 *
	 * this text should tell you how the plugin works. too bad it doesn't.
	 */
	(function( $ ){
			
		//
		// Default settings
		var defaultSettings = {
			'mp4'			: null,
			'ogv'			: null,
			'webm'			: null,
			'flv'			: null,
			'flvplayer'		: '/www/video/dTvideo.swf',
			'poster'		: null,
			'width'			: 291,
			'height'		: 262,
			'autoplay'		: false,
			'controls'		: true,
			'loading'		: '/www/img/loading.gif',
			'forceflash'	: false
		};
		
		//
		// Methods
		var methods = {
		
			//
			// Initialize
			init : function( options ) {
				
				return this.each(function() {
				
					var $this = $(this);
					
					//
					// Apply settings
					var settings = defaultSettings;
					if ( options ) settings = $.extend( {}, defaultSettings, options );
					settings.id = unique_id(12);
					$(this).attr( 'id', settings.id );
							
					//
					// Setup custom callbacks
					if( typeof(settings.play) == 'function' ) { window['dTvideoPlay'+settings.id] = settings.play; } else { window['dTvideoPlay'+settings.id] = function() {} }
					if( typeof(settings.pause) == 'function' ) { window['dTvideoPause'+settings.id] = settings.pause; } else { window['dTvideoPause'+settings.id] = function() {} }
					if( typeof(settings.progress) == 'function' ) { window['dTvideoProgress'+settings.id] = settings.progress; } else { window['dTvideoProgress'+settings.id] = function( current, duration ) {} }
					if( typeof(settings.complete) == 'function' ) { window['dTvideoComplete'+settings.id] = settings.complete; } else { window['dTvideoComplete'+settings.id] = function() {} }
					if( typeof(settings.mouseOver) == 'function' ) { window['dTvideoMouseOver'+settings.id] = settings.mouseOver; } else { window['dTvideoMouseOver'+settings.id] = function() {} }
					if( typeof(settings.mouseOut) == 'function' ) { window['dTvideoMouseOut'+settings.id] = settings.mouseOut; } else { window['dTvideoMouseOut'+settings.id] = function() {} }
					if( typeof(settings.noFlash) == 'function' ) { window['dTvideoNoFlash'+settings.id] = settings.noFlash; } else { window['dTvideoNoFlash'+settings.id] = function() {} }
					
					//
					// Check for iPad / iPhone
					if( navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPhone/i) ) {
						settings.autoplay = false;
						settings.loading = false;
						settings.activate = true;
					}
					
					//
					// Apply loading
					if( settings.loading && ( settings.activate == undefined || !settings.activate ) ) {
						$this.append('<div class="videoLoading" />');
					}
				
					//
					// HTML5
					if( !settings.forceflash &&  !!document.createElement('video').canPlayType ) {
						
						settings.type = 'html5';
						
						var el = document.createElement("video");
						var video = null;
						
						//
						// Mp4
						if( settings.mp4 != null && settings.mp4.length && el.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"') == 'probably' ) {
							video = $('<video width="' + settings.width + '" height="' + settings.height + '"><source src="' + settings.mp4 + '" type="video/mp4" /><img src="' + settings.poster + '" /></video>');
							
						//
						// WebM
						} else if( settings.webm != null && settings.webm.length && el.canPlayType('video/webm; codecs="vp8, vorbis"') == 'probably' ) {
							video = $('<video width="' + settings.width + '" height="' + settings.height + '"><source src="' + settings.webm + '" type="video/webm" /><img src="' + settings.poster + '" /></video>');
							
						//
						// Ogg
						} else if( settings.ogv != null && settings.ogv.length && el.canPlayType('video/ogg; codecs="theora, vorbis"') == 'probably' ) {
							video = $('<video width="' + settings.width + '" height="' + settings.height + '"><source src="' + settings.ogv + '" type="video/ogg" /><img src="' + settings.poster + '" /></video>');
						}
						
						//
						// Embed
						if( video != null && typeof video === 'object' ) {
						
							//
							// Set loop
							if( settings.loop != undefined && settings.loop ) {
								video.attr( 'loop', 'loop' );
							}
						
							//
							// Set autoplay
							if( settings.autoplay != undefined && settings.autoplay ) {
								video.attr( 'autoplay', settings.autoplay );
							}
							
							//
							// Add video
							if( !$this.find('div.player').length ) $this.append('<div class="player" />');
							$this.find('div.player').html(video);
							
							//
							// Save settings
							$this.data( 'dTvideo', settings );
							
							//
							// Apply event listeners
							var video = methods.getVideo.call($this);
							video.addEventListener( 'play', function() { window['dTvideoPlay'+settings.id].call($this); }, false );
							video.addEventListener( 'pause', function() { window['dTvideoPause'+settings.id].call($this); }, false );
							video.addEventListener( 'timeupdate', function(e,f) { window['dTvideoProgress'+settings.id].call( $this, e.target.currentTime, e.target.duration ); }, false);
							video.addEventListener( 'ended', function() { window['dTvideoComplete'+settings.id].call($this); }, false );
							video.addEventListener( 'mouseover', function(e) { window['dTvideoMouseOver'+settings.id].call($this); }, false);
							video.addEventListener( 'mouseout', function(e) { window['dTvideoMouseOut'+settings.id].call($this); }, false);
							video.addEventListener( 'loadeddata', function() { $this.find('.videoLoading').remove(); }, false);
							
							//
							// Apply activate layer
							if( settings.activate ) {
								methods.applyActivate.call($this);
							}
							
							//
							// Apply controls
							if( settings.controls ) {
								methods.applyControls.call($this);
								methods.showControls.call($this);
							}
							
							//
							// Play
							if( settings.play || ( settings.autoplay != undefined && settings.autoplay ) ) {
								methods.play.call($this);
							}
							
						}
						
					
					//
					// Flash
					} else {
					
					
						//
						// Load SWFObject
						$.getScript( '/www/js/swfobject.js', function() {
						
							var settings = $this.data('dTvideo');
							settings.type = 'flash';
							
							//
							// Create element
							settings.flashid = unique_id(12);
							if( !$this.find('div.player').length ) $this.append('<div class="player" />');
							$this.find('div.player').attr( 'id', settings.flashid );
							
							//
							// Flashvars
							var flashvars = {
								'width' 	: settings.width,
								'height' 	: settings.height,
								'source' 	: settings.flv
							};
							
							
							//
							// Set loop
							if( settings.loop ) {
								flashvars.autoRewind = true;
							}
							
							//
							// Set autoplay
							if( settings.autoplay || settings.play ) {
								flashvars.autoPlay = true;
							}
							
							//
							// Apply callbacks
							flashvars.play = 'dTvideoPlay'+settings.id;
							flashvars.pause = 'dTvideoPause'+settings.id;
							flashvars.progress = 'dTvideoProgress'+settings.id;
							flashvars.complete = 'dTvideoComplete'+settings.id;
							flashvars.mouseOver = 'dTvideoMouseOver'+settings.id;
							flashvars.mouseOut = 'dTvideoMouseOut'+settings.id;
							flashvars.setup = 'dTvideoSetup'+settings.id;
							flashvars.loaded = 'dTvideoLoaded'+settings.id;
							
							//
							// Video setup
							window['dTvideoSetup'+settings.id] = function() {
							
								var settings = $this.data('dTvideo');
								
								//
								// Apply activate layer
								if( settings.activate ) {
									methods.applyActivate.call($this);
								}
								
								//
								// Apply controls
								if( settings.controls ) {
									methods.applyControls.call($this);
									methods.showControls.call($this);
								}
								
								//
								// Play
								if( settings.play != undefined && settings.play ) {
									methods.play.call($this);
								}
							
							};
							
							//
							// Video loaded
							window['dTvideoLoaded'+settings.id] = function() {
								$this.find('.videoLoading').remove();
							}
							
							//
							// Embed flvplayer
							swfobject.embedSWF( settings.flvplayer, settings.flashid, settings.width, settings.height, "9", null, flashvars, { 'wmode': 'opaque' } );
							
							//
							// No flash callback
							if( typeof(window['dTvideoNoFlash'+settings.id]) == 'function' && ( swfobject.getFlashPlayerVersion().major == undefined || swfobject.getFlashPlayerVersion().major < 9 ) ) {
								window['dTvideoNoFlash'+settings.id].call($this);
							}
							
							//
							// Save settings
							$this.data( 'dTvideo', settings );
								
						});
						
					}
					
					//
					// Save settings
					$this.data( 'dTvideo', settings );
					
				});
				
			},
			
			//
			// Apply activate layer
			applyActivate : function() { 
			
				return this.each(function() {
				
					var $this = $(this);
					var settings = $this.data('dTvideo');
					
					settings.activate = true;
					settings.activated = false;
					
					if( settings.poster != undefined && settings.poster.length ) {
					
						$this.append( 
							$('<div class="activate" / >')
								.append('<img src="' + settings.poster + '" />')
								.append(
									$('<a href="#activate"></a></div>')
										.bind( 'click.dTvideo', function() { methods.activate.call($this); return false; } )
								)
						);
					
					} else {
					
						$this.append( 
							$('<div class="activate" / >')
								.append(
									$('<a href="#activate"></a></div>')
										.bind( 'click.dTvideo', function() { methods.activate.call($this); return false; } )
								)
						);
					
					}
					
					//
					// Save settings
					$this.data( 'dTvideo', settings);
				
				});
			
			},
			
			//
			// Activate player
			activate : function() {
				
				return this.each(function() {
			
					var $this = $(this);
					var settings = $this.data('dTvideo');
					
					if( !settings.activated ) {
					
						settings.activated = true;
						
						//
						// Hide activate layer
						$this.find('.activate').hide();
						
						//
						// Show player controls
						methods.showControls.call($this);
						
						//
						// Play video
						methods.play.call($this);
					
					} else {
					
						settings.activated = false;
						
						//
						// Show activate layer
						$this.find('.activate').show();
						
						//
						// Hide player controls
						methods.hideControls.call($this);
						
						//
						// Pause video
						methods.pause.call($this);
						
					}

				
				});
			
			},
			
			//
			// Create player controls
			applyControls : function() {
				
				return this.each(function() {
			
					var $this = $(this);
					var settings = $this.data('dTvideo');
					var video = methods.getVideo.call($this);
							
					//
					// Apply controls
					$this.append( $('<div class="controls"><a href="" class="play">play</a><a href="" class="pause">pause</a><div class="seek"></div><a href="#mute" class="mute">mute</a><div class="volume"></div></div>').css({ opacity: 0 }) );
					
					//
					// Social links
					if( settings.url != undefined && settings.url.length > 0 ) {
						$this.find('div.controls').append('<a href="http://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(settings.url) + '" class="facebook">facebook</a><a href="http://twitter.com/home?status=' + encodeURIComponent(settings.url) + '" class="twitter">twitter</a>');
					}
					
					//
					// Set volume
					methods.volume.call( $this, 0.5 );
					
					//
					// Player actions
					$this.find('a.play').bind( 'click.dTvideo', function() { methods.play.call($this); return false; } );
					$this.find('a.pause').bind( 'click.dTvideo', function() { methods.pause.call($this); return false; } );
					$this.find('div.seek').slider({
							range: "min",
							value: 0,
							min: 0,
							max: 100,
							slide: function( event, ui ) {
								methods.seekPercent.call( $this, ( ui.value / 100 ) );
							}
						});
					$this.find('a.mute').bind( 'click.dTvideo', function() { methods.mute.call($this); return false; } );
					$this.find('div.volume').slider({
							range: "min",
							value: Math.ceil( methods.getVolume.call($this) * 100 ),
							min: 0,
							max: 100,
							slide: function( event, ui ) {
								methods.volume.call( $this, ( ui.value / 100 ) );
							}
						});
					
					//
					// Set button status
					if( settings.autoplay ) {
						methods.togglePlayButton.call( $this, 'pause' );
					}
							
					//
					// Update progress event
					var progressFunction = window['dTvideoProgress'+settings.id];
					window['dTvideoProgress'+settings.id] = function( current, duration ) {
							$this.find('div.seek').slider( 'value', ( current / duration ) * 100 );
							progressFunction.call( $this, current, duration );
						};
					
					//
					// Update mouseover event
					var mouseOverFunction = window['dTvideoMouseOver'+settings.id];
					window['dTvideoMouseOver'+settings.id] = function() {
							methods.showControls.call($this);
							mouseOverFunction.call($this);
						};
					
					//
					// Update mouseout event
					var mouseOutFunction = window['dTvideoMouseOut'+settings.id];
					window['dTvideoMouseOut'+settings.id] = function() {
							methods.hideControls.call($this);
							mouseOutFunction.call($this);
						};
					
					//
					// Set mouseover event
					$this.find('div.controls').bind( 'mouseover.dTvideo', function(e) {
							$(this).stop().css('opacity',1);
						});
					
					//
					// Set mouseout event
					$this.find('div.controls').bind( 'mouseout.dTvideo', function(e) {
							methods.hideControls.call($this);
						});
					
					//
					// Save settings
					$this.data( 'dTvideo', settings );
					
				});
				
			},
			
			//
			// Show player controls
			showControls : function() {
				
				return this.each(function() {
			
					var $this = $(this);
					var settings = $this.data('dTvideo');
					
					if( settings.activate == undefined || !settings.activate || settings.activated ) {
						$this.find('div.controls').stop().animate( { opacity: 1 }, 200 );
					}
				
				});
			
			},
			
			//
			// Hide player controls
			hideControls : function() {
				
				return this.each(function() {
			
					var $this = $(this);
					var settings = $this.data('dTvideo');
				
					if( settings.activate == undefined || !settings.activate || settings.activated ) {
						$this.find('div.controls').stop().animate( { opacity: 0 }, 200 );
					}
				
				});
			
			},
			
			//
			// Toggle play button
			togglePlayButton : function(type) {
			
				return this.each(function() {
			
					var $this = $(this);
			
					if( type == 'play' ) {
						$this.find('a.play').show();
						$this.find('a.pause').hide();
					} else if( type == 'pause' ) {
						$this.find('a.play').hide();
						$this.find('a.pause').css({ display: 'block' }).show();
					}
				
				});
			
			},
			
			//
			// Play method
			play : function() {
			
				return this.each(function() {
				
					var $this = $(this);
					var settings = $this.data('dTvideo');
					var video = methods.getVideo.call($this);
					
					if( video != undefined ) {
						if( settings.type == 'html5' ) {
							if( typeof( video.play ) == 'function' )
								video.play();
						} else if( settings.type == 'flash' ) {
							if( typeof( video.dTplay ) == 'function' )
								video.dTplay();
						}
					} else {
						settings.play = true;
						$this.data( 'dTvideo', settings );
					}
					
					methods.togglePlayButton.call( $this, 'pause' );
					
				});
			},
			
			//
			// Pause method
			pause : function() {
			
				return this.each(function() {
			
					var $this = $(this);
					var settings = $this.data('dTvideo');
					var video = methods.getVideo.call($this);
					
					if( video != undefined ) {
						if( settings.type == 'html5' ) {
							if( typeof( video.pause ) == 'function' )
								video.pause();
						} else if( settings.type == 'flash' ) {
							if( typeof( video.dTpause ) == 'function' )
								video.dTpause();
						}
					}
					
					methods.togglePlayButton.call( $this, 'play' );
					
				});
			},
			
			//
			// Seek method
			seek : function( time ) {
			
				return this.each(function() {
			
					var $this = $(this);
					var settings = $this.data('dTvideo');
					var video = methods.getVideo.call($this);
					
					if( video != undefined ) {
						if( settings.type == 'html5' ) {
							if( video.seekable != undefined && video.seekable.length )
								video.currentTime = time;
						} else if( settings.type == 'flash' ) {
							if( typeof( video.dTseek ) == 'function' )
								video.dTseek(time);
						}
					}
					
				});
			
			},
			
			//
			// Seek % method
			seekPercent : function( percent ) {
			
				return this.each(function() {
				
					var $this = $(this);
					var settings = $this.data('dTvideo');
					
					if( percent > 1 )
						percent = percent / 100;
					
					methods.seek.call( $this, percent * methods.getDuration.call($this) );
				
				});
			
			},
			
			//
			// getDuration
			getDuration : function() {
			
				var $this = this;
				var settings = $this.data('dTvideo');
				var video = methods.getVideo.call($this);
				
				if( video != undefined ) {
					if( settings.type == 'html5' ) {
						return video.duration;
					} else if( settings.type == 'flash' ) {
						if( typeof( video.dTduration ) == 'function' )
							return video.dTduration();
					}
				}
				
				return 0;
				
			},
			
			//
			// Mute method
			mute : function() {
			
				return this.each(function() {
			
					var $this = $(this);
					var settings = $this.data('dTvideo');
					var video = methods.getVideo.call($this);
					
					if( video != undefined ) {
						if( settings.type == 'html5' ) {
							if( video.muted ) {
								video.muted = false;
							} else {
								video.muted = true;
							}
						} else if( settings.type == 'flash' ) {
							methods.volume.call( $this, 0 );
						}
					}
					
				});
			},
			
			//
			// Volume
			volume : function( volume ) {
			
				return this.each(function() {
			
					var $this = $(this);
					var settings = $this.data('dTvideo');
				
					if( settings != undefined ) {
				
						var video = methods.getVideo.call($this);
						
						if( video != undefined ) {
							if( settings.type == 'html5' ) {
								video.volume = volume;
							} else if( settings.type == 'flash' ) {
								if( typeof( video.dTvolume ) == 'function' )
									video.dTvolume(volume);
							}
						}
					
					}
					
					return 0;
					
				});
				
			},
			
			//
			// getVolume
			getVolume : function() {
			
				var $this = this;
				var settings = $this.data('dTvideo');
				
				if( settings != undefined ) {
				
					var video = methods.getVideo.call($this);
					
					if( video != undefined ) {
						if( settings.type == 'html5' ) {
							return video.volume;
						} else if( settings.type == 'flash' ) {
							if( typeof( video.dTgetVolume ) == 'function' )
								return video.dTgetVolume();
						}
					}
				
				}
				
				return 0;
			
			},
			
			//
			// getVideo
			getVideo : function() {
			
				var $this = this;
				var settings = $this.data('dTvideo');
				
				if( settings.type == 'html5' ) {
					return $this.find('video')[0];
				} else if( settings.type == 'flash' ) {
					return get_flash(settings.flashid);
				}
			
			}
					
		}
	  
		$.fn.dTvideo = function( method ) {
			
			//
			// Method call
			if ( methods[method] ) {
				return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
			
			//
			// Default init call
			} else if ( typeof method === 'object' || ! method ) {
				return methods.init.apply( this, arguments );
			
			//
			// Method doesn't exist
			} else {
				$.error( 'Method ' +  method + ' does not exist on jQuery.dTvideo' );
			}
		
		};
	  
	})( jQuery );
	
