
var linha;
var linhaFormato;
var idFormato;
var qtdeFormato;

$(document).ready(function(){	
	
	var altura = document.body.offsetHeight;
	
	$('#neblinaAlerta').css('height',altura);
	$('#neblinaAlerta').click(function(){
		$('#neblinaAlerta').hide();
		$('#popup-box').hide();
	});
	
	$('#cep').mask('99999-999');
});



// Example:

// var b = new BrowserInfo();

// alert(b.version); 

function BrowserInfo()

{

  this.name = navigator.appName;

  this.codename = navigator.appCodeName;

  this.version = navigator.appVersion.substring(0,4);

  this.platform = navigator.platform;

  this.javaEnabled = navigator.javaEnabled();

  this.screenWidth = screen.width;

  this.screenHeight = screen.height;

}



function exibrPossibilidadesFrete(form)
{
	$.ajax({
	   type: "GET",
	   url:  esporte + '/carrinho/exibir-fretes/cep/' + form.cep.value,
	   data: '',
	   success: function(retorno) {
		   if(navigator.appName == "Microsoft Internet Explorer")
		   		document.getElementById('fretes').innerHTML = retorno;
	       else 
				$("#fretes").html(retorno);
	   }
	});
	
	return false;
}



function calcularFrete(tipoFrete)
{
	var form = document.getElementById('form_sedex');
	var cep  = form.cep_frete.value;
	var tipo = tipoFrete;

	$.ajax({
	   type: "GET",
	   url:  esporte + '/carrinho/calcular-frete/cep/' + cep + '/tipo-frete/' + tipo,
	   data: '',
	   success: function(retorno) {
		   	j = eval("(" + retorno + ")");
			
			$("#valorFrete").html('R$ ' + j.valorFrete );
	    	$("#valorPedido").html('R$ ' + j.valorPedido );					
	   }
	});
	
	return false;
}




function calcularCupom()
{
	var form   = document.getElementById('form_cupom');
	var codigo = form.codigo.value;

	$.ajax({
	   type: "GET",
	   url:  esporte + '/carrinho/calcular-cupom/codigo/' + codigo,
	   data: '',
	   success: function(retorno) {
		   	j = eval("(" + retorno + ")");
			$("#valorCupom").html('R$ ' + j.valorCupom);
	    	$("#valorPedido").html('R$ ' + j.valorPedido);					
	   }
	});
	
	return false;
}




function adicionar(obj)
{
	$.ajax({
	   type: "POST",
	   url: obj.href,
	   data: "",
	   success: function(retorno){
			$("#fretes").html(retorno);
	   }
	});

	return false;
}




/**  
 * @method retornoAjax(retorno)
 * @param  string url
 * @param  string method
 * @param  string idLinha
 * @return void
 * @desc   função customizada   
 */
function retornoAjax(retorno)
{
	if(linha != '')
	{
		$("#" + linha + "1").hide();
		$("#" + linha + "2").hide();
		linha = '';
	}
	
	if(linhaFormato != '')
	{
		$("#" + linhaFormato).hide();
		linhaFormato = '';
	}

	
	$("#valorTotal").html(retorno);
}
	


/**  
 * @method apagarLinha(msg, url, idLinha)
 * @param  string url
 * @param  string method
 * @param  string idLinha
 * @return void
 * @desc   função customizada   
 */
function apagarLinha(msg, url, idLinha)
{
	if(confirmar(msg)) {
		linha = idLinha;
		requestAjax(url, 'GET');
	}
		
	return false;
}


/**  
 * @method requestAjaxFormato(url, method, idFoto)
 * @param  string url
 * @param  string method
 * @return void
 * @desc   função customizada   
 */
function requestAjaxFormato(url, method, idFoto)
{
	$.ajax({
		type: method,
	   	url:  url,
	   	data: '',
	   	success: function(retorno) {		 	   
			retorno = retorno.replace(/^\s+|\s+$/g,"");
			$("#valorFormatos" + idFoto).html(retorno);
		}
	});
}	






/**  
 * @method requestAjaxFormatoApagar(url, method, idFoto)
 * @param  string url
 * @param  string method
 * @return void
 * @desc   função customizada   
 */
function requestAjaxFormatoApagar(url)
{
	$.ajax({
		type: 'GET',
	   	url:  url,
	   	data: '',
	   	success: function(retorno) {		 	   
			j = eval("(" + retorno + ")");
			
			if(j.totalFormatos == '0,00')
			{
				var sURL = unescape(window.location.pathname);
				window.location.replace( sURL );
			}
			
			$("#totalItens").html('R$ ' + j.totalFormatos);
			$("#valorFrete").html('R$ ' +  moeda.formatar(j.valorFrete));
			$("#valorPedido").html('R$ ' + j.valorPedido);
			
			if(j.valorFrete == '-1') {
				$('#tabelaCarrinho').hide('normal');
			}
		}
	});	
}



function apagarLinhaFormato(msg, url, idLinha, idFoto)
{
	if(confirmar(msg)) 
	{
		linhaFormato = 'linha' + idLinha;
		requestAjaxFormatoApagar(url);
		$('#linhaFormato_' + idLinha).hide();
	}
		
	return false;
}
		
		
		
		

/**  
 * @method requestAjaxCalculo(url, method, idFoto)
 * @param  string url
 * @param  string method
 * @return void
 * @desc   função customizada   
 */
function requestAjaxCalculo(url, idFoto)
{
	$.ajax({
		type: 'GET',
	   	url:  url,
	   	data: '',
	   	success: function(retorno) {		 	   
			retorno = retorno.replace(/^\s+|\s+$/g,"");
			//$("#debug").html(retorno);
			$("#valorFormatos" + idFoto).html(retorno);
		}
	});
}	


/**
 * moeda
 * 
 * @abstract Classe que formata de desformata valores monetários em float e formata valores 
 * de float em moeda.
 * 
 * @author anselmo
 *
 * @email anselmobattisti arroba gmail.com
 * 
 * @site battisti.etc.br
 *
 * @example 
 * 		moeda.formatar(1000) 
 *      	>> retornar 1.000,00
 *
 * 		moeda.desformatar(1.000,00) 
 * 		>> retornar 1000
 * 
 * @version 1.0
 **/
 var moeda = {
 	
	/**
	 * retiraFormatacao
	 * 
	 * Remove a formatação de uma string de moeda e retorna um float
	 * 
	 * @param {Object} num
	 */
	 desformatar: function(num){
	   num = num.replace(".","");
	
	   num = num.replace(",",".");
	
	   return parseFloat(num);
	},

	/**
	 * formatar
	 * 
	 * Deixar um valor float no formato monetário
	 * 
	 * @param {Object} num
	 */
	formatar: function(num){
	   x = 0;
	
	   if(num<0){
	      num = Math.abs(num);
	      x = 1;
	   }
	
	   if(isNaN(num)) num = "0";
	      cents = Math.floor((num*100+0.5)%100);

	   num = Math.floor((num*100+0.5)/100).toString();
	
	   if(cents < 10) cents = "0" + cents;
	      for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	         num = num.substring(0,num.length-(4*i+3))+'.'
	               +num.substring(num.length-(4*i+3));
	
	   ret = num + ',' + cents;
	
	   if (x == 1) ret = ' - ' + ret;return ret;
	},
	
	/**
	 * arredondar
	 * 
	 * @abstract Arredonda um valor quebrado para duas casas decimais.
	 * 
	 * @param {Object} num
	 */
	arredondar: function(num){
		return Math.round(num*Math.pow(10,2))/Math.pow(10,2);
	}
 }


/**  
 * @method requestAjaxTotalPedido()
 * @param  void
 * @return void
 * @desc   função customizada   
 */
function requestAjaxTotalPedido()
{
	$.ajax({
		type: 'GET',
	   	url:  esporte + '/carrinho/valor-pedido',
	   	data: '',
	   	success: function(retorno) {		 	   
			j = eval("(" + retorno + ")");
			$("#valorPedido").html('R$ ' + j.valorPedido);
		}
	});
}	


/**  
 * @method requestAjaxCalculoFormato(url, method, idFoto)
 * @param  string url
 * @param  string method
 * @return void
 * @desc   função customizada   
 */
function requestAjaxCalculoFormato(url, idFormato)
{
	$.ajax({
		type: 'GET',
	   	url:  url,
	   	data: '',
	   	success: function(retorno) {		 	   
			j = eval("(" + retorno + ")");
			$("#" + j.IdTotalFormato).html('R$ ' + j.totalFormato);
			$("#totalItens").html('R$ ' + j.totalFormatos);
			requestAjaxTotalPedido();
		}
	});
}	


function mudarQtdeFormato(idFormato, idFoto, dadosFormato)
{
	idFormato   = idFormato;
	qtdeFormato = $("#" + idFormato).val();
	requestAjaxCalculoFormato(esporte + '/carrinho/recalcular-formatos/pars/' + idFormato + '/qtde/' + qtdeFormato, idFoto);
	
	return false;
}
