/***********************************************
***  BUTTON ROLLOVER & PRELOAD  ****************
************************************************/

$(function(){
	// button rollover
	if ($('.imgBtn') != null) {
		$('.imgBtn').hover(function(){
				if(!$(this).is('.disabled'))
					$(this).attr("src", $(this).attr("src").replace(/^(.*?)(?:-hover)?(\..*?)$/i,'$1-hover$2'));
			},
			function(){
					if(!$(this).is('.disabled'))
						$(this).attr("src", $(this).attr("src").replace(/^(.*)-hover(\..*?)$/i, '$1$2'));
				});
	}
	// preload image rollovers
	if ($("img.imgBtn, img[src^='images/buttons/btn'], input[src^='images/buttons/btn']") != null) {
	    $("img.imgBtn, img[src^='images/buttons/btn'], input[src^='images/buttons/btn']").each(function(){
			    $("<img>").attr("src", $(this).attr("src").replace(/(btn-.*)(\..*?)/i, '$1-hover$2'));
		    });
	}
});

/***********************************************
***  EXTERNAL LINKS  ***************************
************************************************/

$(document).ready(function() {
	$("a[rel=external]").each(function() {
		$(this).addClass('external');
		$(this).attr({
			title: "Open this page in a new window",
			target: "_blank"
		});
	})
});

/***********************************************
***  CUFON  ************************************
************************************************/

$(function() {
	Cufon.replace('#nav a', { hover: true });
	Cufon.replace('.homeTiles h1', { hover: true });
    	Cufon.now();
});

/***********************************************
***  HOME PAGE BANNER ROTATIONS  ***************
************************************************/

$(function(){
	var delay = 8000;
	var fadeTime = 500;

	var curr = 1;

	function intFunc(){
		var prev = curr;
		curr++;
		
		$('#homeBanner > li:nth-child(' + prev + ')').fadeOut(fadeTime, function(){
				$('#homeBanner > li:nth-child(' + curr + ')').fadeIn(fadeTime, function(){
					});
			});
		if (curr == 3){
			curr = 1;
		}
	}
	var interval = window.setInterval(intFunc, delay);
});
	
/***********************************************
***  COMPACT FORM (LABEL OVER INPUT)  **********
************************************************/

$(function(){
	$('form.compact input, form.compact select, form.compact textarea').focus(function(){
			$('label[for=' + $(this).attr('id') + ']').hide();
		});
	$('form.compact input, form.compact select, form.compact textarea').each(function(ele){
			if($(this).val().length > 0)
				$('label[for=' + $(this).attr('id') + ']').css({ display: "none" });
		});
	$('form.compact input, form.compact select, form.compact textarea').blur(function(){
			if($(this).val().length == 0)
				$('label[for=' + $(this).attr('id') + ']').show();
		});
	$('form.compact').removeClass('compact');
});


/***********************************************
***  ENQUIRY FROM  *****************************
************************************************/
$(function() {
	$('#enquiryForm').submit(function() {
		$('#submitID').val('enquiry');
	});
});



/***********************************************
***  MAILING LIST  *****************************
************************************************/
$(function() {
	$('#mailingListForm').submit(function() {
		var $this = $(this);
		var email = $('#subscriptionEmail').val();
		
		if ( checkEmail(email) == false ) {
			alert('Please enter a valid email.')
			return false;
		}
		
		$('#mailSubmitID').val('MailingList');
		$this.fadeOut(1000);

		$.ajax({type: 'POST',
				url: 'mailing-list-post.asp',
				data: $this.serialize(),
				success: function(response) {
					setTimeout(function() {
						$('.subscriptionMessage').fadeIn(1000);
					}, 1000);					
				}
		});	

		return false;
	});
});

function checkEmail(inEmail) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(inEmail)) {
		return false;
	}
}
