function clearText(thefield){
	if (thefield.defaultValue==thefield.value) {
		thefield.value = "";
	}
}

var expand= function() {
	$('.extract').css('opacity','0');
	
	$('.expand').bind('click keypress', function(e) {
		switch (e.keyCode) {
			case 9:
			case 16:
			case 18:
			case 17:
				return;
			default:
				var expandCollapseLink = $(this);
				
				if(!$(this).parent().siblings('.extract').hasClass('collapsed')) {
					$(this).parent().siblings('.extract').animate({opacity: 0}, 500, function () {
						$(this).addClass('collapsed').hide();
						
						expandCollapseLink.html('Read extract');
						expandCollapseLink.removeClass('expand-collapse');
					});
					
				}
				else {
					$(this).parent().siblings('.extract').show().animate({opacity: 1}, 500, function (){
						$(this).removeClass('collapsed');

						expandCollapseLink.html('Hide extract');
						expandCollapseLink.addClass('expand-collapse');
					});
						
				}
				return false;
		}
	});
}

var accordian = function() {
    var text;

    $('.accordian .subhead').each(function() {

        /*text= $('h3', $(this)).text();
        $('h3', $(this)).remove();
        $('.accordian .subhead').append('<h3><a href="#">' +text+ '</a></h3>');
        text="";*/

        $('h3', $(this)).wrapInner('<a href="#"></a>');

        $(this).bind('click keypress', function(e) {
            switch (e.keyCode) {
                case 9:
                case 16:
                case 18:
                case 17:
                    return;
                default:
                    if ($(this).next('.accordian-content').hasClass('accordian-content-expanded')) {
                        $(this).next('.accordian-content').slideUp(300);
                        $(this).next('.accordian-content').removeClass('accordian-content-expanded');
                        $(this).removeClass('subhead-expanded');
                        $(this).find('a.expand img').attr("src", "/img/subhead-expand.gif")

                    }
                    else {
                        $(this).next('.accordian-content').addClass('accordian-content-expanded');
                        $(this).addClass('subhead-expanded');
                        $(this).next('.accordian-content').slideDown(300);
                       $(this).find('a.expand img').attr("src" , "/img/subhead-close.gif")
                     
                    }
            }
            return false;
        });
    });

}

var searchTabs = function() {
	var tabs = $('.search-tabs a');
	var contents = $('.search-content > form > div');
	
	// Provide a hook for styles specific to the JavaScript version
	$('.search-content').addClass('search-content-js');
	
	// Hide all contents and disable all form fields
	contents.hide();
	contents.find('input, select').attr('disabled', 'disabled');
	
	// Show the first tab and enable its form field
	$(tabs.get(0)).parent().addClass('active');
	$(tabs.get(0).hash).removeAttr('disabled');
	$(tabs.get(0).hash).parent().show();
	
	tabs.bind('click', function(){
		// Unactivate whichever tab is currently active
		tabs.each(function(){
			$(this).parent().removeClass('active');
		});
		
		// Activate the current tab
		$(this).parent().addClass('active');
		
		// Hide all contents
		contents.hide();
		
		// Disable all form fields, so that the server only receives a value for the last field the user enters a value for
		contents.find('input, select').attr('disabled', 'disabled');
		
		// Enable the form field for the active tab
		$(this.hash).removeAttr('disabled');
		
		// Show the contents for the active tab
		$(this.hash).parent().show();
		
		return false;
	});
}


var navigation = function() 
{
	$('span.primary-section').each
	(
		function()
		{
			$(this).replaceWith( '<a href="#" class="primary-section heading">' + $(this).text() + '</a>');
		}
	)
	$('#nav').css('height', '31px');
	$('#nav > ul').css('position', 'absolute');
	$('#nav ul ul a').bind
	(
		'mouseover focus',
		function()
		{
			$(this).parent().parent().removeClass('hidden');
			$(this).parent().parent().parent().addClass('active-item');
		}
	)
	$('#nav ul ul a').bind
	(
		'mouseout blur',
		function()
		{
			$(this).parent().parent().addClass('hidden');
			$(this).parent().parent().parent().removeClass('active-item');
		}
	)
	$('a.primary-section').bind
	(
		'mouseover focus',
		function()
		{
			$(this).next().removeClass('hidden');
			$(this).parent().addClass('active-item');
		}
	)
	$('a.primary-section').bind
	(
		'mouseout blur',
		function()
		{
			$(this).next().addClass('hidden');
			$(this).parent().removeClass('active-item');
		}
	)
	$('li.submenu a.primary-section').bind
	(
		'click',
		function()
		{
			return false;
		}
	)
	$('#nav li ul').addClass('hidden');
}



$(document).ready(function(){
	$('body').addClass('js');
	if($('.expand').length>0){expand()};
	if($('.accordian').length>0){accordian()};
	if($('.search-tabs').length>0){searchTabs();};
	if($('nav')){navigation();};
});




