// Function to add cycle effect to banner
function add_banner_cycle() {
	if( $('#banner .item').length > 1 ) {
		$('#banner').cycle({
			cleartypeNoBg: true
		});
	}
}

// Function to add cycle effect to news sections
function add_news_cycle() {
	var c = $('#articles .item');
	var t = $('.tab a');
	var a = 'active';
	if( c.length > 0 ) {
		$('#articles').append( $('<a href="#" class="news-next">Next Article</a>') );
		$('#articles').append( $('<img src="/site/images/news-link.png" alt="" class="news-foot" />') );
		c.cycle({
			next: $('.news-next'),
			timeout: 0
		});
		t.first().addClass(a);
		$('#articles .item + .item').hide();
		t.click( function(e) {
			var h = $(this).attr('href');
			c.cycle(0);
			if( $(h).is(':hidden') ) {
				$('#articles .item:visible').fadeOut( '', function() {
					$(h).fadeIn();
				});
				t.toggleClass(a);
			}
			$(this).blur();
			e.preventDefault();
		});
	}
}

// Function to add map
function add_map() {
	var f = $('#contact-right');
	if( f.length > 0 ) {
		var c = $('<div id="contact-map"></div>');
		f.find('#location').after(c);
		var l = new google.maps.LatLng( 53.838563,-1.789037 );
		var e = new google.maps.LatLng( 53.838057,-1.789377 );
		var o = {
			zoom: 14,
			center: l,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var m = new google.maps.Map( document.getElementById('contact-map'), o );
		var k = new google.maps.Marker({
			position: e,
			title: 'ADI',
			icon: '/site/images/gmapicon.png'
		});
		k.setMap(m);
	}
}

// Function to write an error message
function add_error_message(m) {
	var e = $('#contact-form-error');
	if( m === '' ) {
		e.remove();
	} else {
		if( e.length > 0 ) {
			$('#contact-form-error').text(m);
		} else {
			var p = $('<p class="error" id="contact-form-error">' + m + '</p>');
			$('input[value="error"]').after(p);
		}
	}
}

// Function to validate form input
function validate_form() {
	var e = [];
	var f = $('#contact-form input[type="text"].required, #contact-form textarea.required');
	f.each( function() {
		if( $(this).val() === '' ) {
			e.push( $(this).siblings('input[type="hidden"].validation').first().val() );
		} else if( $(this).hasClass('email') ) {
			var x = /[\(\)<\>\,\;\:\\\"\[\]]/;
			var y = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
			if( $(this).val().match(x) || (!y.test( $(this).val() )) ) {
				e.push( 'a valid email address' );
			}
		}
	});
	if( e.length > 0 ) {
		var m = 'Please enter ';
		m += e[0];
		if( e.length > 1 ) {
			if( e.length > 2 ) {
				for( var i = 1; i < ( e.length - 1 ); i++ ) {
					m += ', ';
					m += e[i];
				}
			}
			m += ' and ';
			m += e[e.length-1];
		}
		m += '.';
		add_error_message(m);
		return false;
	}
}

// Function to activate form validation
function add_validation() {
	var f = $('#contact-form');
	if ( f.length > 0 ) {
		f.submit( function() {
			return validate_form();
		});
		f.bind('reset', function() {
			add_error_message('');
		});
	}
}

// Functions to run on load
$(document).ready( function() {
	add_banner_cycle();
	add_news_cycle();
	add_map();
	add_validation();
});

