$(document).ready(function(){
	$('a[rel=external]').attr('target','_blank');
	var t1 = new Twitter("FabiolaVeloso", true, 60, 4, "twitter", 4);
});

function remove(str, strToRemove) {
   i = str.indexOf(strToRemove);
   r = "";
   if (i == -1) return str;
   r += str.substring(0,i) + remove(str.substring(i + strToRemove.length), strToRemove);
   return r;
}

function validateEmail(email) {
	var ret = false;
	var em = new String();
	em = String(email);
	var RegExPattern = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{ 1,3}){3}\])$/; 
	if ( em.match(RegExPattern) ) {
		ret = true;
	}
	return ret;
}

function trim(str){
	if (typeof(str) == "string") {
		return str.replace(/^\s+|\s+$/g,"");
	} else {
		return str;
	}	
}

function maskFormat(mask,obj){
	if(obj.value != ""){
		sObj = obj.value.toString();
		nString = "";
		for(i=0;i<(sObj.length);i++){
			if(!isNaN(sObj.charAt(i)) && (sObj.charAt(i) != " ")) {
				nString = nString + sObj.charAt(i);
			}
		}
		j = 0;
		k = 0;
		mString = "";
		while(j < nString.length && k < mask.length){
			if(mask.charAt(k).toUpperCase() == "N"){
				mString = mString + nString.charAt(j);
				j = j+1;
			} else {
				mString = mString + mask.charAt(k);
			}
			k = k+1;
		}
		obj.value = mString;
	}
}

function validarCPF(cpf){
	var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
	if(!filtro.test(cpf)){
		return false;
	}
	cpf = remove(cpf, ".");
	cpf = remove(cpf, "-");
	if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
		cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
		cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
		cpf == "88888888888" || cpf == "99999999999"){
			return false;
	}
	soma = 0;
	for(i = 0; i < 9; i++)
		soma += parseInt(cpf.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
		resto = 0;
	if(resto != parseInt(cpf.charAt(9))){
		return false;
	}
	soma = 0;
	for(i = 0; i < 10; i ++)
		soma += parseInt(cpf.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
		resto = 0;
	if(resto != parseInt(cpf.charAt(10))){
		return false;
	}
	return true;
}

function entraNoEdit(obj, txt) {
	if ($.trim(obj.value) == txt) {
		obj.value =	"";
	}
}
function saiDoEdit(obj,	txt) {
	if ($.trim(obj.value) == "") {
		obj.value =	txt;
	}
}

function getEndereco(cep) {
	var arrCep = new Array();
	var cepLimpo = $.trim(cep);
	cepLimpo = cepLimpo.replace("-","");
	if(cepLimpo != ""){
		$.getScript("http://cep.republicavirtual.com.br/web_cep.php?formato=javascript&cep=" + cepLimpo, function(){
			if(resultadoCEP["resultado"]){
				var txtLogradouro = ($.trim(unescape(resultadoCEP["tipo_logradouro"]) + " " + unescape(resultadoCEP["logradouro"])));
				var txtBairro = (unescape(resultadoCEP["bairro"]));
				var txtCidade = (unescape(resultadoCEP["cidade"]));
				var slEstado = (unescape(resultadoCEP["uf"]));
				arrCep.push(txtLogradouro);
				arrCep.push(txtBairro);
				arrCep.push(txtCidade);
				arrCep.push(slEstado);
			}
		});				
	}
	return arrCep;
}

/* twitter*/

/*
1 - From a user
2 - Mentioning a user
3 - Replying to a user
4 - Containing a word
5 - Containing a hashtag
*/
var _key; 
var _atualize; 
var _timeout;
var _tammax;
var _nohRetorno;
var _option;

function Twitter(key, atualize, timeout, tammax, nohRetorno, option) {
	_key = key;
	_atualize = atualize;
	_timeout = timeout;
	_tammax = tammax;
	_nohRetorno = nohRetorno;
	_option = option;
	execute();
}

function execute() {
	if (this._atualize) {
		setInterval('_updateTwitts();', _timeout * 1000);
	}
	_updateTwitts();
}

function _updateTwitts() {
	var s = "<scr"+"ipt src=\"http://search.twitter.com/search.json?callback=_up"+_option+"&";
	switch(_option)
	{
		case 1:
			s += "q=from:"+ _key;
			break;
		case 2:
			s += "q=%40"+ _key;
			break;
		case 3:
			s += "q=to:"+ _key;
			break;
		case 4:
			s += "q="+ _key;
			break;
		case 5:
			s += "q=%23"+ _key;
			break;
		default:
	} 
	s += "&c="+Math.random()+"\"></scr"+"ipt>";
	$("head").append(s);
}

function _up1(d){
	var t = "";
	if (d.results) {
		var tamlst = d.results.length;
		var max = _tammax;
		if (tamlst < max) {
			max = tamlst;
		}
		for (var ix=0;ix<max;ix++) {
			t += "<li>";
			t += "<div>";
			t += "<a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\"><img src=\""+d.results[ix]['profile_image_url']+"\" /></a> ";
			t += "<strong><a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\">@"+d.results[ix]['from_user']+"</a></strong> ";
			t += _getLink(d.results[ix]['text']);
			t += "<span>"+_formatDateTw(d.results[ix]['created_at'])+"</span>";
			t += "</div>";
			t += "</li>";
		}
	}
	$("#"+_nohRetorno).html(t);
	$('a[rel=external]').attr('target','_blank');
}

function _up2(d){
	var t = "";
	if (d.results) {
		var tamlst = d.results.length;
		var max = _tammax;
		if (tamlst < max) {
			max = tamlst;
		}
		for (var ix=0;ix<max;ix++) {
			t += "<li>";
			t += "<div>";
			t += "<a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\"><img src=\""+d.results[ix]['profile_image_url']+"\" /></a> ";
			t += "<strong><a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\">@"+d.results[ix]['from_user']+"</a></strong> ";
			t += _getLink(d.results[ix]['text']);
			t += "<span>"+_formatDateTw(d.results[ix]['created_at'])+"</span>";
			t += "</div>";
			t += "</li>";
		}
	}
	$("#"+_nohRetorno).html(t);
	$('a[rel=external]').attr('target','_blank');
}

function _up3(d){
	var t = "";
	if (d.results) {
		var tamlst = d.results.length;
		var max = _tammax;
		if (tamlst < max) {
			max = tamlst;
		}
		for (var ix=0;ix<max;ix++) {
			t += "<li>";
			t += "<div>";
			t += "<a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\"><img src=\""+d.results[ix]['profile_image_url']+"\" /></a> ";
			t += "<strong><a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\">@"+d.results[ix]['from_user']+"</a></strong> ";
			t += _getLink(d.results[ix]['text']);
			t += "<span>"+_formatDateTw(d.results[ix]['created_at'])+"</span>";
			t += "</div>";
			t += "</li>";
		}
	}
	$("#"+_nohRetorno).html(t);
	$('a[rel=external]').attr('target','_blank');
}

function _up4(d){
	var t = "";
	if (d.results) {
		var tamlst = d.results.length;
		var max = _tammax;
		if (tamlst < max) {
			max = tamlst;
		}
		for (var ix=0;ix<max;ix++) {
			t += "<li class=\"item"+ix+"\">";
			t += "<strong><a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\">@"+d.results[ix]['from_user']+"</a></strong> ";
			t += _getLink(d.results[ix]['text']);
			t += "<br/>";
			//t += "<span>"+_formatDateTw(d.results[ix]['created_at'])+"</span>";
			t += "</li>";
		}
	}
	$("#"+_nohRetorno).html(t);
	$('a[rel=external]').attr('target','_blank');
}

function _up5(d){
	var t = "";
	if (d.results) {
		var tamlst = d.results.length;
		var max = _tammax;
		if (tamlst < max) {
			max = tamlst;
		}
		for (var ix=0;ix<max;ix++) {
			t += "<li class=\""+ix+"\">";
			t += "<a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\"><img src=\""+d.results[ix]['profile_image_url']+"\" /></a> ";
			t += "<strong><a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\">@"+d.results[ix]['from_user']+"</a></strong> ";
			t += _getLink(d.results[ix]['text']);
			t += "<span>"+_formatDateTw(d.results[ix]['created_at'])+"</span>";
			t += "</li>";
		}
	}
	$("#"+_nohRetorno).html(t);
	$('a[rel=external]').attr('target','_blank');
}

function _formatDateTw(time){
	var timeAtual = (new Date()).getTime();
	var date = new Date(time);
	var timeTw = date.getTime();
	var diff = ((timeAtual - timeTw) / 1000);
	var day_diff = Math.floor(diff / 86400);

	if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
		return;
			
	return day_diff == 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}

function _getLink(str) {
	var strFinal = "";
    var regExpUrl = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
    var regExpTwt = /[\@]+([A-Za-z0-9-_]+)/gi;
    var regExpTag = /(?:^| )[\#]+([A-Za-z0-9-_]+)/gi;
	var arrStr = str.split(' ');
	for (var i=0;i<arrStr.length;i++) {
		var parte = arrStr[i];
		parte = parte.replace(regExpUrl,"<a href=\"$1\" rel=\"external\">$1</a>");
		parte = parte.replace(regExpTwt,"<a href=\"http://twitter.com/$1\" rel=\"external\">@$1</a>");
		parte = parte.replace(regExpTag,"<a href=\"http://search.twitter.com/search?q=&tag=$1\" rel=\"external\">#$1</a>");
		strFinal += parte + " ";
    }
	return strFinal;
}
/**/

