$(document).ready(function(){

	// Add smooth scrolling to each of the nav links
	scroll_options = {
		queue:true,
		duration:1500,
		axis:'y',
		offset:0,
		easing:'easeOutQuad'
	};
	$('nav ul, nav, #site-information > ul, #document > header > .content > ul').localScroll(scroll_options);
	
	$('nav a, #document > header li a').click(function(){
		$('nav li.current').removeClass('current');
		loc = $(this).attr('href');
		$(loc).find('a[href="' + loc + '"]').parent('li').addClass('current');
	});
	
	// setup external links	$('a[rel=external]').click(function(){		$(this).attr('target', 'blank');	});
	
	// Our People selector
	$('#staff_list').find('a').click(function(){
		
		// If we're clicking the same one
		if( $(this).parent('li').hasClass('current'))
		{
			return false;
		}
		
		// End all animations
		$('#our_people figure').stop(true,true);
		
		// Remove current highlight
		$('#staff_list .current').removeClass('current');
		
		var target = $(this).attr('href');
		var current = $('#our_people article.current');
		
		// hide the current content and send the image to the back
		$(current).find('header,section').hide();
		$(current).find('figure').css('z-index','1');
		
		// Show the new content
		$(target).find('header,section').show();
		
		// Show the new content, bring the image to the front and fade in the image
		$(target)
			.addClass('current')
			.find('figure')
			.hide()
			.css('z-index', '2')
			.fadeIn('slow', function(){ 
				$(current).removeClass('current'); 
			});
			
		// Highlight the current thumb		
		$(this).parent('li').addClass('current');
		
		return false;
	});

	// Add the grid overlay
	/*
	$("#document").append("<div id='grid'></div>");
    $("#grid").grid();
 	*/
 	
  	// Google Maps
  	var address = '396 Little Bourke Street, Melbourne, Victoria, 3000';
  	var geocoder; 	
 	var myOptions = {
		zoom: 17,
		center: latlng,
		mapTypeControl: false,
		mapTypeId: google.maps.MapTypeId.HYBRID
	}
	
	var map = new google.maps.Map(document.getElementById("map"), myOptions);
 	var latlng = new google.maps.LatLng(-34.397, 150.644);
 	var geocoder = new google.maps.Geocoder();
	
	if (geocoder) 
	{
		geocoder.geocode( {'address':address}, 
		
			function(results, status) 
			{
				if (status == google.maps.GeocoderStatus.OK) 
				{
					map.setCenter(results[0].geometry.location);
					
					 var image = new google.maps.MarkerImage("images/icon-google_map.png",
					      // This marker is 20 pixels wide by 32 pixels tall.
					      new google.maps.Size(141, 60),
					      // The origin for this image is 0,0.
					      new google.maps.Point(0,0),
					      // The anchor for this image is the base of the flagpole at 0,32.
					      new google.maps.Point(30, 70));

					var marker = new google.maps.Marker({ 
						map: map, 
						position: results[0].geometry.location ,
						title: "Grigor Lawyers",
						icon: image
					});
				}
			}
		)
	}

	
	// Form Labels
	$('#contact form, #site-information').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.hide();
			}
			
			$(this)
				.focus(function(){
					label.fadeTo(0,0.2);
				})

				.blur(function(){
					if($(this).val() == "")
					{
						label.fadeTo(0,1);
						label.show();
					}
				})

				.keyup(function(){
					if($(this).val() != "")
					{
						label.hide();
					}
				});
		});
	
	});
	
	// Send form with AJAX after validating
	$('#contact form').validate({
	
		submitHandler: function(form) 
		{
			$('#contact form .form_message').hide();
		
			$(form).ajaxSubmit({
				dataType:'json',
				beforeSubmit: function(){
					$('.loader').fadeTo(0,0).show().fadeTo(1000,1);
				},
				success: function(msg){
					if(msg.status == "3")
					{
						$('.loader').hide().text('Thankyou, your message was sent').addClass('success').show().fadeTo(3000,1).fadeTo(1000,0);
					}
					else
					{
						$('.loader').hide().text('Sorry, there was an error. Please try again').addClass('error').show().fadeTo(3000,1).fadeOut(1000,0);
					}
				}
			});
		},

		errorPlacement: function(error, element) 
		{
			if(element.attr('name') == 'email' && $("#contact form .error").length == 1)
			{
				$("#contact form .form_message").text('Please enter a valid email address');
			}
			else
			{
				$("#contact form .form_message").text('Please complete the highlighted fields');
			}

			$("#contact form .form_message").show();
		}

	});
	
	$("#newsletter_form button").click(function() {	
		
		// First, disable the form from submitting
		$('#newsletter_form').addClass('loading').submit(function() { return false; });
		
		// Remove the error
		$('#site-information .form_message').hide();
		
		// Grab form action
		formAction = $("#newsletter_form").attr("action");
		
		// Hacking together id for email field
		// Replace the xxxxx below:
		// If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
		emailId = "duhiki";
		emailId = emailId.replace("/", "");
		emailId = emailId + "-" + emailId;
		
		// Validate email address with regex
		if (!checkEmail(emailId)) 
		{
			$('#newsletter_form').removeClass('loading');
			$('#site-information .form_message.error').show().fadeTo(0,0).fadeTo(1000,1);
			return;
		}
		
		// Serialize form values to be submitted with POST
		var str = $("#newsletter_form").serialize();
		
		// Add form action to end of serialized data
		final = str + "&action=" + formAction;
		
		// Submit the form via ajax
		$.ajax({
			url: "mail/proxy.php",
			type: "POST",
			data: final,
			success: function(html){
				$("#newsletter_form").removeClass('loading'); // If successfully submitted hides the form
				$('#site-information .form_message.success').show().fadeTo(0,0).fadeTo(1000,1);  // Shows "Thanks for subscribing" div
			}
		});
	});

});

function checkEmail(email)
{	
	var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var emailVal = $("#" + email).val();
	return pattern.test(emailVal);
}