//js con funciones comunes

function get(id){
	var obj = document.getElementById(id);
	return obj;
}

//función que escribe un objeto con vídeos de youtube donde ahí un enlace a youtube
function objectyoutube (){
	var arrayLinksO = document.getElementsByTagName('a');
	for (i=0; i<arrayLinksO.length; i++){
		var enlace = arrayLinksO[i].getAttributeNode("href").value;
		if (enlace.substring(0,22)=='http://www.youtube.com') {
			
			var codigo = enlace.substring(31,42);
			var obj = document.createElement('object');
			obj.setAttribute('type','application/x-shockwave-flash');
			obj.setAttribute('width',"425");
			obj.setAttribute('height',"344");
			
			var p_m1 = document.createElement('param');
			p_m1.setAttribute('name','movie');
			p_m1.setAttribute('value',"http://www.youtube.com/v/"+ codigo +"&amp;hl=es&amp;fs=1");
			obj.appendChild(p_m1);
			
			var p_m2 = document.createElement('param');
			p_m2.setAttribute('allowFullScreen','true');
			obj.appendChild(p_m2);
			
			var p_m3 = document.createElement('param');
			p_m3.setAttribute('allowscriptaccess','always');
			obj.appendChild(p_m3);
			
			var emb = document.createElement('embed');
			emb.setAttribute('width','425');
			emb.setAttribute('height','344');
			emb.setAttribute('type','application/x-shockwave-flash');
			emb.setAttribute('src','http://www.youtube.com/v/'+ codigo +'&amp;hl=es&amp;fs=1');
			emb.setAttribute('allowscriptaccess','always');
			emb.setAttribute('allowfullscreen','true');
			obj.appendChild(emb);

			arrayLinksO[i].appendChild(obj);
			/*var objeto ='<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/'+ codigo +'&hl=es&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+ codigo +'&hl=es&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>';
			document.write(objeto);*/
		}
	}
}

function videoYoutube() {
	if (document.getElementById('cuerpo_noticia')==null) return;
	var direccion, id, cuerpo = document.getElementById('cuerpo_noticia').innerHTML; //alert(cuerpo);
	while (cuerpo.indexOf('youtube')!=-1) {
		cuerpo = cuerpo.substring(cuerpo.indexOf('http')); //alert(cuerpo);
		direccion = cuerpo.substring(0, cuerpo.indexOf('</')); //alert(direccion);
		id = direccion.substring(direccion.indexOf('v=')+2); //alert(id);
		cuerpo = cuerpo.substring(1);
		document.getElementById('cuerpo_noticia').innerHTML = document.getElementById('cuerpo_noticia').innerHTML.replace(direccion, crearObjetoVideo(id));
		//alert(document.getElementById('cuerpo_noticia').innerHTML);
	}
}
function crearObjetoVideo(id) {
	return '<object width="480" height="295"><param name="movie" value="http://www.youtube.com/v/' + id + '&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + id + '&hl=en&fs=1" type="application/x-shockwave-flash" width="480" height="295" allowscriptaccess="always" allowfullscreen="true"></embed></object>';
}


//clase para los enlaces volver
var claseVolver = 'volver';

//función que muestra todos los enlaces de la clase 'volver' y les añade un history.back() en su href
	function enlacesVolver (){
		var arrayLinks = document.getElementsByTagName('a');
		for (i=0; i<arrayLinks.length; i++){
			if (arrayLinks[i].className == claseVolver){
				arrayLinks[i].style.display = 'block';
				arrayLinks[i].onclick = function () { history.back(); return false; };
				arrayLinks[i].onkeypress = function () { history.back(); return false; };
			}
		}
	}	

// FOTO DE PORTADA
//Randomizing Unit Courtesy of Mike Winter as seen at: http://www.dynamicdrive.com/forums/showthread.php?p=8442
function random(n) { return Math.floor((Math.random() % 1) * n); }
Array.prototype.shuffle = function() { var i = this.length; while(i--) { this.swap(i, random(i + 1)); } };
Array.prototype.swap = function(x, y) { var t = this[x]; this[x] = this[y]; this[y] = t; };
//End Randomizing Unit
function cambia_foto_portada() {
	fotosPortada = new Array();
	for (var i=1; i<=6; i++) fotosPortada[fotosPortada.length] = 'foto_polaroid_'+i+'.jpg';
	fotosPortada.shuffle();
	get('foto_portada').style.backgroundImage = "url(../images/" + fotosPortada[0] + ")";
	}


function init() {
	if (TransMenu.isSupported()) {
		TransMenu.initialize();
		desplegable1.onactivate   = function() { document.getElementById('desplegable1').className = 'hover'; };
		desplegable1.ondeactivate = function() { document.getElementById('desplegable1').className = ''; }
		desplegable2.onactivate   = function() { document.getElementById('desplegable2').className = 'hover'; };
		desplegable2.ondeactivate = function() { document.getElementById('desplegable2').className = ''; }
	}
}

// functión para añadir acciones al evento onload
	function addLoadEvent(func) {
	   var oldonload = window.onload;
	  
	   if (typeof window.onload != 'function') {
	      window.onload = func;
	   }
	   else {
	      window.onload = function() {
	      oldonload();
	      func();
	    }
		}
	}

addLoadEvent(init); addLoadEvent(enlacesVolver); /*addLoadEvent(objectyoutube);*/ addLoadEvent(videoYoutube);

