$(document).ready(function(){
	$('a[href^=#]').click(function(){
		return false;
	});
	
	$('a.closePopup').click(function(){
		$('#bannernovo').hide();
	});

	$('#pedido-header .topo:not(.logado)').find('a.cliqueaqui').click(function(){
		$(this).parent().parent().find('div.slide').slideToggle();
		$(this).find('span').toggleClass('active')
		return false;
	});

	if($('.loginheader label[for=lembreh]')){
		$('.loginheader label[for=lembreh]').click(function(){
			val = ($('.loginheader input[name=lembreh]').val() == '0') ? 1 : 0;
			$('.loginheader input[name=lembreh]').val(val);
			$(this).toggleClass('active')
		});
	}

	$('li.cardapio').hover(function(){
		$(this).find("a:first").addClass('lst');
		$(this).find("div").show();
	},function(){
		$(this).find("a:first").removeClass('lst');
		$(this).find("div").hide();
	});
	
	$('.navbar a').hover(function(){
		$(this).addClass("atv");	
	},function(){
		$(this).removeClass("atv");
	});
	
	$('input[name=produto]').keyup(function(){
		CalcularTotal();
	});
	
	if($("input[name=produto]").length > 0)
		CalcularTotal();
			
	function CalcularTotal(){
		var $total = 0;
		var $produtos = Array();
		var $i = 0;
		$('input[name=produto]').each(function(){
			var $qtde = $(this).val();
			if($qtde != ""){
				var $valor = $(this).attr("rel").split("||");
				var $calc = $qtde * $valor[0];
				$total += $calc;
				$produtos[$i] = Array($valor[1], $qtde);
				$i++;
			}
		});
		$total += $('input[name=pedido_entrega]').val() * 1;
		$('input[name=total]').val(num2money($total));
		$('input[name=produtos]').val($produtos);
	}
	
	$("#pedido_forma_pgto").change(function(){
		if($(this).val() == 1)
			$(".troco").show();
		else
			$(".troco").hide();
	});
	$("#lmpPedido").click(function(){
		$("#pedidoForm").attr("action",$("#pedidoForm").attr("action").replace("carrinho","limpar"));
	});
	
	if($("#pedidoForm").length > 0){
		$("#act").click(function(){
			$("#pedidoForm input").each(function(){
				$(this).removeAttr("disabled");
			});
			$("#pedidoForm select").each(function(){
				$(this).removeAttr("disabled");
			});
		});
		$("#endereco_bairro").change(function(){
			$id = $(this).val();
			$val = $('input[name=pedido_entrega]').val();
			$total = parseFloat($('input[name=pedido_total]').val().replace(",", "."));
			$.ajax({
				type: "post",
				url: df.dominio + "pedido/bairro",
				data: "id=" + $id,
				//context: document.body,
				success: function(msg){
					$total += - parseFloat($val) + parseFloat(msg);
					$('.pedido_valor').html('R$ ' + num2money(msg));
					$('input[name=pedido_entrega]').val(msg);
					$('input[name=pedido_total]').val(num2money($total));
				}
			});	
		});
	}
	
	$(".carrinho").find("input[name=produtos]").blur(function(){
		$("#pedidoForm").attr("action",$("#pedidoForm").attr("action").replace("carrinho","carrinho/upd/" + $(this).attr("rel") + "$$" + $(this).val()));
		$("#pedidoForm").submit();
	});
	
	function num2money(n_value) {
		// validate input
		if (isNaN(Number(n_value)))
			return 'ERROR';
	
		// save the sign
		var b_negative = Boolean(n_value < 0);
		n_value = Math.abs(n_value);
	
		// round to 1/100 precision, add ending zeroes if needed
		var s_result = String(Math.round(n_value * 1e2) % 1e2 + '00').substring(0, 2);
		
		// separate all orders
		var b_first = true;
		var s_subresult;
		while (n_value >= 1) {
			s_subresult = (n_value >= 1e3 ? '00' : '') + Math.floor(n_value % 1e3);
			s_result = s_subresult.slice(-3) + (b_first ? '.' : ',') +  s_result;
			b_first = false;
			n_value = n_value / 1e3;
		}
		// add at least one integer digit
		if (b_first)
			s_result = '0.' +  s_result;
		
		s_result = s_result.replace(",","");
		s_result = s_result.replace(".",",");
	
		// apply formatting and return
		return s_result;
	}


	$(".validacao").validate({
		errorElement: 'span',
		errorClass: 'errors',
		errorPlacement: function(error, element) {
			error.appendTo(element.parent());
		}
	});

	$("#pedido_forma_pgto").change(function(){
		if($(this).val() != 1)
			$("#pedido_troco").rules("remove");
		else
			$("#pedido_troco").rules("add", {
				required: true
			});
	});
	
	$(".validacaoh").validate({
		errorElement: 'ul',
		errorClass: 'errors',
		errorPlacement: function(error, element) {
			error.appendTo(element.parent());
		}
	});
	
	if($('.form_new li.selecione')){
		$('.form_new li.selecione a').each(function(i){
			$(this).click(function(){
				$('.form_new li.selecione a').removeClass('active');
				$(this).addClass('active');
				$('#tipoc').val($(this).attr('className'));
			});
		});
	}

	$('input.telefone, input.celular').mask('(99) 9999-9999');
	$('input.cep').mask('99999-999');
	$('input.cpf').mask('999.999.999-99');
	$('input.data').mask('99/99/9999');
	
	$('#bannerhome').flash({
		swf:'/assets/banner/swf/banner.swf',
		width:960,
		height:517,
		allowscriptaccess:'always',
		wmode:'transparent'
	});

	$("a[rel='colorbox']").colorbox();

	$('.boxImage li a').each(function(i){
		$(this).click(function(){
			$('.boxImage div img').attr('src', $(this).attr("href"));
			//$(this).parent().parent().parent().find('div').find('img').
			return false;
		});
	});

	swapValue = [];
	$("div>.input").each(function(i){
		if($(this).attr("readonly") ==  false){
			swapValue[i] = $(this).val();
			$(this).focus(function(){
				if ($(this).val() == swapValue[i]) {
					$(this).val("");
				}
			}).blur(function(){
				if ($.trim($(this).val()) == "") {
					$(this).val(swapValue[i]);
				}
			});
		}
	});
	
	$(function() {
		setInterval(
			function() {
				var $active = $('#slideshow div.active');
 
				if ( $active.length == 0 ) $active = $('#slideshow div:last');
 
				var $next =  $active.next().length ? $active.next() : $('#slideshow div:first'); 
 
				$active.fadeOut();
				$next.css('display','none').addClass('active').fadeIn(function() {
					$active.removeClass('active');
				});
			}, 5000 );
	});
});
