$(document).ready(function() {
	$('#banners').cycle({
		fx: 'fade',
		speed: 'fast',  
	    pager: '#nav',
	    pagerAnchorBuilder: pagerFactory
	});
	function pagerFactory(idx, slide) {
		return '<li id="'+idx+'"><a href="#">'+(idx+1)+'</a></li>';
	};
	
	$('.nav-como li').click(function(){
		var qt = $(this).prevAll().size();
		if($('.overf li:nth-child('+ (qt+1)+')').css('display') == 'none'){
			$('.nav-como li').removeClass('active');
			$(this).addClass('active');
			$('.overf li').hide();
			$('.overf li img').css('top', '-300px');
			$('.overf li:nth-child('+ (qt+1)+')').show('normal',function(){
				$('.overf li img').animate({top : '0'},150).animate({top : '-20'},150).animate({top : '0'},150);
			});
			atv = qt + 1;
		};
	});
	var atv = 1;
	$('.como-f .next').click(function(){
		if(atv == 4){
			atv = 0;
			atv = atv + 1;
			$('.overf li').hide();
			$('.overf li img').css('top', '-300px');
			$('.nav-como li').removeClass('active');
			$('.nav-como li:nth-child('+ (atv)+')').addClass('active');
			$('.overf li:nth-child('+ (atv)+')').show('normal',function(){
				$('.overf li img').animate({top : '0'},150).animate({top : '-20'},150).animate({top : '0'},150);
			});
			
		}else {
			atv = atv + 1;
			$('.overf li').hide();
			$('.overf li img').css('top', '-300px');
			$('.nav-como li').removeClass('active');
			$('.nav-como li:nth-child('+ (atv)+')').addClass('active');
			$('.overf li:nth-child('+ (atv)+')').show('normal',function(){
				$('.overf li img').animate({top : '0'},150).animate({top : '-20'},150).animate({top : '0'},150);
			});
		}
	});

	$('.como-f .prev').click(function(){
		if(atv == 1){
			atv = 5;
			atv = atv - 1;
			$('.overf li').hide();
			$('.overf li img').css('top', '-300px');
			$('.nav-como li').removeClass('active');
			$('.nav-como li:nth-child('+ (atv)+')').addClass('active');
			$('.overf li:nth-child('+ (atv)+')').show('normal',function(){
				$('.overf li img').animate({top : '0'},150).animate({top : '-20'},150).animate({top : '0'},150);
			});
		}else {
			atv = atv - 1;
			$('.overf li').hide();
			$('.overf li img').css('top', '-300px');
			$('.nav-como li').removeClass('active');
			$('.nav-como li:nth-child('+ (atv)+')').addClass('active');
			$('.overf li:nth-child('+ (atv)+')').show('normal',function(){
				$('.overf li img').animate({top : '0'},150).animate({top : '-20'},150).animate({top : '0'},150);
			});
		}
	});
	$('.e1 a').click(function(){
		$('.e1').fadeOut("slow");
		$('.e2').fadeIn("slow");
	});
	$('.e2 a').click(function(){
		$('.e2').fadeOut("slow");
		$('.e1').fadeIn("slow");
	});

	var ajaxLogin = null;
	$('#frmLogin').submit(function() {
		$('#send-login').css('display', 'none');
		$('#check-login').css('display', 'inline');
		if (!ajaxLogin || ajaxLogin.readyState != 1) {
			ajaxLogin = $.ajax({
				url: $(this).attr('action'),
				cache: false,
				type: $(this).attr('method'),
				dataType: 'json',
				data: {
					login: $('#txtLogin').val(),
					password: $('#txtPassword').val()
				},
				success: function(retorno) {
					$('#check-login').css('display', 'none');
					$('#send-login').css('display', 'inline');
					if (retorno.success) {
						if ($('#loginRedirect').val()) {
							document.location.href = $('#loginRedirect').val();
						} else {
							ClickMania.doLogin(retorno.dados);
						}
					} else {
						$('#userMessages').html(retorno.message).slideDown('slow', function() {
							window.setTimeout(function() {
								$('#userMessages').slideUp('slow');
							}, 5000);
						});
					}
				}
	        });
		}
		return false;
	});

	$('#lost-password').click(function() {
		if ($('#txtLogin').val()) {
			$('#send-login').css('display', 'none');
			$('#check-login').css('display', 'inline');
			$.ajax({
				url: $(this).attr('href'),
				cache: false,
				type: 'POST',
				dataType: 'json',
				data: {
					login: $('#txtLogin').val()
				},
				success: function(retorno) {
					if (retorno.success) {
						alert(retorno.message);
					} else {
						$('#userMessages').html(retorno.message).slideDown('slow', function() {
							window.setTimeout(function() {
								$('#userMessages').slideUp('slow');
							}, 5000);
						});
					}
					$('#check-login').css('display', 'none');
					$('#send-login').css('display', 'inline');
				}
	        });
		} else {
			window.alert('Informe o login de acesso para recuperar sua senha');
			$('#txtLogin').focus();
		}
		return false;
	});
});

function number_format(a, b, c, d)
{
	if (!b) b = 2;
	if (!c) c = ',';
	if (!d) d = '.';

	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');
	
	if (!f[0]) {
		f[0] = '0';
 	}
 	if (!f[1]) {
  		f[1] = '';
 	}
 	if (f[1].length < b) {
		g = f[1];
		for (i=f[1].length + 1; i <= b; i++) {
			g += '0';
		}
		f[1] = g;
	}
	if(d != '' && f[0].length > 3) {
		h = f[0];
		f[0] = '';
		for(j = 3; j < h.length; j+=3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? '' : c;
	return f[0] + c + f[1];
}

