$(document).ready(function(){
	$('.hide').css('display', 'hidden');
	
	
	
});

$('#submit').click(function() {
	
		var values = $('#home_form').serialize();
		var valArray = values.split('&');
		var input = new Array();
		var loc = '';
		var price = '';
		var type = '';
		$(valArray).each(function(i){
			input.push($(values.split('&'))[i].split('=')[1]);
		});
		
		
		$('.hp').animate({
			opacity: 0
		}, 200);
		$('#flash_target').hide(); 
		$('.hp').hide(); 
		
		searchResults(input[0], input[1], input[2]);
		
	});
	
function searchResults(loc, pri, typ){
	var inLoc = loc;
	var inPrice = parseInt(pri);
	var inType = typ;
	var theHtml = '';
	$.get('../xml/NewUrban.xml', function (xml) {  
        $('community', xml).each(function (i) {  
			
			// DATA FROM XML
			var name = $(this).attr('name'); 
			var loca = $($(this).find("loc")).text().split(', ')[1]; 
			var type = $($(this).find("type")).attr('val').split(', '); 
			var minPr = parseInt($($(this).find("price")).attr('min')); 
			var maxPr = parseInt($($(this).find("price")).attr('max')); 
			var bedrooms = $($(this).find("bedrooms")).text(); 
			var bathrooms = $($(this).find("bathrooms")).text(); 
			var garage = $($(this).find("garage")).text(); 
			var thelink = $($(this).find("link")).text(); 
			
			// IMAGES
			var logo = $($(this).find("logo")).text(); 
			var img = $($(this).find("img")).text(); 
			
			var matchPrice = checkPrice(inPrice, minPr, maxPr);
			var matchLoc = checkLoc(inLoc, loca);
			var matchType = checkType(inType, type);
			if(matchPrice == true && matchLoc == true && matchType == true){
				theHtml += '<tr class="listing"> ';
				theHtml += '<td class="tb_lspace" ><img src="images/tb_lspace.gif" alt="environmentally friendly housing" /> </td>';
				theHtml += '<td class="tb_icon"><a href="communities/community/' + thelink + '.html" class="toggleopacity"><img src="' + logo + '" alt="' + name + '" /></a></td>';
				theHtml += '<td class="tb_image"><a href="communities/community/'+ thelink + '.html" class="toggleopacity"><img src="' + img + '" width="153" height="107"alt="' + name + '" /></a></td>';
				theHtml += '<td class="tb_location">' + $($(this).find("loc")).text() + '</td>';
				theHtml += '<td  class="tb_type">' + type + '<br /></td>';
				theHtml += '<td class="tb_price">$' + minPr + '-$' + maxPr + '</td>';
				theHtml += '<td class="tb_beds">' + bedrooms + '</td>';
				theHtml += '<td class="tb_baths">' + bathrooms + '</td>';
				theHtml += '<td class="tb_garage">' + garage + '</td>';
				theHtml += '<td class="tb_rspace"><img src="images/tb_rspace.gif" alt="environmentally friendly housing" /></td></tr>';
				
				
				
			}
			
		});
		$('#searchResults').append(theHtml);
	});
	
	$('.hide').css('display', 'block');
}

function checkLoc(inputLoc, xmlLoc){
	if(inputLoc == '0'){
		return(true);
	}else if(inputLoc == xmlLoc){
		 return(true);
	} else{
		return(false);
	}
}

function checkPrice(price, mn, mx){
	if(price == '0'){
		return(true);
	}else if((price >= mx && (price-100) <= mx)||(price >= mn && (price-100) <= mn)){
		 return(true);
	} else{
		return(false);
	}
}

function checkType(inputType, xmlType){
	//$.each(xmlType, function(i, l){
	
		if(inputType == '0'){
			return(true);
		}else if(inputType == xmlType[0]){
			 return(true);
		} else if(inputType == xmlType[1]){
			 return(true);
		}else if(inputType == xmlType[2]){
			 return(true);
		}else if(inputType == xmlType[3]){
			 return(true);
		}else{
			return(false);
		}
	//});
}

