$(function() {
		   
	/* Setup links that automatically popup in a new window */
	$('a[rel=external]').popupLinks();
	
	/* Setup input fields that clear the text when you focus them */
        $('.autoclear').autoClear();
	
	/* Setup friendly HTML forms, highlights and focus */
        $('.friendlyForm').formSetup();

	/* Setup print page functionality */
	$('.printPage').printPage();

        /* Google Maps Configuration */
        if ($('#map').length != 0) {
            var myLatlng = new google.maps.LatLng(51.14072,0.26318);
            var myOptions = {
              zoom: 14,
              center: myLatlng,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            var map = new google.maps.Map(document.getElementById("map"), myOptions);

            var contentString = '<h2>The Counselling Centre</h2>'+
            '<p><img src="/images/office-frontage.jpg" title="The Counselling Centre" width="200" height="149" style="margin-left: 20px; float: right;" />St George\'s Centre<br />'+
            '7 Chilston Road<br />'+
            'Tunbridge Wells<br />'+
            'Kent<br />'+
            'TN4 9LP</p>'+
            '<p>Telephone:<br />01892 548750</p>';

            var infowindow = new google.maps.InfoWindow({
                content: contentString,
                maxWidth: 400
            });

            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: "The Counselling Centre"
            });

            //google.maps.event.addListener(marker, 'load', function() {
              infowindow.open(map,marker);
            //});
        }


        if (getParameterByName('q')) {
            $("#searchResults").highlight(getParameterByName('q'), 1, "searchHighlight");
        }

});

$.fn.extend({
	printPage: function()
	{
		$(this).click(function(e){
			window.print();
			return false;
		});
	},
	popupLinks: function() {
		$(this).live('click', function(){
			window.open($(this).attr('href'));
			return false;
		});
	},
	autoClear: function() {
		$(this).focus(function() {
			if ($(this).attr('value') == $(this).attr('alt')) {
				$(this).attr('value','');
			}
		});
		$(this).blur(function() {
			if ($(this).attr('value') == '') {
				$(this).attr('value',$(this).attr('alt'));
			}
		});
	},
	formSetup: function () {
		$(this).find('.inputField').each(function () {
			$(this).blur().focus(function () {
				$(this).addClass('focus');
				$(this).closest('li').find('label').addClass('focus');
				$(this).parent().siblings('.helper').fadeIn();
			}).blur(function () {
				$(this).removeClass('focus');
				$(this).closest('li').find('label').removeClass('focus');
				$(this).parent().siblings('.helper').fadeOut();
			});
		});
		$(this).submit(function() {
			$(this).find('input[type=submit]').attr('disabled', 'true');
		});
	},
        highlight: function(search, insensitive, highlight_class) {
            var regex = new RegExp("(<[^>]*>)|(\\b" + search.replace(/([-.* ?^${}()|[\]\/\\])/g,"\\$1") + ")", insensitive ? "ig" : "g");
            return this.html(this.html().replace(regex, function(a, b, c){
                return (a.charAt(0) == "<") ? a : "<strong class=\"" +  highlight_class + "\">" + c + "</strong>";
            }));
        }
});

//Querystring Finder
function getParameterByName( name )
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
    return "";
    else
    return results[1];
}
