var procesador= new precesadorNET('php/modelo.php','GET');
var resultResponse;
var links;
var max,current;
var numberShow=12;
var cristalWall;
var isNotFindResponse=false;
var imagenes;
function onclickFilterSearch(zone,pricing,bedrooms,name,type){
  look();
  var manager=  
			  {
				 parameters:"columnas=DISTINCT os.name,os.pricing,os.bedrooms,os.url_image,os.url_detail,os.bath,os.sleeps,os.description&orderby=os.name",
				 onSuccess:setResultResponse
			  }
  var wherecolumns="";
  var wherevalues="";
  var wherelikecolumns="";
  var wherelikevalues="";
  var tables="";
  if(zone&&zone>-1){
    wherecolumns+=",zo.zone";
    wherevalues+=","+zone;
  }
  //if(location&&location>-1){
  //  wherecolumns+=",lo.location";
  //	wherevalues+=","+location;
  //}
  if(pricing&&pricing>-1){
    tables+=",tc_price pr";
    wherecolumns+=",os.pricing>,os.pricing<,pr.price";
	wherevalues+=",IFNULL(pr.min_range;os.pricing),IFNULL(pr.max_range;os.pricing),"+pricing;
  }
  if(bedrooms&&bedrooms>-1){
    tables+=",tc_bedroom br";
    wherecolumns+=",os.bedrooms>,os.bedrooms<,br.bedroom";
	wherevalues+=",IFNULL(br.min_val;os.bedrooms),IFNULL(br.max_val;os.bedrooms),"+bedrooms;
  }
  if(type&&type>-1){
    wherecolumns+=",os.type";
	wherevalues+=","+type;
  }
  if(name&&name.length>0){
    wherelikecolumns+="UPPER(os.name)";
	wherelikevalues+=name.toUpperCase().replace('Ñ','N');
  }
  manager.parameters+="&tabla=td_ownership os,tc_zone zo,tc_location lo"+tables;
  manager.parameters+="&wherecolumns=os.location,lo.zone"+wherecolumns;
  manager.parameters+="&wherevalues=lo.location,zo.zone"+wherevalues;
  manager.parameters+="&wherelikecolumns="+wherelikecolumns;
  manager.parameters+="&wherelikevalues="+wherelikevalues;
  procesador.makeRequest(manager);
}

function clearContent(contenedor){
  if(!contenedor){
    return;
  }
  while(contenedor.childNodes.length>0){//borramos todo el contenido del panel central
		contenedor.removeChild(contenedor.firstChild);
  };
}

function setResultResponse(response){
  unlook();
  resultResponse=procesador.toJsonObject(response.responseText);//convertimos la cadena JSON a un objeto.
  //alert(resultResponse.queryUsed);
  current=0;
  if(resultResponse.name.length>0){
     imagenes=null;  
     max=resultResponse.name.length/numberShow;
     if(isNotFindResponse){
	     isNotFindResponse=false;
	     fillResultSearch(current);
	 }
	 else{
         onFindOwnerships();
	 }
	 preloadImages();
  }
  else{
     onDoNotFindOwnerships();
  }
}
function onDoNotFindOwnerships(){
  clearContent($("panelCentral"));
  $("panelCentral").innerHTML='<div class="notResult"><br>No properties were found for your search. We recommend these properties:</div><div id="resultSearchDiv" class="result"></div>';
  isNotFindResponse=true;
  onclickFilterSearch(1);
}
function onFindOwnerships(){
  clearContent($("panelCentral"));
  var i;
  var links_aux="";
  var limitDown=((i*numberShow)+1);
  var limitUp=((i*numberShow)+numberShow);
  links='<button class="bBack"  onclick="javascript:fillResultSearch(current-1);" ></button>';
  for(i=0;i<max;i++){
    limitDown=((i*numberShow)+1);
    limitUp=((i*numberShow)+numberShow);
	limitUp=limitUp>resultResponse.name.length?resultResponse.name.length:limitUp;
	links_aux+='<button type="button" name="buttonResult" class="buttonResult" onclick="fillResultSearch('+i+');" >'+limitDown+'-'+limitUp+'</button>';
  }
  links+=links_aux;//.substring(2);
  links+='<button class="bNext" onclick="javascript:fillResultSearch(current+1);"></button>';
  $("panelCentral").innerHTML='<div class="headFoot" id="linksHead">'+links+'</div>';
  $("panelCentral").innerHTML+='<div id="resultSearchDiv" class="result"></div>';
  $("panelCentral").innerHTML+='<div class="headFoot" id="linksFoot">'+links+'</div>';
  fillResultSearch(current);
}

function fillResultSearch(range){
  if(range<0||(range-max)>0){
    return;
  }
  look();
  clearContent($("resultSearchDiv"));
  current=range<0?0:(range-max>0)?(max-1):range;
  var botons=document.getElementsByName('buttonResult');
  var cell;
  var limitDown=((current*numberShow));
  var limitUp=((current*numberShow)+numberShow);
  if(botons&&botons.length>0){
	  for(cell=0;cell<botons.length;cell++){
		 botons[cell].className='buttonResult';
	  }
	  botons[current].className='buttonResultCurrent';
	  botons[current+(botons.length/2)].className='buttonResultCurrent';
	  $('linksHead').focus();
  }
  var img;
	for(cell=limitDown;cell<limitUp&&cell<resultResponse.name.length;cell++){
		$("resultSearchDiv").innerHTML+=
						'<div class="cjacasas">'+
							'<div class="titulocasas">'+
							'  <a href="'+resultResponse.url_detail[cell]+'" class="titulocasas">'+resultResponse.name[cell]+'</a>'+
							'</div>'+
							'<div class="imgcasas">'+
							'  <a href="'+resultResponse.url_detail[cell]+'" id="imgcasa_'+cell+'">'+
							//' <img src="'+resultResponse.url_image[cell]+'" alt="'+resultResponse.name[cell]+'" width="193" height="94" border="0">'+
							'  </a> '+
							'</div>'+
							'<div class="textizq"> &bull; '+resultResponse.bedrooms[cell]+' Bedrooms<br>'+
							' &bull; '+resultResponse.bath[cell]+'  Bath</div>'+
							'<div id="textder">&bull; Sleeps '+resultResponse.sleeps[cell]+'<br>'+
							' &bull; '+resultResponse.description[cell]+'</div>'+
							'<div class="press">'+
							  '<a href="'+resultResponse.url_detail[cell]+'" class="Press">From: $'+Math.round(resultResponse.pricing[cell])+' / Night</a>'+
							'</div>'+
						'</div>';
	}
	if(imagenes){
	  for(cell=limitDown;cell<limitUp&&cell<resultResponse.name.length;cell++){
	    $('imgcasa_'+cell).appendChild(imagenes[cell]);
	  }
	}
	else{
		for(cell=limitDown;cell<limitUp&&cell<resultResponse.name.length;cell++){
		  img= new Image(193,94);document.createElement('img');
		  img.src=resultResponse.url_image[cell];
		  img.alt=resultResponse.name[cell];
		  img.border=0;
		  $('imgcasa_'+cell).appendChild(img);
		}
	}
	unlook();
}
function preloadImages(){
 var img,index;
 imagenes=new Array();
 for(index=0;index<resultResponse.name.length;index++){
	  img= new Image(193,94);document.createElement('img');
	  img.src=resultResponse.url_image[index];
	  img.alt=resultResponse.name[index];
	  img.border=0;
	  imagenes[index]=img;
	}
}

function look(){
  if(cristalWall){
    return;
  }
  var body=document.getElementsByTagName('body')[0];
  cristalWall=document.createElement('iframe');
  body.appendChild(cristalWall);
  cristalWall.className='cristalWall';
  setOpacity(cristalWall,0.5);
}
function unlook(){
  if(!cristalWall){
    return;
  }
  var body=document.getElementsByTagName('body')[0];
  body.removeChild(cristalWall);
  cristalWall=null;
}
   
 function setOpacity( element, alpha ) {
  var style = element.style;
  if( style.MozOpacity != undefined ) { //Moz and older
    style.MozOpacity = alpha;
  }
  else if( style.filter != undefined ) { //IE
    style.filter = "alpha(opacity=50)";
    element.filters.alpha.opacity = ( alpha * 100 );
  }
  else if( style.opacity != undefined ) { //Opera
    style.opacity = alpha;
  }
}
