String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, '');
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/, '');
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/, '');
}


$(function(){
	$(".produto-thumblist-carousel").carousel({ dispItems:3, animSpeed:"normal", loop:true });
	$(".jui-accordion").accordion({ 'header': "h3", 'autoHeight': false });
	initialMask();
	/*
	$(".form-radio").click(function(){
		alert('');
		var id = $(this).attr('value');
		$(".form-page").removeClass('form-page-selected');
		$("#form-page-"+ id).addClass('form-page-selected');
    });
	*/
	
	$(".produto-detalhe-imagens").fancybox({
		'width'				: '700',
		'height'			: '50%',
		'autoScale'			: true,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
	
	$(".ui-popup").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
	
	
    $(".frmajax").live("submit", function( e ) {
		e.preventDefault();

		var frm = $(this);
		//console.log( frm );
		var _url = frm.attr("action");
		var _param = frm.serialize();

		$.ajax({
			type: "POST",
			url: _url,
			data: _param + "&noCache=" + Math.random(),
			dataType: 'json',
			beforeSend: function(){
			},
			error: function(msg){
				//if( onError != '' ) eval(onError);
				return false;
			},
			success: function(msg){
				eval( msg.result );
				/*
				if( msg.result == true ){
					//alert('ok');
					frm.removeClass('frm_crud');
					frm.submit();
					return true;
				} else {
					showNotify( 'error', msg.result );
				}
				*/
			},
			complete: function(msg){
				//$('#sys-loading').hide();
			}
		});
	});
	
});

function _postForm( _form , onSend, onSuccess, onError ){
	$(document).ready(function(){

		if( typeof onSend == 'undefined' ) onSend = '';
		if( typeof onSuccess == 'undefined' ) onSuccess = '';
		if( typeof onError == 'undefined' ) onError = '';
		
		var f = $('#'+ _form);
		var _param = f.serialize();

		$.ajax({
			type: "POST",
			url: f.attr('action'),
			data: _param + "&noCache=" + Math.random(),
			dataType: 'json',
			beforeSend: function(){
				eval(onSend);
			},
			error: function(msg){
				eval(onError);
			},
			success: function(msg){
				eval( msg.callings );
				//alert( msg );
				//if( onSuccess != '' ) eval(onSuccess);
			}
		});

	});
}

function _post( _url, _param, onSend, onSuccess, onError ){
	$(document).ready(function(){
		if( typeof onSend == 'undefined' ) onSend = '';
		if( typeof onSuccess == 'undefined' ) onSuccess = '';
		if( typeof onError == 'undefined' ) onError = '';
		//alert( param );
		$.ajax({
			type: "POST",
			url: _url,
			data: _param + "&noCache=" + Math.random(),
			dataType: 'json',
			beforeSend: function(){
				if( onSend != '' ) eval(onSend);
			},
			error: function(msg){
				//alert( msg );
				if( onError != '' ) eval(onError);
			},
			success: function(msg){
				eval( msg.callings );
				//alert( msg );
				//if( onSuccess != '' ) eval(onSuccess);
			}
		});
		
	});
}

function initialMask(){
	$(function(){
		/*
		$.mask.masks : {
		'phone'     : { mask : '(99) 9999-9999' },
		'phone-us'  : { mask : '(999) 9999-9999' },
		'cpf'       : { mask : '999.999.999-99' },
		'cnpj'      : { mask : '99.999.999/9999-99' },
		'date'      : { mask : '39/19/9999' }, //uk date
		'date-us'   : { mask : '19/39/9999' },
		'cep'       : { mask : '99999-999' },
		'time'      : { mask : '29:69' },
		'cc'        : { mask : '9999 9999 9999 9999' }, //credit card mask
		'integer'   : { mask : '999.999.999.999', type : 'reverse' },
		'decimal'   : { mask : '99,999.999.999.999', type : 'reverse', defaultValue: '000' },
		'decimal-us'    : { mask : '99.999,999,999,999', type : 'reverse', defaultValue: '000' },
		'signed-decimal'    : { mask : '99,999.999.999.999', type : 'reverse', defaultValue : '+000' },
		'signed-decimal-us' : { mask : '99,999.999.999.999', type : 'reverse', defaultValue : '+000' }
		}
		*/
		
		if( $(document).setMask ){
			$("input.mask-fone").setMask("(99) 9999-9999");
			$("input.mask-data").setMask("39/19/9999");
			$("input.mask-cpf").setMask("999.999.999-99");
			$("input.mask-cep").setMask("99999-999");
			$("input.mask-hora").setMask("29:69");
			$("input.mask-cnpj").setMask("99.999.999/9999-99");
			$("input.mask-inscricao").setMask("99.999.999-9");

			$("input.mask-decimal").setMask({ mask : '999.999.999.999,99', type : 'reverse', defaultValue : '+000' });

			$("input.mask-inteiro").setMask("99999999");
		}

	})
}

function onFormRadioPageClick( el ){
	$(function(){
		//alert('');
		var id = $(el).attr('value');
		$(".form-page").removeClass('form-page-selected');
		$("#form-page-"+ id).addClass('form-page-selected');
	})
}

function initialFormBusca( el ){
	$(function(){
		var msg = 'Buscar em todo o site';
		var input = "#"+ el +" input[type='text']";
		$(input).attr('value', msg);

		$( input ).focus(function(){
			if( $(input).attr('value') == msg ) $(input).attr('value', '');
		});
		$( input ).blur(function(){
			if( $(input).attr('value') == '' ) $(input).attr('value', msg);
		});
		
		$("#"+ el).submit(function(){
			var t = $(input).attr('value').trim();
			if( t != '' || t != msg ){
				window.location = base_url + 'buscar/1/'+ t +'/';
			}
			return false;
		});
	})
}

function ajax_calculaFrete( cepDestino ){
	$(document).ready(function(){
		$("#produto-detalhe-frete .msg").hide();
		
		//var cepDestino = $('#cepFrete').attr('value').replace("-", "");
		$.ajax({
			type: "POST",
			url: base_url +"calculaFrete/",
			data: "&cepDestino="+ cepDestino,
			dataType: 'json',
			beforeSend: function(){
				//if( onSend != '' ) eval(onSend);
			},
			error: function(result){
				//alert( 'error: '+ msg.responseText );
			},
			success: function(result){
				if( result.error == '' ){
					
					$(".frete-msg").show('fast');
					$(".frete-msg").html( result.html );
					eval( result.callings );
					
				} else {
					//alert('er: '+ msg);
				}
			}
		});
	});
}

function ajax_carrinhoFrete( cepDestino ){
	$(document).ready(function(){
		$("#produto-detalhe-frete .msg").hide();
		
		//var cepDestino = $('#cepFrete').attr('value').replace("-", "");
		$.ajax({
			type: "POST",
			url: base_url +"calculaFrete/",
			data: "&cepDestino="+ cepDestino,
			dataType: 'json',
			beforeSend: function(){
				//if( onSend != '' ) eval(onSend);
			},
			error: function(result){
				//alert( 'error: '+ msg.responseText );
			},
			success: function(result){
				if( result.error == '' ){
					
					$(".frete-msg").show('fast');
					$(".frete-msg").html( result.html );
					$("#frete-valor").html( result.frete_valor );
					$("#pedido-valor").html( result.pedido_total );
					eval( result.callings );
					
				} else {
					alert(result.error);
				}
			}
		});
	});
}

function ajax_produtoListaOptions( opt, v ){
	$(document).ready(function(){

		$.ajax({
			type: "POST",
			url: base_url +"produtoListOptions/",
			data: "opt="+ opt +"&v="+ v,
			dataType: 'json',
			beforeSend: function(){
				//if( onSend != '' ) eval(onSend);
			},
			error: function(result){
				//alert( 'error: '+ msg.responseText );
			},
			success: function(result){
				if( result.error == '' ){

					eval( result.callings );
					
				} else {
					//alert('er: '+ msg);
				}
			}
		});
	});
}

function carrinhoMudarQnt( produto, qnt ){
	$(document).ready(function(){

		$.ajax({
			type: "POST",
			url: base_url + "carrinho/mudarQnt",
			data: "produto="+ produto +"&qnt="+ qnt,
			dataType: 'json',
			beforeSend: function(){
				//if( onSend != '' ) eval(onSend);
			},
			error: function(result){
				//alert( 'error: '+ msg.responseText );
			},
			success: function(result){
				if( result.error == '' ){

					eval( result.callings );
					
				} else {
					alert( result.error );
				}
			}
		});
	});
}

function ajax_searchCEP( cep ){
	$(document).ready(function(){
		cep = cep.replace('-', '');

		if( cep.length == 8 ){
			$.ajax({
				type: "GET",
				url: "http://republicavirtual.com.br/web_cep.php?formato=javascript&cep="+ cep,
				dataType: 'script',
				beforeSend: function(){
				},
				error: function(){
				},
				success: function(msg){
					$('#endereco').val( unescape(resultadoCEP.logradouro) );
					$('#bairro').val( unescape(resultadoCEP.bairro) );
					$('#cidade').val( unescape(resultadoCEP.cidade) );
					$('#estado').val( unescape(resultadoCEP.uf) );
					//$("#estado option[value='"+ resultadoCEP.estado +"']").attr('selected', 'selected');
					//$('#setEstado').val(resultadoCEP.estado);
				}
			});
		} //else alert('CEP está com formato inválido.');

	});
}
