$(document).ready(function()
{
	var shadowbox_options = ''; //';options={animate:false}';
	
	//---------------------------------------------START CUSTOM JS//
	
	if($('div.media a.page_video').length > 0)
	{
		$('div.media a.page_video').removeAttr('rel');
	}
	
	/************************************** CUSTOM FORMS*/
	
	if($('select#available_county').length > 0)
	{
		if($('select#available_county').val() != 'null' || $('select#available_city').val() != 'null' || $('select#available_region').val() != 'null')
		{
			if($('select#available_county').val() == 'null')
			{
				$('select#available_county option[value="null"]').after('<option class="override" value="N/A">N/A</option>');
				$('select#available_county option.override').attr('selected','selected');
			}
			if($('select#available_city').val() == 'null')
			{
				$('select#available_city option[value="null"]').after('<option class="override" value="N/A">N/A</option>');
				$('select#available_city option.override').attr('selected','selected');
			}
			if($('select#available_region').val() == 'null')
			{
				$('select#available_region option[value="null"]').after('<option class="override" value="N/A">N/A</option>');
				$('select#available_region option.override').attr('selected','selected');
			}
		}
		$('select#available_county').bind('change',function()
		{
			if($(this).val() != 'null' && $(this).val() != 'N/A')
			{
				if($('select#available_city option.override').length == 0)
				{
					$('select#available_city option[value="null"]').after('<option class="override" value="N/A">N/A</option>');
					
					if($('select#available_city').val() == 'null')
					{
						$('select#available_city option.override').attr('selected','selected');
					}
				}
				if($('select#available_region option.override').length == 0)
				{
					$('select#available_region option[value="null"]').after('<option class="override" value="N/A">N/A</option>');
					
					if($('select#available_region').val() == 'null')
					{
						$('select#available_region option.override').attr('selected','selected');
					}
				}
			}
			else
			{
				if($('select#available_city option.override').length > 0)
				{
					$('select#available_city option.override').remove();
				}
				if($('select#available_region option.override').length > 0)
				{
					$('select#available_region option.override').remove();
				}
			}
		});
		$('select#available_city').bind('change',function()
		{
			if($(this).val() != 'null' && $(this).val() != 'N/A')
			{
				if($('select#available_county option.override').length == 0)
				{
					$('select#available_county option[value="null"]').after('<option class="override" value="N/A">N/A</option>');
					
					if($('select#available_county').val() == 'null')
					{
						$('select#available_county option.override').attr('selected','selected');
					}
				}
				if($('select#available_region option.override').length == 0)
				{
					$('select#available_region option[value="null"]').after('<option class="override" value="N/A">N/A</option>');
					
					if($('select#available_region').val() == 'null')
					{
						$('select#available_region option.override').attr('selected','selected');
					}
				}
			}
			else
			{
				if($('select#available_county option.override').length > 0)
				{
					$('select#available_county option.override').remove();
				}
				if($('select#available_region option.override').length > 0)
				{
					$('select#available_region option.override').remove();
				}
			}
		});
		$('select#available_region').bind('change',function()
		{
			if($(this).val() != 'null' && $(this).val() != 'N/A')
			{
				if($('select#available_county option.override').length == 0)
				{
					$('select#available_county option[value="null"]').after('<option class="override" value="N/A">N/A</option>');
					
					if($('select#available_county').val() == 'null')
					{
						$('select#available_county option.override').attr('selected','selected');
					}
				}
				if($('select#available_city option.override').length == 0)
				{
					$('select#available_city option[value="null"]').after('<option class="override" value="N/A">N/A</option>');
					
					if($('select#available_city').val() == 'null')
					{
						$('select#available_city option.override').attr('selected','selected');
					}
				}
			}
			else
			{
				if($('select#available_county option.override').length > 0)
				{
					$('select#available_county option.override').remove();
				}
				if($('select#available_city option.override').length > 0)
				{
					$('select#available_city option.override').remove();
				}
			}
		});
	}
	
	if($('input#current_detail').length > 0)
	{
		$('input#status_yes').bind('change',function()
		{
			if($('input#status_yes:checked') && ($('input#current_detail').val() == '' || $('input#current_detail').val() == 'N/A'))
			{
				$('input#current_detail').val('');
				$('input#current_detail').focus();
			}
		});
		$('input#status_no').bind('change',function()
		{
			if($('input#status_no:checked'))
			{
				$('input#current_detail').val('N/A');
			}
		});
	}
	
	/************************************** CUSTOM FORMS*/
	
	/************************************** MAILING LIST*/
	
	if($('body').attr('id') == 'mailing-list')
	{
		if($('input#email_address').length > 0)
		{
			$('div#content form ol').before('<h1>Join Our Mailing List</h1><p>Complete the form below to join the The Dyslexia SpLD Trust mailing list, or to unsubscribe from our mailing list <a href="http://www.thedyslexia-spldtrust.org.uk/mailing-list/unsubscribe/" title="Unsubscribe From Our Mailing List">click here</a>.</p>');
		}
		else if ($('input#confirm_email').length > 0)
		{
			$('div#content form ol').before('<h1>Unsubscribe From Our Mailing List</h1><p>If you would like to unsubscribe from our mailing list, please complete the form below.</p>');
		}
		else
		{
			$('div#content form h3:first').before('<h1>Thank you for joining our mailing list</h1><p>Please let us know a little more about you by completing the form below.</p>');
		}
	}
	
	/************************************** MAILING LIST*/
	
	/************************************** NAVIGATION*/
	
	if($('a.nav_'+$('body').attr('id')).length > 0)
	{
		$('a.nav_'+$('body').attr('id')).parent().addClass('open');
	}
	
	var subsubnav = $('li.subnav.information ul').html();
	
	$('li.subnav.information').after(subsubnav.replace(/subsubnav/g,"subnav"));
	//$('li.subnav ul').hide();
	$('li.subnav.information').remove();
	
	var location = $('body').attr('class');
	
	if(location != 'undefined' && location != '')
	{
		if($('li.'+location).length > 0)
		{
			$('li.'+location).addClass('active');
			$('li.'+location+' ul').show();
			$('li.'+location+' ul').addClass('current');
		}
		
		var nav_found = 'false';
		
		// Also try and target active page, firstly at subsubnav level
		$('li.subnav ul li a').each(function()
		{
			var cur_href = $(this).attr('href').split('/');
			
			if(cur_href[(cur_href.length - 2)] == location)
			{
				$(this).parent().parent().parent().addClass('active');
				$(this).addClass('active');
				$(this).parent().parent().show();
				$(this).parent().addClass('current');
				
				nav_found = 'true';
			}
		});
		
		// If still not found check at subnav level
		if(nav_found == 'false')
		{
			$('li.subnav.'+$('body').attr('id')+' ul li a').each(function()
			{
				var cur_href = $(this).attr('href').split('/');
				
				if(cur_href[(cur_href.length - 2)] == location)
				{
					$(this).addClass('active');
					
					nav_found = 'true';
				}
			});
		}
	}
	
	var location = $('body').attr('id');
	
	if(location != 'undefined' && location != 'information')
	{
		$('li.'+location+' ul').show();
		$('li.'+location+' ul').addClass('current');
	}
	
	$('li.subnav a').click(function()
	{
		if($(this).parent().hasClass('subnav'))
		{
			if($(this).parent().children('ul').hasClass('open'))
			{
				$(this).parent().removeClass('active');
				$(this).parent().children('ul').removeClass('open');
				$(this).parent().children('ul').hide();
			}
			else if(!$(this).parent().children('ul').hasClass('current'))
			{
				$('li.subnav.active ul').hide();
				$('li.subnav.active').removeClass('active');
				$('li.subnav ul.open').removeClass('open');
				$(this).parent().addClass('active');
				$(this).parent().children('ul').show();
				$(this).parent().children('ul').addClass('open');
			}
			
			return false;
		}
		else
		{
			return true;
		}
	});
	
	/************************************** NAVIGATION*/
	
	//---------------------------------------------END CUSTOM JS//
	/*
	$("li.subnav").hover(function()
	{
		$(this).addClass("active");
		
		if($("a.nav_search").hasClass("active"))
		{
			$("a.nav_search").removeClass("active");
			$("form.search").animate({ opacity: 0 }, 250);
			
			search_height_timeout = setTimeout(function()
			{
				$("form.search").css("height","0");
			}, 1000);
		}
	},
	function()
	{
		$(this).removeClass("active");
	});
	*/
	if($("ul#subnav").length > 0)
	{
		if(!$.browser.msie || $.browser.version != 5.5)
		{
			$("ul#subnav").css("display","none");
		}
	}
	
	$('a[rel*="external"]').click(function()
	{
		open($(this).attr('href'));
		return false;
	});
	
	$('a[rel*="shadowbox"]').each(function()
	{
		if(!$(this).hasClass('page_video'))
		{
			var enlarge_img = $(this).attr('rel').split(' ');
			
			if(enlarge_img != 'undefined' && enlarge_img != '')
			{
				$(this).removeAttr('rel');
				
				if(enlarge_img[1] != 'undefined')
				{
					$(this).attr('rel', enlarge_img[1]+shadowbox_options);
				}
				else
				{
					$(this).attr('rel', enlarge_img[0]+shadowbox_options);
				}
			}
		}
	});
	
	$("ul.thumbs li a img").each(function()
	{
		$.preloadImages.add($(this).attr("src").replace("thumb", "mid"));
	});
	
	$("ul.thumbs li a").mouseover(function()
	{
		$("img#full").attr("src", ($(this).attr('href')).replace("full", "mid"));
		$("img#full").attr("alt", ($(this).attr('alt')));
		$("a#enlarge").attr("href", ($(this).attr('href')).replace("thumb", "full"));
		$("a#enlarge").attr("title", ($(this).attr('title')));
	});
	
	$("ul.thumbs li:first a").click(function()
	{
		$("a#enlarge").click();
	});
	
	$("ul.thumbs li a").click(function()
	{
		$("img#full").attr("src", ($(this).attr('href')).replace("full", "mid"));
		$("img#full").attr("alt", ($(this).attr('alt')));
		$("a#enlarge").attr("href", ($(this).attr('href')).replace("thumb", "full"));
		$("a#enlarge").attr("title", ($(this).attr('title')));
		return false;
	});
	
	if($('a.page_video').length > 0)
	{
		var page_video = $('a.page_video').attr('rel').split(' ');
		
		if(page_video != 'undefined' && page_video != '')
		{
			$('a.page_video').removeAttr('rel');
			$('a.page_video').attr('rel', page_video[1]);
		}
	}
	
	if($("li.help a").length > 0)
	{
		$("li.help a").click(function()
		{
			return false;
		});
		
		var help_timeout = 'undefined';
		
		$("li.help a").bind("mouseenter",function()
		{
			if(help_timeout != 'undefined')
			{
				clearTimeout(help_timeout);
			}
			$("li.help div").fadeOut();
			$(this).next().addClass('active').fadeIn();
		}).bind("mouseleave",function()
		{
			help_timeout = setTimeout(function()
			{
				$('li.help div.active').removeClass('active').fadeOut();
			},2000);
		});
		
		$('input').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
		
		$('select').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
		
		$('textarea').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
	}
});