var nextUrl="http://"+location.host+"/"+langid+"/user";
var postData={tipo:tipobusqueda};
var mozaico=true;
var retbusqueda=null;
$(document).ready(function() {
	$("#anuncios_div").removeClass("bl_search_escort_box2");
	
	if (tipobusqueda=="favoritos"){
		$("#barraTitulo").addClass("bl_right2");
	}

	postData.nombre=$("#nombre").val();
	postData.atiende=$("#atiende").val();
	postData.tarifa1=$("#tarifa1").val();
	postData.tarifa2=$("#tarifa2").val();
	postData.pais=$("#pais").val();
	postData.ciudad=$("#ciudad").val();
	buscar(1);
	
	$("#btnBuscar").click(function(){
		postData.nombre=$("#nombre").val();
		postData.atiende=$("#atiende").val();
		postData.tarifa1=$("#tarifa1").val();
		postData.tarifa2=$("#tarifa2").val();
		postData.pais=$("#pais").val();
		postData.ciudad=$("#ciudad").val();
		buscar(1);
	});
	
	$('#nombre').autocomplete(
		"http://"+location.host+"/goduo/application/libraries/searchScort.php",
		{
		 delay:10,
		 minChars:1,
		 matchSubset:1,
		 matchContains:1,
		 cacheLength:-1,
		 formatItem:formatItem,
		 maxItemsToShow:15,
		 autoFill:true
		 }		
	);
		
});

 	
function formatItem(row) {
 //return row[0] + " (id: " + row[1] + ")";
 return row[0];
}

function buscar(pagina){
		var url="http://"+location.host+"/"+langid+"/scort/busqueda";
		postData.pagina=pagina;
		$("#errormsg").html('').hide();
		$("#anunciosList").html('<img src="/img/loading.gif">');
		$.post(
			url,
			postData,
			function(ret) {
				retbusqueda=ret;
				showBusqueda(retbusqueda);					
			},
			"json");			
}

function showBusqueda(ret){
	resultado=ret.resultado;
	showPaginas(ret.pagina,ret.total,ret.resxpagina);
	$("#anunciosList").empty();
	for (i=0;i<resultado.length;i++){
		agregarAnuncio(resultado[i]);
	}

}

function showPaginas(pagina, total, resxpagina ){

	if (resxpagina>0){
		var total_paginas=(total/resxpagina)|0;
		if (total_paginas*resxpagina<total) total_paginas++;
	}
	else{
		var total_paginas=1;
	}

	
	if (pagina>1){
		var inicio='<a href="#" onClick="buscar(1)"><<</a>';
		var anterior= '<a href="#" onClick="buscar('+(pagina-1)+')"><</a>';
		var previo= '<a href="#" onClick="buscar('+(pagina-1)+')">'+(pagina-1)+'</a> | ';
	}
	else{
		var inicio='';
		var anterior= '';
		var previo= '';
	}
	
	var actual= (pagina)+'';
	
	if (pagina<total_paginas){
		var posterior= '| <a href="#" onClick="buscar('+(pagina+1)+')">'+(pagina+1)+'</a>';
		var siguiente= '<a href="#" onClick="buscar('+(pagina+1)+')">></a>';
		var ultimo='<a href="#" onClick="buscar('+total_paginas+')">>></a>';
	}
	else{
		var posterior='';
		var siguiente='';
		var ultimo='';
	}

	

	var templatePaginador = $("#templatePaginador").html();	
	var template=templatePaginador;

	
	
	template = template.replace(/\{inicio\}/g, inicio);
	template = template.replace(/\{anterior\}/g, anterior);
	template = template.replace(/\{previo\}/g, previo);
	template = template.replace(/\{actual\}/g, actual);
	template = template.replace(/\{posterior\}/g, posterior);
	template = template.replace(/\{siguiente\}/g, siguiente);
	template = template.replace(/\{ultimo\}/g, ultimo);
	template = template.replace(/\{total_paginas\}/g, total_paginas);
	template = template.replace(/\{total\}/g, total);
	var linkmodo="";
	if (mozaico){
		linkmodo='<a href="#" onclick="modo_normal();">['+lang_js17+']</a>';
	}
	else{
		linkmodo='<a href="#" onclick="modo_mozaico();">['+lang_js18+']</a>';
	}
	template = template.replace(/\{modo\}/g, linkmodo);
	
	$("#paginadorTop").html(template);	
	$("#paginadorBottom").html(template);	

}

function modo_mozaico(){
	$("#anuncios_div").removeClass("bl_search_escort_box2");
	mozaico=true;
	showBusqueda(retbusqueda);
}

function modo_normal(){
	$("#anuncios_div").addClass("bl_search_escort_box2");
	mozaico=false;
	showBusqueda(retbusqueda);
}

function agregarAnuncio(anuncio){	
	var thumb = '<a href="/'+langid+'/scort/anuncio/'+anuncio.anuncio_id+'"><img src="'+anuncio.thumb+'"></a>';
	var nombre = '<a href="/'+langid+'/scort/anuncio/'+anuncio.anuncio_id+'">'+anuncio.nombre+'</a>';
	var medidas=anuncio.medida1+"-"+anuncio.medida2+"-"+anuncio.medida3;
	
	var idiomas="";
	idiomas_array=eval(anuncio.idiomas);		
	for (j=0; j<idiomas_array.length;j++){
		idiomas = idiomas+' <img src="/img/flag/'+idiomas_array[j]+'-flag.gif"/>';
	}
	
	var marcadores="";
	if (anuncio.oculto==1) marcadores="<b>Oculto</b>";
	if (anuncio.publicado==0) marcadores=marcadores+" - <b>No Publicado</b>";
	if (anuncio.aprobado==0) marcadores=marcadores+" - <b>No Aprobado</b>";
	
	
	var linea=$("#lineaBus");
	
	linea.attr("id","lineaBus"+anuncio.anuncio_id);
	
	if (mozaico){
		var templateAnuncios = $("#resBusquedaTemplate2").html();	
	}
	else{
		var templateAnuncios = $("#resBusquedaTemplate").html();	
	}
	var template=templateAnuncios;

	
	template = template.replace(/\{nombre\}/g, nombre);
	template = template.replace(/\{thumb\}/g, thumb);
	template = template.replace(/\{edad\}/g, anuncio.edad);
	template = template.replace(/\{tarifa\}/g, anuncio.tarifa);
	template = template.replace(/\{medidas\}/g, medidas);
	template = template.replace(/\{estatura\}/g, anuncio.estatura);
	template = template.replace(/\{idiomas\}/g, idiomas);
	template = template.replace(/\{ciudad\}/g, anuncio.ciudad);
	if(langid == 'es'){
		template = template.replace(/\{pais\}/g, anuncio.pais);
	} else {
		template = template.replace(/\{pais\}/g, anuncio.country);
	}
	template = template.replace(/\{marcadores\}/g, marcadores);
	
	$("#anunciosList").append(template);
	linea.attr("id","lineaBus");

}

function eliminarFavorito(anuncio_id){
	var url = "http://"+location.host+"/"+langid+"/anuncios/eliminarFavorito/"+anuncio_id;
	var postData =	{	};	
	$.post(
		url,
		postData,
		function(ret) {
			if (ret.exito){
				$("#lineaBus"+anuncio_id).remove();
			}
		},
		"json");		
}

