var rowclicked = false;
$(document).ready(function(){
  changeSelect("selectbox","Choose Properties");
  changeSelect("selectbox_city","City &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
  changeSelect("selectbox_square","sq.ft. &nbsp;&nbsp;&nbsp;");
  changeSelect("selectbox_status","Status &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
 changeSelect("selectbox_corridor","Market Area &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");





//$("#city_heading").append($("#selectbox_city").remove());

/*
var selectbox_city = $("#selectbox_city")
$("#city_heading").append(selectbox_city.remove());
$("#" + "selectbox_city" + '_row').click(function(){handleRowClick("selectbox_city")})
*/
     
     


$("#select_table").css("margin-left","190px");
/*
$("#selectbox_city").css("margin-right","40px");
$("#selectbox_square").css("margin-right","16px");
$("#selectbox_status").css("margin-right","35px");
$("#selectbox_corridor").css("margin-right","28px");
*/


$("body").click(function(e){
  //console.log("body click");
  //alert("body click");
    
if (rowclicked == false)
{
      
       $('.dropdown').each(function(){
        
          //console.log(this.id + " " + id + "_dropdown")
     			this.clicked = false
     			$(this).css("display","none")
     			
     			$("#" + this.id.substring(0,this.id.indexOf("_dropdown")) + "_down").attr("src","http://utaz.com/down.bmp")
     			//console.log("#" + this.id.substring(0,this.id.indexOf("_dropdown")) + "_down")
          
     	})
 
}
rowclicked = false; 
})

  
})


//simple XHR wrapper. it needs to be wrapped because it is such a present




var ajax = {};
ajax.get = function(url,postvals,returnfunc){
  var req = jQuery.ajax(); //new XMLHttpRequest();
  data = "";
  for (var y in postvals)
  {
    data += y + "=" + encodeURIComponent(postvals[y]) + "&";
  }
  data = data.substring(0,data.length - 1);
	req.open("POST", url, true);
	req.onreadystatechange = function (aEvt) {
  	if (req.readyState == 4) {
     if(req.status == 200)
      returnfunc(req.responseText);
     else
      alert("Error loading page\n");
  	}  
  }
  req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  req.setRequestHeader("Content-length", data.length);
  req.send(data);
  return req;
}


var req
function gatherSend()
{

	//if (req && req.abort) req.abort();
	var properties = [];
	var cities = [];
	var sqfts = [];
	var status = [];
	var corridor = [];
	
	$('#selectbox input').each(function(){
		if (this.checked == true)
		{	
			properties.push(this.name);
			
		}
		
	})


	$('#selectbox_city input').each(function(){
		if (this.checked == true)
		{	
			cities.push(this.name);
			
		}

	})
	
	$('#selectbox_square input').each(function(){
		if (this.checked == true)
		{	
			sqfts.push(this.name);
			
		}

	})
	
	$('#selectbox_status input').each(function(){
		if (this.checked == true)
		{	
			status.push(this.name);
			
		}

	})
	
		$('#selectbox_corridor input').each(function(){
		if (this.checked == true)
		{	
			corridor.push(this.name);
			
		}

	})
	
		//console.log("properties:" + properties.join(","));
		//console.log("cities:" + cities.join(","));
		//console.log("sqfts:" + sqfts.join(","));
		
		req = ajax.get("http://www.utaz.com/listview_back.php",{"properties": properties, "cities":cities, "sqfts":sqfts, "status": status, "corridor":corridor},updateTable)
  
}

function updateTable(htmlio)
{
	$('#locations_table_wrapper')[0].innerHTML = htmlio;//empty().append(htmlio);
	//console.log(htmlio);
//	console.log($("#locations_table_wrapper"));

$("#locations_table_wrapper tr:even").css("background-color", "#29261F");
					//console.log($("#locations_table_wrapper tr:even"));
					$("#locations_table_wrapper tr:odd").css("background-color", "#39362F");


 
 
	
}

function handleRowClick(id){
     
     //console.log("row click")
     //alert("row click")
     rowclicked = true;
     $('.dropdown').each(function(){
        
          //console.log(this.id + " " + id + "_dropdown")
     			if (this.id != id + "_dropdown")
     			{
     			
     			this.clicked = false
     			$(this).css("display","none")
     			
     			$("#" + this.id.substring(0,this.id.indexOf("_dropdown")) + "_down").attr("src","http://utaz.com/down.bmp")
     			//console.log("#" + this.id.substring(0,this.id.indexOf("_dropdown")) + "_down")
          }
     	})
     
     $("#" + id + "_dropdown")[0].clicked = !($("#" + id + "_dropdown")[0].clicked)
     //this.clicked = !this.clicked //cool!
     //alert($("#" + id + "_dropdown")[0].clicked)
     //console.log($("#" + id + "_dropdown")[0].clicked)
     
     if ($("#" + id + "_dropdown")[0].clicked)
     {
     
      
      $("#" + id + "_dropdown").css("display","block")
      //console.log($("#" + id + "_dropdown").css("display"))
      //$("#" + id).append("<div id = '"+id+"_dropdown'>"+x+"</div>")
      $("#" +id + "_down").attr("src","http://utaz.com/down_down.bmp")
     }
     else
     {
      $("#" + id + "_dropdown").css("display","none")
       //$("#" + id + "_dropdown").remove();
        $("#" +id + "_down").attr("src","http://utaz.com/down.bmp")
     }
  
  }
  
  


function changeSelect(id,name)
{
  options = $("#" + id).children();
  for (i = 0; i < options.length; i++)
  {
    //console.log($(options[i]).text() + $(options[i]).val());
  }
  
  $("#" + id).replaceWith("<div id = '"+id+"'><table width = '100%'cellspacing = '0' cellpadding='0' class = 'newselect'><tr id = '"+id+"_row'><td><div class = 'dropdowntext' id = '"+id+"_2' >"+name+"</div></td><td><img width = '16' height = '16' align  = 'right' src = 'http://utaz.com/down.bmp' id = '" + id + "_down'></td></tr></table></div>")
  
 
  
  $("#" + id).append("<div class = 'dropdown' id = '"+id+"_dropdown'></div>");
  //var width = $("#" + id).width()
  var width = 200;
  
  $("#" + id + "_dropdown").width(width);
  
  for (i = 0; i < options.length; i++)
  {
     $("#" + id + "_dropdown").append("<div class = 'dropdownoption'><table><tr><td><input  name = '"+$(options[i]).text()+"'type = 'checkbox' onclick = 'gatherSend()' id = '"+id+"_option_" + (i + 1) + "'/></td><td width = '100%'><label for='"+id+"_option_" + (i + 1) + "'>"+$(options[i]).text()+"</label></td></tr></table></div>")
  }
 
 $("#" + 'select_table').click(function(){
    rowclicked = true; //the body click function uses this. that function is in the listview.php file
 })

 
  $("#" + id + '_row').click(function(){handleRowClick(id)})
       
}
