 
$(document).ready(function() {

	/** Smooth scroll to top **/
	$('#backToTop').click(function(){ 
		$('html, body').animate({scrollTop:0}, 'slow');
		return false; 
	}); 
	
	/** Smooth scroll to More Box **/
	$('#moreIdeas').click(function(){ 
		$('html,body').animate({ scrollTop: $('#moreBox').offset().top }, { duration: 'slow', easing: 'swing'});
	});

	/** Discount tooltip **/
	$('.hasdiscount').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		fixPNG: true,
		left: -120,
		bodyHandler: function() { 
			return $(this).children('.result .tooltip').html(); 
		}		
	});
	
	
	$(function() {
		$(".overlayRel[rel]").overlay();
	});
	
	
	if( $('#related').length)
	{
		$gift_title = $('#related').html();
		
		//$('#related').html('<h3>Loading things you might be interested in...</h3><img src="/images/ajax-loader.gif" /><div class="clear"></div>');
		//$('#related').css('display','');
		
		$.post("/ajax.php?q=related_items", {gift_title : $gift_title}, function(data) 
		{
			if (data)
			{
				$('#related').html(data);
				$('#related').slideDown().delay(50);
			}
			else
			{
				//$('#related').html('<h3>No similar gifts found.</h3><div class="clear"></div>');	
			}
			
		}, "json");

	}
	

	/** Convert checkbox to funky checkbox **/
	$('.checkbox-converter').click(function(){
		
		$checkbox_name = $('.checkbox-converter').attr('id');
		$checkbox_name = $checkbox_name.replace( '-checkbox', '' );
		
		if ($(this).hasClass('checked'))
		{
			$('.checkbox-converter').removeClass('checked');
			$('input[name=' + $checkbox_name + ']').val(0);
		}
		else
		{
			$('.checkbox-converter').addClass('checked');
			$('input[name=' + $checkbox_name + ']').val(1);
		}
	});
	
	if ($('#newsletter').attr('checked')) $('.checkbox-converter').addClass('checked');
	
	/** Process sign up form **/
	$('.update_account').click(function(){ 
										
		$('.error').slideUp(300);
		$('.success').slideUp(300);
		
		$username = $('#username').val();
		$fullname = $('#fullname').val();
		$email = $('#email').val();
		$dob_day = $('#dob-day').val();
		$dob_month = $('#dob-month').val();
		$dob_year = $('#dob-year').val();
		$gender = $('.gender:checked').val();
		$newsletter = $('#newsletter').val();
										
		$.post("/ajax.php?q=update_account", { 
									  username: $username , 
									  fullname: $fullname, 
									  email: $email, 
									  dob_day: $dob_day,
									  dob_month: $dob_month,
									  dob_year: $dob_year,
									  gender: $gender,
									  newsletter: $newsletter
									 },
		function(data, textStatus) 
		{
			if (data == 1)
			{
				$('.success').html('Your details have successfully been updated.');
				$('.success').slideDown('slow');
			}
			else
			{
				$('.error').html(data);
				$('.error').slideDown('slow');
			}
		}, "json");
									 
	});
	
	/** Process sign up form **/
	$('.sign-up-btn').click(function(){ 
		
		$('.msg').html('<h3>Thanks for registering an account</h3><p><img src="/images/ajax-loader.gif" /></p><p>Please wait while we check your details.</p>');
		$('.container').slideUp(300);
		$('.error').slideUp('fast');
		$('.msg').slideDown('slow');
		Recaptcha.reload();
		
		$username = $('#username').val();
		$fullname = $('#fullname').val();
		$email = $('#email').val();
		$password = $('#password').val();
		$recaptcha_resp = $('#recaptcha_response_field').val();
		$recaptcha_chal = $('#recaptcha_challenge_field').val();
		
		$.post("/ajax.php?q=signup", { 
									  username: $username , 
									  fullname: $fullname, 
									  email: $email, 
									  password: $password, 
									  recaptcha_resp: $recaptcha_resp,
									  recaptcha_chal: $recaptcha_chal
									 },
				   
			  function(data, textStatus) {
				  
				  
				if (data === 1)
				{
					$('.msg').html('<h3>Account Created!</h3><p><img src="/images/ajax-loader.gif" /></p><p>Please wait while we take you to your account.</p>');
					window.location.replace("/account.php");

				}
				else if (data)
				{
					$('.error').html(data);
					$('.msg').slideUp('fast');
					$('.container').slideDown('slow');
					$('.error').slideDown('slow');	
				}
				else
				{
					$('.msg').html('<h3>Oooops. Not sure what just happened.</h3><p>We are very sorry for any inconvenience. Please report this matter to us <a href="mailto:info@seekgifts.co.uk">here</a>.</p>');
				}
			  
			}, "json");					 
	});

});

/** Set ReCaptcha custom styles **/
var RecaptchaOptions = {
	theme : 'custom'
};



function processCompetitionForm()
{
	$name = $('#competition-form input[name=name]').val();
	$email = $('#competition-form input[name=email]').val();
	$tacs = $('#competition-form input[name=tacs]').val();
	
	$('.error').slideUp('slow');
	
	$submitBtn = $('#competition-submit').html();
	
	$('#competition-submit').html('<img src="/images/ajax-loader.gif" />');
	
	$.post("/ajax.php?q=competition_entry", { 
									  name: $name , 
									  email: $email, 
									  tacs: $tacs
									 },
		function(data, textStatus) 
		{
			if (data == 1)
			{
				$('.error').html('');
				$('.success').html('You have successfully entered the competition.');
				$('.success').slideDown('slow').delay(1200).slideUp('slow', function(){
					$('.overlay').fadeOut('slow');
				});
				
				$('#competition-submit').html($submitBtn);
			}
			else
			{
				$('.error').html(data);
				$('.error').slideDown('slow');
				$('#competition-submit').html($submitBtn);
			}
		}, "json");
}



/* FUNCTION TO CLEAR INPUT ON CLICK */
function ClearInput(value, id){ // This calls our function ClearInput, and the two variables we will need for it to function the original value and the id.
var input = document.getElementById(id); // Gets the input field based on its id.

if(value == input.value){ // If the default value is equal to the current value.
input.value = ''; // Empty It.
}else{ // Else the value is not equal to the current input field value.
input.value = input.value; // Leave it the same.
} // End Else.
} // Close Function.


// To call this simply use the following:
// onclick="ClearInput('YOUR TEXT', this.id);"


/* FUNCTION TO SUBMIT FORM ON KEYPRESS ENTER */
	function pressenter(e) {
		if (e.keyCode == 13) {
			document.getElementById('search').submit(); return false
		}
	}
	
function showLoader($id)
{
	document.getElementById($id).innerHTML = '<img src="/images/ajax-loader.gif" alt="Loading" class="loading-small" />';
	return true;
}
