/*
 *
 * START: jQuery WINDOW LOAD FUNCTIONS
 *
 */
$(window).load(function() {
	$("#mcs_container").mCustomScrollbar("vertical", 400, "easeOutCirc", 1.05, "auto", "yes", "yes", 10);

	$("button").button();
	
	// HOMEPAGE BANNER SWAPPER
	switch_homepage_banners();
	
	// SUBMIT FORM CHECKER
	check_contact_form();
	
	// INSTRUCTOR PROFILE
	swap_instructor();
	
	// ACTIVE FORM CHECKER
	$('.req').change(function() {
		if($(this).val()!='') {
			$(this).removeClass('text-error');
		}
	});
	$('.req-either').change(function() {
		if($(this).val()!='') {
			$('.req-either').removeClass('text-error');
		}
	});

	// REMOVE LAST SLASH FROM MAIN MENU
	$('#menu ul li:last').addClass('last');
	
	$('.technique-gallery a').lightBox({fixedNavigation:true});
});
/*
 * END: jQuery WINDOW LOAD FUNCTIONS
 */



/*
 *
 * START: jQuery WINDOW READY FUNCTIONS
 *
 */
$(window).ready(function() {
	force_full_height();
	
	// HOMEPAGE GALLERY
	$("#club-gallery-thumbs a").fancybox();
});
/*
 * END: jQuery WINDOW LOAD FUNCTIONS
 */


/*
 * GLOBAL VARS
 */
var form_tries = 0;


/*
 * CUSTOM FUNCTIONS
 */
function switch_homepage_banners() {
	$('#banner_swapper li').hover(function() {
		// GET NUMERIC ID OF SELECTED BANNER
		var id = $(this).attr('id');
		id = id.substring(id.length-1);
		
		// SWITCH OFF ALL HIGHLIGHTED SECTIONS
		$('#banner_swapper li').each(function() {
			$(this).removeClass('on');
		});
		// SWITCH ON ACTIVE ONE
		$(this).addClass('on');
		
		// SWITCH OFF ALL BANNERS
		$('.banner-images').each(function() {
			$(this).addClass('hidden');
		});
		// SWITCH ON ACTIVE ONE
		$('#banner-image-'+id).removeClass('hidden');
	});
}

function check_contact_form() {
	$('#submit-contact-form').click(function() {
		$('#sender').removeClass('hidden');
		var form_ok = true;
		$('.req').each(function() {
			if($(this).val()=='') {
				$(this).addClass('text-error');
				form_ok = false;
			} else {
				$(this).removeClass('text-error');
			}
		});
	
		// CHECK THE SECURITY ANSWER IS CORRECT
		var q1 = $('#sec1').html();
		var q2 = $('#sec2').html();
		var q_a = eval(q1) + eval(q2);
		var sec_answer = $('#c-security').val();
		if(q_a != sec_answer) {
			form_ok = false;
			$('#c-security').addClass('text-error');
			$('#sender').addClass('hidden');
		} else {
			$('#c-security').removeClass('text-error');
		}
		
		if(form_ok) {
			$('#submit-contact-form').attr('disabled', true);
			var cname	= $('#c-name').val();
			var cmail	= $('#c-email').val();
			var ctel	= $('#c-telephone').val();
			var cenq	=	$('#c-question').val();
			$.ajax({
				url:		"/wp-content/themes/stockton-taekwondo/contact.php",
				global:	true,
				type:		"POST",
				data:		'cn='+cname+'&cm='+cmail+'&ct='+ctel+'&ce='+cenq,
				async:	true,
				success: function(msg){
					if(msg=='sent') {
						var html = '<p>Thank you for contacting us.</p><p>We will get back to you as soon as possible.</p><p>Remember, the club is open every Friday (except Bank Holidays) from 6:00pm until 8:30pm, have a look at the Club Details section for more information.</p><p>Finally, if you are enquiring about checking out the club, do feel free to come down anytime and have a look around and speak to Mike, it is a friendly club and we are more than happy for any new or experienced people to come along and join in.</p>';
						$('#contact-form').html(html);
					} else {
						form_tries++;
						var html = 'Ooooops, unfortunately, an error has occurred and this form has not been submitted.';
						if(form_tries > 1)
							html = 'Ummm, how embarressing, still not working. You could click on the Club Details link for more contact info.';
						if(form_tries > 5)
							html = 'Aha, you are persistent. A trait we like in our students, maybe you should pop down to the club for some training instead of clicking this button.';
						$('#contact-title').html(html).addClass('highlight');
						$('#submit-contact-form').attr('disabled', false);
					}
				}
			});
		}
		return false;
	});
}

function swap_instructor() {
	$('.instructor').click(function () {
		var profile = $(this).attr('id');
		var swap_speed = 300;
		$('.profile-holder').fadeOut(swap_speed);
		$.timer(swap_speed, function(timer) {
			$('#profile-'+profile).fadeIn(swap_speed);
			timer.stop();
		});
	});
}

function force_full_height() {
	var footer = $('#footer');
	var f_position = footer.position();
	var contentWrap_Height = $('.contentWrap').height();
	var new_height = f_position.top - $('#footer').height() + 10;

	// HARD SET THE HEIGHT OF THE BACKGROUND DIV
	$('.contentWrap').height(new_height);
}

function recordOutboundLink(link, category, action) {
	_gat._getTrackerByName()._trackEvent(category, action);
	setTimeout('document.location = "' + link.href + '"', 100);
}
