var clonarConteudo = null;

function ClonarConteudo(container,e,extra) {
	this.init(container,e,extra);
}

ClonarConteudo.prototype = {
	init: function(container,e,extra) {
		var me = this;
		this.target = e.target;
		this.container = $(container);
		this.extra = extra;
		this.children = this.container.children();
		this.indice = 0;
	},

	atualizaIndice: function(val) {
		val = parseInt(val);
		var qntd = $(this.target).parents('.input').find('input').val();
		if(val!=0) $(this.target).parents('.input').find('input').val(parseInt(qntd)+val);
		this.indice = qntd;
	},

	add: function() {
		var me = this;
		this.atualizaIndice(1);
		$clone = this.children.filter('tr:lt(3)').clone();
		$clone.find('select').each(function() {
			var nome = $(this).attr('name');
			$(this).attr('name',nome.replace(/\[(\d)\]/,'['+me.indice+']')).val('');
		});
		$clone.find('input').each(function() {
			var nome = $(this).attr('name');
			$(this).attr('name',nome.replace(/\[(\d)\]/,'['+me.indice+']'));
			if($(this).is('input[type=text]')){
				$(this).val('');
			} else if($(this).is('input[type=radio]') || $(this).is('input[type=checkbox]')){
				$(this).attr('checked', false);
			}

			if($(this).is('.mask_cpf')){
				$(this).mask('999.999.999-99').keydown(maskCPF).keyup(maskCPF).each(function () {
					maskCPF({target: this});
				});
			} else if($(this).is('.mask_date')){
				$(this).mask('99/99/9999');
			}

		});

		this.container.append(this.extra);
		$clone.appendTo(this.container);
	},

	rem: function() {
		this.atualizaIndice(0);
		console.warn(this.indice);
		console.log((((parseInt(this.indice)-1)*3)-3));
		if (this.children.filter('tr.clonable').length > 3) {
			this.container.find('.linha02:last').parents('tr').remove();
			this.children.filter('tr.clonable:gt('+(((parseInt(this.indice)-1)*3)-1)+')').remove();
			this.atualizaIndice(-1);
		}
	}
};

(function($){
	$(function (){

		$('input[type="text"]').resetDefaultValue();
		$('.infield').inFieldLabels();
		$('.fancy_foto').fancybox();
		$('.fancy_noticias').fancybox(
			{
				autoDimensions:false,
				width:380,
				height:338,
				type:'iframe',
				padding:20
			}
		);
		$(".offprime").fancybox({autoScale:false});

	});
})(jQuery);



