var CurrentItem=1;
var MaxItems;
var AutoPlay = true;
var Timer;

$(document).ready(
	function(){
		if($('#ScrollerCropArea').size()>0){			
			//InitScroller();
			$('#ScrollerItems').cycle({
				fx:     'scrollHorz', 
				prev:   '#carousel-left', 
				next:   '#carousel-right', 
				timeout: 8000
			});

		}
		

		$(".sub_section h3").click(function() {
			$(".accordian_body").hide();
			$(".sub_section h3").removeClass("alt");
			$(this).addClass("alt");
			$(this).next().show();
		});

		$('a[rel="pj-external"]').click (function() {
			answer = confirm("This link will open a new window which is not part of the Pearson Jones plc website and we therefore have no control over its content.");
			if (answer) {
				window.open($(this).attr('href'));
			}
			return false;
		});

		$('a[rel="pfc-external"]').click (function() {
			answer = confirm("This link will open a new window which is not part of the Parnell Fisher Child website and we therefore have no control over its content.");
			if (answer) {
				window.open($(this).attr('href'));
			}
			return false;
		});

		$('a[rel="external"]').click (function() {
			answer = confirm("This link will open in a new window.");
			if (answer) {
				window.open($(this).attr('href'));
			}
			return false;
		});		

		
		$("#content-sub-nav ul.news-archive li span").click(function() {
			$(this).siblings().slideToggle();
		});

		$(".wpcf7 input[name='name']").click(function() {
			if($(this).val() == "Name") {
				$(this).val("");
			}
		});

		$(".wpcf7 input[name='name']").blur(function() {
			if($(this).val() == "") {
				$(this).val("Name");
			}		
		});

		$(".wpcf7 input[name='email']").click(function() {
			if($(this).val() == "Email") {
				$(this).val("");
			}
		});

		$(".wpcf7 input[name='email']").blur(function() {
			if($(this).val() == "") {
				$(this).val("Email");
			}		
		});

		$(".wpcf7 input[name='contact-number']").click(function() {
			if($(this).val() == "Contact number") {
				$(this).val("");
			}
		});

		$(".wpcf7 input[name='contact-number']").blur(function() {
			if($(this).val() == "") {
				$(this).val("Contact number");
			}		
		});

		$(".wpcf7 textarea").click(function() {
			if($(this).val() == "Enter your message") {
				$(this).val("");
			}
		});

		$(".wpcf7 textarea").blur(function() {
			if($(this).val() == "") {
				$(this).val("Enter your message");
			}		
		});						
	}
);

function InitScroller(){
	MaxItems = $('#ScrollerItems li').size();
	ToggleArrows();

	$('#ScrollerItems').append('<li>'+$('#ScrollerItems li:first').html()+'</li>');
	
	$('#carousel-right').click(
		function(){
			RightButtonClick();
			clearInterval(Timer);
			AutoPlay=false;			
		}
	);
	$('#carousel-left').click(
		function(){
			AutoPlay = false;
			if(!$(this).hasClass('Off')){
				ScrollItems('Left');
				CurrentItem--;
			}
			ToggleArrows();
			clearInterval(Timer);
			AutoPlay=false;
		}
	);

}

function RightButtonClick(){
	if((CurrentItem<MaxItems) || (AutoPlay == true)){
		ScrollItems('Right');
		CurrentItem++;
		if(CurrentItem>=0){
			$('#carousel-left').addClass('On');
		}
	}
	ToggleArrows();
}

function ScrollItems(Dir){
	if(Dir=='Left'){
		$('#ScrollerItems').animate(
			{
				left	:	'+=800px'
			}
			,500
		)
		
	}
	else{
		$('#ScrollerItems').animate(
			{
				left	:	'-=800px'
			}
			,500
		)
	}
}

function ToggleArrows(){
	if (CurrentItem==1){
		$('#carousel-left').addClass('Off');
	}
	else{
		$('#carousel-left').removeClass('Off');
	}
	if (CurrentItem==MaxItems){
		$('#carousel-right').addClass('Off');
	}
	else{
		$('#carousel-right').removeClass('Off');
	}
}

function autoScroller() {
	if (CurrentItem > MaxItems) {
		CurrentItem = 1;
		$('#ScrollerItems').css("left","0");
		setTimeout(
			function(){
				RightButtonClick();
			}
			,100
		);
		
	} else {
		RightButtonClick();
	}
}


