
//Evento que se lanza cando o documento cargou todo guai (exceptuando as imaxes)
$(document).ready(function () {
	var html_update_w = '';
	//Fálame
	var html_defecto = $('#contido_falame').html();
	$("li.ficheiro a[rel!='modal'], li.webcam a[rel!='modal']").livequery('click', function () {
		$("#contido_falame").html('<div class="load-falame"></div>');
		var url = $(this).attr('href');
		$.ajax({
			url: url,
			cache: false,
			success: function(html){
				$('#contido_falame').html(html);
			}
		});
		return false;
	});
	
	//Fálame webcam
	$('form.webcam').livequery('submit', function () {
		var control = $('form.webcam #num_control').val();
		var titulo = $.trim($('#titulo', this).val());
		var texto = $.trim($('#texto', this).val());
		
		//Validar os datos
		if (!titulo || !texto) {
			alert("Non te esquezas de poñerlle un título e unha descrición ao vídeo");
			return false;
		}
		//Comprobar
		$.ajax({
			url: '/ajax/comprobar_control.php',
			data: 'control=' + control,
			success: function(html) {
				if ($.trim(html)) {
					alert(html);
				} else{
					//Gardar video
					$.ajax({
						url: '/ajax/gardar_video_webcam.php',
						type: 'POST',
						data: $('form.webcam').serialize(),
						success: function (html2) {
							if (html2) {
								$('#enviando').css("visibility","visible");
								html_update_w = '<div class="aviso-falame-anuncio">' + html2 + '</div>';
								setTimeout("updateacapa()",1000);
							} else {
								alert("Non se gardou ben o arquivo. Por favor, inténtao de novo.");
								$('#enviando').css("visibility","hiddden");
								
							}
						}
					});
				}
			}
		});
		return false;
	});
	
	updateacapa = function(){
		$('#contido_falame').html(html_update_w);
	}
	
	//Cancelar
	$('#falame-cancelar').livequery('click', function () {
		$('#contido_falame').html(html_defecto);
	});
	
	
	//Fálame ficheiro
	$('form.ficheiro').livequery('submit', function () {
		var titulo = $.trim($('#titulo', this).val());
		var texto = $.trim($('#texto', this).val());
		var ficheiro = $.trim($('#ficheiro', this).val());

		//Validar os datos
		if (!titulo || !texto) {
			alert("Non te esquezas de poñerlle un título e unha descrición ao vídeo");
			return false;
		}
		if(!ficheiro){
			alert("Ei, non escolleches ningún arquivo. Selecciona o vídeo antes de darlle a enviar.");
			return false;
		}
		
		//Enviar datos
		$(this).ajaxSubmit({
			beforeSubmit: function (){
				$('#enviando').css("visibility","visible");
			},
			success: function (html) {
				if (html) {
					var html_final = '<div class="aviso-falame-anuncio">' + html + '</div>';
					$('#contido_falame').html(html_final);
				} else {
					alert("Non se gardou ben o arquivo. Por favor, inténtao de novo.");
					$('#enviando').css("visibility","hidden");
				}
			}
		});
		return false;
	});
	
});


// Estas son as funcións que actualiza o player

function btPlayPressed(){
	//alert("btPlayPressed");
	//this function is called whenever the Play button is pressed
}
function btSavePressed(streamName,streamDuration,userId){
	//alert("btSavePressed("+streamName+","+streamDuration+","+userId+")");
	//this function is called when the SAVE button is pressed and it is called with 3 parameters: 
	//streamName: a string representing the name of the stream recorded on the video server including the .flv extension
	//userId: the userId sent via flash vars or via the avc_settings.XXX file, the value in the avc_settings.XXX file takes precedence if its not empty
	//duration of the recorded video/audio file in seconds but acccurate to the millisecond (like this: 4.322)
	
	$('#titulo').focus();
}

function btStopPressed(appState){
	//alert("btStopPressed("+appState+")");
	//the appState parameter specifyes in what state teh recorder is when the STOP button is pressed. Its values are: recording  OR playing
	
	/*
	if (appState=="recording"){
	}else if (appState=="playing"){
	}
	*/
}

function btRecordPressed(){
	//alert("btRecordPressed");
	//this function is called whenever the Record button is pressed
}
