//
// Classe de listas de fotolog
function ListaFotolog(tipo, pais, res, htmlModel, htmlDest){
	res=res || 10;
	this.tipo=tipo;
	this.htmlModel=htmlModel;
	this.htmlDest=htmlDest;
	this.onReceiveData=function(){}
	if(!this.url[tipo]){
		return false;
	}
	this.ajUrl=this.url[tipo];
	this.ajUrl=this.ajUrl.replace("[:pais:]", PaisesListas["_"+pais]);
	this.ajUrl+="nitems="+res;
}
ListaFotolog.prototype.load=function(){
	var aj=new FlashRequest();
	aj.dest=this;
	aj.onData=function(x, t){
		this.dest.lista=new Lista("flog", x, this.dest.nos[this.dest.tipo]);
		this.dest.items=this.dest.lista.items;
		this.dest.onReceiveData();
	}
	aj.send(this.ajUrl);
}
ListaFotolog.prototype.exibir=function(){
	this.lista.exibir(this.htmlModel, this.htmlDest);
}
//
// Classe de listas de videolog
function ListaVideolog(tipo, res, antig, dom, cache, htmlModel, htmlDest){
	res=res || 10;
	antig=antig || 0;
	dom=dom || false;
	cache=cache || false;
	this.tipo=tipo;
	this.htmlModel=htmlModel;
	this.htmlDest=htmlDest;
	this.onReceiveData=function(){}
	if(!this.url[tipo]){
		return false;
	}
	this.ajUrl=this.url[tipo];
	this.ajUrl+="&nitems="+res;
	this.ajUrl+="&antiguidade="+antig;
	this.ajUrl+=(dom)? "&dominio="+dom : "";
	this.ajUrl+=(cache)? "&cache="+cache : "";
	
}
ListaVideolog.prototype.load=function(){
	var aj=new FlashRequest();
	aj.dest=this;
	aj.onData=function(x, t){
		try{
			this.dest.lista=new Lista("vlog", x, this.dest.nos);
			this.dest.items=this.dest.lista.items;
			this.dest.onReceiveData();
		}catch(e){}
	}
	aj.send(this.ajUrl);
}
ListaVideolog.prototype.exibir=function(){
	this.lista.exibir(this.htmlModel, this.htmlDest);
}
//
// Classe de listas de Espaço Terra
function ListaEspaco(tipo, res, htmlModel, htmlDest){
	res=res || 10;
	this.tipo=tipo;
	this.htmlModel=htmlModel;
	this.htmlDest=htmlDest;
	this.onReceiveData=function(){}
	if(!this.url[tipo]){
		return false;
	}
	this.ajUrl=this.url[tipo];
	this.ajUrl+="nitems="+res;
	
}
ListaEspaco.prototype.load=function(){
	var aj=new FlashRequest();
	aj.dest=this;
	aj.onData=function(x, t){
		try{
			this.dest.lista=new Lista("espaco", x, this.dest.nos[this.dest.tipo]);
			this.dest.items=this.dest.lista.items;
			this.dest.onReceiveData();
		}catch(e){ }
	}
	aj.send(this.ajUrl);
}
ListaEspaco.prototype.exibir=function(){
	this.lista.exibir(this.htmlModel, this.htmlDest);
}
//
// Classe de listas de Blog
function ListaBlog(tipo, pais, res, htmlModel, htmlDest){
	this.tipo=tipo;
	this.htmlModel=htmlModel;
	this.htmlDest=htmlDest;
	this.maxResults=res || 1024;
	this.onReceiveData=function(){}
	if(!this.url[tipo]){
		return false;
	}
	this.ajUrl=this.url[tipo];
	this.ajUrl=this.ajUrl.replace("[:pais:]", PaisesBlogs["_"+pais][0]).replace("[:paisCod:]", PaisesBlogs["_"+pais][1]);
	
}
ListaBlog.prototype.load=function(){
	var aj=new FlashRequest();
	aj.dest=this;
	aj.onData=function(x, t){
		try{
			this.dest.lista=new Lista("blog", x, this.dest.nos[this.dest.tipo], this.dest.maxResults);
			this.dest.items=this.dest.lista.items;
			this.dest.onReceiveData();
		}catch(e){}
	}
	aj.send(this.ajUrl);
}
ListaBlog.prototype.exibir=function(){
	this.lista.exibir(this.htmlModel, this.htmlDest);
}
//
// classe de Listas
function Lista(tipo, objXml, nos, limite){
	limite=limite||1024;
	this.tipo=tipo;
	this.items=[];
	var nos=nos.replace(/ /g, "").split(",");
	var noPrincipal=objXml.getElementsByTagName(nos[0])[0];
	var nosSecundarios=noPrincipal.getElementsByTagName(nos[1]);
	for(var i=0; i<nosSecundarios.length && i<limite; i++){
		this.items.push({});
		var filhos=nosSecundarios[i].childNodes;
		for(var t=0; t<filhos.length; t++){
			if(filhos[t].nodeType==1){
				if(filhos[t].firstChild){
					this.items[i][filhos[t].nodeName]=unescape(filhos[t].firstChild.nodeValue);
				}else{
					this.items[i][filhos[t].nodeName]="";
				}
			}
			this.items[i]._lista=this;
		}
	}
}
Lista.prototype.exibir=function(orig, dest){
	if(!orig || !dest){
		return;
	}
	var box=document.getElementById(dest);
	box.innerHTML="";
	for(var i=0; i<this.items.length; i++){
		var cod=document.getElementById(orig).innerHTML;
		cod=unescape(cod);
		var item=this.items[i];
		for(var ti in item){
			var re=new RegExp("\\[\\:"+ti+"\\:\\]", "g");
			cod=cod.replace(re, item[ti]);
		}
		box.innerHTML+=cod;
	}
}
function ListaConjunta(res, sort, models){
	this.items=[];
	sort=sort.split(/ *, */);
	this.sort=sort[0];
	this.sortReverse=sort[1]=="reverse";
	this.models=models;
	this.onReceiveData=function(){}
	this.listas=0;
	this.totListas=arguments.length-3;
	this.maxItems=res*this.totListas;
	this.args=[];
	for(var i=3; i<arguments.length; i++){
		this.args.push(arguments[i]);
	}
}
ListaConjunta.prototype.load=function(){
	for(var i=0; i<this.args.length; i++){
		var l=this.args[i].split(/ *, */);
		var str="var c=new "+this.args[i];
		eval(str);
		c.destLista=this;
		c.onReceiveData=function(){
			this.destLista.adicionar(this.lista);
		}
		c.load();
	}
}
ListaConjunta.prototype.adicionar=function(lista){
	this.listas++;
	this.items=this.items.concat(lista.items);
	if(this.listas==this.totListas){
		if(this.sort=="random"){
			this.items.shuffle();
		}else{
			this.items.sortOn(this.sort);
		}
		if(this.sortReverse){
			this.items.reverse();
		}
		this.onReceiveData();
	}
}
ListaConjunta.prototype.exibir=function(){
	var box=document.getElementById(this.models.dest);
	box.innerHTML="";
	for(var i=0; i<this.items.length && i<this.maxItems; i++){
		var item=this.items[i];
		var tipo=item._lista.tipo;
		if(this.models[tipo]){
			var cod=document.getElementById(this.models[tipo]).innerHTML;
			cod=unescape(cod);
			for(var ti in item){
				var re=new RegExp("\\[\\:"+ti+"\\:\\]", "g");
				cod=cod.replace(re, item[ti]);
			}
			box.innerHTML+=cod;
		}
	}
}
//
// configurações gerais
PaisesListas={};
PaisesListas._ar="terra.com.ar";
PaisesListas._br="terra.com.br";
PaisesListas._cl="terra.cl";
PaisesListas._co="terra.com.co";
PaisesListas._ec="terra.com.ec";
PaisesListas._mx="terra.com.mx";
PaisesListas._pe="terra.com.pe";
PaisesListas._us="terra.com";
PaisesListas._uy="terra.com.uy";
PaisesListas._ve="terra.com.ve";
PaisesListas._cr="terra.com.cr";
PaisesListas._do="terra.com.do";
PaisesListas._gt="terra.com.gt";
PaisesListas._ve="terra.com.ve";
PaisesListas._ni="terra.com.ni";
PaisesListas._pa="terra.com.pa";
PaisesListas._sv="terra.com.sv";
PaisesBlogs={};
PaisesBlogs._ar=["terra_com_ar", "terra_ar"];
PaisesBlogs._br=["terra_com_br", "terra_br"];
PaisesBlogs._cl=["terra_cl", "terra_cl"];
PaisesBlogs._co=["terra_com_co", "terra_co"];
PaisesBlogs._ec=["terra_com_ec", "terra_ec"];
PaisesBlogs._mx=["terra_com_mx", "terra_mx"];
PaisesBlogs._pe=["terra_com_pe", "terra_pe"];
PaisesBlogs._us=["terra_com", "terra_us"];
PaisesBlogs._uy=["terra_com_uy", "terra_uy"];
PaisesBlogs._ve=["terra_com_ve", "terra_ve"];
PaisesBlogs._cr=["terra_com_cr", "terra_cr"];
PaisesBlogs._do=["terra_com_do", "terra_do"];
PaisesBlogs._gt=["terra_com_gt", "terra_gt"];
PaisesBlogs._ve=["terra_com_ve", "terra_ve"];
PaisesBlogs._ni=["terra_com_ni", "terra_ni"];
PaisesBlogs._pa=["terra_com_pa", "terra_pa"];
PaisesBlogs._sv=["terra_com_sv", "terra_sv"];

//
// configurações de fotolog
// - nos do xml
ListaFotolog.prototype.nos={};
ListaFotolog.prototype.nos.flogsDestacados = ListaFotolog.prototype.nos.flogsAcessados = ListaFotolog.prototype.nos.flogsPopulares = ListaFotolog.prototype.nos.flogsNovos = ListaFotolog.prototype.nos.flogsAtualizados="profile_list, profile";
ListaFotolog.prototype.nos.postsRecentes = ListaFotolog.prototype.nos.postsComentados = ListaFotolog.prototype.nos.postsVistos = ListaFotolog.prototype.nos.postsVotados="post_list, post";
// - urls
ListaFotolog.prototype.url={};
ListaFotolog.prototype.url.flogsDestacados="http://fotolog.[:pais:]/ws/flog:getFlogs/";
ListaFotolog.prototype.url.flogsAcessados="http://fotolog.[:pais:]/ws/flog:getMostVisitedFlogs/"; //errado
ListaFotolog.prototype.url.flogsPopulares="http://fotolog.[:pais:]/ws/flog:getMostPopularFlogs/";
ListaFotolog.prototype.url.flogsNovos="http://fotolog.[:pais:]/ws/flog:getNewFlogs/";
ListaFotolog.prototype.url.flogsAtualizados="http://fotolog.[:pais:]/ws/flog:getLastModifiedFlogs/";
ListaFotolog.prototype.url.postsRecentes="http://fotolog.[:pais:]/ws/flog:getLastPosts/";
ListaFotolog.prototype.url.postsComentados="http://fotolog.[:pais:]/ws/flog:getMostCommentedPosts/";
ListaFotolog.prototype.url.postsVistos="http://fotolog.[:pais:]/ws/flog:getMostAccessedPosts/";
ListaFotolog.prototype.url.postsVotados="http://fotolog.[:pais:]/ws/flog:getMostVotedPosts/";
//
// configurações de videolog
// - nos do xml
ListaVideolog.prototype.nos="video_list, video";
// - urls
ListaVideolog.prototype.url={}
ListaVideolog.prototype.url.assistidos="http://videos.terra.com/templates/videologXML.aspx?type=most_viewed";
ListaVideolog.prototype.url.comentados="http://videos.terra.com/templates/videologXML.aspx?type=most_commented";
ListaVideolog.prototype.url.destacados="http://videos.terra.com/templates/videologXML.aspx?type=listid&code=7";
ListaVideolog.prototype.url.recentes="http://videos.terra.com/templates/videologXML.aspx?type=most_recent";
ListaVideolog.prototype.url.votados="http://videos.terra.com/templates/videologXML.aspx?type=most_voted_alltimes";
ListaVideolog.prototype.url.votadosHoje="http://videos.terra.com/templates/videologXML.aspx?type=most_voted_today";
ListaVideolog.prototype.url.votadosSemana="http://videos.terra.com/templates/videologXML.aspx?type=most_voted_week";
ListaVideolog.prototype.url.votadosMes="http://videos.terra.com/templates/videologXML.aspx?type=most_voted_month";
//
// configurações de Espaço Terra
// - nos do xml
ListaEspaco.prototype.nos={};
ListaEspaco.prototype.nos.comunidadesDestaque = ListaEspaco.prototype.nos.comunidadesRecentes="community_list, community";
ListaEspaco.prototype.nos.perfisDestaque = ListaEspaco.prototype.nos.perfisRecentes="profile_list, profile";
// - urls
ListaEspaco.prototype.url={};
ListaEspaco.prototype.url.comunidadesDestaque="http://espaco.wsv.terra.com.br/ws/gaia:getTopCommunities/";
ListaEspaco.prototype.url.perfisDestaque="http://espaco.wsv.terra.com.br/ws/gaia:getTopProfiles/";
ListaEspaco.prototype.url.comunidadesRecentes="http://espaco.wsv.terra.com.br/ws/gaia:getRecentCommunities/";
ListaEspaco.prototype.url.perfisRecentes="http://espaco.wsv.terra.com.br/ws/gaia:getRecentProfiles/";
//
// configurações de Blog
// - nos do xml
ListaBlog.prototype.nos={};
ListaBlog.prototype.nos.blogsComentados = ListaBlog.prototype.nos.blogsAcessados = ListaBlog.prototype.nos.postsAcessados="blog_list, blog";
// - urls
ListaBlog.prototype.url={};
ListaBlog.prototype.url.blogsComentados="http://blogimg.terra.com/[:paisCod:]/spool0/jpcache/blog_[:pais:]-jpc-index_a_5__s_6__domain__s_0____s_7__channel__s_0____s_4__skin__s_24__trrlatam_listas_blog_com__s_6__locale__s_0____s_6__device__s_3__xml___.zhtml";
ListaBlog.prototype.url.blogsAcessados="http://blogimg.terra.com/[:paisCod:]/spool0/jpcache/blog_[:pais:]-jpc-index_a_5__s_6__domain__s_0____s_7__channel__s_0____s_4__skin__s_24__trrlatam_listas_blog_acc__s_6__locale__s_0____s_6__device__s_3__xml___.zhtml";
ListaBlog.prototype.url.postsAcessados="http://blogimg.terra.com/[:paisCod:]/spool0/jpcache/blog_[:pais:]-jpc-index_a_5__s_6__domain__s_0____s_7__channel__s_0____s_4__skin__s_24__trrlatam_listas_post_acc__s_6__locale__s_0____s_6__device__s_3__xml___.zhtml";