//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;
	
	
	//set the light background on the first option
	$('#promoBoxOne').addClass('selectedPromoIndicator');
	
	
	//Promo slide show for homepage
	pause = 10000;
	rotatingInterval = setInterval(nextPromoSlide, pause);
	
	
	$('#promoBoxOne h2').click(function(){
		setSlidePromo(1);	
		$('.selectedPromoIndicator').removeClass('selectedPromoIndicator');
		$('#promoBoxOne').addClass('selectedPromoIndicator');
	});
	
	$('#promoBoxTwo h3').click(function(){
		setSlidePromo(2);
		$('.selectedPromoIndicator').removeClass('selectedPromoIndicator');
		$('#promoBoxTwo').addClass('selectedPromoIndicator');
	});
	
	$('#promoBoxThree h4').click(function(){
		setSlidePromo(3);
		$('.selectedPromoIndicator').removeClass('selectedPromoIndicator');
		$('#promoBoxThree').addClass('selectedPromoIndicator');
	});
	
	
	
	//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' ){
				
				if(userAnswer == 'tackled'){
					$('#problemSurvey').html('<p class="mediumHeading mainColour bold">Thank you for the feedback</p><p class="bold">We are pleased you found the information helpful.</p><p class="bold">Please spread the word about this site so others can tackle<br /> their problems too.</p>');
				}
				if( userAnswer == 'tackling' ){
					$('#problemSurvey').html('<p class="mediumHeading mainColour bold">Thank you for your feedback and keep at it!</p><p class="bold">We welcome your <a href="mailto:info@healthpress.co.uk?subject=comment on embarrassingproblems.com" title="Give us your comments">comments</a> so that we can continue to improve <br />the site and help more people tackle their problems.</p>');
				}
				if( userAnswer == 'struggling' ){
					$('#problemSurvey').html('<p class="mediumHeading mainColour bold">Thank you for your feedback</p><p class="bold">We are sorry you\'re still struggling.</p><p class="bold">Please <a href="mailto:info@healthpress.co.uk?subject=comment on embarrassingproblems.com" title="Give us your comments">tell us</a> how we can improve the site to help<br /> you tackle your problem.</p><p class="bold">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;
	});
	
	
	//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.</p> <p class="bold">We are pleased you found the information helpful.</p> <p>&nbsp;</p> <p class="bold">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!</p> <p class="bold">We welcome your <a style="color:#FC5615;" href="mailto:info@healthpress.co.uk?subject=comment on embarrassingproblems.com" 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.</p> <p class="bold">We are sorry you\'re still struggling.</p> <p>&nbsp;</p> <p class="bold">Please <a style="color:#FC5615;" href="mailto:info@healthpress.co.uk?subject=comment on embarrassingproblems.com" title="Give us your comments">tell us</a> how we can improve the site to help<br /> you tackle your problem.</p> <p>&nbsp;</p> <p class="bold">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 style="font-size:1.3em; line-height:1.2em;" class="bold">All comments are moderated before publishing so there will be a delay before your comment appears. We cannot respond to individual requests for feedback, but your comments are reviewed when we update this section of the site, and sharing your experiences will help others to tackle their problems too.</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');
	
	$('.selectedPromoIndicator').removeClass('selectedPromoIndicator');
	
	switch(currentItem){
		case 'activePromoBannerOne':
			$('#promoBoxTwo').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':
			$('#promoBoxThree').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':
			$('#promoBoxOne').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



