/*
 * autor: Ciro Feitosa - http://cirofeitosa.com.br
 * jan/2009
 */

function nl2br (str, is_xhtml) {
    var breakTag = '';
 
    breakTag = '<br />';
    if (typeof is_xhtml != 'undefined' && !is_xhtml) {
        breakTag = '<br>';
    }
 
    return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}

var nota_atual;
var nota_i;

nota_estrelas = function(id, pid)
{
	for (nota_i=1; nota_i<=parseInt(id); nota_i++)
		$('#nota_'+pid+'_'+nota_i).attr('src', '/_img/star-on.gif');

	if (id < 5)
	{
		for (nota_i=parseInt(id)+1; nota_i<=5; nota_i++)
			$('#nota_'+pid+'_'+nota_i).attr('src', '/_img/star-off.gif');
	}
}

nota_estrelas_off = function(id, pid)
{
	for (nota_i=1; nota_i<=parseInt(id); nota_i++)
		$('#nota_'+pid+'_'+nota_i).attr('src', '/_img/star-off.gif');
}

nota_nota = function(id, pid)
{
	$('#avalia_'+pid).text('Avaliando...');

	$.post('/ajax/voto', { nota: id, video: pid }, function (data)
	{
		$('#avalia_'+pid).fadeOut('fast', function()
		{
			$('#avalia_'+pid).text('Obrigado pela avaliação!').fadeIn('fast');
		});
	});
}

$(document).ready(function()
{
	/*
	 * ativa links para notas
	 */
	$('.avalia img').each(function()
	{
		$(this)
			.mouseover(function()
			{
				nota_estrelas($(this).attr('nota'), $(this).attr('video'));
			})
			.mouseout(function()
			{
				nota_estrelas_off($(this).attr('nota'), $(this).attr('video'));
			})
			.click(function()
			{
				nota_nota($(this).attr('nota'), $(this).attr('video'));
			});
	});


	/*
	 * botoes de navegacao videos
	 */
	$('#vid_botoes a').click(function()
	{
		$('#vid_botoes a').removeClass('atual');
		$(this).addClass('atual');

		$('.videos_body:visible').hide();
		$($(this).attr('href')).show();

		return false;
	});


	/*
	 * form de indicacao
	 */
	$('#indica').submit(function()
	{
		if ($('#indica_nome').val() == '')
		{
			alert('Preencha o Nome do seu amigo');
			$('#indica_nome').focus();
			return false;
		}
		else if ($('#indica_email').val() == '')
		{
			alert('Preencha o E-mail do seu amigo');
			$('#indica_email').focus();
			return false;
		}
		else if ($('#indica_email').val().indexOf('@') < 1  ||  $('#indica_email').val().lastIndexOf('.') < 2)
		{
			alert('Informe um E-mail válido.');
			$('#indica_email').focus();
			return false;
		}

		$('#indica_botao').fadeOut('fast', function()
		{
			$('#indica_load').fadeIn('fast');

			$.post('/ajax/indica', { url: escape($('#indica_url').val()), Nome: escape($('#indica_nome').val()), Email: escape($('#indica_email').val()), Mensagem: escape($('#indica_msg').val()) }, function(data)
			{
				$('#indica_nome').val('');
				$('#indica_email').val('');
				$('#indica_msg').val('');

				$('#indica_load').fadeOut('fast', function()
				{
					$('#indica_botao').fadeIn('fast');
					alert('Vídeo indicado com sucesso!');
				});
			});
		});
		return false;
	});


	/*
	 * form de comentario
	 */
	$('#frm-comenta').submit(function()
	{
		if ($('#frmmsg').val() == '')
		{
			alert('Preencha seu comentário');
			$('#frmmsg').focus();
		}
		else
		{
			$('#frmbotao').val('Aguarde...').attr('disabled', 'true');
			var msg = $('#frmmsg').val();
			$.post('/ajax/comentarios', { video: $('#video_id').text(), msg: escape($('#frmmsg').val()) }, function(data)
			{
				$('#frmtxt').fadeOut(function()
				{
					$('#frm-comenta input').hide();
					$(this).empty().html('<i style="background: #ffffcc; padding: 5px; display: block;"><b>Seu comentário foi enviado com sucesso!</b><br />Aguarde moderação...</i><br/><p style="color:black; font-size: 12px">' + nl2br(msg) + '</p>').fadeIn();
				});
			});
		}
		return false;
	});

});