var busy = false;
	
function applyAjaxSearch(){	
	
	window.addEvent('domready',function(){	
		$('property-search-form').addEvent('submit',function(){
			
			if(!busy){	
				busy = true;						
				var propertytypeid = $('propertytypeid').value;
				var clusterid = $('clusterid').value;
				var postcodeid = $('postcodeid').value;
				var developmentstatusid = $('developmentstatusid').value;
				var searchText = $('keywords').value;
			    	
				var time = $random(0,100) - $time();
				var myHTMLRequest = new Request.HTML({
					url:'/include/fn_getProperties.asp',
					onComplete:function(responseTree, responseElements, responseHTML, responseJavaScript){
						//alert('here ' + busy);
						resultsContainer = $('search-results');
						resultsContainer.set('html',responseHTML);
						if($$('#search-results .prop-search-res').length > 0 ){										
							fadeInProperty(0)
						}
						
						/*
						resultsContainer.set('tween',{
							duration:'500',
							onComplete:function(){						
								resultsContainer.set('html',responseHTML);
								//$$('#search-results .prop-search-res').setStyle('visibility','hidden');
								//resultsContainer.setStyles({'visibility':'visible'});
								//resultsContainer.removeStyle('opacity');
								
								if($$('#search-results .prop-search-res').length > 0 ){			
									
									fadeInProperty(0);
								}
							}
						});
						resultsContainer.tween('opacity',[1,0]);
						*/
						
						//update the search heading					
						var tStr = '';
						if(propertytypeid != ''){
							tStr = '<span class="search-param">' + $$('#propertytypeid option')[$('propertytypeid').selectedIndex].get('text') + '</span>';
						}
						if(clusterid != ''){
							tStr != '' ? tStr+= ' + ': 0;
							tStr += '<span class="search-param">' + $$('#clusterid option')[$('clusterid').selectedIndex].get('text') + '</span>';
						}
						if(postcodeid != ''){
							tStr != '' ? tStr+= ' + ': 0;
							tStr += '<span class="search-param">' + $$('#postcodeid option')[$('postcodeid').selectedIndex].get('text') + '</span>';
						}
						if(developmentstatusid != ''){
							tStr != '' ? tStr+= ' + ': 0;
							tStr += '<span class="search-param">' + $$('#developmentstatusid option')[$('developmentstatusid').selectedIndex].get('text') + '</span>';
						}
						if(searchText != 'Keyword(s)' && searchText != undefined){
							tStr != '' ? tStr+= ' + ': 0;
							tStr += '<span class="search-param">"' + searchText + '"</span>';
						}
												
						if($('psr-search-title')){
							$('psr-search-title').innerHTML = tStr == '' ? '<h2>Search results for: <span class="search-param">All properties</span></h2>' : '<h2>Search results for: ' + tStr + '</h2>';
						}
						Cufon.refresh('#psr-search-title h2');
						
						
						
					},onError:function(){busy=false;alert('error fetching search results');}
				}).post({
					'propertytypeid': propertytypeid,
					'clusterid': clusterid,
					'postcodeid': postcodeid,
					'developmentstatusid': developmentstatusid,
					'searchText':searchText,
					'time':time});		
				busy = false;
			}
			return false;
		});
	});
}



window.addEvent('domready',function(){
	if($('search-results')){
		applyAjaxSearch();
		
		//apply sort function to arrows
		$('sort-title-up').addEvent('click',function(){sortResults('title',1);return false;});
		$('sort-title-down').addEvent('click',function(){sortResults('title',0);return false;});
		$('sort-cluster-up').addEvent('click',function(){sortResults('cluster',1);return false;});
		$('sort-cluster-down').addEvent('click',function(){sortResults('cluster',0);return false;});
		$('sort-postcode-up').addEvent('click',function(){sortResults('postcode',1);return false;});
		$('sort-postcode-down').addEvent('click',function(){sortResults('postcode',0);return false;});
	}

	//IE bug where load event is fired before DOM ready
	if(!Browser.Engine.trident || (Browser.Engine.trident && Browser.Engine.version > 5)){
		$$('#search-results .prop-search-res').setStyle('visibility','hidden');		
	}
});

window.addEvent('load',function(){
	
	//IE bug where load event is fired before DOM ready		
	if(Browser.Engine.trident && Browser.Engine.version <= 5){			
		$$('#search-results .prop-search-res').setStyle('visibility','hidden');		
	}
	
	if($$('#search-results .prop-search-res').length > 0 ){			
		fadeInProperty(0);
	}
});

function fadeInProperty( index ){			
	//if(index < 1){alert('fade in');}
	if($$('#search-results .prop-search-res')[index]){
		if(!Browser.Engine.trident || (Browser.Engine.trident && Browser.Engine.version > 5)){
			//if(index < 1){alert('fade in moz');}
			$$('#search-results .prop-search-res')[index].set('tween',{duration:'normal'});
			$$('#search-results .prop-search-res')[index].tween('opacity',[0,1]);
			if($$('#search-results .prop-search-res').length > (index + 1) ){
				(function(){fadeInProperty(index+1)}).delay(50);
			}
		}else{			
			$$('#search-results .prop-search-res')[index].setStyle('visibility','visible');
			if($$('#search-results .prop-search-res').length > (index + 1) ){
				(function(){fadeInProperty(index+1)}).delay(50);
			}
		}
	}
	
}

function sortResults(sortBy,dir){
	
	//remove black selected arrow class from all sort arrows
	$$('.sort-up-selected').each(function(item,index){item.removeClass('sort-up-selected');item.addClass('sort-up');});
	$$('.sort-down-selected').each(function(item,index){item.removeClass('sort-down-selected');item.addClass('sort-down');});
	
	//create sortable array and add selected class to appropriate arrow
	var res = $$('#search-results .prop-search-res').map(function(item,index){
		
		switch( sortBy ){
			case 'postcode':						
				dir==1 ? $('sort-postcode-up').addClass('sort-up-selected') : $('sort-postcode-down').addClass('sort-down-selected');
				return new Array(item.getElement('div.psr-postcode').get('text') + item.getElement('div.psr-title a').get('text'),index);
				break;
			case 'cluster':			
				dir==1 ? $('sort-cluster-up').addClass('sort-up-selected') : $('sort-cluster-down').addClass('sort-down-selected');
				return new Array(item.getElement('div.psr-cluster').get('text') + item.getElement('div.psr-title a').get('text'),index);
				break;
			default:
				dir==1 ? $('sort-title-up').addClass('sort-up-selected') : $('sort-title-down').addClass('sort-down-selected');
				return new Array(item.getElement('div.psr-title a').get('text'),index);
		}	
	});			
	
	//sort array	
	res = res.sort();
	//set direction of sort
	if(dir == 1){
		res = res.reverse();
	}
	//reduce original array down to just the position index of the item
	res = res.map(function(item,index){return item[1];});
	//rearrange items without animation
	var resSort = new Fx.Sort($$('#search-results .prop-search-res'));
	//resSort.sort(res);
	resSort.rearrangeDOM(res);
}