$(document).ready(function(){
	
	// Home page testimonials
	
	if( $("#home_testimonials ul li").length ){
		
		var max_testimonial_height = 0;
		
		$("#home_testimonials ul li").each(function(){
			if( $(this).height() > max_testimonial_height ){
				max_testimonial_height = $(this).height();
			}
		});
		
		$("#home_testimonials ul").height( max_testimonial_height );
		
		$("#home_testimonials li:not(:first)").hide();
		
		if( $("#home_testimonials ul li").length > 1 ){
			
			setInterval(function(){
				
				$("#home_testimonials ul li:eq(0)").fadeTo( 500, 0, function(){
					$(this).appendTo( $("#home_testimonials ul") );
				});
				
				$("#home_testimonials ul li:eq(1)").fadeTo( 500, 1 );
				
			}, 6500);
			
		}
		
	}
	
	// Services menu
	
	$("li#menu_what-we-do").hover(function(){
		$(this).addClass("hover");
		$("#services_outer").css("height","auto");
	},function(){
		$(this).removeClass("hover");
		$("#services_outer").css("height","0");
	});
	
	// Home partners
	
	if( $("#home_hpanel_center_pic li").length > 1 ){
		
		var scrolling_people = false;
		
		var autoscroll = setInterval( function(){
			scroll_next();
		}, 4000 );
		
		$("#partners_prev").click(function(){
			
			if( autoscroll ){
				clearInterval( autoscroll );
			}
			
			scroll_prev();
			
			return false;
		});
		
		$("#partners_next").click(function(){
			
			if( autoscroll ){
				clearInterval( autoscroll );
			}
			
			scroll_next();
			
			return false;
		});
		
		function scroll_next(){
			
			if( scrolling_people ){
				return false;
			}
			
			scrolling_people = true;
			
			$("#home_hpanel_center_pic ul").css("left","0");
			
			$("#home_hpanel_center_pic ul").animate({
				left: "-270px"
			}, 400, "swing", function(){
				$(this).css("left","0").find("li:first").appendTo("#home_hpanel_center_pic ul");
				scrolling_people = false;
			});
			
		}
		
		function scroll_prev(){
			
			if( scrolling_people ){
				return false;
			}
			
			scrolling_people = true;
			
			$("#home_hpanel_center_pic ul").css("left","-270px").find("li:last").prependTo("#home_hpanel_center_pic ul");
			
			$("#home_hpanel_center_pic ul").animate({
				left: "0"
			}, 400, "swing", function(){
				scrolling_people = false;
			});
			
		}
		
	}
	
	// Date select
	
	$("#f_bookappointment_date").datepicker({
		dateFormat: "yy-mm-dd",
		changeYear: true,
		changeMonth: true,
		yearRange: '-0:+1',
		monthNamesShort: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
	});
	
	// Ajax - Go Back
	
	$("a.go_back").live("click",function(){
		
		$(this).closest("div.submit_status").fadeOut( 200 ).prev("div.fields").fadeIn( 200 );
		
		return false;
		
	});
	
	/* Newsletter */
	
	$("#newsletter").submit(function(){
		
		var form = $(this);
		
		form.height( form.height() );
		
		form.children("div.fields").fadeOut( 200 ).next("div.submit_status").html('<p class="status status_alert">Sending message, please wait...</p>').fadeIn( 200 );
		
		$.ajax({
			type: form.attr('method'),
			url: form.attr('action'),
			data: form.serialize(),
			success: function( data, textStatus, XMLHttpRequest ){
				alert(data);
				if( data == "1" ){
					form.children("div.submit_status").html('<p class="status status_success">Thanks for subscribing!</p>');
				}else{
					form.children("div.submit_status").html('<p class="status status_error">Email address invalid.<br /><br /><a href="" class="go_back">Go Back</a></p>');
				}
				
			},
			error: function(){
				form.children("div.submit_status").html('<p class="status status_error">Error sending message, please try again.<br /><br /><a href="" class="go_back">Go Back</a></p>');
			}
		});
		
		return false;
		
	});
	
});

