
	function resetError() {
		var $this = $(this);
		if($this.val().length > 0) {						
			$this.removeClass('error');
			$this.next('.error').remove();
		}
	}

	function submit_competition() {
		var $form = $('#contact_form');
		var errors = false;
		var $msg = $("<label generated=\"true\" class=\"error\">This field is required.</label>");

		$('label.error').remove();
		$('.error').removeClass('error');

		$form.find('.required').each(function() {
			var $this = $(this);
			var $value = $this.val();
			var error = false;

			if($this.hasClass('required') && $value.length == 0) {
				error = true;
				$this.addClass('error').after($msg.clone().attr('for', $this.attr('name')).text("This field is required"));
			}
			/*
			else if($this.hasClass('email') && !isValidEmailAddress($value)) {
				error = true;
				$this.addClass('error').after($msg.clone().attr('for', $this.attr('name')).text("This field must contain a valid email address"));
			}
			*/

			if(error) {
				errors = true;
				$this.blur(resetError).change(resetError);
			}
		});

		var contact_method = '';
		var $contact_methods = $form.find('.contact_method');
		$contact_methods.each(function() { contact_method += this.value; });
		if(contact_method.length == 0) $contact_methods.each(function() { 
			errors = true;
			var $this = $(this);
			$this.addClass('error').after($msg.clone().attr('for', $this.attr('name')).text("You must provide either home phone or mobile phone"));
			$this.blur(resetError).change(resetError);
		});

		var $terms = $form.find('#terms');
		if(!$terms.is(':checked')) {
			errors = true;
			$terms.closest('li').addClass('error').prepend($msg.clone().text("You must agree to the terms and conditions")).change(resetError);
		}

		if(!errors) {
			$email_field = $('#ohujtk-ohujtk');
			if(!isValidEmailAddress($email_field.val())) {
				newDate = new Date;
				uid = newDate.getTime() + Math.floor(Math.random() * 101);
				$email_field.attr('value', 'dummy_' + uid + '@wisdom.com.au');
			}

			$form.find('button').slideUp();

			$.ajax({
				url: RecaptchaURL, 
				data: {
					recaptcha_challenge_field: $form.find('#recaptcha_challenge_field').val(), 
					recaptcha_response_field: $form.find('#recaptcha_response_field').val()
				},
				type: 'POST', 
				success: function(data, textStatus, jqXHR) {
					if(data.substr(0, 'true'.length) === 'true') $form.submit();
					else {
						$('#recaptcha_widget').after($msg.clone().text("The CAPTCHA does not match"));
						$form.find('button').slideDown();
					}
				}
			});
		}
	}

	// functions for the contact page
	
	function isValidEmailAddress(emailAddress) {
				var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
				return pattern.test(emailAddress);
			}
						
			function submit_form() {
				var errors = false;	
			
				$('label.error').remove();
				$('.error').removeClass('error');
				
				$('#contact_form .required').each(function() {
					
					$this_element = $(this);
				
					var this_name = $this_element.attr('name');
					var this_value = $this_element.val();
					var contact_method_select = $('#preferred-method-of-contact').val();
					
					if(!$this_element.hasClass('contact_method') || ($this_element.hasClass('contact_method') && this_name == contact_method_select)) {
						
						if(this_value.length == 0) {
							errors = true;
							$this_element.addClass('error');
							$e = $("<label for=\"" + this_name + "\" generated=\"true\" class=\"error\">This field is required.</label>");
							$e.insertAfter($this_element);
							
							$this_element.blur(function() {	
								if($(this).val().length > 0) {						
									$(this).removeClass('error');
									$(this).next('.error').remove();								
								}
							});
							$this_element.change(function() {	
								if($(this).val().length > 0) {						
									$(this).removeClass('error');
									$(this).next('.error').remove();								
								}
							});							
												
						} else if($this_element.hasClass('email')) {
							if(!isValidEmailAddress(this_value)) {
								$this_element.addClass('error');
								$e = $("<label for=\"" + this_name + "\" generated=\"true\" class=\"error\">This field must be a valid email address.</label>");
								$e.insertAfter($this_element);							
								errors = true;
																
								$this_element.blur(function() {	
									if($(this).val().length > 0) {						
										$(this).removeClass('error');
										$(this).next('.error').remove();								
									}
								});
								$this_element.change(function() {	
									if($(this).val().length > 0) {						
										$(this).removeClass('error');
										$(this).next('.error').remove();								
									}
								});
							}										
						}
					} else if($this_element.hasClass('email') && this_value.length > 0) {
							if(!isValidEmailAddress(this_value)) {
								errors = true;
								$this_element.addClass('error');
								$e = $("<label for=\"" + this_name + "\" generated=\"true\" class=\"error\">This field must be a valid email address.</label>");
								$e.insertAfter($this_element);							
								errors = true;
																
								$this_element.blur(function() {										
									$(this).removeClass('error');
									$(this).next('.error').remove();																	
								});
								$this_element.change(function() {										
									$(this).removeClass('error');
									$(this).next('.error').remove();									
								});
							}										
						}									
				});
				if(!errors) {
					$('#contact_form').submit();
				}
			}





// loads a javascript file on demand (with optional callback function)
// [plain javascript, does not require jQuery]
function load_script(sScriptSrc, callbackfunction) {
	var oHead = document.getElementsByTagName('head')[0];

	if(oHead) {	
		var oScript = document.createElement('script');
		oScript.setAttribute('src', sScriptSrc);
		oScript.setAttribute('type', 'text/javascript');

		var loadFunction = function() {
			if(this.readyState == 'complete' || this.readyState == 'loaded') {
				callbackfunction(); 
			}
		};

		oScript.onreadystatechange = loadFunction;
		oScript.onload = callbackfunction;	
		oHead.appendChild(oScript);
	}
}

// The algorithm used to center align a background image rounds differently than the
// one used to center align a block element ('margin: 0 auto')
// [plain javascript, does not require jQuery]
var html_obj = document.getElementsByTagName('html')[0];
var fix_alignment_bug = function() {
	needs_fix = (document.width + parseInt(html_obj.style.marginLeft)) % 2 ? true : false;
	html_obj.style.marginLeft = needs_fix ? '-1px' : '0px';
};
window.onload = fix_alignment_bug;
window.onresize = fix_alignment_bug;

//--

$(function() {

	if($('body').hasClass('competition')) {
		var club = $('#club_li').hide();
		$('#cm214244').click(function() { if(!club.is(':visible')) club.slideDown(); });
		$('#cm214245').click(function() { if(club.is(':visible')) club.slideUp(); });
	}


	// IE6 doesn't understand how to ':hover' anything that isn't an 'a'
	$('#secondary_nav > li').hover(
		function() { $(this).addClass('over'); }, 
		function() { $(this).removeClass('over'); }
	);

	if(top.location == location) {
		if(document.body.id == 'shop-sub') {
			$h2 = $('h2');
			$h2_words = $h2.text().split(' ');
			if($h2_words.length > 1) {
				$first_word = $h2_words.shift();
				$h2.html('<span>' + $first_word + '</span> ' + $h2_words.join(' '));
			}
		}

		Cufon.replace('h2 span');

		if(document.body.id == 'gallery') {
			load_script('includes/template/js/tinybox.js', function() {
				$('.children a').click(function() {
					$src = $(this).attr('href');
					TINY.box.show('<img src="' + $src + '" onclick="TINY.box.hide()" />');
					return false;
				});
			});
		}
		else if(document.body.id == 'the-latest') Cufon.replace('li div h5');
		else Cufon.replace('.children a');
	}
	if(document.body.id == 'home') {
		load_script('includes/template/js/jquery.tools.min.js', function() {
			$slideshow = $('#slideshow');
			$results = $('#results');
			$gallery = $('#mini-gallery');
			$main_gallery_img = $('> img', $gallery);

			$tab_settings = { effect: 'fade', fadeOutSpeed: 'slow', rotate: true, event: 'mouseover' };
			$slideshow_settings = { autoplay: true, clickable: false, interval: 5000 };
			$scrollable_settings = { size: 4 };

			$('.tabs', $slideshow).tabs($('.tabbed > *', $slideshow), $tab_settings).slideshow($slideshow_settings);
			$('.tabs', $results).tabs($('.tabbed > *', $results));
			$('.scrollable', $gallery).scrollable($scrollable_settings).navigator();
			$('.navi a', $gallery).html('&bull;');

			$('.scrollable img', $gallery).click(function() {
			
				$this_img = $(this).attr('title');
			
				$main_gallery_img.attr('src', $this_img);
			}).filter(':first').click();
						
		});
	}
	
	// add active class to menus
	var this_text = '';
	var this_page = '';
	var this_parent = '';
	
	$('.siblings li a').each(function() {
		$(this).attr('class', '');	
		
		this_text = $(this).text();
		this_text = this_text.replace('+', 'and');
		this_text = this_text.toLowerCase();
		
		this_page = page.url.replace('-', ' ');
		this_page = this_page.toLowerCase();
					 			
		if(this_text == this_page) {
			$(this).addClass('active');
		}
	});

	$('#secondary_nav li a').each(function() {
		
		this_text = $(this).text();
		this_text = this_text.replace('+', 'and');
		this_text = this_text.toLowerCase();
				
		this_page = page.url.replace('-', ' ');		
		this_page = this_page.toLowerCase();
		
		this_parent = page.parent;
		this_parent = this_parent.replace('&', 'and');
		this_parent = this_parent.toLowerCase();
					 			
		if(this_text == this_parent) {
			$(this).parent('li').addClass('active');
		} else if(this_text == this_page) {
			$(this).parent('li').addClass('active');
		}
	});
	
	//contact form validation
	$('#subscribe_form').validate();
	$('#contact_form input').click(function() { $('.sent').html('').removeClass('sent'); });								
	$('.sent').click(function() { $(this).html('').removeClass('sent'); });
	
	$('#search').focus(function() {
		if($(this).val() == 'Search...') { 
			$(this).val('');
		}
	}).blur(function() {
		if($(this).val().length == 0) { 
			$(this).val('Search...');
		}
	});
	
	// fix text in "the-latest" sub menu links
	var old_text = '';
	var new_text = '';
	$('#the-latest-sub-menu li a').each(function() {
		old_text = $(this).html();
		new_text = 'Click here for <strong>' + old_text + '</strong>';
		$(this).html(new_text);	
	});	
	
   	$("#tabs").tabs();
   	
   	$('.no-link').click(function() {
   		return false;
   	});
   	
   	$('a[rel="external"], #attractions-menu a').click(function() {
	    window.open( $(this).attr('href') );
	    return false;
	});	
		
	
	// styling images on corporates page
	var c = 0;
	var $elem = null;
	$('#memberships #page_content img').each(function() {
		
		$elem = $(this);
		if(c == 0) $elem.addClass('first-gallery-img');
		c++;
	
	});
	
	if($elem) $elem.addClass('last-gallery-img');
	
	// remove image links hover background colours
	
	$('a img').parent('a').hover(function() {	
		$(this).css('background', 'transparent');
	});
	
	
});

