
var VB = {}

VB.External = function() {

	var opt = {
		relname: 'external'
	};	
	function init() {
		$('a[rel*="'+opt.relname+'"]').attr('target','_blank');
	};
	return {
		init: init
	};

}();

VB.Placar = function() {

	function init() {
		var next    = $('#placar .next');
		var prev    = $('#placar .prev');
		var length  = $('#placar .placar-slider li').size();
		var visible = 8;
		var count   = 8;
		$(next).click(function(){
			if (count < length) {
				$('#placar .placar-slider').animate({ left : '-=102px' }, '1000');
				count = count+1;
			}
			return false;
		});
		$(prev).click(function(){
			if (count != visible) {
				$('#placar .placar-slider').animate({ left : '+=102px' }, '1000');
				count = count-1;
			}
			return false;
		});
	};
	return {
		init: init
	};

}();

// valida e-mail
var validaEmail = function(s) {
	var regexpEmail = /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/;
	return regexpEmail.test(s);
}; // fim validaEmail

$(function(){
	
	
	VB.External.init();
	VB.Placar.init();
	
	$('#btfechar').click(function(){
		$('#overpage').hide();
	});
	
	
	$('#search-submit').val('');
	
	var tabContainers = $('#article .tabs'),
		tabNav        = $('#article .tab-nav a');
	$(tabNav).click(function() {
		tabContainers.hide().filter(this.hash).show();
		$(tabNav).removeClass('active');
		$(this).addClass('active');
		return false;
	}).filter(':first').click();
			
	$('#slider').aviaSlider({
		blockSize: {height: 'full', width: 40},
		display: 'topleft',
		transition: 'drop',
		betweenBlockDelay:80,
		animationSpeed: 800,
		switchMovement: true,
		slideControlls: 'items',
		appendControlls: '.aviaslider'
	});
	
	$("a[rel='fotos']").colorbox({
		current: '',
		height: '80%'
	});
	$("a[rel='videos']").colorbox({
		current: ''
	});

	if(window.opera) {
		if ($("a.bt-fav").attr("rel") != ""){
			$("a.bt-fav").attr("rel","sidebar");
	    }
    }
	$("a.bt-fav").click(function(e) {
		e.preventDefault();
		var url   = this.href;
		var title = this.title;
		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
			return false; // do nothing - the rel="sidebar" should do the trick
		} else { // for Safari, Konq etc
			 alert('Infelizmente este navegador não possui esta funcionalidade, por favor adicione esta página manualmente.');
		}
	});

	$('#quadra .thumb').each(function(){
		$(this).mouseover(function(){
			$('#quadra .thumb').css('z-index','1');
			$(this).css('z-index','500');
			$('.bio',this).css({'bottom':'34px','left':'-55px'}).show();
		}).mouseout(function(){
			$('#quadra .thumb').css('z-index','500');
			$('.bio',this).hide();
		});
	});
	
	$('#voce-sabia .toggle').click(function(){
		var current = $(this).parents('li');
		if ($(current).hasClass('on') == false) {
			$('#voce-sabia li').removeClass('on').addClass('off');
			$('#voce-sabia .answer').slideUp();
			$(current).removeClass('off').addClass('on').children('.answer').slideDown();
		} else {
			$(current).removeClass('on').addClass('off').children('.answer').slideUp();
		}
		return false;	
	});

	var flag_reload = false;
	$('#frases a.vote').colorbox({
		inline: true, 
		href: '#over-votar',
		width: '600px',
		onOpen: function(){
			var fid = $(this).attr('id');
			$('#frm-frases #hfrase').val(fid);
		},
		onClosed: function(){
			$('#frm-frases').show();
			$('#frm-frases-resultado').hide();
			if (flag_reload == true) {
				window.location.reload();
			};
		}
	});
	$('#frm-frases').submit(function(){
		flag_reload = false;
		// post de ajax
        var cod         = $('#hfrase').val().replace(/f/, '');
        var captcha     = $('#captcha').val();
        var raiz_site_lm = $('#raiz_site_lm').val();
        $.post(raiz_site_lm + 'votar.php', { cod_frases : cod, captcha : captcha }, function(data){
            if( data == 'OK' )
            {
                $('#frm-frases').fadeOut( '1000', function(){
                    $('#frm-frases').hide();
                    $('#frm-frases-resultado').fadeIn('1000');
                    atualizaVotos( cod );
                    flag_reload = true;
                });
            }
            else
            {
                $('#captcha').addClass('erro');
            }
        });
		return false;
	});

});

var atualizaVotos = function( cod )
{
    var atual = parseInt( $('#total-votos-'+cod).text() ) + 1;
    if( atual == 1 ) atual = atual + ' voto';
    else atual = atual + ' votos';
    $('#total-votos-'+cod).text( atual );
};

