/* CottageCountry.com Search */
var sV = 'Type to Search...';

window.addEvent('domready',function(){
	positionSearch();
	var timer = false;
	if($('sBox') && $('sRes')){
		
		$('sBox').addEvent('focus', function(e){
			var sVal = $('sBox').get('value');
			if(sVal == sV){
				$('sBox').set('value','');	
			}
		});
		
		$('sBox').addEvent('blur', function(e){
			var sVal = $('sBox').get('value');
			if(sVal == ''){
				$('sBox').set('value',sV);	
			}
		});
		
		
		
		$('sBox').addEvent('keyup', function(e) {
			e.stop(); 
			var sVal = $('sBox').get('value');
			if(timer) {
				$clear(timer);
			}
			timer = (function(){		  
				showSearch();		  
				var req = new Request.HTML({url:xhrSearchBar,method:'post',
					onRequest: function() {
						$('sRes').empty().addClass('load');
					},
					onSuccess: function(html,elements,thehtml) {
						if(html){
							$('sRes').adopt(html);	
						} else {
							hideSearch();
						}
					},
					onFailure: function() {
						$('sRes').removeClass('load');
						$('sRes').set('txt','There was an error.');
					}
				}).send('s='+sVal);
			}).delay(800, this);
		}.bind(this));
	}
});
function showSearch(){
	if($('sAd')){
		$('sAd').setStyle('display','none');	
	}
	if($('sRes')){
		$('sRes').fade(1);
		$('sRes').addEvent('outerClick',function(e){
			hideSearch();										
		});
	}
}
function hideSearch(){
	if($('sAd')){
		$('sAd').setStyle('display','block');	
	}
	if($('sRes')){
		$('sRes').fade(0);
		$('sRes').removeEvents();
	}
}
function positionSearch(){
	if($('sRes')){
		$('sRes').fade('hide');
	}
	if($('sCont') && $('sRef')){
		var refPos = $('sRef').getPosition();
		$('sCont').setStyles({
			top: refPos.y + 17,
			left: refPos.x - 183,
			display: 'block'
		});
	}
	$('sBox').set('value',sV);
}
