$(document).ready(function(){
	
	$("#floatingBoxCloseBtn").live("click", function() {   $('#floatingBox').fadeOut('slow'); });
	$('#seachsite').hint();
	$('span.mailme').mailme();
	
	// ----------------------------------------------------------------------------------------
	// Detect a change of uri
	// ----------------------------------------------------------------------------------------
	$.address.change(function(event) {  
    	var addrVal = event.value;
    	if(addrVal != '/') {
    		var arrAdd = addrVal.split("/");
    		if(arrAdd[1] == 'query') searchSite( arrAdd );
    	}  
	});

	// ----------------------------------------------------------------------------------------
	// Search Request
	// ----------------------------------------------------------------------------------------
	function searchSite(arrAdd){
		$('#loadingSearch').show();
		
		if( arrAdd[2]!='' && arrAdd[2]!='Search Site' ) {
			var mystart= '';
			if(arrAdd[4]!=null)
				mystart = arrAdd[4];
			
			$.post(url,{ sect:'search', method:'ajax', query:arrAdd[2], start:mystart }, function(content) { 
				$('#search_results_content').html(content) ;
			});
			
			// move back to the top of the page	
		  	var destination = $('#header').offset().top;
		   	$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 500 );
		   	$('#loadingSearch').animate({opacity: 1.0}, 1000).fadeOut('fast');
		   	return false;	
		}
	}
	
	$("#seachsiteBtn").live("click", function() {
		window.location = '/search/#/query/'+$('#seachsite').val();
	});
	
	
	
	$("#topBtn").click(function() {               
		var destination = $('#header').offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 500 );
		   	
	
	});
	
	
	// ----------------------------------------------------------------------------------------
	// Contact Form
	// ----------------------------------------------------------------------------------------
	$("#contactSendBtn").click(function() {               
		$('#errors').fadeOut('fast');
		errs ='';
		if($('#name').val() == '') errs += 'Please enter your name<br />';
		if($('#email').val() == '') errs += 'Please enter your email address<br />';
		if($('#message').val() == '') errs += 'Please enter a message<br />';
		
		if(errs =='') {
			$.post(url,{ sect:'contact', p:'index', method:'ajax', submit:'', name:$('#name').val(), email:$('#email').val(), 
						 message: $('#message').val() }, function(content) {
				if(content.search('error')<0/*  && content !='' */) {
					$('#name').val('');
					$('#email').val('');
					$('#message').val('');
					$('#AjaxSuccess').html('Your message has been sent').centerInClient().show().animate({opacity: 1.0}, 2000).fadeOut('slow')  ;
				}
				else{
					// server side errors
					content = content.replace("error - ", "");
					$('#errors').html(content).fadeIn('slow');
				}
				return false;
			});
		}
		else{
			$('#errors').html(errs).fadeIn('slow');
		}
	});
});
