$(document).ready(function(){

	/**
	 * Most jQuery.localScroll's settings, actually belong to jQuery.ScrollTo, check it's demo for an example of each option.
	 * @see http://flesler.demos.com/jquery/scrollTo/
	 * You can use EVERY single setting of jQuery.ScrollTo, in the settings hash you send to jQuery.LocalScroll.
	 */
	$('#header_page_nav,#footer').localScroll({
		queue:true,
		duration:1500,
		axis:'y',
		offset:0,
		easing:'easeOutExpo'
	});
	
	
	// Map Swap Image 
	$("#location_img p img").hover(
		function()
		{
			this.src = this.src.replace("-off","-over");
		},
		function()
		{
			this.src = this.src.replace("-over","-off");
		}
	);		


	/**
	 * Header Image Rotator
	 */	
	var imageRotateTotal = $('#header_rotation_nav li').length;
	var imageRotateTimeout;
	var imageRotateTimerFirst = true;
	var imageRotateTimerSeconds = 5;
	var imageRotateTimerOn = true;
	
	// Header Image Rotator - Init
	imageRotateInit();
	
	// Header Image Rotator - Init	
	function imageRotateInit(){
		// Append title tags to Nav from Images
		$('#header_rotation_nav li').each(function(i) {
			var imgAlt = $('#header-txt'+(i+1)+' img').attr('alt');		
			$('#'+$(this).attr('id')+' a').attr('title', imgAlt);
		});
		// Start with image #1
		imageRotateNext();
	}
	
	// Header Image Rotator - Fade image
	function imageRotateFade(ref){
		// See if same image is clicked
		if( $(ref).parent('li').hasClass('active'))
		{
			imageRotateTimer();
			return false;
		}
 
		// End Animation
		$('#header_rotation_txt div').stop(true,true);
		
		// Reset Current Image + Nav Item
		$('#header_rotation_nav .active').removeClass('active');
		var current = $('#header_rotation_txt div.active');
		$(current).hide();
		$(current).css('z-index','3');
		
		// Display New Image + Nav Item
		var target = $(ref).attr('href');
		$(target).show();
		$(target)
			.addClass('active')
			.hide()
			.css('z-index', '4')
			.fadeIn('slow', function(){ 
				$(current).removeClass('active'); 
			});
		$(ref).parent('li').addClass('active');
		
		// Set the next autoplay timer
		if(imageRotateTimerOn){
			imageRotateTimer();
		}
	}

	// Header Image Rotator - Image Rotate Timer
	function imageRotateTimer() {
		clearTimeout(imageRotateTimeout);
		imageRotateTimeout = setTimeout(function(){
			imageRotateNext();
		},(imageRotateTimerSeconds*1000));
	}	
	
	// Header Image Rotator - Next Image
	function imageRotateNext() {
		clearTimeout(imageRotateTimeout);
		var current_li = $('#header_rotation_nav li.active');
		if($('li').index(current_li) >= (imageRotateTotal-1) || imageRotateTimerFirst){
			imageRotateTimerFirst = false;
			var next_li = $('#header_rotation_nav li:first-child');
		} else {
			var next_li = $(current_li).next();
		}
		var target_id = $(next_li).attr('id');
		var next_a = $('#'+target_id+' a');
		imageRotateFade(next_a);
	}
	
	// Header Image Rotator - Previous Image
	function imageRotatePrevious() {
		clearTimeout(imageRotateTimeout);
		var current_li = $('#header_rotation_nav li.active');
		if($('li').index(current_li) <= 0){
			var next_li = $('#header_rotation_nav li:last-child');
		} else {
			var next_li = $(current_li).prev();
		}
		var target_id = $(next_li).attr('id');
		var next_a = $('#'+target_id+' a');
		imageRotateFade(next_a);
	}
	
	// Header Image Rotator -  ul > li nav
	$('#header_rotation_nav').find('a').click(function(){
		clearTimeout(imageRotateTimeout);
		imageRotateFade(this);
		return false;
	});	
	
	// Header Image Rotator - Pause Timer on rollover of nav etc.
	$('#header_rotation_nav, #header_rotation_txt').mouseover(function() {
		clearTimeout(imageRotateTimeout);
		imageRotateTimerOn = false;
	});
	$('#header_rotation_nav, #header_rotation_txt').mouseout(function() {
		imageRotateTimerOn = true;
		imageRotateTimer();
	});
	
		
	// External links init
	$('a[rel=external]').click(function(){
		$(this).attr('target', 'blank');
	});
	

	/**
	 * Contact Form
	 */	
	// Form init
	$('#contact_form form').find('.text, .textarea').each(function(){
		var control = $(this);
		var label = $(this).find('label');

		label.click(function(){
			control.find('input,textarea').focus();
		});
		
		$(this).find('input, textarea').each(function(){
			if($(this).val() != "")
			{
				label.removeClass();
				label.addClass('hidden');
			}			
			$(this)
				.focus(function(){
					label.removeClass();
					label.addClass('inactive');
				})
				.blur(function(){
					if($(this).val() == "")
					{
						label.removeClass();
						label.addClass('active');
					} else {
						label.removeClass();
						label.addClass('hidden');
					}
				})
				.keyup(function(){
					if($(this).val() != "")
					{
						label.removeClass();
						label.addClass('hidden');
					} else {
						label.removeClass();
						label.addClass('active');
					}
				});
		});
	});
	
	// Form validation
	$('#contact_form form').validate({
		submitHandler: function(form) 
		{
			$('#contact_form form .form_message').hide();
			$('#contact_form form input[type=submit]').attr("disabled", "disabled");
			
			// Submit Data
			$(form).ajaxSubmit({
				type: "POST",  
				url: "contact.php",  
				dataType : 'json',
				beforeSubmit: function(){
					$('#contact_form form .loader').fadeIn('slow');
				},
				success: function(msg){
					$('#contact_form form .loader').hide();
					if(msg.status == "3")
					{
						$('#contact_form form .form_message').text('Thankyou, your message was sent.');
						$("#contact_form form .form_message").addClass('success');
					}
					else
					{
						$('#contact_form form .form_message').text('Sorry, there was an error.');
						$("#contact_form form .form_message").addClass('error');
					}
					$("#contact_form form .form_message").fadeIn('slow');
					$('#contact_form form input[type=submit]').removeAttr("disabled");
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					//alert(textStatus.type);
					$('#contact_form form .loader').hide();
					$('#contact_form form .form_message').text('Sorry, there was an error. Please try again later.');
					$("#contact_form form .form_message").addClass('error');
					$("#contact_form form .form_message").fadeIn('slow');
					$('#contact_form form input[type=submit]').removeAttr("disabled");
				}				
			});
		},
		errorPlacement: function(error, element) 
		{
			if(element.attr('name') == 'email' && $("#contact_form form .error").length == 1)
			{
				$("#contact_form form .form_message").text('Please enter a valid email address');
			}
			else
			{
				$("#contact_form form .form_message").text('Please complete the highlighted fields');
			}
			$("#contact_form form .form_message").fadeIn('slow');
			$('#contact_form form .loader').hide();			
		}

	});
	function checkEmail(address)
	{	
		var emailpattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var email = $("#" + address).val();
		return emailpattern.test(email);
	}	
});



