// dirty hack - converti you tube

function youtubeExtract(url){

	// creo una regex
	var re = new RegExp('.*v=([A-Za-z_0-9-]*)&?.*'); 
	// ricerco
	var result = url.match(re);
	
	// controllo se è definito 
	if( typeof( result[1] ) != 'string' || result[1] == '' ) 
		return('');
		
	// ritorno il codice
	return( result[1] );
	
}

function youtubeConvert(){

	var idx = document.location;
	
	
	// cambi aa true per disabilitare l'automatico
	if( false ){

		if( idx.href.indexOf('youtube=on', 0) == -1 ) return;
	
		document.title = document.title + ' [YOUTUBE]';
	
	}
	
	
	// ciclo elementi href
	var anchors = document.getElementsByTagName('a');
	
	var ytString = 'http://www.youtube.com/watch?';
	var ytStringLen = ytString.length;
	
	for (var i=0; i < anchors.length; i++){
		
		// se non inizia con http://www.youtube.com/watch?
		// salto
		if( anchors[i].href.substring(0, ytStringLen).toLowerCase() != ytString ) continue;
		
		// shortcut
		var anchor = anchors[i];
		
		// estraggo il codice
		var code = youtubeExtract( anchor.href );
		
		// se non c'è il codice, salto
		if( code == '' ) continue;
		
		// creo un nodo div 
		var containerDiv = document.createElement('div');
		containerDiv.className='msgBodyYoutube';
		containerDiv.innerHTML='<object type="application/x-shockwave-flash" style="width:450px; height:366px;" data="http://www.youtube.com/v/' + code + '"><param name="movie" value="http://www.youtube.com/v/' + code + '" />';
		
		// <object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/DEZNDO4pdys&hl=it&fs=1&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/DEZNDO4pdys&hl=it&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>
				
		// rimpiazzo il codice
		code = anchor;
		
		anchor.parentNode.insertBefore( containerDiv, anchor );
		
		// aggiungo url originale
		anchor.parentNode.insertBefore( document.createTextNode('Url Youtube: '), anchor );
	
	
	}
	
	
	// riposizionamento dell'hash
	if( idx.hash != '' ){
	
		// ottengo l'elemento
		var aNames = document.getElementsByName( idx.hash.substring(1) );
		
		if( aNames.length == 1 ){
			// solo se ce n'è uno solo
			aNames[0].focus();
		
		}
	
	
	}
	


}