var VIDEOS = {};

VIDEOS.cargarVideo = function(id_video, id, ancho, alto) {
	
	var preview = $('#vp_' + id_video);
	var media = preview.parent();
	
	//preview.remove();
	
	var contenido = '<object id="' + id + '" class="BrightcoveExperience">';
	contenido += '<param name="bgcolor" value="#FFFFFF" />';
	contenido += '<param name="width" value="' + ancho + '" />';
	contenido += '<param name="height" value="' + alto +  '" />';
	contenido += '<param name="playerID" value="66035019001" />';
	contenido += '<param name="publisherID" value="55837803001"/>';
	contenido += '<param name="@videoPlayer" value="' + id_video + '"/>';
	contenido += '<param name="isVid" value="true" />';
	contenido += '<param name="isUI" value="true" />';
	contenido += '<param name="optimizedContentLoad" value="true" />';
	contenido += '<param name="wmode" value="transparent" />';
	contenido += '<param name="autoStart" value="true" />';
	contenido += '</object>';
	
	media.html(contenido);
			
	if(typeof(brightcove) == 'undefined') {
		$.getScript('http://admin.brightcove.com/js/BrightcoveExperiences.js', function() {
				brightcove.createExperiences(null, id);
			}
		);
	}
	else {
		brightcove.createExperiences(null, id);
	}
};

VIDEOS.buscar = function(frm) {
	var q = (typeof(frm.q) != "undefined") ? frm.q.value : "";
	window.location = "/fotos/buscador/index.html?q=" + q;
};

VIDEOS.capturasBC = function() {
	var ids = [];
	$(".MM_BC").each(function() {
		if($(".MM_BC_id",this).length == 1) {
			var id = $(".MM_BC_id",this).text();
			if(!isNaN(id)) {
				$(this).attr("id_bc",id);
				$(".MM_BC_id",this).remove();
				if($.inArray(id,ids) == -1) ids.push(id);
			}
		}
	});
	if(ids.length > 0) {
		$.getJSON(
			"/backend/VIDEOS.infoBC.php",
			{"ids[]": ids},
			function(respuesta) {
				$.each(respuesta,function() {
					var video = this;
					$(".MM_BC[id_bc='" + video.id + "']").each(
						function() {
							$(this).removeAttr("id_bc");
							$(".MM_BC_captura_still",this).each(function() {
								var captura = $('<img src="' + video.still + '"/>').load(function() {VIDEOS.centrarImagen(this)});
								$(this).html(captura);
							});
							$(".MM_BC_captura_thumb",this).each(function() {
								var captura = $('<img src="' + video.thumb + '" width="120"/>').load(function() {VIDEOS.centrarImagen(this)});
								$(this).html(captura);
							});
							$(".MM_BC_captura_thumb40",this).each(function() {
								var captura = $('<img src="' + video.thumb + '" width="40"/>').load(function() {VIDEOS.centrarImagen(this)});
								$(this).html(captura);
							});
							$(".MM_BC_info",this).each(function() {
								$(this).text(video.duracion + " - " + video.hace);
							});
							$(".MM_BC_reloj",this).each(function() {
								$(this).text(video.reloj);
							});
						}
					);
				});
			}
		);
	}
}

VIDEOS.centrarImagen = function(objeto) {
	$(objeto).each(function () {
		var altura = $(this).height();
		if(altura > 0) {
			var alto = $(this).parents(".media,.photo").height();
			var margen = Math.round((alto - altura)/2);
			if(margen > 0) $(this).css("margin-top",margen + "px");
		}
	});
}

VIDEOS.calendario = function(fecha) {
	var tipo = "mes";
	if(typeof(fecha) == "object") {
		tipo = "dia";
		var frm = fecha;
		if(frm.dia.value == "") {
			alert("No has seleccionado el día");
			return false;
		}
		if(frm.mes.value == "") {
			alert("No has seleccionado el mes");
			return false;
		}
		if(frm.ano.value == "") {
			alert("No has seleccionado el año");
			return false;
		}
		fecha = frm.ano.value + frm.mes.value + frm.dia.value;
	}
	$.get(
		"/backend/VIDEOS.calendario.php",
		{fecha: fecha},
		function(respuesta) {
			if(tipo == "dia") {
				if(respuesta == "") alert("No existen vídeos para esta fecha.");
				else window.location = respuesta;
			}
			else {
				$("#MM-videos-calendario table.calendario").html(respuesta);
			}
		}
	);
}

VIDEOS.timeHace = function() {
	$(".MM_time").each(function() {$(this).text(GENERAL.timeHace($(this).text())).show()});
}

VIDEOS.buscar = function(frm) {
	var q = (typeof(frm.q) != "undefined") ? frm.q.value : "";
	window.location = "/videos/buscador/index.html?q=" + q;
};

