$(document).ready(function() {
	// This adds a gradient background to all text form fields.

	$('input[type="text"]').addClass('type_text');

	// This clears default values on form fields "on blur". Simply use the class "default_value".

	var default_values = new Array();

	$('input.default_value').focus(function() {
		if ( ! default_values[this.id]) {
			default_values[this.id] = this.value;
		}

		if (this.value == default_values[this.id]) {
			this.value = '';
		}

		$(this).blur(function() {
			if (this.value == '') {
				this.value = default_values[this.id];
			}
		});
	});

	// Set up the Newsletter form for validation.

	if ($('#form_newsletter').length != 0) {
		$('#form_newsletter_submit').click(function() {
			$('#form_newsletter').submit();

			return false;
		});

		$('#form_newsletter').validationEngine();
	}

	// Set up the Search form for validation.

	if ($('#form_search').length != 0) {
		$('#form_search_submit').click(function() {
			$('#form_search').submit();

			return false;
		});

		$('#form_search').validationEngine({
			promptPosition: "bottomLeft"
		});
	}

	// Set up the Contact Us form for validation.

	if ($('#form_contact_us').length != 0) {
		$('#form_contact_us_submit').click(function() {
			$('#form_contact_us').submit();

			return false;
		});

		$('#form_contact_us').validationEngine();
	}

	// Set up the Product email form for validation.

	if ($('#form_product_info_email').length != 0) {
		$('#form_product_info_email_submit').click(function() {
			$('#form_product_info_email').submit();

			return false;
		});

		$('#form_product_info_email').validationEngine();
	}

	$('#email_sps_more_info').click(function() {
		showRecaptcha('recaptcha_div');

		return false;
	});

	// Set up the tooltips.

	if ($('.tooltips').length != 0) {
		var api = $('.tooltips a[title]').tooltip({
			api: true
		});
	}

	// Set up a Colorbox for the Product Information.

	$('.product_image, .read_more').click(function() {
		api.hide();

		$.colorbox({
			href: '#colorbox_product_information',
			inline: true
		});

		return false;
	});

	// Set up the print function for the Product Information.

	$('#print_product_information').click(function() {
		$('#print_info_container').printElement();

		return false;
	});

	// Set up a Colorbox for the Technical Diagram.

	$('.technical_diagram').click(function() {
		api.hide();
		
		$.colorbox({
			href: '#colorbox_technical_diagram',
			inline: true
		});

		return false;
	});

	// Set up the print function for the Technical Diagram.

	$('#print_technical_diagram').click(function() {
		$('#technical_diagram_print_container').printElement();

		return false;
	});

	// Set up a Colorbox for Email SPS.

	$('.email_sps').click(function() {
		api.hide();

		$.colorbox({
			href: '#colorbox_request_info',
			inline: true,
			scrolling: false
		});

		return false;
	});

	// Set up a Colorbox for "Zoom In".

	$('.zoom_in').click(function() {
		api.hide();

		$.colorbox({
			href: '#zoom_in',
			inline: true
		});

		return false;
	});

	// Set up a slideshow for the homepage.
	
	if ($('.slideshow').length != 0) {
		$('.slideshow').cycle({
			fx: 'fade',
			speed: 5000
		});
	}

	// Set up the maps for the Branch Locator.

	function branch_map(lat, lng, branch) {
		new google.maps.Marker({
			position: new google.maps.LatLng(lat, lng),
			map: new google.maps.Map(document.getElementById('map_' + branch), {
				zoom: 15,
				center: new google.maps.LatLng(lat, lng),
				mapTypeId: google.maps.MapTypeId.ROADMAP
			}),
			icon: '/assets/images/icons/pin.png'
		});
	}

	if ($('.branches').length != 0) {
		branch_map(50.841153, 0.465758, 'bexhill');
		branch_map(50.788702, 0.298113, 'eastbourne');
		branch_map(50.860105, 0.249848, 'hailsham');
		branch_map(50.881788, 0.540926, 'hastings');
	}

	$('.tooltip').supersleight({shim: '/assets/images/x.gif'});
});
