$(document).ready(function(){




	var austDay = new Date();
	austDay = new Date(2013, 0, 26);
	$('#counter').countdown({until: austDay, format: 'd', layout: '{dn}'});
	$('#counter').countdown('pause');
	Cufon.replace('#counter');

	//Admin Stuff
	$('#sublist tr:even').addClass('odd');
	$('#adminview select').change(function() {
			$('#adminview').submit();
	});

	//Edit Email Address
	$('a.emedit').click(function() {
			row = $(this).parents('tr');
			$('#adminview #id').val($('td.row-id', row).html());
			$('#adminview #email').val($('td.row-email', row).html());
			var checked = confirm("Change email address: " + $('#adminview #email').val() + " ?");
			if (checked)
			{
				var newemail = prompt("Enter new address:");
				checked = confirm("Update email address to: " + newemail + " ?");
				if (checked)
				{
					$('#adminview #email').val(newemail);
					$('#adminview #action').val('edit');
					$('#adminview').attr('ACTION',	 document.location.href);
					$('#adminview').submit();
				}
			}
			return false;
	});

	//Resend Confirmation
	$('a.resender').click(function() {
			row = $(this).parents('tr');
			$('#adminview #id').val($('td.row-id', row).html());
			$('#adminview #fname').val($('td.row-fname', row).html());
			$('#adminview #dob').val($('td.row-dob', row).html());
			$('#adminview #email').val($('td.row-email', row).html());
			var checked = confirm("Resend confirmation email to " + $('#adminview #email').val() + "?");
			if (checked)
			{
				alert("A confirmation email has been sent to " + $('#adminview #email').val());
				$('#adminview #action').val('resend');
				$('#adminview').attr('ACTION',	 document.location.href);
				$('#adminview').submit();
			}
			return false;
	});



	//Nav
	var $curtab;
	$('#nav > li').hover(
		function () {
			//$curtab = $('#nav > li.on');
			//$($curtab).removeClass('on');
			$(this).addClass('over');
			$('ul', this).slideDown({
			 duration: 500,
			 easing: 'easeOutBounce'
			});
		},
		function () {
			//$($curtab).addClass('on');
			$(this).removeClass('over');
			$('ul', this).slideUp(100);
		}
	);

	//Backstretch
	if(!$('#admin').length) $.backstretch("/images/bg_image.jpg");

	//Slideshow
	$('#slideshow').cycle({
		fx:      'turnDown',
		easing: 'easeOutBounce',
		random: 1,
		timeout: 10000
	});

	//Gallery/Album grid
	$('#album-list li:nth-child(5n)').addClass('last');
	$('#gallery-list li:nth-child(3n)').addClass('last');
	$('#gallery-list-alt li:nth-child(2n)').addClass('last');

	//Map Off
	$('#map').mouseleave(function () {
		$('#map').css('background-position', '0 0');
	});

	//Accordion
	$('.accordion .handle').click(function () {
		$('.accordion .slide').slideUp('fast');
		$('.accordion .handle').removeClass('on');
		$(this).siblings('.slide').slideDown('fast');
		$(this).addClass('on');
		return false;
	});

	// ON
	$('#ausmap area[alt="QLD"]').hover(
		function () {$('#map').css('background-position', '0 -345px');},
		function () {$('#map').css('background-position', '0 0');}
	);
	$('#ausmap area[alt="NSW"]').hover(
		function () {$('#map').css('background-position', '0 -690px');},
		function () {$('#map').css('background-position', '0 0');}
	);
	$('#ausmap area[alt="VIC"]').hover(
		function () {$('#map').css('background-position', '0 -1035px');},
		function () {$('#map').css('background-position', '0 0');}
	);
	$('#ausmap area[alt="SA"]').hover(
		function () {$('#map').css('background-position', '0 -1380px');},
		function () {$('#map').css('background-position', '0 0');}
	);
	$('#ausmap area[alt="WA"]').hover(
		function () {$('#map').css('background-position', '0 -1725px');},
		function () {$('#map').css('background-position', '0 0');}
	);


	$(function(){
		if ($('#comp').length)
		{
			word_count();
			$(this).keyup(function() { word_count() });
		}
	});






	//Lightbox
	$("a[rel*='lightbox']").fancybox({
		'overlayColor' : '#000000',
		'overlayOpacity'	: 0.9,
		'scrolling' : 'no',
		'padding' :  0,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'titleShow' : false,
		'showCloseButton' : true
	});

	//Fancy Box Popups
	$("#butt-email, #butt-tell").fancybox({
		'overlayColor' : '#000000',
		'overlayOpacity'	: 0.8,
		'padding' :  0,
		'width' : 400,
		'scrolling' : 'no',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titleShow' : false,
		'showCloseButton' : true
	});


	// Subscription Validation
	$("#butt_sub").click(function() {
		// Set Validation Variables
		var error = 0;
		var msg ='';
		var errormsg = 'Please correct the following issues and try again:\n\n';
		// Run Checks

		// Name
		if ($("#sub-name").val().length == 0) {
			error = 1;
			msg = 'First Name field cannot be blank\n';
			errormsg += msg;
		}
		// Surname
		if ($("#sub-surname").val().length == 0) {
			error = 1;
			msg = 'Last Name field cannot be blank\n';
			errormsg += msg;
		}
		// Email
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($("#sub-email").val()) == false) {
			error = 1;
			msg = 'Email Address must be valid\n';
			errormsg += msg;
		}
		// Lists
		if (!document.getElementById('list-htc').checked && !document.getElementById('list-news').checked) {
			error = 1;
			msg = 'Choose at least one mailing list\n';
			errormsg += msg;
		}

		// Error
		if (error == 1) {
			alert(errormsg);
		}
		else {

			//Hide the Form
			$("#sub-form").hide();
			// Submit the form via ajax
			$.ajax({
				url: $(this).attr('href'),
				type: "POST",
				data: $("#sub-form").serialize(),
				success: function(html){
					$("#sub-sysrep").show();
					setTimeout(function() { $.fancybox.close(); }, 5000);
				}
			});
		}
		return false;
	});

	// Tell Friend Validation
	$("#butt_tell").click(function() {
		// Set Validation Variables
		var error = 0;
		var msg ='';
		var errormsg = 'Please correct the following issues and try again:\n\n';
		// Run Checks

		// Name
		if ($("#tell-name").val().length == 0) {
			error = 1;
			msg = 'Your Name field cannot be blank\n';
			errormsg += msg;
		}
		// Email
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($("#tell-email").val()) == false) {
			error = 1;
			msg = 'Your Email Address must be valid\n';
			errormsg += msg;
		}
		// Friend's Name
		if ($("#tell-fname").val().length == 0) {
			error = 1;
			msg = 'Your Friend\'s Name field cannot be blank\n';
			errormsg += msg;
		}
		// Friend's Email
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($("#tell-femail").val()) == false) {
			error = 1;
			msg = 'Your Friend\'s Email Address must be valid\n';
			errormsg += msg;
		}

		// Error
		if (error == 1) {
			alert(errormsg);
		}
		else {

			// Submit the form via ajax
			$.ajax({
				url: $(this).attr('href'),
				type: "POST",
				data: $("#tell-form").serialize(),
				success: function(html){
					$("#tell-sysrep").show();
					$("#tell-fname").val('');
					$("#tell-femail").val('');
				}
			});
		}
		return false;
	});


});


function word_count() {
	var number = 0;
	var max = 25;
	var field = '#comp';
	var counter = '#wordcount';

	var matches = $(field).val().match(/\b/g);
	if(matches)
	{
		number = matches.length/2;
	}

	$(counter).val(number);
	if (number <= max)
	{
		$remaining = max - number;
		$(count_text).text( $remaining + ' word' + ($remaining > 1 ? 's' : '') + ' remaining.');
	}
	else
	{
		$(count_text).text('Too many words!');
	}

}


