
window.addEvent('domready', function() 
{
	
	var tabs = $$('#mainNav a');
	
	tabs.each(function(tab, index) 
	{	
		//tab.addEvent('mouseenter', onMouseEnter);
		//tab.addEvent('mouseleave', onMouseLeave);
		
		tab.addEvent('click', onClick);
		
		tab.style.outline = 'none';
	
	});
	
	// Remove all outlines on .button links (those using an backgroudn image and a negative text indent)
	$$('a.button').each(function(button, index) { button.style.outline = 'none'; });
		
}); 



function onMouseEnter(event) { }
function onMouseLeave(event) { }


function onClick(event)
{
	event.stop();
	
	var section = event.target.title.toLowerCase();
	var div = $('content');
	div.fade('hide');
	
	var ajax = new Request.HTML( { url: 'content.php', 
									method: 'get', 
									autoCancel: true, 
									onSuccess: function(html) 
									{			
										// clear the current content 
										// and add the results of the new request
										$('content').set('text', '');
										$('content').adopt(html);
										
										// get a reference to the tab that was clicked
										var tab = event.target;
										
										// loop through all tabs and remove selected state styles
										var tabs = $$('#mainNav li a');
										tabs.each( function(tab, index) 
										{
											tab.className = ''; 
											tab.style.color = '#484848';
										});
										
										// set selected styles on the new active tab
										tab.className = 'selected';
										tab.style.color = '#8c9c00';
										
										init(section);
										div.fade(1);
										
									} });
	
	ajax.send("section=" + section);
}


function init(section)
{
	switch(section)
	{
		case 'contact':
		
			//resetFormFields();
			$('form').addEvent('submit', onSubmit);
			break;
		
	}
}



function onSubmit(event)
{
	event.stop();
	var valid = true;
	
	var fields = $$('#form .required');
	
	fields.each(function(field, index) 
	{	
		if(field.get('value') == '')
		{
			valid = false;
			field.addClass('alert');
		}
	});
	
	
	if(!valid)
	{
		alert('Please complete all required fields.');
		return false;
	}
	else
	{	
		$('processing').addClass('ajax-loading');
		
		this.set('send', { onComplete: function(response) 
										{ 
											$('processing').removeClass('ajax-loading');
											$('confirmation').highlight('#eceae8');
											$('confirmation').set('html', response);
											
											// reset all the form fields
											$$('#form .text').each(function(field, index) { field.set('value') == ''; } );
										} });
		this.send("form.php?ajax=true");
	}
}