
$(function($) {
	console.log('started')
	var createNewElement = function (name) {
		return $('<' + name + '>&nbsp;</' + name + '>');
	};
	var $navItems = $('#menu').find('.nav-item');
	if ($navItems.length > 0) {
		$navItems.each(function(i,a) {
			var $this = $(this);
			if ($this.find('.reach-around').length < 0) {
				var $ro = createNewElement('div');
				$ro.attr({
					'class':'reach-around'
				}).appendTo($this);
			} else {
				var $ro = $this.find('.reach-around');
			}
			var roPos = $ro.position();
			var roLeft = -roPos.left
			/*
			$this.find('a').hover(
				function() {
					$ro.animate({
						left: 0
					}, 100, function() {
					});
	//				.animate( properties, [ duration ], [ easing ], [ callback ] 
					//alert('hovered')
				},
				function() {
					$ro.animate({
						left: '-100px'
					}, 100, function() {
					
					});
				}
			);
			*/
		})
	}
	if ($('#clear-form').length > 0) {
		$('#clear-form').click(function(e) {
			e.preventDefault();
			var $form = $('form');
			var $inputs = $form.find('input[type=text], textarea');
			$inputs.each(function(i,a) {
				var $a = $(a);
				var val = $a.val();
				if (val != 'General Enquiry') {
					var newVal = ($a.attr('name') == 'fields[subject]') ? 'General Enquiry' : '';
					$a.val(newVal);
				}
			});
			return false;
		});
	}
	if ($('#search form').length > 0) {
		var $submit = $('#search form input[type=submit]');
		var $search = $('#search form input[type=text]');
		var action = $('#search form').attr('action');
		$submit.click(function(e) {
			e.preventDefault();
			var search_val = $search.val();
			if (search_val.length == 0) {
				$search.addClass('error');
			} else {
				var replace_spaces = search_val.replace(/\s+/gi, '|');
				var new_action = action + '?query=' + replace_spaces;
				$search.removeClass('error');
				window.location.href = new_action;
			}
			return false;
		})
		
	}

 
  $('a[href*=#]').click(function() {
	  if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
      	&& location.hostname == this.hostname) {
				var $target = $(this.hash);
				$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
				if ($target.length) {
					var targetOffset = $target.offset().top;
					$('html,body').animate({scrollTop: targetOffset}, 500);
					return false;
				}
		}
  });


});


