var EnhancedVideos = new Array();
/**
 * Video class
 * @constructor
 * @author Hadrien Lanneau
 */
var EnhancedVideo = function(video)
{
	/**
	 * video
	 * @type video element
	 */
	this.video = video;
	
	this.init();
}
EnhancedVideo.prototype = {
	/**
	 * init
	 */
	init: function()
	{
		// Construct a relative psotionned container
		this.container = document.createElement('div');
		this.container.className = 'enhancedVideo';
		this.video.parentNode.insertBefore(
			this.container,
			this.video
		);
		this.container.appendChild(
			this.video
		);
		// Construct control panel
		this.panel = document.createElement('div');
		this.panel.className = 'panel';
		this.panel.style.position = 'absolute';
		this.panel.style.top = 0;
		this.panel.style.left = 0;
		// Controls
		this.panel.playpause = document.createElement('span');
		this.panel.appendChild(this.panel.playpause);
		// fullscreen
		this.panel.changesize = document.createElement('span');
		this.panel.appendChild(this.panel.changesize);
		
		this.container.appendChild(
			this.panel
		);
		
		// Show Hide panel
		YAHOO.util.Event.on(
			this.container,
			'mouseenter',
			function(e)
			{
				(new YAHOO.util.Anim(
					this.panel,
					{
						opacity: {
							to: 1
						}
					},
					e.shiftKey ? 1 : 0.4,
					YAHOO.util.Easing.easeOut
				)).animate();
			},
			this,
			true
		);
		YAHOO.util.Event.on(
			this.container,
			'mouseleave',
			function(e)
			{
				(new YAHOO.util.Anim(
					this.panel,
					{
						opacity: {
							to: 0
						}
					},
					e.shiftKey ? 1 : 0.4,
					YAHOO.util.Easing.easeOut
				)).animate();
			},
			this,
			true
		);
		
		// Play pause event
		YAHOO.util.Event.on(
			this.panel.playpause,
			'click',
			function(e)
			{
				this.toggle(e.shiftKey);
			},
			this,
			true
		);
		
		// Init change size on video ready state
		YAHOO.util.Event.on(
			this.video,
			'loadedmetadata',
			function(e)
			{
				this.originalSize = YAHOO.util.Region.getRegion(this.video);
				this.originalSize.width = this.originalSize.width - parseInt(
						YAHOO.util.Dom.getStyle(this.video, 'padding-left')
				 	) - parseInt(
						YAHOO.util.Dom.getStyle(this.video, 'padding-right')
					) - parseInt(
						YAHOO.util.Dom.getStyle(this.video, 'border-left-width')
				 	) - parseInt(
						YAHOO.util.Dom.getStyle(this.video, 'border-right-width')
					);
				this.originalSize.height = this.originalSize.height - parseInt(
						YAHOO.util.Dom.getStyle(this.video, 'padding-top')
				 	) - parseInt(
						YAHOO.util.Dom.getStyle(this.video, 'padding-bottom')
					) - parseInt(
						YAHOO.util.Dom.getStyle(this.video, 'border-top-width')
				 	) - parseInt(
						YAHOO.util.Dom.getStyle(this.video, 'border-bottom-width')
					);
				
				// Change size event
				YAHOO.util.Dom.setStyle(
					this.video,
					'min-width', '0'
				);
				YAHOO.util.Dom.setStyle(
					this.video,
					'max-width', '9999px'
				);
				YAHOO.util.Dom.setStyle(
					this.video,
					'min-height', '0'
				);
				YAHOO.util.Dom.setStyle(
					this.video,
					'max-height', '9999px'
				);
				YAHOO.util.Dom.setStyle(
					this.video,
					'width', this.originalSize.width + 'px'
				);
				YAHOO.util.Dom.setStyle(
					this.video,
					'height', this.originalSize.height + 'px'
				);
				YAHOO.util.Event.on(
					this.panel.changesize,
					'click',
					function(e)
					{
						this.changesize(e.shiftKey);
					},
					this,
					true
				);
				
				// init size
				this.panel.changesize.className = 'minimized';
				this.panel.changesize.innerHTML = '+';
			},
			this,
			true
		);
		
		// Init video state
		if (this.video.autostart)
		{
			this.play();
		}
		else
		{
			this.pause();
		}
	},
	toggle: function(misc)
	{
		if (this.video.paused)
		{
			this.play(misc);
		}
		else
		{
			this.pause(misc);
		}
	},
	/**
	 * Play video
	 */
	play: function(misc)
	{
		this.video.play();
		// Display pause button
		this.panel.playpause.className = 'playing';
		this.panel.playpause.innerHTML = '<span>▐▐</span>';
	},
	/**
	 * Pause video
	 */
	pause: function(misc)
	{
		this.video.pause();
		// Display pause button
		this.panel.playpause.className = 'paused';
		this.panel.playpause.innerHTML = '<span>▶</span>';
	},
	/**
	 * changesize
	 */
	changesize: function(misc)
	{
		if (this.maximized)
		{
			this.minimize(misc);
		}
		else
		{
			this.maximize(misc);
		}
	},
	/**
	 * Minimize video
	 */
	minimize: function(misc)
	{
		this.maximized = false;
		
		for (var i in el = [this.video, this.panel])
		{
			// Change Size
			if (el[i].anim)
			{
				el[i].anim.stop();
			}
			el[i].anim = new YAHOO.util.Anim(
				el[i],
				{
					width: {
						to: this.originalSize.width
					},
					height: {
						to: this.originalSize.height
					},
					'margin-top': {
						to: 0
					},
					'margin-left': {
						to: 0
					}
				},
				misc ? 2 : 0.4,
				YAHOO.util.Easing.bounceOut
			);

			el[i].anim.onComplete.subscribe(
				function(e, a)
				{
					console.debug(this);
				},
				this,
				true
			);
			el[i].anim.animate();
		}
		
		this.panel.changesize.className = 'minimized';
		this.panel.changesize.innerHTML = '<span>+</span>';
	},
	/**
	 * Maximize video
	 */
	maximize: function(misc)
	{
		this.maximized = true;
		
		for (var i in el = [this.video, this.panel])
		{
			// Change Size
			if (el[i].anim)
			{
				el[i].anim.stop();
			}

			// calculate max dimensions
			if (YAHOO.util.Dom.getViewportWidth() > YAHOO.util.Dom.getViewportHeight())
			{
				var h = YAHOO.util.Dom.getViewportHeight();
				var w = YAHOO.util.Dom.getViewportHeight() *
					(this.originalSize.width / this.originalSize.height);
			}
			else
			{
				var w = YAHOO.util.Dom.getViewportWidth();
				var h = YAHOO.util.Dom.getViewportWidth() *
					(this.originalSize.height / this.originalSize.width);
			}
			el[i].anim = new YAHOO.util.Anim(
				el[i],
				{
					width: {
						to: w - 50
					},
					height: {
						to: h - 50
					},
					'margin-top': {
						to: (
								(
									YAHOO.util.Dom.getDocumentScrollTop() +
									YAHOO.util.Dom.getViewportHeight() / 2
								) -
								(h - 50) / 2
							) - this.originalSize.top
					},
					'margin-left': {
						to: (
								(
									YAHOO.util.Dom.getDocumentScrollLeft() +
									YAHOO.util.Dom.getViewportWidth() / 2
								) -
								(w - 50) / 2
							) - this.originalSize.left
					}
				},
				misc ? 2 : 0.4,
				YAHOO.util.Easing.bounceOut
			);
			el[i].anim.animate();
		}
		
		this.panel.changesize.className = 'maximized';
		this.panel.changesize.innerHTML = '<span>-</span>';
	}
};


// Enhance video tags
YAHOO.util.Event.onDOMReady(
	function()
	{
		var videos = document.getElementsByTagName('video');
	
		for (var i = 0; videos[i]; i++)
		{
			var v = new EnhancedVideo(videos[i]);
		}
	}
);
