//Carry out things when the page is loaded
$(document).ready(function(){					   
	var myTimer = {}; 
	var timer;
	
	
	//DO NOT USE KEYWORD VAR TO DEFINE OBJECT AS IT DOESN'T THEN WORK IN THE FUNCTIONS BELOW AS IT IS NOT TRULY GLOBAL
	sliding = {};
	sliding.inProgress = false;
	sliding.currentElement = null;
	
	
	
	/*
	Client doesn't want homepage slide to scroll automatically.
	//Promo slide show for homepage
	pause = 10000;
	rotatingInterval = setInterval(nextPromoSlide, pause);
	*/
	
	$('#promoBoxOne h2').click(function(){
		setSlidePromo(1);	
	});
	
	$('#promoBoxTwo h3').click(function(){
		setSlidePromo(2);
	});
	
	$('#promoBoxThree h4').click(function(){
		setSlidePromo(3);
	});
	
	
	//Needed as CK editor put in inline styles so have to strip them out.
	$('#mainContent h2, #mainContent h3, #mainContent h4, #mainContent h5, #mainContent h6').each(function(){
		$(this).removeAttr('style');		   
	});
	
	
	//problem page survey
	$('#tackledFeedbackSurvey').click(function(){
		var userAnswer = $('#problemSurveyForm input:radio:checked').val();
		var currentPage = $('#problemSurveyForm input[name=pageTitle]').val();
		$.get('/feedback/setTellUsYourThoughts/'+userAnswer+'/'+currentPage, function(responseData){
			if( responseData == 'true' ){
				$('#problemSurvey').html('<p class="mediumHeading mainColour bold">Thank you for the feedback</p>');
			}
			else{
				alert('We\'re very sorry but we are unable to log your feedback at the moment. Please try again and if the problem persists contact us. Thank you for your support.');	
			}
		})
		return false;
	});
	
	
	//Homepage survey
	$('#submitSurvey').click(function(){
		var userAnswer = $('#homeSurvey input:radio:checked').val();
		var currentPage = $('#homeSurvey input[name=pageTitle]').val();
		if(userAnswer === undefined){
			alert('Please pick an option before submitting');
			return false;
		}
		$.get('/feedback/setTellUsYourThoughts/'+userAnswer+'/'+currentPage, function(responseData){
			if( responseData == 'true' ){
				if(userAnswer == 'tackled'){
					$('#homeInfoBoxTwo').html('<p class="mainColour homeInfoBoxTitle" style="line-height:1.3em;">Thank you for your feedback. <br /><br />We are pleased you found the information helpful. Please spread the word about this site so others can tackle their problems too.</p>');
				}
				else if( userAnswer == 'tackling' ){
					$('#homeInfoBoxTwo').html('<p class="mainColour homeInfoBoxTitle" style="line-height:1.3em;">Thank you for your feedback and keep at it! <br /><br />We welcome your <a style="color:#FC5615;" href="mailto:info@healthpress.co.uk" title="Comment">comments</a>, so that we can continue to improve the site and help more people tackle their problems.</p>');
				}
				else{
					$('#homeInfoBoxTwo').html('<p class="mainColour homeInfoBoxTitle" style="line-height:1.3em;">Thank you for your feedback. <br /><br />We are sorry you\'re still struggling. Please <a style="color:#FC5615;" href="mailto:info@healthpress.co.uk" title="Comment">tell us</a> how we can improve the site to help you tackle your problem. Keep at it!</p>');
				}
				
			}
			else{
				alert('We\'re very sorry but we are unable to log your feedback at the moment. Please try again and if the problem persists contact us. Thank you for your support.');	
			}
		})
		return false;
	});
	
	
	//Comment form on pages 
	$('#submitPageComment').click(function(){								   
		var userName = $('#problemCommentForm input[name=displayName]').val();
		var userAge = $('#problemCommentForm select[name=age]').val();
		var userSex = $('#problemCommentForm select[name=sex]').val();
		var userCountry = $('#problemCommentForm select[name=country]').val();
		var userCommentText = $('#problemCommentForm textarea[name=commentText]').val();
		var currentPage = $('#problemCommentForm input[name=pageTitle]').val();
		
		if(userCountry == ''){
			alert('Please tell us what country you are in');
			return false;
		}
		
		if(userCommentText == ''){
			alert('You have to at least write something in the comment box! Remember don\'t be embarrassed!');
			return false;
		}
		
		$.post("/feedback/setPageComment/", { name:userName, age:userAge, sex:userSex, country:userCountry, comment:userCommentText, page:currentPage },
	   		function(data){
				if( data == 'true' ){				
				$('#problemCommentForm').fadeOut('slow', function(){
					$(this).remove();
					$('#addComment').html('<p class="largeHeading mainColour">Thank you for your comment</p><p>Please bear in mind that we moderate all comments before publishing so there is a delay between you submitting your comment and it showing up, assuming we deem it appropiate.</p>');
				});
			}
			else{
				alert('We\'re very sorry but we are unable to log your comment at the moment. Please try again and if the problem persists contact us. Thank you for your support.');	
			}
	   });

		return false;
	});
	
	
	//Highlight or remove text in search box
	$('#queryText').focus(function(){
		if( $(this).val() == 'Search for your problem...' ){
			$(this).val('');
		}
		else{
			$(this).select();
		}
	});
	
	
	//On search button click run checks
	$('#searchButton').click(function(){
		if( $('#queryText').val() == '' ){
			alert('You must want to search for something! Try filling out the search box before clicking search.');
			return false;
		}
		else if( $('#queryText').val() == 'Search for your problem...' ){
			alert('Try searching for something other than the default text.');
			return false;
		}
	});
		
	

	
});//CLOSING DOCUMENT READY




/**
*	
*	GENERAL FUNCTIONS TO USE IN DOCUMENT.READY ABOVE
*
**/


function nextPromoSlide(){
	
	var currentItem = $('.activePromo > div.show').attr('id');
	
	switch(currentItem){
		case 'activePromoBannerOne':
			//$('.selectedPromoIndicator').removeClass('selectedPromoIndicator');
			//$('#promoBoxOne').addClass('selectedPromoIndicator');
			
			$('#activePromoImageOne').removeClass('show').addClass('hide');
			$('#activePromoImageTwo').removeClass('hide').addClass('show');
		
			$('#activePromoBannerOne').removeClass('show').addClass('hide');
			$('#activePromoBannerTwo').removeClass('hide').addClass('show');
			
			break;
		case 'activePromoBannerTwo':
			//$('.selectedPromoIndicator').removeClass('selectedPromoIndicator');
			//$('#promoBoxTwo').addClass('selectedPromoIndicator');
			
			$('#activePromoImageTwo').removeClass('show').addClass('hide');
			$('#activePromoImageThree').removeClass('hide').addClass('show');
		
			$('#activePromoBannerTwo').removeClass('show').addClass('hide');
			$('#activePromoBannerThree').removeClass('hide').addClass('show');
			
			break;
		case 'activePromoBannerThree':
			//$('.selectedPromoIndicator').removeClass('selectedPromoIndicator');
			//$('#promoBoxThree').addClass('selectedPromoIndicator');
			
			$('#activePromoImageThree').removeClass('show').addClass('hide');
			$('#activePromoImageOne').removeClass('hide').addClass('show');
		
			$('#activePromoBannerThree').removeClass('show').addClass('hide');
			$('#activePromoBannerOne').removeClass('hide').addClass('show');
			
			break;
		default:
		
	}
	
	
}//closing function



function setSlidePromo( goToSlide ){
	
	$('.activePromo > *').each( function(index){
		if( $(this).hasClass('show') == true ){
			$(this).removeClass('show').addClass('hide');		
		}//closing if												   
	})
	
	
	//$('.selectedPromoIndicator').removeClass('selectedPromoIndicator');
	
	
	if(goToSlide == 1){
		$('#activePromoImageOne').addClass('show');
		$('#activePromoBannerOne').addClass('show');
		
		//$('#promoBoxOne').addClass('selectedPromoIndicator');
	}
	else if(goToSlide == 2){
		$('#activePromoImageTwo').addClass('show');
		$('#activePromoBannerTwo').addClass('show');
		
		//$('#promoBoxTwo').addClass('selectedPromoIndicator');
	}
	else if(goToSlide == 3){
		$('#activePromoImageThree').addClass('show');
		$('#activePromoBannerThree').addClass('show');
		
		//$('#promoBoxThree').addClass('selectedPromoIndicator');
	}
		
	
	
	
}//closing function
	





function resetNavNoDelay(){
	
	$('#primaryNav ul li').find('a').each(function(){
		if( $(this).css('color') != '#FFF' ){
			$(this).css('color','#FFF');
		}	 
	});
	
	$('.hoverNavigation').each( function(i){
		
		if( $(this).hasClass('expandedHover') ){
			$('div.expandedHover').removeClass('show');
			$('div.expandedHover').addClass('hide');
			$('div.expandedHover').removeClass('expandedHover');			
		}
	});	
}//close function



//DEALS WITH RESETTING ALL PRIMARY NAV LINKS BACK TO WHITE AND HIDING ALL SUB NAV DROP DOWNS.
function resetNav(){
	$('#primaryNav ul li').find('a').each(function(){
		if( $(this).css('color') != '#FFF' ){
			$(this).css('color','#FFF');
		}	 
	});
	
	$('.hoverNavigation').each( function(i){
		if( $(this).hasClass('expandedHover') ){
			timer = setTimeout(function(){
				$('div.expandedHover').removeClass('show');
				$('div.expandedHover').addClass('hide');
				$('div.expandedHover').removeClass('expandedHover');
			}, 500);
		}
	});
}//close function



function noHoverCheck(){	
	//var numActiveHovers = $('#primaryNav ul li:hover').length;
	//console.log(numActiveHovers);
	/*setTimeout(function(){
	});*/
}//close function



//DEALS WITH SLIDING DOWN THE RIGHT SUB NAV AND HIDING ANY OTHERS THAT WERE SHOWN OR IN THE PROCESS OF BEING SHOWN.
function showSubNav(toHighlight){
	var curId = $(toHighlight).attr('id');//eg navAnal
	$(toHighlight).find('a').css('color','#000');
	$('#'+curId+'Hover').removeClass('hide');
	$('#'+curId+'Hover').addClass('expandedHover show');
}//close function


