/** ATTENTION CE FICHIER EST UTILISE COTE SITE PUBLIE
  * TOUTE MODIFICATION PEUT ENTRAINER LE DYSFONCTIONNEMENT DE LA PAGE CONSULTEE 
  **/

/** Insére un noeud contenant le lecteur flash afin de visionner un fichier vidéo ou écouter un fichier audio.
  * @param object
  * @param array
  * @return boolean
  **/
function insertMediaBlock( oNode, aData, iMediaType, sMediaTitle ){
//	var oNode = getBlockNode( oNode );
	if( oNode == false ){
		return false;
	}
	
	if(!DetectFlashVer(8, 0, 0)){
		oNewNode = document.createElement('div');
		oNewNode.innerHTML = '<div class="text_image"><div class="col1"><h3><span>'+sMediaTitle+'</span></h3><span class="text_with_options">'+lng_published_site[language_site]['pluginFlashMissing']+'</span></div></div>';
		oNode.parentNode.replaceChild(oNewNode, oNode);
		return true;
	}
	
	// définition du noeud
	var sNode = '<dl><dt><div style="margin-top: {marginTop};">&nbsp;</div>{object}</dt><dd>{link}</dd></dl>';
	var sNodeObject = '<object wmode="transparent" width="{width}" height="{height}" type="application/x-shockwave-flash" data="{server}modules/multimedia/flash/player_flv.swf" >{contentObject}</object>';
	var sNodesObjectContent = '<param name="wmode" value="transparent" /><param name="movie" value="{server}modules/multimedia/flash/player_flv.swf" /><param name="FlashVars" value="configxml={configxml}" />';
	// définition des dimensions du lecteur
	if( iMediaType == 2 ){
		var iWidth = Math.floor( ( 90 * oNode.offsetWidth / 100 ) );
		var iHeight = Math.floor( iWidth / aData[ 'ratio' ] );
		delete( aData[ 'ratio' ] );
	}else{
		var iWidth = Math.floor( ( 90 * oNode.offsetWidth / 100 ) );
		var iHeight = 20;// en deça de 23 pixels, ie7 n'affiche pas la console du lecteur.
	}
	// Gestion des caractères pour l'affichage du titre sur le lecteur
	var tmp_media_title = aData[ 'title' ].replace(/&amp;/g, escape("&"));
	sNodesObjectContent = sNodesObjectContent.replace( /\{configxml\}/, aData[ 'configxml' ] + ';w|' + iWidth + ';h|' + iHeight + ';c1|' + aData[ 'c1' ] + ';c2|' + aData[ 'c2' ] + ( aData[ 'showTitle' ] == 1 ? '&title=' + tmp_media_title : '' ) );
	delete( aData[ 'configxml' ] );
	// dimensions formelles (sauf pour ie)
	sNodeObject = sNodeObject.replace( /\{width\}/, iWidth );
	sNodeObject = sNodeObject.replace( /\{height\}/, iHeight );
	sNode = sNode.replace( /\{marginTop\}/, aData[ 'marginTop' ] );
	delete( aData[ 'marginTop' ] );
	// indique l'hôte
	sNodesObjectContent = sNodesObjectContent.replace( /\{server\}/, aData[ 'server' ] );
	sNodeObject = sNodeObject.replace( /\{server\}/, aData[ 'server' ] );
	delete( aData[ 'server' ] );
	// creation du lien
	var sNodeLink = '<a style="text-decoration: none; cursor: default;">' + aData[ 'title' ] + '</a>';
	sNode = sNode.replace( /\{link\}/, sNodeLink );
	delete( aData[ 'title' ] );
	// assemblage du noeud définitif
	sNodeObject = sNodeObject.replace( /\{contentObject\}/, sNodesObjectContent );
	sNode = sNode.replace( /\{object\}/, sNodeObject );
	// insertion du noeud
	oNewNode = document.createElement( 'div' );
	oNewNode.className ='image';
	oNewNode.innerHTML = sNode;
	oNode.parentNode.replaceChild( oNewNode, oNode );
	return true;
}


/** Retourne un tableau contenant les codes décimaux RGB.
  * @param string code couleur d'un noeud
  * @return array integer
  **/
function getDecimalRGB( sStyle ){
	// si aucune couleur n'est détecté, on se positionne sur le gris
	if( sStyle == "transparent" ){
		sStyle = '#888';
	}
	var sRed = 8;
	var sGreen = 8;
	var sBlue = 8;
	try{
		switch( true ){
			case sStyle.length == 4:// #123
				sRed = getDecimalFromHexa( sStyle.charAt( 1 ) ) * 16 + getDecimalFromHexa( sStyle.charAt( 1 ) );
				sGreen = getDecimalFromHexa( sStyle.charAt( 2 ) ) * 16 + getDecimalFromHexa( sStyle.charAt( 2 ) );
				sBlue = getDecimalFromHexa( sStyle.charAt( 3 ) ) * 16 + getDecimalFromHexa( sStyle.charAt( 3 ) );
				break; 
			case sStyle.length == 7:// #122237
				sRed = getDecimalFromHexa( sStyle.charAt( 1 ) ) * 16 + getDecimalFromHexa( sStyle.charAt( 2 ) );
				sGreen = getDecimalFromHexa( sStyle.charAt( 3 ) ) * 16 + getDecimalFromHexa( sStyle.charAt( 4 ) );
				sBlue = getDecimalFromHexa( sStyle.charAt( 5 ) ) * 16 + getDecimalFromHexa( sStyle.charAt( 6 ) );
				breal
			default:// rgb( 0, 12, 237 )
				var sRegExp = /\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})/i;
				aRGB = sRegExp.exec( sStyle );
				sRed = parseInt( aRGB[ 1 ] );
				sGreen = parseInt( aRGB[ 2 ] );
				sBlue = parseInt( aRGB[ 2 ] );
				break;
		}
	}catch( e ){
		// arf ...
	}
	return new Array( Math.floor( parseInt( sRed ) ), Math.floor( parseInt( sGreen ) ), Math.floor( parseInt( sBlue ) ) );
}

/** Retourne un décimal à parir d'un caractère hexadécimal
  * @param string caractère hexadécimal
  * @return integer
  **/
function getDecimalFromHexa( sHexa ){
	var i;
	switch( sHexa ){
		case 'a':
			i = 10;
			break;
		case 'b':
			i = 11;
			break;
		case 'c':
			i = 12;
			break;
		case 'd':
			i = 13;
			break;
		case 'e':
			i = 14;
			break;
		case 'f':
			i = 15;
			break;
		default:
			i = sHexa * 1;
			break;
	}
	return i;
}

function getHexaFromDecimal( iDec ){
	var i;
	switch( iDec ){
		case 10:
			i = 'a';
			break;
		case 11:
			i = 'b';
			break;
		case 12:
			i = 'c';
			break;
		case 13:
			i = 'd';
			break;
		case 14:
			i = 'e';
			break;
		case 15:
			i = 'f';
			break;
		default:
			i = iDec + '';
			break;
	}
	return i;
}

/** Retourne un code couleur (sans le dièse) à partir d'un tableau RGB décimal
  * @param array integer
  * return string
  **/
function getHexaRGB( aRGB ){
	var sReturn = '';
	for( var i = 0 ; i < 3 ; i++ ){
		sReturn += getHexaFromDecimal( Math.floor( aRGB[ i ] / 16 ) ); 
		sReturn += getHexaFromDecimal( Math.floor( aRGB[ i ] % 16 ) );
	}
	return sReturn;
}

/** Retourne un tableau de décimaux (RGB) négatif par rapport à celui passé en argument. 
  * @param array
  * @return array
  **/
function getNegativeRGB( aRGB ){
	return new Array( Math.floor( Math.abs( aRGB[ 0 ] - 255 ) ), Math.floor( Math.abs( aRGB[ 1 ] - 255 ) ), Math.floor( Math.abs( aRGB[ 2 ] - 255 ) ) );  
}

/** Retourne un tableau de décimaux (RGB) médien par rapport à ceux passés en argument. 
  * @param array
  * @return array
  **/
function getAverageRGB( aRGB1, aRGB2 ){
	return new Array( Math.floor( aRGB1[ 0 ] + aRGB2[ 0 ] ) / 2, Math.floor( aRGB1[ 1 ] + aRGB2[ 1 ] ) / 2, Math.floor( aRGB1[ 2 ] + aRGB2[ 2 ] ) / 2 );  
}


