
function addEvent(elm, evType, fn, useCapture){
if (typeof(fn)=="string")
{

	if (elm.addEventListener) 
			{	
				elm.addEventListener(evType, function (){DispatchEvent(fn,elm)}, useCapture);
				return true;
			} else if (elm.attachEvent) {
				var r = elm.attachEvent('on' + evType, function (){DispatchEvent(fn,elm);});
				return r;
			} else {
				elm['on' + evType] = function (){DispatchEvent(fn,elm)};
			}
	
	}else{
			if (elm.addEventListener) 
			{	
				elm.addEventListener(evType, fn, useCapture);
				return true;
			} else if (elm.attachEvent) {
				var r = elm.attachEvent('on' + evType, fn);
				return r;
			} else {
				elm['on' + evType] = fn;
			}
	}
}
function addClick(lista)
{
ul_leyenda=document.getElementById(lista);

for(i in ul_leyenda.childNodes)
{
	if(ul_leyenda.childNodes[i].tagName!=undefined)
	{
		
		var li = ul_leyenda.childNodes[i];
		li.firstChild.checked = (li.className!="notselected")
		
		addEvent(li.firstChild,"click","check",false);
		addEvent(li.childNodes[4],"mouseover","muestraLeyenda",false);
		addEvent(li.childNodes[4],"mouseout","ocultaLeyenda",false);
		addEvent(li.childNodes[4],"click","check",false);
		//addEvent(li.childNodes[1],"click","check",false);
		distribuyeElementos(li)
	}
}
}


function init()
{
 addClick("ul_leyenda_residencial");
 addClick("ul_leyenda_terciarios");
}
function distribuyeElementos(li)
{
	var numeroProyectos=0;
	
	if(li.className==""){
		muestraproductos (li.id,"items");
		muestraproductos (li.id,"tipospromociones");
		numeroProyectos= cuentaproductos();
	}else{
		ocultaproductos (li.id,"items");
		ocultaproductos (li.id,"tipospromociones");
		numeroProyectos= cuentaproductos();
	}
	if(numeroProyectos>0){
		ocultaProximamente("locales");
		ocultaProximamente("oficinas");
		
	}
	ocultaBocadillo(numeroProyectos==0 && !isSet("proximamente","items"));
	document.getElementById('numeroproyectos').innerHTML=numeroProyectos+" proyectos";
	
}

function manejapadre(obj)
{
	
		if(obj.parentNode.className=="notselected")
		{obj.parentNode.firstChild.checked=true;}
		else
		{obj.parentNode.firstChild.checked=false;}
		obj.parentNode.className=(obj.parentNode.className=="notselected")?"":"notselected";
		distribuyeElementos(obj.parentNode);
}

function ocultaproductos(clasemostrada,capa)
{
	var div_items= document.getElementById(capa);
	var condicion=false;
	
	for (i=0;i<div_items.childNodes.length;i++)
	{	
		//if (capa=="tipospromociones")alert(div_items.childNodes[i].tagName);
		
		if (div_items.childNodes[i].tagName=="DIV")
		{
			var item  = div_items.childNodes[i];
			if (item.className=="item "+clasemostrada)
			{
				item.className+=" invisible";
				condicion=true;
			}
		}else if(div_items.childNodes[i].tagName!=undefined)
		{
			var item  = div_items.childNodes[i];
			if (item.className==clasemostrada)
			{
				item.className=clasemostrada+" invisible";
			}
		}
	}
	if(condicion==false && capa=="items")ocultaProximamente(clasemostrada);
	
}
function ocultaBocadillo(condicion)
{
document.getElementById("infoResultados").style.display=document.getElementById("items").style.display=(condicion)?'none':'block';
document.getElementById("bocadillosinSeleccionar").style.display=(condicion)?'block':'none';
if (condicion==true)
{
	if (isEmpty("ul_leyenda_terciarios"))
	{
	ocultaBocadillo(false);
	
	}
}
}
function cuentaproductos()
{
var div_items= document.getElementById("items");
contador=0;
for (i=0;i<div_items.childNodes.length;i++)
{
	var item  = div_items.childNodes[i];
	if ((div_items.childNodes[i].tagName=="DIV" && div_items.childNodes[i].className.indexOf("item")!=-1) || div_items.childNodes[i].tagName=="HR")
	{
	contador+=(item.className.indexOf("invisible")==-1)?1:0;
	}
}

return contador/2;
}
function muestraproductos(clasemostrada,capa)
{
	var div_items= document.getElementById(capa);
	var condicion=false;
	for (i=0;i<div_items.childNodes.length;i++)
	{	if (div_items.childNodes[i].tagName=="DIV")
		{
			var item  = div_items.childNodes[i];
			if (item.className=="item "+clasemostrada+" invisible")
			{
				item.className="item "+clasemostrada;
				condicion=true;
			}
		}else if(div_items.childNodes[i].tagName!=undefined)
		{
			var item  = div_items.childNodes[i];
			if (item.className==clasemostrada+" invisible")
			{
				item.className=clasemostrada;
			}
		}
	}
	//muestraProximamente
	
	if(condicion==false && capa=="items"){
		muestraProximamente(clasemostrada);
	}

	
}

function isSet(clase,capa)
{
	var div_items= document.getElementById(capa);
	var condicion=false;
	for (i=0;i<div_items.childNodes.length;i++)
	{
		var item  = div_items.childNodes[i];
		if (item.className==clase)
		{return true;}
	}
	return condicion;
}
function isEmpty(capa)
{
	var div_items= document.getElementById(capa);
	var condicion=false;
	for (i=0;i<div_items.childNodes.length;i++)
	{
		var item  = div_items.childNodes[i];
		
		if (item.className=="")
		{
		distribuyeElementos(item);
		condicion= true;
		}
	}
	return condicion;
}
function muestraProximamente(clase)
{
var  capa = document.getElementById("proximamente"+clase);
if(capa!=undefined)
{
capa.className="proximamente";
}
}
function ocultaProximamente(clase)
{
	var  capa = document.getElementById("proximamente"+clase);
	
	if(capa!=null)
	{
		capa.className="proximamenteHidden";
	}
}
function checkAll()
{
	var matrizAll=new Array('viviendalibre','preciocontenido','viviendaprotegida','viviendavacacional','locales','oficinas');
	for (var i=0;i<matrizAll.length;i++)
	{
		var li=document.getElementById(matrizAll[i]);
		li.firstChild.checked=true;
		li.className="";
	}
	
init();
init();
}

AddEventListener("check",manejapadre);
AddEventListener("muestraLeyenda",manejaLeyenda);
AddEventListener("ocultaLeyenda",ocultaLeyenda);
this.onload=init