// JavaScript Document

$(document).ready(function()
{
	//$('.menu_lg').removeClass('no_script');
	
	init_ajax_form('form.ajax_form');

	$('.center_content .col_right .contact .details').each(function()
	{
		if($(this).find('.contact_email_span').width() > $(this).width())//check width of email against width of parent email div
		{
			var email = $(this).find('.contact_email_span a').html();
			$(this).find('.contact_email_span a').html(email.replace("@", "<br/>@"));
		}/**/
	});
	
	if($('.lead_blue_bar .title').height() > 30) $('.lead_blue_bar .title').css({'margin-top':'35px'});
	if($('.lead_blue_bar .title').height() > 60) $('.lead_blue_bar .title').css({'margin-top':'18px'});
	
	$('.staff_float').equalHeights();

	// cycle images in slideshow
	$('.slideshow').cycle({
		fx:      'fade',
		timeout:  6000,
		speed: 2000
	});
	
	// hide the text in the search bar
	$('.hide_text').each(function()
	{
		document['def_'+$(this).attr('name')] = $(this).attr('value');
		$(this).focus(function()
		{
			if($(this).attr('value') == document['def_'+$(this).attr('name')]) $(this).attr('value','');
		});
		$(this).blur(function()
		{
			if($(this).attr('value') == '') $(this).attr('value',document['def_'+$(this).attr('name')]);
		});
	});
	
	$('img[src$=.png]').ifixpng();
	
	$("hr").wrap("<div class='hr'></div>");
	
	// add a pick background to top menu (this causes 2nd level to break)
	$('.menu_lg ul li').hover(
		function() {
			$(this).css({
				"background-color": "#E0166B"
				});
		},
		function() {
			$(this).css({
				"background-color": "transparent"
				});
		}
	);
	
	// fix bug from above
	$('.menu_lg ul li ul').each(function(){
		$(this).find('li').hover(function(){
			$(this).css({
				"background-color": "#E0166B"
			});
		},function(){
			$(this).css({
				"background-color": "#E0166B"
			});
		});
	});
	
	// hide all the menu's on load
	$('.menu_lg ul li ul').hide();
	
	// show/hide menus
	$('.menu_lg ul li').hover(function(){
		$(this).find('ul').show();
		$(this).find('ul').find('ul').hide();
	},function(){
		$(this).find('ul').hide();
	});
	
	// large menu drop down
	$('.menu_lg ul li ul').each(function() {
	
		// if this is a child, add an arrow to the parent
		$(this).find('li ul').parent().css({
			"background-image": "url(/images/arrows.png)",
			"background-position": "right center",
			"background-repeat": "no-repeat"});
		
		// add a fade while hovering over menu item
		$(this).find('li ul').parent().hover(function(){
			$(this).css({
				"background": "#E0166B none",
				"background-position": "right top",
				"background-repeat": "repeat-y"});
		},
		// place the arrow back on the menu
		function() {
			$(this).css({
				"background-image": "url(/images/arrows.png)",
				"background-position": "right center",
				"background-repeat": "no-repeat"});
		});
	});
	
	$('a#print_screen').click(function(ev){
			ev.preventDefault();
			print();
	});
		
	if($('.main_text_left').height()+240 > $(window).height())
	{
		$('.main_text_left').append('<p class="back_to_top"><a href="#">Back to top</a></p>');
	}
});

function init_ajax_form(selector)
{
	$(selector).append('<input type="hidden" name="ajax" value="true" />');
		
	$(selector).submit(function() {
	  	// Serialise the form data
	  var value = $(this).find('.ajax_staff_name').attr('value');
	  value = escape(value);
	  var url = '/includes/ajax_search_staff/' + value + '/';
		var form = $(this);
		
		$('.ajax_output').load(url,function(data){
			init_ajax_form(selector);
		});
		
		// Don't submit the form
	 	return false;
	});	
}
