function sameAddresses() {

	$('#srf-p-address-1').val($('#srf-address-1').val());
	$('#srf-p-address-2').val($('#srf-address-2').val());
	$('#srf-p-town').val($('#srf-town').val());
	$('#srf-p-postcode').val($('#srf-postcode').val());
	$('#srf-p-county').attr('selectedIndex', $('#srf-county').attr('selectedIndex'));
		
}

function sendQuickEnquiry() {
	
	// Remove the 'error' class from all form elements
	$('#qe-form input').each(function() { $(this).removeClass('error'); });
	$('#qe-form textarea').each(function() { $(this).removeClass('error'); });
	
	var errors = 0;

	if($('#qe-name').val().length == 0) {
		$('#qe-name').addClass('error');
		errors++;
	}
	
	if($('#qe-email').val().length == 0) { 
		$('#qe-email').addClass('error');
		errors++;
	}
	
	if($('#qe-requirements').val().length == 0) { 
		$('#qe-requirements').addClass('error');
		errors++;
	}
	
	// If we have any errors
	if(errors > 0) {
		
		// Show our error message
		$('#qe-message').text('Please complete all required fields.');
		$('#qe-message').css('display', 'block');
		
	} else {

		// Start our query string
		var qs = "action=handle-qe-survey-form";

		// Generate the AJAX query string
		$('#qe-form > input').each(function() { 
			var qsName = $(this).attr('name');
			var qsValue = $(this).val();
			qs += "&" + qsName + "=" + qsValue;
		});
		
		$('#qe-form > textarea').each(function() { 
			var qsName = $(this).attr('name');
			var qsValue = $(this).val();
			qs += "&" + qsName + "=" + qsValue;
		});
		
		$.ajax({
			type: "POST",
			url: "/admin/ajax.php",
			data: qs,
			success: function(msg){
				$('#qe-message').css('color', '#090');
				$('#qe-message').text('Thank you for your enquiry.');
				$('#qe-message').css('display', 'block');
				scroll(0, 0);
			}
		});
		
	}
	
}

function sendEnquiry() {

	// Remove the 'error' class from all form elements
	$('#survey-request-form input').each(function() { $(this).removeClass('error'); });
	$('#survey-request-form select').each(function() { $(this).removeClass('error'); });
	$('#survey-request-form textarea').each(function() { $(this).removeClass('error'); });

	var errors = 0;

	if($('#srf-forename').val().length == 0) {
		$('#srf-forename').addClass('error');
		errors++;
	} 
	
	if($('#srf-surname').val().length == 0) { 
		$('#srf-surname').addClass('error');
		errors++;
	}
			
	if($('#srf-email').val().length == 0) { 
		$('#srf-email').addClass('error');
		errors++;
	}

	if($('#srf-postcode').val().length == 0) { 
		$('#srf-postcode').addClass('error');
		errors++;
	}
	
	if($('#srf-type').val().length == 0) { 
		$('#srf-type').addClass('error');
		errors++;
	}
	
	if($('#srf-floors').val().length == 0) { 
		$('#srf-floors').addClass('error');
		errors++;
	}
			
	if($('#srf-bedrooms').val().length == 0) { 
		$('#srf-bedrooms').addClass('error');
		errors++;
	}
	
	if($('#srf-bedrooms').val().length == 0) { 
		$('#srf-bedrooms').addClass('error');
		errors++;
	}
	
	// If we have any errors
	if(errors > 0) {
		
		// Show our error message
		$('#form-message').text('There were some problems with your submission. Please make sure all the required fields have been completed.');
		
		// Move us to the top of the form
		scroll(0, 0);
		
	} else {

		// Start our query string
		var qs = "action=handle-survey-form";

		// Generate the AJAX query string
		$('#survey-request-form > fieldset > input').each(function() { 
			var qsName = $(this).attr('name');
			var qsValue = $(this).val();
			qs += "&" + qsName + "=" + qsValue;
		});
		$('#survey-request-form > fieldset > select').each(function() { 
			var qsName = $(this).attr('name');
			var qsValue = $(this).val();
			qs += "&" + qsName + "=" + qsValue;
		});
		$('#survey-request-form > fieldset > textarea').each(function() { 
			var qsName = $(this).attr('name');
			var qsValue = $(this).val();
			qs += "&" + qsName + "=" + qsValue;
		});
		
		$.ajax({
			type: "POST",
			url: "/admin/ajax.php",
			data: qs,
			success: function(msg){
				$('#form-message').css('color', '#090');
				$('#form-message').text('Thank you for your enquiry, a member of the Space Air Solutions team will contact you shortly. If you need any further information in the mean time, please call us on: 01483 252 252.');
				scroll(0, 0);
			}
		});
		
	}
			
}

// Highlight current page in nav
function HighlightCurrentPage(pageID) {
	
	// Hide all current sub page lists
	$('nav > ul > li > ul:visible').each(function() { $(this).hide(); });
	
	// Remove current class from any links
	$('nav > ul > li > a').each(function() { $(this).removeClass('current'); });
	$('nav > ul > li > ul > li > a').each(function() { $(this).removeClass('current'); });
	
	// If this page is in a subpage list, show the list
	if($('nav ul li a[data-rel='+pageID+']').hasClass('sub')) {
		
		$('nav ul li a[data-rel='+pageID+']').parents('li').parents('ul').show();
		
		// Add the current class to the page link
		$('nav > ul > li > ul > li > a[data-rel='+pageID+']').addClass('current');
		
	} else {
		
		// Add the current class to the page link
		$('nav > ul > li a[data-rel='+pageID+']').addClass('current');
		
	}
	
	// If this page has a sublist, show the list
	if($('nav ul li a[data-rel='+pageID+']').siblings().length == 2) {
		$('nav ul li a[data-rel='+pageID+']').siblings('ul').show();	
	}	
	
}

function initialize() {

	var latlng = new google.maps.LatLng(51.262214, -0.570519);
	
	var myOptions = {
		zoom: 15,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.HYBRID
	};
	
	if(document.getElementById("map-canvas")) {
		var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
		var image = new google.maps.MarkerImage('http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png');
		
		var marker = new google.maps.Marker({
		  position: latlng, 
		  map: map, 
		  icon : image
		});
		
		marker.setMap(map); 
	}
	
}

function run() {

	$('a.fb-link').fancybox({ overlayColor: '#333' });
	$('a.fb-iframe').fancybox({ overlayColor: '#333', type: 'iframe', width:450, height:350, titlePosition: 'inside' });
	
	$('#banner').cycle({ timeout: 5000 });
	$('#conformity').cycle({ timeout: 6000 });
	
	$('ol.qa > li > a').click(function() {
		if($(this).siblings('ul').is(':visible')) {
			$(this).siblings('ul').slideUp('fast');
			$(this).removeClass('down');
		} else {
			$('ol.qa > li > ul:visible').each(function() { $(this).slideUp('fast'); $(this).siblings('a').removeClass('down'); });
			$(this).siblings('ul').slideDown('slow');
			$(this).addClass('down');
		}
	});
	
	$('img[data-hover]').hover(function() {
        $(this).attr('tmp', $(this).attr('src')).attr('src', $(this).attr('data-hover')).attr('data-hover', $(this).attr('tmp')).removeAttr('tmp');
    }).each(function() {
        $('<img />').attr('src', $(this).attr('data-hover'));
    });
	
	$('img[data-hover]').click(function() { window.location = '/qa/certifications-conformity'; });
	
	$('#complete-house').mouseover(function() { $('#example').attr('src', '/images/example-complete-house.jpg'); });
	$('#roof-cutaway').mouseover(function() { $('#example').attr('src', '/images/example-roof-cutaway.jpg'); });
	$('#full-cutaway').mouseover(function() { $('#example').attr('src', '/images/example-full-cutaway.jpg'); });
	
	$("#myController").jFlow({
		controller: ".jFlowControl", // must be class, use . sign
		slideWrapper : "#jFlowSlider", // must be id, use # sign
		slides: "#mySlides",  // the div where all your sliding divs are nested in
		selectedWrapper: "jFlowSelected",  // just pure text, no sign
		width: "700px",  // this is the width for the content-slider
		height: "400px",  // this is the height for the content-slider
		duration: 400,  // time in miliseconds to transition one slide
		prev: ".jFlowPrev", // must be class, use . sign
		next: ".jFlowNext", // must be class, use . sign
		auto: true	
    });
	
	// Load pages from links within pages
	$('.ajax-page-link').click(function() {
		
		var pageID = $(this).attr('data-rel');
		var url = $(this).attr('href');
		
		// Load the page content with AJAX
		$.ajax({
			type: "POST",
			url: "/admin/ajax.php",
			data: "action=get-content&id=" + pageID,
			success: function(msg) {
				
				$('#content').html(msg);
				$('html, body').animate({scrollTop:0}, 0);
				history.pushState(null, null, url);
				run();
				
				// Highlight this page in the navigation
				if($('#lhs > nav ul li a[data-rel='+pageID+']').hasClass('sub')) {
					$('#lhs nav ul li a[data-rel='+pageID+']').parents('li').parents('ul').slideDown('fast');
				}
				
				$('#lhs > nav > ul > li > a').each(function() { $(this).removeClass('current'); });
				$('#lhs > nav > ul > li > ul > li > a').each(function() { $(this).removeClass('current'); });
				$('#lhs > nav > ul > li a[data-rel='+pageID+']').addClass('current');	
			
			}
		});
		
		// Change the page title with AJAX
		$.ajax({
			type: "POST",
			url: "/admin/ajax.php",
			data: "action=get-page-title&id=" + pageID,
			success: function(msg) { document.title = msg; }
		});
		
		// Disable link
		return false;
		
	});
	
	// Search form
	$('#search-form #q').keyup(function(e) {
		if(e.keyCode == 13) {
			if($('#q').val().length > 0) {
				window.location = '/search-site/' + $('#q').val();
			} else {
				window.location = '/search-site';
			}
			return false;
		}
	});
	$('#search-form #go').click(function() {
		if($('#q').val().length > 0) {
			window.location = '/search-site/' + $('#q').val();
		} else {
			window.location = '/search-site';
		}
	});
	
	// Tabs
	$(".tab-content").hide(); //Hide all content
	$("#tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab-content:first").show(); //Show first tab content

	// Clicking on each tab
	$('#tabs li').click(function() {
		
		$("#tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab-content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
		
	});
	
	if(window.location.hash) { 
		var hash = window.location.hash;
		$('#tabs li').each(function() {
			if($(this).find('a').attr('href') == hash) { 
				
				$("#tabs li").removeClass("active"); //Remove any "active" class
				$(this).addClass("active"); //Add "active" class to selected tab
				$(".tab-content").hide(); //Hide all tab content
				
				var activeTab =  $(this).find('a[href=' + hash + ']').attr('href');
				$(activeTab).fadeIn(); //Fade in the active ID content
				return false;
				
			}
		});
	}
	
	$('#mySlides > div').each(function() { $(this).css('display', 'block'); });
	
	$('#container-4').tabs({ fxFade: true, fxSpeed: 'fast' });
	
	$('#send-enquiry').click(function() { sendEnquiry(); });
	
	// Changing 'jump to' menu
	$('#jump-to').change(function() {
		window.location = 'http://www.' + $(this).val();
	});
	
	$('#asjw .col > ul > li > a').click(function() {
				
		if($(this).siblings('div').attr('data-display') == 'visible') { 
			$(this).siblings('div').slideUp('slow').attr('data-display', 'hidden');
		} else {
		
			$('#asjw .col ul li div[data-display=visible]').each(function() { 
				$(this).slideUp(400).attr('data-display', 'hidden');
			});
			
			$(this).siblings('div').slideDown('slow').attr('data-display', 'visible');
			
		}
		
		return false;
		
	});
	
	initialize();
	
}

$(document).ready(function() { 

	run();
	
	// Left hand menu drop downs (top level links)
	$('#lhs > nav > ul > li > a').click(function() {
		
		// Forward the user
		if($(this).attr('data-rel') == 12) { window.location = '/news/case-studies';	}
		if($(this).attr('data-rel') == 33) { window.location = '/news/news';			}
		
		// Hide currently open sub menus
		$('#lhs > nav > ul > li > ul:visible').each(function() {
			$(this).slideUp('fast');
		});
		
		// Show sub menu
		$(this).siblings('ul').slideDown('fast');
		
		// Make the link clicked the current one
		$('#lhs > nav > ul > li > a').each(function() { $(this).removeClass('current'); });
		$('#lhs > nav > ul > li > ul > li > a').each(function() { $(this).removeClass('current'); });
		$(this).addClass('current');				
		
		// What URL are we visiting?
		var thisPageID = $(this).attr('data-rel');
		var url = $(this).attr('href');
		
		// Load the page content with AJAX
		$.ajax({
			type: "POST",
			url: "/admin/ajax.php",
			data: "action=get-content&id=" + $(this).attr('data-rel'),
			success: function(msg) {
				
				$('#content').html(msg);
				$('html, body').animate({scrollTop:0}, 0);
				history.pushState(null, null, url);
				run();
				
				// Highlight this page in the navigation
				HighlightCurrentPage(thisPageID);
			}
		});
		
		// Change the page title with AJAX
		$.ajax({
			type: "POST",
			url: "/admin/ajax.php",
			data: "action=get-page-title&id=" + $(this).attr('data-rel'),
			success: function(msg) { document.title = msg; }
		});
		
		// Disable link
		return false;
		
	});
	
	$('#lhs > nav > ul > li > ul > li > a').click(function() {
		
		// Make the link clicked the current one
		$('#lhs > nav > ul > li > a').each(function() { $(this).removeClass('current'); });
		$('#lhs > nav > ul > li > ul > li > a').each(function() { $(this).removeClass('current'); });
		$(this).addClass('current');				
		
		// What URL are we visiting?
		var thisPageID = $(this).attr('data-rel');
		var url = $(this).attr('href');
		
		// Load the page content with AJAX
		$.ajax({
			type: "POST",
			url: "/admin/ajax.php",
			data: "action=get-content&id=" + $(this).attr('data-rel'),
			success: function(msg) {
				
				$('#content').html(msg);
				$('html, body').animate({scrollTop:0}, 0);
				history.pushState(null, null, url);
				run();
				
				// Highlight this page in the navigation
				HighlightCurrentPage(thisPageID);
				
			}
		});
		
		// Change the page title with AJAX
		$.ajax({
			type: "POST",
			url: "/admin/ajax.php",
			data: "action=get-page-title&id=" + $(this).attr('data-rel'),
			success: function(msg) { document.title = msg; }
		});
		
		// Disable link
		return false;
		
	}); 
	
});

