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

var tmp;
var foto_total;
var foto_atual;
var foto_atual_id;
var coords;
var offset;

function mycarousel_initCallback(carousel) {
    $('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    $('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

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');
}

load_comentarios = function()
{
	$('#info_loading').fadeIn('fast', function()
	{
		$.post('/ajax/comentarios-show', { foto: foto_atual_id }, function(data)
		{
			$('#info_loading').fadeOut('fast');
			if (data)
			{
				$('#comentarios_show').empty().html(data);
				$('#comentarios').slideDown();
			}
		});
	});

	return false;
}

foto_proxima_anterior = function(acao)
{
	if (acao == '+')
	{
		foto_atual++;
		if (foto_atual >= foto_total)
			foto_atual = 0; // vai para o inicio
	}
	else
	{
		foto_atual--;
		if (foto_atual < 0)
			foto_atual = foto_total - 1; // vai para o final
	}

	foto_mostra($('#gallery_list li a:eq('+foto_atual+')'), true);
}

foto_botoes = function()
{
	/**
	 * posiciona os botoes avancar e voltar na foto
	 */
	coords = $('#gallery_show').offset();
	$('a.ant,a.pro').css({'top' : coords.top+50, 'height' : $('#gallery_show img#foto_show').height() });
	$('a.ant').css('left', coords.left);
	
	coords = parseInt (($(window).width() - $('#text_internal').width() + 10) / 2);
	if (coords < 0)
		coords = 0;
	$('a.pro').css('right', coords);
}

foto_mostra = function(fid, param)
{
	tmp = fid;
	if ($(tmp).attr('rel') != foto_atual  ||  param == true) {
		foto_atual = $(tmp).attr('rel');
		foto_atual_id = $(tmp).attr('foto');
		$('a.ant,a.pro').show();
	
		$('#comentarios').slideUp();
	
		$('#gallery_list li a').removeClass('ativo');
		$(tmp).addClass('ativo');
		offset = $(tmp).offset();
	
		$.post('/ajax/contador', { secao: 'fotos', id: $(tmp).attr('foto') });
		
		var height = $('#gallery_show').height() + 10;
		$('#gallery_show p, #gallery_show img#foto_show, #gallery_show ul').fadeOut('fast', function()
		{
			$('#gallery_show').css('background', 'url(/_img/load.gif) no-repeat center center').css('height', height + 'px');
			$('#gallery_show p').text($(tmp).attr('title'));
			$('#gallery_show h2 span').text('(foto ' + (parseInt(foto_atual) + 1) + ' de ' + foto_total + ')');
			$('#gallery_show img#foto_show').attr('src', $(tmp).attr('href')).load(function()
			{
				location.replace("#foto="+foto_atual);
	
				$('#gallery_show img#foto_show').attr('alt', $(tmp).attr('title'));
				$('#gallery_show').css('background', 'none').css('height', 'auto');
				$('#gallery_show p, #gallery_show img#foto_show, #gallery_show ul').fadeIn('fast');
				foto_botoes();
			});
		});
	
	
		/**
		 * altera posicao do jcarousel 
		 */
		if (foto_atual == 0)
		{
			$('a.ant').hide(); // retira botao voltar
		}
		else if (foto_atual == (foto_total - 1))
		{
			$('a.pro').hide(); // retira botao avancar
		}
		else if (offset.left > 1000)
		{
			$('#mycarousel-next').click();
		}
		else if (offset.left < 300)
		{
			$('#mycarousel-prev').click();
		}
	}
}

function mycarousel_initCallback2(carousel) {
    $('#mycarousel-next2').bind('click', function() {
        carousel.next();
        return false;
    });

    $('#mycarousel-prev2').bind('click', function() {
        carousel.prev();
        return false;
    });
};

document.onkeydown = function(e){ 	
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	if(keycode == 39){ // direita
		foto_proxima_anterior('+');
	}else if(keycode  == 37){ //esquerda
		foto_proxima_anterior('-');
	}	
};

$(document).ready(function()
{
	foto_total = $('#gallery_list li a').length;

	$('#gallery_list a[href!=javascript:void(0)]').click(function()
	{
		foto_mostra($(this));
		return false;
	});

	$('#gallery_list').jcarousel(
	{
		scroll: 7,
		initCallback: mycarousel_initCallback,
		// This tells jCarousel NOT to autobuild prev/next buttons
		buttonNextHTML: null,
		buttonPrevHTML: null
	});

	$('#gallery_others').jcarousel(
	{
		scroll: 2,
		initCallback: mycarousel_initCallback2,
		// This tells jCarousel NOT to autobuild prev/next buttons
		buttonNextHTML: null,
		buttonPrevHTML: null
	});

	var foto_pre = location.href.split('#');
	if (foto_pre[1])
	{
		foto_pre = foto_pre[1].split('=');
		foto_pre = parseInt(foto_pre[1]);
		if (foto_pre != '')
			foto_mostra($('#gallery_list li a:eq('+foto_pre+')'), true);
		else
			foto_mostra($('#gallery_list li a')[0], true);
	}
	else
	{
		foto_mostra($('#gallery_list li a')[0], true);
	}
	foto_botoes();

	if ($('#gallery_list li').length < 16)
	{
		$('#gallery_list a.controls').hide();
	}

	$('#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', { foto: foto_atual_id, msg: $('#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;
	});

});