/**
 * Funciones para cargar los vídeos en UniPlacement TV
 */

var start=false;

// Iniciar
$(document).ready(function()
{
    listadoVideos();
    if (getUrlVars()['video']) { 
    	var videoId=getUrlVars()['video'];
    	start=true;
    	$('#'+videoId).click();    	
    }
    else {
    	$('#outplacement').click();
    	start=true;
    }
});


// Para importar otros archivos js
function importScript(url){
    var tag = document.createElement("script");
    tag.type="text/javascript";
    tag.src = url;
    document.body.appendChild(tag);
}

// Para leer los parámetros de una URL
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

// Coge los vídeos disponibles de UniPlacement TV
function listadoVideos()
{
	$('ul#listadoVideos > li > a').click(function(event) 
	{
		event.preventDefault();
		$('ul#listadoVideos > li > a').each(function() {
				$(this).removeClass();
		});
		$(this).addClass('actual');
		var videoUrl = $(this).attr('href');
		var videoTitle = $(this).find('span.tituloVideo').text();
		var videoDesc = $(this).find('div.descripcionVideo').html();
		//alert('Titulo: ' + videoTitle + '\nUrl: ' + videoUrl + '\nDescripcion:\n' + videoDesc);
		cargaVideo(videoUrl,videoTitle,videoDesc,start);
	});
}

function cargaVideo(url,titulo,desc,start)
{
	var imageVideo="../img/nota_video.jpg";
	var flashvars = {
	  file: url,
	  image: imageVideo,
	  bufferlength: "5",
	  autostart: start
	};
	var params = {
	  menu: "false",
	  allowfullscreen: "true",
	  allowscriptaccess: "true",
	  wmode: "transparent"	  
	};
	var attributes = {
	  id: "videoPlayer",
	  name: "videoPlayer"
	};
    swfobject.embedSWF("../flash/player.swf", "videoPlayer", "453", "385", "9.0.0","",flashvars,params,attributes);
    $('#tituloVideoActual').text(titulo);
    $('#descripcionVideo').html(desc);
}

