$(document).ready(function()
{
	//reset cbos when page loads
	reset_search();

    //reset the combo boxes
    $("#reset").click( function()
    {
    	reset_search();
    	$("a#series-promo-homes").click();	
    });


	//triggers the search
    $("select").change( updateSearch );


});//end of document.ready()

function reset_search()
{
    var lists = $("select");
    for(i = 0; i < lists.length; i++)
    {
        lists[i].selectedIndex = 0;
    }
}

function updateSearch()
{
	//get all cbos
    var cbos = $("select");
    
	//remove class of tabs if set
	$(".seriesTabs").children("li").removeClass("yellowtab");
	
	//we could call show_house_by_id here if we wanted to
	//show the home no matter the values of the other
	//combo boxes. It would make sense but may not seem logical
  
    $("#houses").fadeOut();
    
    var criteria =
    {
    	//we send the SQL compare method to dynamically build our queries
        "Price":          { "value": cbos[0].value, "compare": "<=" },
        "Stories":        { "value": cbos[1].value, "compare": "=" },
        "Bedrooms":       { "value": cbos[2].value, "compare": "=" },
        "ExteriorWidth":  { "value": cbos[3].value*1000, "compare": "<" },
        "HouseID":        { "value": cbos[4].value, "compare": "="},
        "Living":   	  { "value": cbos[6].value, "compare": "=" }
        
    };
	//testing error reporting
	//cbos = null;
    if( cbos[5].value == 1)
    {
		criteria.FrontView = { "value":1, "compare":"=" };
	}
	else if( cbos[5].value == 2)
	{
		criteria.RearView = { "value":1, "compare":"=" };
	}
	else if( cbos[5].value == 3)
	{
		criteria.FrontView = { "value":1, "compare":"=" };
		criteria.RearView =  { "value":1, "compare":"=" };
	}
	
    //ajax call
    $.ajax({
        type: "POST",
        url: base_url + "home/get_search_results/",
        data: criteria,
        dataType: "json",
        success : function( result )
        {
        	//only show a single house if it match other criteria also
        	if( result.count > 0 )
        	{
			    //shows the individual home
			    if(cbos[4].value != 0)
			    {
			        //match the house
					show_house_by_id( cbos[4].value );
			    }          		
        	}
        	
            $("#houses").html( result.html );
            $(".search-feedback").html( "Search Results - " + result.count + " Homes Found" );
            
            $("#houses").fadeIn(400, function() 
            {
            	//only scroll down to search results if they didn't select a house
            	if( cbos[4].value == 0)
				{
					/*
					 * We check if the plugin exists because errors were being logged
					 * regarding the scrollTo plugin for some browsers. I have been unable
					 * to replicate the error, but this has prevent the error been logged.
					 * The user would hardly notice a difference with the page not scrolling down
					 * and I have a feeling it may be Googlebot anyway.
					 */
					if(jQuery().scrollTo)
					{
						$.scrollTo( $("#search-feedback"), 500 );
					}
            	}
        	 });

            bind_clicked_house();
        }
    });

}



