jQuery.preloadImages = function() {
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

// Funçoes para consertar o bug do zindex no ie7
function isIE() {
    if(navigator.userAgent.match(/MSIE \d\.\d+/))
        return true;
    return false;
}

function validaEmail(email) {
	var reg_email = /^[a-z0-9_.-]+@([a-z0-9_]+\.)+[a-z]{2,4}$/i;
	return reg_email.test(email);
}

function validaCPF(cpf){
	var numeros, digitos, soma, i, resultado, digitos_iguais;
	digitos_iguais = 1;
	if (cpf.length < 11) {
		return false;
	}

	for (i = 0; i < cpf.length - 1; i++) {
		if (cpf.charAt(i) != cpf.charAt(i + 1))
		{
			digitos_iguais = 0;
			break;
		}
	}

	if (!digitos_iguais) {
		numeros = cpf.substring(0,9);
		digitos = cpf.substring(9);
		soma = 0;
		for (i = 10; i > 1; i--)
		soma += numeros.charAt(10 - i) * i;
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;

		if (resultado != digitos.charAt(0)) {
			return false;
		}

		numeros = cpf.substring(0,10);
		soma = 0;

		for (i = 11; i > 1; i--) {
			soma += numeros.charAt(11 - i) * i;
		}

		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(1)) {
			return false;
		}

		return true;
	} else {
		return false;
	}
}

/**************************** DESTAQUE HOME ***********************************/

/**** CALENDARIO ***/

function getEvento(dia, mes, ano, eventos) {
	//alert(dia)
	for (var i=0;i < eventos.length;i++) {
		if ((eventos[i]['dia']==dia) && (eventos[i]['mes']==mes) && (eventos[i]['ano']==ano)) {
			return eventos[i];
		}
	}
	return false;
}

function buildCal(m, y, eventos) {
	var mn=['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'];
	var dim=[31,0,31,30,31,30,31,31,30,31,30,31];
	var oD = new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
	oD.od = oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st
	var todaydate = new Date() //DD added
	var scanfortoday = (y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0
	dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
	var t = mn[m-1] + ' - ' + y;
	t+='<table id="tbl-agenda" cols="7">';
	t+='<tr>';
	for(s=0;s<7;s++)t+='<th>'+"DSTQQSS".substr(s,1)+'</th>';
	t+='</tr><tr>';
	for(var i=1;i<=42;i++) {
		var x=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : '&nbsp;';
		var css = '';
		if (x == scanfortoday) {
			css ='class="today"';
		}
		obj_evento = getEvento(x,m,y,eventos);
		if (obj_evento) {
                    css ='class="on"';
                    id = obj_evento['id'];
                    url = obj_evento['url']
                    titulo = obj_evento['titulo'];
                    x = '<a href="'+url+'" title="'+titulo+'">' + x + '</a>';
		}
		t+='<td '+css+'>'+x+'</td>';
		if(((i)%7==0)&&(i<36))t+='</tr><tr>';
	}
	return t+='</tr></table>';
}
/*********************/


function btClick(nid) {
	$('#layer-texto, #layer-foto, #layer-youtube, #layer-webcam').hide();
	if (nid == 'texto') {
		$("#botoes-envie").css("backgroundPosition","0 -138px");
		$('#layer-texto').show();
	}
	if (nid == 'foto') {
		$("#botoes-envie").css("backgroundPosition","0 -92px");
		$('#layer-foto').show();
	}
	if (nid == 'youtube') {
		$("#botoes-envie").css("backgroundPosition","0 -46px");
		$('#layer-youtube').show();
	}
	if (nid == 'webcam') {
		$("#botoes-envie").css("backgroundPosition","0 0");
		$('#layer-webcam').show();
	}
}

function validaTexto(nform) {
	with (nform) {
		if (texto.value == '') {
			alert('Por favor, preencha o texto.');
			texto.focus();
			return false;
		}
                if (texto.value.length > 500) {
                    alert('Você ultrapassou o limite permitido de 500 caracteres..');
			texto.focus();
			return false;
                }
	}
}

function validaYouTube(nform) {
	with (nform) {
		if (url_youtube.value == '') {
			alert('Por favor, preencha a URL do video no YouTube.');
			url_youtube.focus();
			return false;
		}
	}
}

function validaFoto(nform) {
	with (nform) {
		if (file_photo.value == '') {
			alert('Por favor, selecione sua foto.');
			file_photo.focus();
			return false;
		}
	}
}

function getRandom(inf,sup){
   	n = sup - inf;
   	rnd = Math.floor(Math.random() * n);
   	return parseInt(inf + rnd);
}

function isInArray(search, arr) {
	for (i=0; i<arr.length; i++) {
		if (search == arr[0]) {
			return true;
		}
	}
	return false;
}