var flashPlayer;

/*
=============
 FlashPlayer
=============
	classe permettant de jouer un fichier .swf
*/
function FlashPlayer( id ) {
	var fname;
	var container;
	var timer;
	var alpha = 100;

	var div = document.getElementById( id );

	if ( !div )
		return false;

	var links = div.getElementsByTagName( "a" );
	for ( var l = 0; l < links.length; l++ ) {
		links[ l ].onclick = function() {
			flashPlayer.open( this.name );
		}
	}

/*
======
 open
======
	c'est LA méthode à utiliser pour lancer une animation flash ;)
*/
	this.open = function( filename ) {
		fname = "downloads/videos/" + filename + ".swf";

		container = document.createElement( "div" );
		// un peu de CSS...
		with ( container.style ) {
			backgroundColor = "#333";
			position = "absolute";
			top = "0";
			left = "0";
			textAlign = "center";
			zIndex = 10000;
			opacity = "0";
			filter = "alpha( opacity=0 )";
			paddingTop = "165px";

			width = "0";
			height = "0";
			border = "solid 1px #000";
		}

		document.body.appendChild( container );

		if ( !window.innerWidth || !window.innerHeight ) {
			window.innerWidth = document.body.width;
			window.innerHeight = document.body.height;
		}

		timer = setInterval( "flashPlayer.animateOpen();", 40 );

		var obj = document.getElementsByTagName( 'object' );
		for ( var o = 0; o < obj.length; o++ ) {
			obj[ o ].style.visibility = 'hidden';
		}
	}

/*
=======
 close
=======
*/
	this.close = function() {
		container.innerHTML = "";
		timer = setInterval( "flashPlayer.animateClose();", 40 );
	}

/*
=============
 animateOpen
=============
*/
	this.animateOpen = function() {
		if ( !timer )
			return;

		setElementOpacity( container, ( 100 - alpha ) );
		alpha -= 3;

		if ( window.innerWidth )
			container.style.width = parseInt( container.style.width ) + window.innerWidth / 25 + "px";
		else
			container.style.width = parseInt( container.style.width ) + document.body.offsetWidth / 25 + "px";

		if ( window.innerHeight )
			container.style.height = parseInt( container.style.height ) + window.innerHeight / 25 + "px";
		else
			container.style.height = parseInt( container.style.height ) + document.body.offsetWidth / 25 + "px";

		if ( alpha <= 10 ) {
			clearInterval( timer );
			timer = null;
			setElementOpacity( container, 90 );
			alpha = 10;

			var video = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="300" height="300" title="flash animation">';
			video += '  <param name="movie" value="' + fname + '" />';
			video += '  <param name="quality" value="high" />';
			video += '  <embed src="' + fname + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="300" height="300"></embed>';
			video += '</object>';

			var img = '<img src="mambots/content/plugin_jw_sig/closelabel.gif" alt="Close" onclick="flashPlayer.close();" />';

			container.innerHTML = video + "<br />" + img;
		}
	}

/*
==============
 animateClose
==============
*/
	this.animateClose = function() {
		if ( !timer || !container )
			return;

		setElementOpacity( container, ( 90 - alpha ) );
		alpha += 3;

		if ( window.innerWidth )
			container.style.width = parseInt( container.style.width ) - window.innerWidth / 25 + "px";
		else
			container.style.width = parseInt( container.style.width ) - document.body.offsetWidth / 25 + "px";

		if ( window.innerHeight )
			container.style.height = parseInt( container.style.height ) - window.innerHeight / 25 + "px";
		else
			container.style.height = parseInt( container.style.height ) - document.body.offsetWidth / 25 + "px";
			// container.style.height = parseInt( container.style.height ) - document.body.offsetHeight / 25 + "px";

		if ( alpha >= 97 ) {
			clearInterval( timer );
			timer = null;
			setElementOpacity( container, 0 );
			alpha = 100;

			container.style.display = "none";
			var parent = container.parentNode;
			parent.removeChild( container );

			var obj = document.getElementsByTagName( 'object' );
			for ( var o = 0; o < obj.length; o++ ) {
				obj[ o ].style.visibility = 'visible';
			}
		}
	}
}

/*
===================
 setElementOpacity
===================
	value est compris entre 0 et 100 (inclus);
*/
function setElementOpacity( element, value ) {
	element.style.opacity = ( value / 100 );
	element.style.filter = "alpha( opacity=" + value + " )";
}
