$(document).ready(function() {
	$("#cmdBuscar").click(function() {
		return buscarFormulario();
	})
});

function init_Noticias(){
	$.ajax({type: "GET",url: "fotos/novedades/novedades.xml",dataType: "xml",success: 
	function(xml) {
		$("#lstNovedades").html(genhtml_Noticias(xml));
	}});
	setTimeout("showPag(1)",500);
}

function init_ultimaNoticia(){
	$.ajax({type: "GET",url: "fotos/novedades/novedades.xml",dataType: "xml",success: 
	function(xml) {
		$("#ultiNoti").html(genhtml_ultimaNoticia(xml));
	}});
}

function genhtml_Noticias(xml){
var itemsPORpagina=5;
var Paginas="";
var Paginador="";
var contPagina=0;
var contItems=0;

var htmlLstNoti="";
var paginaid="";
var titulo="";
var subtitulo="";
var imagen="";

	$(xml).find('datos>item').each(
	function(i){
		contItems +=1;
		paginaid=$(this).find("paginaid").text();
		titulo=$(this).find("titulo").text();
		subtitulo=$(this).find("subtitulo").text();
		imagen=$(this).find("imagen").text();
		if(imagen==""){
			imagen="<td colspan=\"2\" valign=\"top\">";
		}else{
			imagen ="			  <td width=\"30%\" valign=\"top\"><img src=\"fotos/novedades/"+imagen+"\" /></td>";
			imagen +="			  <td valign=\"top\">";
		}
		htmlLstNoti +="<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"tblBuscador\">";
		htmlLstNoti +="<tr>";
		htmlLstNoti +="  <td><table width=\"99%\" border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"1\">";
		htmlLstNoti +="	  <tr>";
		htmlLstNoti +="		<td><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
		htmlLstNoti +="			<tr>";
		htmlLstNoti +=imagen;
		htmlLstNoti +="				  <table width=\"100%\" border=\"0\" cellspacing=\"2\" cellpadding=\"5\">";
		htmlLstNoti +="					  <tr>";
		htmlLstNoti +="						<td><strong>"+titulo+"</strong></td>";
		htmlLstNoti +="					  </tr>";
		htmlLstNoti +="					  <tr>";
		htmlLstNoti +="						<td>"+subtitulo+"</td>";
		htmlLstNoti +="					  </tr>";
		htmlLstNoti +="					  <tr>";
		htmlLstNoti +="						<td align=\"right\"><a href=\"fotos/novedades/"+paginaid+"\" class=\"linkCategorias\">[+] info</a></td>";
		htmlLstNoti +="					  </tr>";
		htmlLstNoti +="				  </table>";
		htmlLstNoti +="				</td>";
		htmlLstNoti +="			</tr>";
		htmlLstNoti +="		</table></td>";
		htmlLstNoti +="	  </tr>";
		htmlLstNoti +="  </table></td>";
		htmlLstNoti +="</tr>";
		htmlLstNoti +="</table>";
		htmlLstNoti +="<br/>";
		if (contItems % itemsPORpagina==0){
			contPagina +=1;
			Paginas+="<div id=\"pag_"+contPagina+"\" class=\"hd_pag OCULTO\">"+htmlLstNoti+"</div>"
			htmlLstNoti="";
			Paginador +="&nbsp;<a href=\"#\" onclick=\"showPag("+contPagina+");\" class=\"linkCategorias\" >"+contPagina+"</a>";
		}
	});
	//el resto de las noticias
	if (contItems % itemsPORpagina!=0){
			contPagina +=1;
			Paginas+="<div id=\"pag_"+contPagina+"\" class=\"hd_pag OCULTO\">"+htmlLstNoti+"</div>"
			htmlLstNoti="";
			Paginador +="&nbsp;<a href=\"#\" onclick=\"showPag("+contPagina+");\" class=\"linkCategorias\" >"+contPagina+"</a>";
		}
	if	(contPagina>1){Paginador = "<br/>Mas noticias " +Paginador;}else{Paginador ="";}
	
return Paginas+Paginador;
}
function showPag(nroP){
	$(".hd_pag").hide();
	$("#pag_"+nroP).show();
}

function genhtml_ultimaNoticia(xml){
var htmlItem="";
var paginaid="";
var titulo="";
var subtitulo="";
var imagen="";

	$(xml).find('datos>item').each(
	function(i){
		paginaid=$(this).find("paginaid").text();
		titulo=$(this).find("titulo").text();
		subtitulo=$(this).find("subtitulo").text();
		imagen=$(this).find("imagen").text();
		return false;
	});
	if(imagen==""){
		imagen="&nbsp;";
	}else{
		imagen="<img src=\"fotos/novedades/"+imagen+"\" height=\"121\" width=\"180\" />";
	}
htmlItem +="<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"tblBuscador\">";
htmlItem +="<tr>";
htmlItem +="  <td><table width=\"99%\" border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"1\">";
htmlItem +="	  <tr>";
htmlItem +="		<td height=\"1\"></td>";
htmlItem +="	  </tr>";
htmlItem +="	  <tr>";
htmlItem +="		<td><strong>&raquo; NOTICIA DEL DIA</strong></td>";
htmlItem +="	  </tr>";
htmlItem +="	  <tr>";
htmlItem +="		<td><hr size=\"1\" noshade=\"noshade\" color=\"#999999\" /></td>";
htmlItem +="	  </tr>";
htmlItem +="	  <tr>";
htmlItem +="		<td><table width=\"655\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
htmlItem +="		  <tr>";
htmlItem +="			<td valign=\"top\">"+imagen+"</td>";
htmlItem +="			<td valign=\"top\"><table width=\"460\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\">";
htmlItem +="			  <tr>";
htmlItem +="				<td><strong>"+titulo+"</strong></td>";
htmlItem +="			  </tr>";
htmlItem +="			  <tr>";
htmlItem +="				<td>"+subtitulo+"</td>";
htmlItem +="			  </tr>";
htmlItem +="			  <tr>";
htmlItem +="				<td align=\"right\"><a href=\"fotos/novedades/"+paginaid+"\" class=\"linkCategorias\">[+] info</a></td>";
htmlItem +="			  </tr>";
htmlItem +="			</table></td>";
htmlItem +="		  </tr>";
htmlItem +="		</table>";
htmlItem +="		</td>";
htmlItem +="	  </tr>";
htmlItem +="  </table></td>";
htmlItem +="</tr>";
htmlItem +="</table>";
	
return htmlItem;
}

function init_publicidad_index(dinamico){
var m_url="";
	if(typeof(dinamico)=="undefined"){
		m_url=pre+"fotos/publicidades/tres/publicidades.xml";
	}else{
		if(qParam.ids!="" && qParam.idp!="" ){
			//m_url=pre+"publicidades.php?"+getDatosURL();
			m_url=pre+"fotos/publicidades/tres/publicidades.xml";
		}else{
			m_url=pre+"fotos/publicidades/tres/publicidades.xml";
		}
	}
	
	$.ajax({type: "GET",url: m_url,dataType: "xml",success: 
	function(xml) {
		$("#avisoTop").html(genhtml_index_top(xml));
		$("#avisosIzquierda").html(genhtml_index_izq(xml));
	}});
}

function genhtml_index_top(xml){
var htmlItem=""
var archivo="";
var tipo="";
var ext="";
htmlItem ="<table border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"brdeTbl\">";
	$(xml).find('datos>item').each(
	function(i){
		tipo=$(this).find("tipo").text();
		archivo=$(this).find("archivo").text();
		ext= archivo.substr(archivo.length-3);
		if (tipo=="3"){
			if(ext.toLowerCase()=="swf"){
				htmlItem +="<tr><td>";
				htmlItem +="<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0\" width=\"588\" height=\"57\">";
				htmlItem +="<param name=\"movie\" value=\""+pre+"fotos/publicidades/tres/"+archivo+"\" />";
				htmlItem +="<param name=\"quality\" value=\"high\" />";
				htmlItem +="<embed src=\""+pre+"fotos/publicidades/tres/"+archivo+"\" quality=\"high\" pluginspage=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"588\" height=\"57\"></embed></object>";
				htmlItem +="</td></tr>";
			}else{
				htmlItem +="<tr><td>";
				htmlItem +="<img src=\""+pre+"fotos/publicidades/tres/"+archivo+"\" width=\"588\" height=\"57\" />";
				htmlItem +="</td></tr>";
			}
		}
	});
	htmlItem +="</table>";
return htmlItem;
}

function genhtml_index_izq(xml){
var htmlItem_izq="";
var htmlItem_izq1=""; //primeros 3
var htmlItem_izq2=""; //segundos 3
var archivo="";
var tipo="";
var ext="";
	//primeros
	$(xml).find('datos>item').each(
	function(i){
		tipo=$(this).find("tipo").text();
		archivo=$(this).find("archivo").text();
		ext= archivo.substr(archivo.length-3);
		if (tipo=="4"){
			if(ext.toLowerCase()=="swf"){
				htmlItem_izq1 +="<tr><td>";
				htmlItem_izq1 +="<table border=\"0\" align=\"right\" cellpadding=\"0\" cellspacing=\"0\" class=\"brdeTbl\">";
				htmlItem_izq1 +="	  <tr>";
				htmlItem_izq1 +="		<td><object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0\" width=\"216\" height=\"107\">";
				htmlItem_izq1 +="		  <param name=\"movie\" value=\""+pre+"fotos/publicidades/tres/"+archivo+"\" />";
				htmlItem_izq1 +="		  <param name=\"quality\" value=\"high\" />";
				htmlItem_izq1 +="		  <embed src=\""+pre+"fotos/publicidades/tres/"+archivo+"\" quality=\"high\" pluginspage=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"216\" height=\"107\"></embed></object>";
				htmlItem_izq1 +="	</td>";
				htmlItem_izq1 +="  </tr>";
				htmlItem_izq1 +="</table>";
				htmlItem_izq1 +="</td></tr>";
			}else{
				htmlItem_izq1 +="<tr><td>";
				htmlItem_izq1 +="	<table border=\"0\" align=\"right\" cellpadding=\"0\" cellspacing=\"0\" class=\"brdeTbl\">";
				htmlItem_izq1 +="	  <tr>";
				htmlItem_izq1 +="		<td><img src=\""+pre+"fotos/publicidades/tres/"+archivo+"\" width=\"216\" height=\"107\" /></td>";
				htmlItem_izq1 +="	  </tr>";
				htmlItem_izq1 +="	</table>";
				htmlItem_izq1 +="</td></tr>";
			}
			htmlItem_izq1 +="<tr>";
			htmlItem_izq1 +="<td>&nbsp;</td>";
			htmlItem_izq1 +="</tr>";
		}
		
	});
	//segundos
	$(xml).find('datos>item').each(
	function(i){
		tipo=$(this).find("tipo").text();
		archivo=$(this).find("archivo").text();
		ext= archivo.substr(archivo.length-3);
		if (tipo=="5"){
			if(ext.toLowerCase()=="swf"){
				htmlItem_izq2 +="<tr><td>";
				htmlItem_izq2 +="<table border=\"0\" align=\"right\" cellpadding=\"0\" cellspacing=\"0\" class=\"brdeTbl\">";
				htmlItem_izq2 +="	  <tr>";
				htmlItem_izq2 +="		<td><object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0\" width=\"216\" height=\"107\">";
				htmlItem_izq2 +="		  <param name=\"movie\" value=\""+pre+"fotos/publicidades/tres/"+archivo+"\" />";
				htmlItem_izq2 +="		  <param name=\"quality\" value=\"high\" />";
				htmlItem_izq2 +="		  <embed src=\""+pre+"fotos/publicidades/tres/"+archivo+"\" quality=\"high\" pluginspage=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"216\" height=\"107\"></embed></object>";
				htmlItem_izq2 +="	</td>";
				htmlItem_izq2 +="  </tr>";
				htmlItem_izq2 +="</table>";
				htmlItem_izq2 +="</td></tr>";
			}else{
				htmlItem_izq2 +="<tr><td>";
				htmlItem_izq2 +="	<table border=\"0\" align=\"right\" cellpadding=\"0\" cellspacing=\"0\" class=\"brdeTbl\">";
				htmlItem_izq2 +="	  <tr>";
				htmlItem_izq2 +="		<td><img src=\""+pre+"fotos/publicidades/tres/"+archivo+"\" width=\"216\" height=\"107\" /></td>";
				htmlItem_izq2 +="	  </tr>";
				htmlItem_izq2 +="	</table>";
				htmlItem_izq2 +="</td></tr>";
			}
			htmlItem_izq2 +="<tr>";
			htmlItem_izq2 +="<td>&nbsp;</td>";
			htmlItem_izq2 +="</tr>";
		}
		
	});
htmlItem_izq ="<table width=\"237\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" + htmlItem_izq1 + htmlItem_izq2 + "</table>";
return htmlItem_izq;
}

function init_buscador(){
	if (location.search!=""){
		buscar(location.search);
	}
}
var qParam={idc:"",ids:"",idp:"",txtc:"",txts:"",pa:""}

function colocarParametros(valores){
var mtzV,mtzK,i;
	if(valores!=""){
		valores = valores.replace("?","");
		mtzV=valores.split("&");
		for(i=0;i<mtzV.length;i++){
			mtzK= mtzV[i].split("=");
			if(mtzK[1]&&mtzK[1]!=""){
				switch (mtzK[0]){
				   case "idc":
						qParam.idc=mtzK[1];
				   break;
				   case "ids":
						qParam.ids=mtzK[1];
				   break;
				   case "idp":
						qParam.idp=mtzK[1];
						$("#provincia").val(mtzK[1]);
				   break;
				   case "txtc":
						qParam.txtc=mtzK[1];
						$("#txtComercio").val(unescape(mtzK[1]));
				   break;
				   case "txts":
						qParam.txts=mtzK[1];
						$("#txtRubro").val(unescape(mtzK[1]));
					case "pa":
						qParam.pa=mtzK[1];
				   break;
				}
			}
		}
	}
}

function buscar(valores){
var mtzDatos="";
var datos="";
var _html="";
var tmpPaginador="";
	colocarParametros(valores);
	datos ="buscador.php?"+getDatosURL();
	$.ajax({type: "GET",url: datos,dataType: "xml"
	,beforeSend:
	function(){
		//muestro el procesando
		$("#procesando").show();
	}
	,error:
	function(XMLHttpRequest, ajaxOptions, thrownError) {
		alert("Ocurrio un error al realizar la busqueda");
	}
	,success: 
	function(xml) {
		_html +="<b>Resultado de la b&uacute;squeda:</b> ";
		_html +=$("CANTIDAD",xml).text();
		_html +="<br/>";
		_html +=$("DESCRIPCION",xml).text();
		_html +="<hr/>";
		_html +=$("CONTENIDO",xml).text();
		tmpPaginador=$("PAGINADOR",xml).text();
		if(tmpPaginador!=""){
			_html +="Paginas "+tmpPaginador;
		}		
		$("#resbuscador").html(_html);
		//actualizo los posibles popup
		actualizarPopUps();
		//oculto el procesando
		$("#procesando").hide();
	}});
	//refresco publicidad
	//init_publicidad_index(true);
	return false;
}

function datosValidos(){
var strError="";
var txtRubro= $("#txtRubro").val();
var txtComercio= $("#txtComercio").val();
	if((txtRubro=="" || txtRubro.length<3) && (txtComercio=="" || txtComercio.length<3)){
		strError="Debe buscar por 'Rubro' o por 'Comercio'.\nIngresando al menos 3 letras";
	}
return strError;
}
function buscarFormulario(){
var datos;
var sError="";
sError=datosValidos();
	if (sError==""){
		//limpio los ids
		qParam.idc="";
		qParam.ids="";
		qParam.pa="";
		//armo la cadena search
		qParam.idp= $("#provincia").val() || -1;
		qParam.txts= $("#txtRubro").val();
		qParam.txtc= $("#txtComercio").val();
		window.location="buscador.htm?"+getDatosURL();
	}else{
		alert (sError);
	}
}

function getDatosURL(){
	return "idc="+qParam.idc+"&ids="+qParam.ids+"&idp="+qParam.idp+"&txtc="+qParam.txtc+"&txts="+qParam.txts+"&pa="+qParam.pa;
}

function actualizarPopUps(){
	$(".linkMasInfo").lightBox({imageLoading:'global_lib/lightbox/images/loading.gif',imageBtnClose:'global_lib/lightbox/images/closed.gif'});
}